quantization.cpp 10.2 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.
 */
Shucai Xiao's avatar
Shucai Xiao committed
24
25
#include <iostream>
#include <vector>
26
#include <migraphx/gpu/fuse_mlir.hpp>
jerryyin's avatar
jerryyin committed
27
#include <migraphx/gpu/mlir.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
28
29
#include <migraphx/operators.hpp>
#include <migraphx/instruction.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
30
#include <migraphx/quantization.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
31
#include <migraphx/generate.hpp>
32
#include <migraphx/register_target.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
33
34
#include <migraphx/verify.hpp>
#include <migraphx/dead_code_elimination.hpp>
jerryyin's avatar
jerryyin committed
35
#include <migraphx/make_op.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
36
37
38
#include <migraphx/propagate_constant.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/onnx.hpp>
39
#include <test.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
40
41
#include <migraphx/half.hpp>

jerryyin's avatar
jerryyin committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//TEST_CASE(gpu_target_copy)
//{
//    migraphx::target gpu_t = migraphx::make_target("gpu");
//    migraphx::target ref_t = migraphx::make_target("ref");
//    migraphx::shape s{migraphx::shape::int8_type, {2, 3, 4, 5}};
//
//    auto ref_arg_orig  = migraphx::generate_argument(s, 0x123456L);
//    auto gpu_arg       = gpu_t.copy_to(ref_arg_orig);
//    auto ref_arg_final = gpu_t.copy_from(gpu_arg);
//
//    std::vector<int8_t> val_orig;
//    ref_arg_orig.visit([&](auto v) { val_orig.assign(v.begin(), v.end()); });
//    std::vector<int8_t> val_final;
//    ref_arg_final.visit([&](auto v) { val_final.assign(v.begin(), v.end()); });
//
//    EXPECT(migraphx::verify_range(val_orig, val_final));
//}
Shucai Xiao's avatar
Shucai Xiao committed
59

jerryyin's avatar
jerryyin committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//TEST_CASE(int8_quantization)
//{
//    auto run_prog = [](migraphx::program p,
//                       const migraphx::target& t,
//                       migraphx::parameter_map& m_in,
//                       std::vector<float>& res) {
//        std::vector<migraphx::parameter_map> cali_data;
//        cali_data.push_back(m_in);
//        migraphx::quantize_int8(p, t, cali_data);
//        p.compile(t);
//        migraphx::parameter_map m;
//        for(auto&& x : p.get_parameter_shapes())
//        {
//            if(m_in.count(x.first) > 0)
//            {
//                m[x.first] = t.copy_to(m_in[x.first]);
//            }
//            else
//            {
//                m[x.first] = t.allocate(x.second);
//            }
//        }
//
//        auto result = t.copy_from(p.eval(m).back());
//        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
//    };
//
//    auto create_program = [] {
//        migraphx::program p;
//        auto* mm = p.get_main_module();
//        migraphx::shape sa{migraphx::shape::float_type, {5, 16}};
//        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
//        migraphx::shape sc{migraphx::shape::float_type, {5, 8}};
//        auto pa = mm->add_parameter("a", sa);
//        auto pb = mm->add_parameter("b", sb);
//        mm->add_instruction(migraphx::op::dot{}, pa, pb);
//
//        return p;
//    };
//
//    {
//        auto p = create_program();
//        migraphx::parameter_map m;
//        migraphx::shape sa{migraphx::shape::float_type, {5, 16}};
//        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
//        migraphx::shape sc{migraphx::shape::float_type, {5, 8}};
//        m["a"] = migraphx::generate_argument(sa);
//        m["b"] = migraphx::generate_argument(sb);
//        std::vector<float> ref_result;
//        migraphx::target ref_t = migraphx::make_target("ref");
//        run_prog(p, ref_t, m, ref_result);
//        // print ref_result
//        std::cout << "ref_result: ";
//        for(auto&& v : ref_result)
//            std::cout << v << " ";
//        std::cout << std::endl;
//
//        std::vector<float> gpu_result;
//        migraphx::target gpu_t = migraphx::make_target("gpu");
//        run_prog(p, gpu_t, m, gpu_result);
//        std::cout << "gpu_result: ";
//        for(auto&& v : gpu_result)
//            std::cout << v << " ";
//        std::cout << std::endl;
//
//       auto s = migraphx::gpu::dump_mlir(*p.get_main_module());
//       //std::cout << s << std::endl;   
//        // Note: the tolerance for mlir_enabled result is temporarily bumped
//        // higher because the lowering pipeline between mlir fallback and
//        // regular non-mlir pipeline diverged. MLIR fallback uses the
//        // rewrite_quantization at the very end of the pipeline, whereas
//        // the regular pipeline uses the rewrite_quantization in the much
//        // earlier stage.
//        //if(migraphx::gpu::mlir_enabled())
//        //    EXPECT(migraphx::verify_range(ref_result, gpu_result, 1e5));
//        //else
//            EXPECT(migraphx::verify_range(ref_result, gpu_result));
//    }
//}
Shucai Xiao's avatar
Shucai Xiao committed
139

jerryyin's avatar
jerryyin committed
140
TEST_CASE(int8_quantization_self)
Shucai Xiao's avatar
Shucai Xiao committed
141
142
143
{
    auto run_prog = [](migraphx::program p,
                       const migraphx::target& t,
144
                       migraphx::parameter_map& m_in,
Shucai Xiao's avatar
Shucai Xiao committed
145
                       std::vector<float>& res) {
146
        std::vector<migraphx::parameter_map> cali_data;
Shucai Xiao's avatar
Shucai Xiao committed
147
        cali_data.push_back(m_in);
jerryyin's avatar
jerryyin committed
148
        //migraphx::quantize_int8(p, t, cali_data);
Shucai Xiao's avatar
Shucai Xiao committed
149
        p.compile(t);
150
        migraphx::parameter_map m;
Shucai Xiao's avatar
Shucai Xiao committed
151
152
153
154
155
156
157
158
159
160
161
162
        for(auto&& x : p.get_parameter_shapes())
        {
            if(m_in.count(x.first) > 0)
            {
                m[x.first] = t.copy_to(m_in[x.first]);
            }
            else
            {
                m[x.first] = t.allocate(x.second);
            }
        }

163
        auto result = t.copy_from(p.eval(m).back());
Shucai Xiao's avatar
Shucai Xiao committed
164
165
166
167
168
        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
    };

    auto create_program = [] {
        migraphx::program p;
169
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
170
        migraphx::shape sa{migraphx::shape::float_type, {5, 16}};
Shucai Xiao's avatar
Shucai Xiao committed
171
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
Shucai Xiao's avatar
Shucai Xiao committed
172
        migraphx::shape sc{migraphx::shape::float_type, {5, 8}};
jerryyin's avatar
jerryyin committed
173
174
175
        //migraphx::shape sa{migraphx::shape::int8_type, {5, 16}};
        //migraphx::shape sb{migraphx::shape::int8_type, {16, 8}};
        //migraphx::shape sc{migraphx::shape::int32_type, {5, 8}};
176
177
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
jerryyin's avatar
jerryyin committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

        // quantizelinear for arg0
        migraphx::shape ss1{migraphx::shape::int8_type, {5, 16}};
        auto literal1 = mm->add_literal(0.00738189f);
        auto bcast1 = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", ss1.lens()}}), literal1);
        auto quant_linear1 = mm->add_instruction(migraphx::make_op("quantizelinear"), pa, bcast1);
        //quant_linear1->debug_print();
        // quantizelinear for arg1
        migraphx::shape ss2{migraphx::shape::int8_type, {16, 8}};
        auto literal2 = mm->add_literal(0.00787402f);
        auto bcast2 = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", ss2.lens()}}), literal2);
        auto quant_linear2 = mm->add_instruction(migraphx::make_op("quantizelinear"), pb, bcast2);

        //auto dot = mm->add_instruction(migraphx::op::dot{}, pa, pb);
        //auto dot = mm->add_instruction(migraphx::op::quant_dot{}, pa, pb);
        auto dot = mm->add_instruction(migraphx::op::quant_dot{}, quant_linear1, quant_linear2);

       migraphx::shape ss{migraphx::shape::float_type, {5, 8}};
       auto literal = mm->add_literal(5.81251188e-05f);
       auto bcast = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", ss.lens()}}), literal);
    auto dequant = mm->add_instruction(migraphx::make_op("dequantizelinear"), dot, bcast);
        mm->add_return({dequant});

Shucai Xiao's avatar
Shucai Xiao committed
201
202
203
204
205
206

        return p;
    };

    {
        auto p = create_program();
207
        migraphx::parameter_map m;
Shucai Xiao's avatar
Shucai Xiao committed
208
        migraphx::shape sa{migraphx::shape::float_type, {5, 16}};
Shucai Xiao's avatar
Shucai Xiao committed
209
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
Shucai Xiao's avatar
Shucai Xiao committed
210
        migraphx::shape sc{migraphx::shape::float_type, {5, 8}};
jerryyin's avatar
jerryyin committed
211
212
213
        //migraphx::shape sa{migraphx::shape::int8_type, {5, 16}};
        //migraphx::shape sb{migraphx::shape::int8_type, {16, 8}};
        //migraphx::shape sc{migraphx::shape::int32_type, {5, 8}};
Shucai Xiao's avatar
Shucai Xiao committed
214
        m["a"] = migraphx::generate_argument(sa);
Shucai Xiao's avatar
Shucai Xiao committed
215
        m["b"] = migraphx::generate_argument(sb);
216
        std::vector<float> ref_result;
217
        migraphx::target ref_t = migraphx::make_target("ref");
218
        run_prog(p, ref_t, m, ref_result);
jerryyin's avatar
jerryyin committed
219
220
221
222
223
        // print ref_result
        std::cout << "ref_result: ";
        for(auto&& v : ref_result)
            std::cout << v << " ";
        std::cout << std::endl;
Shucai Xiao's avatar
Shucai Xiao committed
224
225

        std::vector<float> gpu_result;
226
        migraphx::target gpu_t = migraphx::make_target("gpu");
Shucai Xiao's avatar
Shucai Xiao committed
227
        run_prog(p, gpu_t, m, gpu_result);
jerryyin's avatar
jerryyin committed
228
229
230
231
        std::cout << "gpu_result: ";
        for(auto&& v : gpu_result)
            std::cout << v << " ";
        std::cout << std::endl;
Shucai Xiao's avatar
Shucai Xiao committed
232

jerryyin's avatar
jerryyin committed
233
234
       auto s = migraphx::gpu::dump_mlir(*p.get_main_module());
       //std::cout << s << std::endl;   
235
236
237
238
239
240
        // Note: the tolerance for mlir_enabled result is temporarily bumped
        // higher because the lowering pipeline between mlir fallback and
        // regular non-mlir pipeline diverged. MLIR fallback uses the
        // rewrite_quantization at the very end of the pipeline, whereas
        // the regular pipeline uses the rewrite_quantization in the much
        // earlier stage.
jerryyin's avatar
jerryyin committed
241
242
243
        //if(migraphx::gpu::mlir_enabled())
        //    EXPECT(migraphx::verify_range(ref_result, gpu_result, 1e5));
        //else
244
            EXPECT(migraphx::verify_range(ref_result, gpu_result));
Shucai Xiao's avatar
Shucai Xiao committed
245
246
247
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
248
int main(int argc, const char* argv[]) { test::run(argc, argv); }