quantize_ins.cpp 1.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include <migraphx/quantize_ins.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/stringutils.hpp>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
instruction_ref insert_fp16(program& prog, instruction_ref& ins, shape::type_t type
    std::unordered_map<instruction_ref, instruction_ref>& map_fp16)
{
    if (map_fp16.count(ins) > 0)
    {
        return map_fp16[ins];
    }

    assert(ins->get_shape().type() == shape::float_type || ins->get_shape().type() == shape::double_type);
    instruction_ref ins_fp16{};
    if(ins == std::prev(prog.end()))
    {
        ins_fp16 = prog.add_instruction(op::fp_conversion{}, ins);
    }
    else
    {
        ins_fp16 = prog.insert_instruction(std::next(ins), op::fp_conversion{}, ins);
    }
    map_fp16[ins] = ins_fp16;

    return ins_fp16;
}

34
35
void quantize_ins(program& prog, const std::vector<std::string>& ins_names)
{
36
    std::unordered_map<instruction_ref, instruction_ref> map_fp16;
Shucai Xiao's avatar
Shucai Xiao committed
37
    for(auto ins : iterator_for(prog))
38
39
    {
        auto name_it = std::find(ins_name.begin(), ins_name.end(), ins->name());
Shucai Xiao's avatar
Shucai Xiao committed
40
        if(name_it == ins_name.end())
41
42
43
        {
            continue;
        }
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

        auto inputs = ins->inputs();
        for (auto input : inputs)
        {
            auto s = input->get_shape();
            if (s.type() == shape::float_type || s.type() == shape::double_type)
            {
                auto input_fp16 = insert_fp16(prog, input, s.type(), map_fp16);
                instruction::replace_argument(ins, input, input_fp16, false);
            }
        }
        ins->recompute_ins_shape();

        if (ins->get_shape().type() == shape::half_type)
        {
            
        }
61
62
63
64
65
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx