rewrite_quantization.cpp 4.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
turneram's avatar
turneram committed
24
25
26
27
28
29
30
31
32
33
34
#include <migraphx/rewrite_quantization.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/tune_axis.hpp>
#include <migraphx/program.hpp>
#include <migraphx/shape.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

turneram's avatar
turneram committed
35
36
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_BROADCAST_Q);

turneram's avatar
turneram committed
37
38
39
40
41
42
43
44
void apply_quantizelinear(module& m, instruction_ref ins)
{
    assert(ins->name() == "quantizelinear");
    auto x       = ins->inputs()[0];
    auto y_scale = ins->inputs()[1];

    if(x->get_shape().type() != y_scale->get_shape().type())
    {
45
46
        x = m.insert_instruction(
            ins, make_op("convert", {{"target_type", y_scale->get_shape().type()}}), x);
turneram's avatar
turneram committed
47
48
49
50
51
52
    }
    auto div            = m.insert_instruction(ins, make_op("div"), x, y_scale);
    auto add_zero_point = m.insert_instruction(ins, make_op("round"), div);

    if(ins->inputs().size() == 3)
    {
53
54
55
56
        auto zero_point =
            m.insert_instruction(ins,
                                 make_op("convert", {{"target_type", y_scale->get_shape().type()}}),
                                 ins->inputs()[2]);
turneram's avatar
turneram committed
57
58
59
60
61
62
63
64
65
        add_zero_point = m.insert_instruction(ins, make_op("add"), add_zero_point, zero_point);
    }

    int64_t max_quant = 0;
    int64_t min_quant = 0;
    ins->get_shape().visit_type([&](auto qt) {
        max_quant = qt.max();
        min_quant = qt.min();
    });
turneram's avatar
turneram committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    if (enabled(MIGRAPHX_BROADCAST_Q{}))
    {
        auto s       = add_zero_point->get_shape();
        auto min_arg = m.add_literal(literal{shape{s.type()}, {min_quant}});
        auto max_arg = m.add_literal(literal{shape{s.type()}, {max_quant}});
        auto min_mbcast =
            m.insert_instruction(ins, make_op("multibroadcast", {{"out_lens", s.lens()}}), min_arg);
        auto max_mbcast =
            m.insert_instruction(ins, make_op("multibroadcast", {{"out_lens", s.lens()}}), max_arg);
        
        auto saturate =
            m.insert_instruction(ins, make_op("clip"), add_zero_point, min_mbcast, max_mbcast);
        m.replace_instruction(
            ins, make_op("convert", {{"target_type", ins->get_shape().type()}}), saturate);
    }
    else 
    {
        auto s = add_zero_point->get_shape();
        std::vector<int> min_data(s.elements(), min_quant);
        std::vector<int> max_data(s.elements(), max_quant);
        auto min_arg = m.add_literal(literal(s, min_data));
        auto max_arg = m.add_literal(literal(s, max_data));
turneram's avatar
turneram committed
88

turneram's avatar
turneram committed
89
90
91
92
        auto saturate = m.insert_instruction(ins, make_op("clip"), add_zero_point, min_arg, max_arg);
        m.replace_instruction(
            ins, make_op("convert", {{"target_type", ins->get_shape().type()}}), saturate);
    }
turneram's avatar
turneram committed
93
94
95
96
97
98
}

void apply_dequantizelinear(module& m, instruction_ref ins)
{
    assert(ins->name() == "dequantizelinear");
    auto x_scale = ins->inputs()[1];
99
100
    auto x       = m.insert_instruction(
        ins, make_op("convert", {{"target_type", x_scale->get_shape().type()}}), ins->inputs()[0]);
turneram's avatar
turneram committed
101
102
103

    if(ins->inputs().size() == 3)
    {
104
105
106
107
        auto x_zero_point =
            m.insert_instruction(ins,
                                 make_op("convert", {{"target_type", x_scale->get_shape().type()}}),
                                 ins->inputs()[2]);
turneram's avatar
turneram committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        x = m.insert_instruction(ins, make_op("sub"), x, x_zero_point);
    }

    m.replace_instruction(ins, make_op("mul"), x, x_scale);
}

void rewrite_quantization::apply(module& m) const
{
    for(auto ins : iterator_for(m))
    {
        if(ins->name() == "quantizelinear")
        {
            apply_quantizelinear(m, ins);
        }

        else if(ins->name() == "dequantizelinear")
        {
            apply_dequantizelinear(m, ins);
        }
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx