quantization.cpp 3.73 KB
Newer Older
Shucai Xiao's avatar
Shucai Xiao committed
1
#include <migraphx/quantization.hpp>
2
3
4
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
5
#include <migraphx/op/convert.hpp>
6
#include <migraphx/stringutils.hpp>
7
#include <migraphx/ranges.hpp>
8
9
10
11
12
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

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

Shucai Xiao's avatar
Shucai Xiao committed
23
24
    assert(ins->get_shape().type() == shape::float_type ||
           ins->get_shape().type() == shape::double_type);
25
    instruction_ref ins_fp16{};
Shucai Xiao's avatar
Shucai Xiao committed
26
    if(ins->name() == "@literal" && ins->outputs().size() == 1)
27
    {
28
29
30
31
32
        std::vector<float> values;
        auto l_fp32 = ins->get_literal();
        shape s     = ins->get_shape();
        l_fp32.visit([&](auto val) { values.assign(val.begin(), val.end()); });
        ins_fp16 = prog.add_literal(literal({shape::half_type, s.lens()}, values));
33
34
35
    }
    else
    {
36
        ins_fp16 = prog.insert_instruction(std::next(ins), op::convert{type}, ins);
37
38
39
40
41
42
    }
    map_fp16[ins] = ins_fp16;

    return ins_fp16;
}

43
void quantize(program& prog, const std::vector<std::string>& ins_names)
44
{
45
    std::unordered_map<instruction_ref, instruction_ref> map_fp16;
Shucai Xiao's avatar
Shucai Xiao committed
46
    for(auto ins : iterator_for(prog))
47
    {
48
        // all indicates every instruction is converted
Shucai Xiao's avatar
Shucai Xiao committed
49
        if((not contains(ins_names, "all")) and (not contains(ins_names, ins->name())))
50
51
52
        {
            continue;
        }
53

54
        shape::type_t orig_type = ins->get_shape().type();
Shucai Xiao's avatar
Shucai Xiao committed
55
        // process all inputs, if input is a fp32 or fp64, convert it
56
        // to a fp16 by adding a convert operator.
57
        auto inputs = ins->inputs();
58
        std::vector<instruction_ref> converted_inputs;
Shucai Xiao's avatar
Shucai Xiao committed
59
        for(auto input : inputs)
60
61
        {
            auto s = input->get_shape();
Shucai Xiao's avatar
Shucai Xiao committed
62
            if(s.type() == shape::float_type || s.type() == shape::double_type)
63
            {
64
                // if the input is a convert operator, uses its input
65
66
                // as its current input
                instruction_ref input_fp16{};
67
                if(input->name() == "convert")
68
69
70
71
72
73
74
                {
                    input_fp16 = input->inputs().front();
                }
                else
                {
                    input_fp16 = insert_fp16(prog, input, shape::half_type, map_fp16);
                }
75
                converted_inputs.push_back(input_fp16);
76
            }
77
78
79
80
81
82
            else
            {
                converted_inputs.push_back(input);
            }
        }

83
        // no change for the input, go to the next instruction
Shucai Xiao's avatar
Shucai Xiao committed
84
        if(inputs == converted_inputs)
85
        {
86
            continue;
Shucai Xiao's avatar
Shucai Xiao committed
87
88
89
90
91
92
93
94
        }

        auto op        = ins->get_operator();
        auto ins_shape = compute_shape(op, converted_inputs);
        if(ins_shape.type() != orig_type)
        {
            // insert another convert instruction to convert it back
            if(ins == std::prev(prog.end()))
95
            {
Shucai Xiao's avatar
Shucai Xiao committed
96
97
98
99
100
101
102
103
104
                prog.add_instruction(op::convert{orig_type}, ins);
            }
            else
            {
                // check the dead code case to avoid assert
                bool output_empty = ins->outputs().empty();
                auto ins_orig_type =
                    prog.insert_instruction(std::next(ins), op::convert{orig_type}, ins);
                if(!output_empty)
105
                {
Shucai Xiao's avatar
Shucai Xiao committed
106
                    prog.replace_instruction(ins, ins_orig_type);
107
                }
108
            }
109
        }
Shucai Xiao's avatar
Shucai Xiao committed
110
111

        prog.replace_instruction(ins, op, converted_inputs);
112
113
114
115
116
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx