quantization.cpp 3.49 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
    ins_fp16      = prog.insert_instruction(std::next(ins), op::convert{type}, ins);
27
28
29
30
31
    map_fp16[ins] = ins_fp16;

    return ins_fp16;
}

32
void quantize(program& prog, const std::vector<std::string>& ins_names)
33
{
34
    std::unordered_map<instruction_ref, instruction_ref> map_fp16;
Shucai Xiao's avatar
Shucai Xiao committed
35
    for(auto ins : iterator_for(prog))
36
    {
37
        // all indicates every instruction is converted
Shucai Xiao's avatar
Shucai Xiao committed
38
        if((not contains(ins_names, "all")) and (not contains(ins_names, ins->name())))
39
40
41
        {
            continue;
        }
42

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

72
        // no change for the input, go to the next instruction
Shucai Xiao's avatar
Shucai Xiao committed
73
        if(inputs == converted_inputs)
74
        {
75
            continue;
Shucai Xiao's avatar
Shucai Xiao committed
76
77
78
79
80
81
82
83
        }

        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()))
84
            {
Shucai Xiao's avatar
Shucai Xiao committed
85
86
87
88
89
90
91
92
93
                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)
94
                {
Shucai Xiao's avatar
Shucai Xiao committed
95
                    prog.replace_instruction(ins, ins_orig_type);
96
                }
97
            }
98
        }
Shucai Xiao's avatar
Shucai Xiao committed
99
100

        prog.replace_instruction(ins, op, converted_inputs);
101
102
103
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
104
void quantize(program& prog) { quantize(prog, {"all"}); }
Shucai Xiao's avatar
Shucai Xiao committed
105

106
107
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx