quantization.cpp 8.04 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
instruction_ref insert_quant_ins(program& prog,
Shucai Xiao's avatar
Shucai Xiao committed
14
15
16
17
18
                                 instruction_ref& ins,
                                 shape::type_t type,
                                 std::unordered_map<instruction_ref, instruction_ref>& map_ins,
                                 float scale = 1.0f,
                                 float shift = 0.0f)
19
{
Shucai Xiao's avatar
Shucai Xiao committed
20
    if(map_ins.count(ins) > 0)
21
    {
Shucai Xiao's avatar
Shucai Xiao committed
22
        return map_ins[ins];
23
24
    }

Shucai Xiao's avatar
Shucai Xiao committed
25
    assert(ins->get_shape().type() == shape::float_type ||
Shucai Xiao's avatar
Shucai Xiao committed
26
27
28
           ins->get_shape().type() == shape::double_type ||
           ins->get_shape().type() == shape::int32_type);
    instruction_ref quant_ins{};
Shucai Xiao's avatar
Shucai Xiao committed
29
    quant_ins    = prog.insert_instruction(std::next(ins), op::convert{type}, ins);
Shucai Xiao's avatar
Shucai Xiao committed
30
    map_ins[ins] = quant_ins;
31

Shucai Xiao's avatar
Shucai Xiao committed
32
    return quant_ins;
33
34
}

Shucai Xiao's avatar
Shucai Xiao committed
35
36
37
// This function is to convert any instructions specified in the input
// from double or float to float16 by inserting a convert operator.
// For the conversion, there could be cases of overflowing, but it
Shucai Xiao's avatar
Shucai Xiao committed
38
// is very rare in the area of deeping learning, so we just do a
Shucai Xiao's avatar
Shucai Xiao committed
39
// truncate of the input to get the fp16.
40
void quantize(program& prog, const std::vector<std::string>& ins_names)
41
{
42
    std::unordered_map<instruction_ref, instruction_ref> map_fp16;
Shucai Xiao's avatar
Shucai Xiao committed
43
    for(auto ins : iterator_for(prog))
44
    {
45
        // all indicates every instruction is converted
Shucai Xiao's avatar
Shucai Xiao committed
46
        if((not contains(ins_names, "all")) and (not contains(ins_names, ins->name())))
47
48
49
        {
            continue;
        }
50

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

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

        auto op        = ins->get_operator();
        auto ins_shape = compute_shape(op, converted_inputs);
        if(ins_shape.type() != orig_type)
        {
Shucai Xiao's avatar
Shucai Xiao committed
90
91
92
93
94
            // 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)
95
            {
Shucai Xiao's avatar
Shucai Xiao committed
96
                prog.replace_instruction(ins, ins_orig_type);
Shucai Xiao's avatar
Shucai Xiao committed
97
            }
Shucai Xiao's avatar
Shucai Xiao committed
98
99
100
101
102
103
104
105
106
        }

        prog.replace_instruction(ins, op, converted_inputs);
    }
}

void quantize(program& prog) { quantize(prog, {"all"}); }

// int8 quantization is different from fp16 since int8 can only handle value
Shucai Xiao's avatar
Shucai Xiao committed
107
// -128 ~ 127. To convert the float or double to int8, we need a scale and
Shucai Xiao's avatar
Shucai Xiao committed
108
// a shift, then the convert can be done as v_int8 = fp * scale + shift.
Shucai Xiao's avatar
Shucai Xiao committed
109
// To simplify the changes, we consider shift as 0.0f for now.
Shucai Xiao's avatar
Shucai Xiao committed
110
111
112
113
void quantize_int8(program& prog, const std::vector<std::string>& ins_names)
{
    // For now, we only support the int8 quantization of gemm and convolution
    std::vector<std::string> op_names = {"dot", "convolution"};
Shucai Xiao's avatar
Shucai Xiao committed
114
115
116
    if(!std::all_of(ins_names.begin(), ins_names.end(), [&](auto name) {
           return std::find(op_names.begin(), op_names.end(), name);
       }))
Shucai Xiao's avatar
Shucai Xiao committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    {
        MIGRAPHX_THROW("QUANTIZE_INT8: only support DOT and CONVOLUTION operation");
    }

    // tmp value used just testing
    std::vector<std::pair<float, float>> int8_param{{1.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 0.0f}};

    std::unordered_map<instruction_ref, instruction_ref> map_quant_ins;
    for(auto ins : iterator_for(prog))
    {
        if(not contains(ins_names, ins->name()))
        {
            continue;
        }

        shape::type_t orig_type = ins->get_shape().type();

        // for the dot operator, there could be 2 or 3 input arguments
        // if the 3rd argument is available, convert it to an int32.
        std::vector<instruction_ref> converted_inputs;

        // process all inputs, if input is a fp32 or fp64, convert it
Shucai Xiao's avatar
Shucai Xiao committed
139
        // to a int8 type by adding a convert operator and replace
Shucai Xiao's avatar
Shucai Xiao committed
140
        // the operator with the corresponding int8 version
Shucai Xiao's avatar
Shucai Xiao committed
141
        auto inputs             = ins->inputs();
Shucai Xiao's avatar
Shucai Xiao committed
142
143
144
        std::size_t param_index = 0;
        for(auto input : inputs)
        {
Shucai Xiao's avatar
Shucai Xiao committed
145
146
            // In general, the target_type is int8, but for the dot
            // operation, if it has 3 inputs, then the last one should
Shucai Xiao's avatar
Shucai Xiao committed
147
148
            // be converted to int32_type
            shape::type_t quant_type = shape::int8_type;
Shucai Xiao's avatar
Shucai Xiao committed
149
            if(ins->name() == "dot" and inputs.size() == 3 and input == inputs.back())
Shucai Xiao's avatar
Shucai Xiao committed
150
            {
Shucai Xiao's avatar
Shucai Xiao committed
151
152
                quant_type = shape::int32_type;
            }
Shucai Xiao's avatar
Shucai Xiao committed
153

Shucai Xiao's avatar
Shucai Xiao committed
154
            auto param = int8_param[param_index++];
Shucai Xiao's avatar
Shucai Xiao committed
155
156
157
            auto s     = input->get_shape();
            if(s.type() == shape::float_type || s.type() == shape::double_type ||
               s.type() == shape::int32_type)
Shucai Xiao's avatar
Shucai Xiao committed
158
159
160
161
162
163
164
            {
                // if the input is a convert operator, uses its input
                // as its current input
                instruction_ref quant_input{};
                if(input->name() == "convert")
                {
                    auto tmp_ins = input->inputs().front();
Shucai Xiao's avatar
Shucai Xiao committed
165
                    if(tmp_ins->get_shape().type() == quant_type)
Shucai Xiao's avatar
Shucai Xiao committed
166
167
168
169
170
                    {
                        quant_input = input->inputs().front();
                    }
                    else
                    {
Shucai Xiao's avatar
Shucai Xiao committed
171
172
                        quant_input = insert_quant_ins(
                            prog, input, quant_type, map_quant_ins, param.first, param.second);
Shucai Xiao's avatar
Shucai Xiao committed
173
174
175
                    }
                }
                else
176
                {
Shucai Xiao's avatar
Shucai Xiao committed
177
178
                    quant_input = insert_quant_ins(
                        prog, input, quant_type, map_quant_ins, param.first, param.second);
179
                }
Shucai Xiao's avatar
Shucai Xiao committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
                converted_inputs.push_back(quant_input);
            }
            else
            {
                converted_inputs.push_back(input);
            }
        }

        // no change for the input, go to the next instruction
        if(inputs == converted_inputs)
        {
            continue;
        }

        auto op        = ins->get_operator();
        auto ins_shape = compute_shape(op, converted_inputs);
        if(ins_shape.type() != orig_type)
        {
            // check the dead code case to avoid assert
            bool output_empty = ins->outputs().empty();
            // this conversion can be only from int32 to float or double
            auto ins_orig_type =
                prog.insert_instruction(std::next(ins), op::convert{orig_type}, ins);
            if(!output_empty)
            {
                prog.replace_instruction(ins, ins_orig_type);
206
            }
207
        }
Shucai Xiao's avatar
Shucai Xiao committed
208

Shucai Xiao's avatar
Shucai Xiao committed
209
210
211
212
        // When converting from other types to int8_type, there are parameters
        // used as scale and shift(.0f), which will generate results diffrent from
        // the original results. To adjust the output to be "correct(approximatly
        // equal)", we need additional calculation for that.
Shucai Xiao's avatar
Shucai Xiao committed
213

Shucai Xiao's avatar
Shucai Xiao committed
214
        prog.replace_instruction(ins, op, converted_inputs);
215
216
217
218
219
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx