quantize_ins.cpp 1.82 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 {

Shucai Xiao's avatar
Shucai Xiao committed
11
12
13
14
instruction_ref
insert_fp16(program& prog,
            instruction_ref& ins,
            shape::type_t type std::unordered_map<instruction_ref, instruction_ref>& map_fp16)
15
{
Shucai Xiao's avatar
Shucai Xiao committed
16
    if(map_fp16.count(ins) > 0)
17
18
19
20
    {
        return map_fp16[ins];
    }

Shucai Xiao's avatar
Shucai Xiao committed
21
22
    assert(ins->get_shape().type() == shape::float_type ||
           ins->get_shape().type() == shape::double_type);
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    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;
}

37
38
void quantize_ins(program& prog, const std::vector<std::string>& ins_names)
{
39
    std::unordered_map<instruction_ref, instruction_ref> map_fp16;
Shucai Xiao's avatar
Shucai Xiao committed
40
    for(auto ins : iterator_for(prog))
41
42
    {
        auto name_it = std::find(ins_name.begin(), ins_name.end(), ins->name());
Shucai Xiao's avatar
Shucai Xiao committed
43
        if(name_it == ins_name.end())
44
45
46
        {
            continue;
        }
47
48

        auto inputs = ins->inputs();
Shucai Xiao's avatar
Shucai Xiao committed
49
        for(auto input : inputs)
50
51
        {
            auto s = input->get_shape();
Shucai Xiao's avatar
Shucai Xiao committed
52
            if(s.type() == shape::float_type || s.type() == shape::double_type)
53
54
55
56
57
58
59
            {
                auto input_fp16 = insert_fp16(prog, input, s.type(), map_fp16);
                instruction::replace_argument(ins, input, input_fp16, false);
            }
        }
        ins->recompute_ins_shape();

Shucai Xiao's avatar
Shucai Xiao committed
60
        if(ins->get_shape().type() == shape::half_type)
61
62
        {
        }
63
64
65
66
67
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx