quantization.cpp 53.8 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
26
27
#include <iostream>
#include <vector>
#include <migraphx/literal.hpp>
#include <migraphx/instruction.hpp>
28
#include <migraphx/generate.hpp>
29
#include <migraphx/register_target.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
30
#include <migraphx/verify.hpp>
31
#include <migraphx/apply_alpha_beta.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
32
#include <migraphx/quantization.hpp>
33
34
#include <migraphx/quantize_int8.hpp>
#include <migraphx/quantize_fp16.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
35
#include <migraphx/dead_code_elimination.hpp>
36
37
#include <migraphx/simplify_reshapes.hpp>
#include <migraphx/eliminate_common_subexpression.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
38
#include <migraphx/propagate_constant.hpp>
39
#include <migraphx/simplify_qdq.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
40
41
#include <migraphx/pass_manager.hpp>
#include <migraphx/onnx.hpp>
42
43
#include <migraphx/make_op.hpp>
#include <migraphx/serialize.hpp>
44
45
46
#include <migraphx/argument.hpp>
#include <migraphx/program.hpp>
#include <migraphx/shape.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
47
48
49
#include "test.hpp"
#include <migraphx/half.hpp>

50
static void optimize_prog_int8(migraphx::program& prog)
kahmed10's avatar
kahmed10 committed
51
{
52
53
54
55
    migraphx::run_passes(prog,
                         {migraphx::simplify_qdq{},
                          migraphx::eliminate_common_subexpression{},
                          migraphx::dead_code_elimination{}});
kahmed10's avatar
kahmed10 committed
56
57
}

Shucai Xiao's avatar
Shucai Xiao committed
58
59
TEST_CASE(param_add)
{
60
    auto create_program_float = [](bool add_return = false) {
Shucai Xiao's avatar
Shucai Xiao committed
61
        migraphx::program p;
62
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
63
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
64
65
        auto p1  = mm->add_parameter("x", s);
        auto p2  = mm->add_parameter("y", s);
66
        auto sum = mm->add_instruction(migraphx::make_op("add"), p1, p2);
67
68
        if(add_return)
        {
69
            mm->add_return({sum});
70
        }
Shucai Xiao's avatar
Shucai Xiao committed
71
72
73
74

        return p;
    };

75
    auto create_program_half = [](bool add_return = false) {
Shucai Xiao's avatar
Shucai Xiao committed
76
        migraphx::program p;
77
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
78
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
79
80
        auto p1  = mm->add_parameter("x", s);
        auto p2  = mm->add_parameter("y", s);
81
82
        auto hp1 = mm->add_instruction(migraphx::make_op("convert"), p1);
        auto hp2 = mm->add_instruction(migraphx::make_op("convert"), p2);
83
        auto hs  = mm->add_instruction(migraphx::make_op("add"), hp1, hp2);
84
        auto fs  = mm->add_instruction(
85
            migraphx::make_op("convert",
86
                               {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
87
            hs);
88
89
        if(add_return)
        {
90
91
92
93
94
            mm->add_return({fs});
        }
        else
        {
            mm->add_instruction(migraphx::make_op("identity"), {fs});
95
        }
Shucai Xiao's avatar
Shucai Xiao committed
96
97
98
99
100
101
102
103

        return p;
    };

    {
        auto p1 = create_program_float();
        auto p2 = create_program_half();

Shucai Xiao's avatar
Shucai Xiao committed
104
        migraphx::quantize_fp16(p1);
Shucai Xiao's avatar
Shucai Xiao committed
105
106
107
108
109
110
111
        EXPECT(p1 == p2);
    }

    {
        auto p1 = create_program_float();
        auto p2 = create_program_half();

Shucai Xiao's avatar
Shucai Xiao committed
112
        migraphx::quantize_fp16(p1, {"add"});
Shucai Xiao's avatar
Shucai Xiao committed
113
114
        EXPECT(p1 == p2);
    }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

    {
        auto p1 = create_program_float(true);
        auto p2 = create_program_half(true);

        migraphx::quantize_fp16(p1);
        EXPECT(p1 == p2);
    }

    {
        auto p1 = create_program_float(true);
        auto p2 = create_program_half(true);

        migraphx::quantize_fp16(p1, {"add"});
        EXPECT(p1 == p2);
    }
Shucai Xiao's avatar
Shucai Xiao committed
131
132
133
134
135
136
}

TEST_CASE(param_add_sub)
{
    auto create_program_float = [] {
        migraphx::program p;
137
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
138
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
139
140
        auto p1   = mm->add_parameter("x", s);
        auto p2   = mm->add_parameter("y", s);
141
142
        auto sum  = mm->add_instruction(migraphx::make_op("add"), p1, p2);
        auto diff = mm->add_instruction(migraphx::make_op("sub"), sum, p2);
143
144
        auto r    = mm->add_instruction(migraphx::make_op("add"), diff, p1);
        mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
145
146
147
148
149
150

        return p;
    };

    auto create_program_half_add = [] {
        migraphx::program p;
151
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
152
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
153
154
        auto p1  = mm->add_parameter("x", s);
        auto p2  = mm->add_parameter("y", s);
155
156
157
158
        auto hp1 = mm->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p1);
        auto hp2 = mm->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p2);
159
160
        auto hsum = mm->add_instruction(migraphx::make_op("add"), hp1, hp2);
        auto sum  = mm->add_instruction(
161
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), hsum);
162
        auto diff  = mm->add_instruction(migraphx::make_op("sub"), sum, p2);
163
        auto hdiff = mm->add_instruction(
164
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), diff);
165
166
167
168
        auto hadd = mm->add_instruction(migraphx::make_op("add"), hdiff, hp1);
        auto fadd = mm->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), hadd);
        mm->add_return({fadd});
Shucai Xiao's avatar
Shucai Xiao committed
169
170
171
172
173
174

        return p;
    };

    auto create_program_half_sub = [] {
        migraphx::program p;
175
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
176
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
177
178
        auto p1   = mm->add_parameter("x", s);
        auto p2   = mm->add_parameter("y", s);
179
180
        auto sum  = mm->add_instruction(migraphx::make_op("add"), p1, p2);
        auto hsum = mm->add_instruction(
181
182
183
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), sum);
        auto hp2 = mm->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p2);
184
185
        auto hdiff = mm->add_instruction(migraphx::make_op("sub"), hsum, hp2);
        auto diff  = mm->add_instruction(
186
187
188
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), hdiff);
        auto r = mm->add_instruction(migraphx::make_op("add"), diff, p1);
        mm->add_return({r});
189
190
191
192

        return p;
    };

Shucai Xiao's avatar
Shucai Xiao committed
193
194
195
196
    {
        auto p1 = create_program_float();
        auto p2 = create_program_half_add();

Shucai Xiao's avatar
Shucai Xiao committed
197
        migraphx::quantize_fp16(p1, {"add"});
Shucai Xiao's avatar
Shucai Xiao committed
198
199
200
201
202
203
204
        EXPECT(p1 == p2);
    }

    {
        auto p1 = create_program_float();
        auto p2 = create_program_half_sub();

Shucai Xiao's avatar
Shucai Xiao committed
205
        migraphx::quantize_fp16(p1, {"sub"});
206

Shucai Xiao's avatar
Shucai Xiao committed
207
208
        EXPECT(p1 == p2);
    }
209
210

    {
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
        auto create_program_fp16 = [] {
            migraphx::program p;
            auto* mm = p.get_main_module();
            migraphx::shape s{migraphx::shape::float_type, {2, 3}};
            auto p1  = mm->add_parameter("x", s);
            auto p2  = mm->add_parameter("y", s);
            auto hp1 = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p1);
            auto hp2 = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p2);
            auto hsum = mm->add_instruction(migraphx::make_op("add"), hp1, hp2);
            auto sum  = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), hsum);
            auto hsum1 = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), sum);
            auto p3 = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p2);
            auto diff  = mm->add_instruction(migraphx::make_op("sub"), hsum1, p3);
            auto fdiff = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), diff);
            auto hdiff1 = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), fdiff);
            auto p4 = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p1);
            auto res = mm->add_instruction(migraphx::make_op("add"), hdiff1, p4);
            auto r   = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), res);
            mm->add_return({r});

            return p;
        };

        auto create_program_quant_fp16 = [] {
            migraphx::program p;
            auto* mm = p.get_main_module();
            migraphx::shape s{migraphx::shape::float_type, {2, 3}};
            auto p1  = mm->add_parameter("x", s);
            auto p2  = mm->add_parameter("y", s);
            auto hp1 = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p1);
            auto hp2 = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), p2);
            auto hsum  = mm->add_instruction(migraphx::make_op("add"), hp1, hp2);
            auto hdiff = mm->add_instruction(migraphx::make_op("sub"), hsum, hp2);
            auto hres  = mm->add_instruction(migraphx::make_op("add"), hdiff, hp1);
            auto r     = mm->add_instruction(
                migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), hres);
            mm->add_return({r});

            return p;
        };

        auto p0 = create_program_float();
264
265
        migraphx::run_passes(
            p0, {migraphx::quantize_fp16_pass{{"all"}}, migraphx::dead_code_elimination{}});
266
        EXPECT(p0 == create_program_fp16());
267

268
        auto p1 = create_program_float();
Shucai Xiao's avatar
Shucai Xiao committed
269
        migraphx::quantize_fp16(p1);
270
        EXPECT(p1 == create_program_quant_fp16());
271
    }
Shucai Xiao's avatar
Shucai Xiao committed
272
273
274
275
276
277
}

TEST_CASE(literal_add)
{
    auto create_program_float = [] {
        migraphx::program p;
278
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
279
280
281
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
        std::vector<float> data(2 * 3);
        std::iota(data.begin(), data.end(), 1.0f);
282
283
        auto l1 = mm->add_literal(migraphx::literal(s, data));
        auto l2 = mm->add_literal(migraphx::literal(s, data));
284
        mm->add_instruction(migraphx::make_op("add"), l1, l2);
Shucai Xiao's avatar
Shucai Xiao committed
285
286
287
288
289
        return p;
    };

    auto create_program_half = [] {
        migraphx::program p;
290
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
291
292
293
        migraphx::shape s{migraphx::shape::half_type, {2, 3}};
        std::vector<migraphx::half> data(2 * 3);
        std::iota(data.begin(), data.end(), 1.0f);
294
295
        auto l1 = mm->add_literal(migraphx::literal(s, data));
        auto l2 = mm->add_literal(migraphx::literal(s, data));
296
        auto hs = mm->add_instruction(migraphx::make_op("add"), l1, l2);
297
        auto fs = mm->add_instruction(
298
299
300
            migraphx::make_op("convert",
                              {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
            hs);
301
        mm->add_instruction(migraphx::make_op("identity"), fs);
Shucai Xiao's avatar
Shucai Xiao committed
302
303
304
305
306
307
308
        return p;
    };

    {
        auto p1 = create_program_float();
        auto p2 = create_program_half();

Shucai Xiao's avatar
Shucai Xiao committed
309
        migraphx::quantize_fp16(p1, {"all"});
310
        migraphx::run_passes(*p1.get_main_module(),
Shucai Xiao's avatar
Shucai Xiao committed
311
                             {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
312
        migraphx::run_passes(*p2.get_main_module(),
Shucai Xiao's avatar
Shucai Xiao committed
313
                             {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
Shucai Xiao's avatar
Shucai Xiao committed
314
315
316
317
318
319
320
321

        EXPECT(p1 == p2);
    }

    {
        auto p1 = create_program_float();
        auto p2 = create_program_half();

Shucai Xiao's avatar
Shucai Xiao committed
322
        migraphx::quantize_fp16(p1, {"add"});
323
        migraphx::run_passes(*p1.get_main_module(),
Shucai Xiao's avatar
Shucai Xiao committed
324
                             {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
325
        migraphx::run_passes(*p2.get_main_module(),
Shucai Xiao's avatar
Shucai Xiao committed
326
                             {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
Shucai Xiao's avatar
Shucai Xiao committed
327
328
329
330
        EXPECT(p1 == p2);
    }
}

331
TEST_CASE(fp16_subgraph)
332
{
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
    auto create_program = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape sd{migraphx::shape::float_type, {1}};
        auto l1 = mm->add_literal(migraphx::literal(sd, {1}));
        auto l2 = mm->add_literal(migraphx::literal(sd, {2}));
        auto l3 = mm->add_literal(migraphx::literal(sd, {3}));
        migraphx::shape sx{migraphx::shape::float_type, {1, 4}};
        migraphx::shape sy{migraphx::shape::float_type, {3, 4}};
        migraphx::shape sc{migraphx::shape::bool_type};
        auto cond = mm->add_parameter("cond", sc);
        auto x    = mm->add_parameter("x", sx);
        auto y    = mm->add_parameter("y", sy);

        auto* then_mod = p.create_module("If_6_if");
        auto m1        = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {1, 4}}}), l1);
        auto add0 = then_mod->add_instruction(migraphx::make_op("add"), x, m1);
        auto m2   = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {3, 4}}}), l2);
        auto mul0  = then_mod->add_instruction(migraphx::make_op("mul"), y, m2);
        auto mfp16 = then_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), mul0);
        then_mod->add_return({add0, mul0, mfp16});

        auto* else_mod = p.create_module("If_6_else");
        auto me1       = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {1, 4}}}), l3);
        auto mul1 = else_mod->add_instruction(migraphx::make_op("mul"), x, me1);
        auto me2  = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {3, 4}}}), l3);
        auto add1  = else_mod->add_instruction(migraphx::make_op("add"), y, me2);
        auto afp16 = else_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), add1);
        else_mod->add_return({mul1, add1, afp16});

        auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
        auto r0  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
        auto r1  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), ret);
        auto r16 = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 2}}), ret);
        mm->add_return({r0, r1, r16});

        return p;
    };

    auto create_fp16_program = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();
381
        migraphx::shape sd{migraphx::shape::half_type, {1}};
382
383
384
385
386
387
388
        migraphx::shape sx{migraphx::shape::float_type, {1, 4}};
        migraphx::shape sy{migraphx::shape::float_type, {3, 4}};
        migraphx::shape sc{migraphx::shape::bool_type};
        auto cond      = mm->add_parameter("cond", sc);
        auto x         = mm->add_parameter("x", sx);
        auto y         = mm->add_parameter("y", sy);
        auto* then_mod = p.create_module("If_6_if");
389
390
391
        auto hl2       = then_mod->add_literal(migraphx::literal(sd, {2}));
        auto hl1       = then_mod->add_literal(migraphx::literal(sd, {1}));
        auto mhl1      = then_mod->add_instruction(
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
            migraphx::make_op("multibroadcast", {{"out_lens", {1, 4}}}), hl1);
        auto hx = then_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), x);
        auto ad  = then_mod->add_instruction(migraphx::make_op("add"), hx, mhl1);
        auto fad = then_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), ad);
        auto mhl2 = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {3, 4}}}), hl2);
        auto hy1 = then_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), y);
        auto mu  = then_mod->add_instruction(migraphx::make_op("mul"), hy1, mhl2);
        auto fmu = then_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), mu);
        then_mod->add_return({fad, fmu, mu});

        auto* else_mod = p.create_module("If_6_else");
408
409
        auto hl3       = else_mod->add_literal(migraphx::literal(sd, {3}));
        auto mhl3      = else_mod->add_instruction(
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
            migraphx::make_op("multibroadcast", {{"out_lens", {1, 4}}}), hl3);
        auto hx2 = else_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), x);
        auto mu1  = else_mod->add_instruction(migraphx::make_op("mul"), hx2, mhl3);
        auto fmu1 = else_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), mu1);
        auto mhl4 = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {3, 4}}}), hl3);
        auto hy = else_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), y);
        auto ad1  = else_mod->add_instruction(migraphx::make_op("add"), hy, mhl4);
        auto fad1 = else_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), ad1);
        else_mod->add_return({fmu1, fad1, ad1});

        auto iff = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
        auto r0  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), iff);
        auto r1  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), iff);
        auto r2  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 2}}), iff);
        mm->add_return({r0, r1, r2});

        return p;
432
433
    };

434
435
436
437
438
439
440
441
442
443
    auto p1 = create_program();
    migraphx::quantize_fp16(p1);

    auto p2 = create_fp16_program();

    EXPECT(p1 == p2);
}

TEST_CASE(op_capture)
{
444
445
    auto create_program_float = [] {
        migraphx::program p;
446
        auto* mm = p.get_main_module();
447
448
449
        migraphx::shape s1{migraphx::shape::float_type, {3, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {3, 6}};

450
451
452
453
        auto p1 = mm->add_parameter("x", s1);
        auto p2 = mm->add_parameter("y", s1);
        auto pb = mm->add_parameter("b", s2);
        auto pc = mm->add_parameter("c", s2);
454
        auto pa = mm->add_instruction(migraphx::make_op("add"), p1, p2);
455
456
        auto ps =
            migraphx::add_apply_alpha_beta(*mm, {pa, pb, pc}, migraphx::make_op("dot"), 1.0f, 1.0f);
457
        mm->add_instruction(migraphx::make_op("dot"), pa, ps);
458
459
460
461
462
463

        return p;
    };

    auto create_program_op = [&] {
        migraphx::program p;
464
        auto* mm = p.get_main_module();
465
466
467
        migraphx::shape s1{migraphx::shape::float_type, {3, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {3, 6}};

468
469
470
471
        auto p1  = mm->add_parameter("x", s1);
        auto p2  = mm->add_parameter("y", s1);
        auto pb  = mm->add_parameter("b", s2);
        auto pc  = mm->add_parameter("c", s2);
472
        auto pa  = mm->add_instruction(migraphx::make_op("add"), p1, p2);
473
474
        auto opa = mm->add_instruction(migraphx::make_op("capture", {{"ins_index", 0}}), pa);
        auto opb = mm->add_instruction(migraphx::make_op("capture", {{"ins_index", 1}}), pb);
475
476
477
478
        auto ps  = migraphx::add_apply_alpha_beta(
            *mm, {opa, opb, pc}, migraphx::make_op("dot"), 1.0f, 1.0f);
        auto opm = mm->add_instruction(migraphx::make_op("capture", {{"ins_index", 2}}), pa);
        auto ops = mm->add_instruction(migraphx::make_op("capture", {{"ins_index", 3}}), ps);
479
        mm->add_instruction(migraphx::make_op("dot"), opm, ops);
480
481
482
483
484

        return p;
    };

    {
485
486
        auto p                  = create_program_float();
        auto op_capture_p       = create_program_op();
487
        migraphx::target t      = migraphx::make_target("ref");
488
489
490
        std::size_t param_index = 0;
        migraphx::run_passes(
            p, {migraphx::capture_arguments_pass{{"dot", "convolution"}, {}, &param_index}});
491
492
493
494
        EXPECT(p == op_capture_p);
    }
}

495
TEST_CASE(op_capture_subgraph)
496
497
498
{
    auto create_program = [] {
        migraphx::program p;
499
        auto* mm = p.get_main_module();
500
501
502
503
504
505
        migraphx::shape sx{migraphx::shape::float_type, {2, 2, 4, 8}};
        migraphx::shape sy{migraphx::shape::float_type, {2, 2, 8, 6}};
        migraphx::shape sc{migraphx::shape::bool_type};
        auto cond = mm->add_parameter("cond", sc);
        auto a    = mm->add_parameter("a", sx);
        auto b    = mm->add_parameter("b", sy);
506

507
508
509
510
        migraphx::shape sd{migraphx::shape::float_type, {2, 2, 4, 6}};
        migraphx::shape sw{migraphx::shape::float_type, {2, 2, 1, 1}};
        auto x = mm->add_parameter("x", sd);
        auto w = mm->add_parameter("w", sw);
511

512
513
514
        auto* then_mod = p.create_module("If_6_if");
        auto out1      = then_mod->add_instruction(migraphx::make_op("dot"), a, b);
        then_mod->add_return({out1});
515

516
517
518
        auto* else_mod = p.create_module("If_6_else");
        auto out2      = else_mod->add_instruction(migraphx::make_op("convolution"), x, w);
        else_mod->add_return({out2});
519

520
521
        auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
        mm->add_return({ret});
522
523
524
525

        return p;
    };

526
    auto create_program_op = [&] {
527
        migraphx::program p;
528
        auto* mm = p.get_main_module();
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
        migraphx::shape sx{migraphx::shape::float_type, {2, 2, 4, 8}};
        migraphx::shape sy{migraphx::shape::float_type, {2, 2, 8, 6}};
        migraphx::shape sc{migraphx::shape::bool_type};
        auto cond = mm->add_parameter("cond", sc);
        auto a    = mm->add_parameter("a", sx);
        auto b    = mm->add_parameter("b", sy);

        migraphx::shape sd{migraphx::shape::float_type, {2, 2, 4, 6}};
        migraphx::shape sw{migraphx::shape::float_type, {2, 2, 1, 1}};
        auto x = mm->add_parameter("x", sd);
        auto w = mm->add_parameter("w", sw);

        auto* then_mod = p.create_module("If_6_if");
        auto ca   = then_mod->add_instruction(migraphx::make_op("capture", {{"ins_index", 2}}), a);
        auto cb   = then_mod->add_instruction(migraphx::make_op("capture", {{"ins_index", 3}}), b);
        auto out1 = then_mod->add_instruction(migraphx::make_op("dot"), ca, cb);
        then_mod->add_return({out1});

        auto* else_mod = p.create_module("If_6_else");
        auto cx   = else_mod->add_instruction(migraphx::make_op("capture", {{"ins_index", 0}}), x);
        auto cw   = else_mod->add_instruction(migraphx::make_op("capture", {{"ins_index", 1}}), w);
        auto out2 = else_mod->add_instruction(migraphx::make_op("convolution"), cx, cw);
        else_mod->add_return({out2});

        auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
        mm->add_return({ret});
555
556
557
558

        return p;
    };

559
560
561
    {
        auto p                  = create_program();
        auto op_capture_p       = create_program_op();
562
        migraphx::target t      = migraphx::make_target("ref");
563
564
565
        std::size_t param_index = 0;
        migraphx::run_passes(
            p, {migraphx::capture_arguments_pass{{"dot", "convolution"}, {}, &param_index}});
566

567
568
        EXPECT(p == op_capture_p);
    }
569
570
}

571
TEST_CASE(dot_float)
572
573
574
{
    auto create_program = [] {
        migraphx::program p;
575
        auto* mm = p.get_main_module();
576
577
578
        migraphx::shape sa{migraphx::shape::float_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
        migraphx::shape sc{migraphx::shape::float_type, {2, 8}};
579
580
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
581

582
        auto r = migraphx::add_apply_alpha_beta(*mm, {pa, pb}, migraphx::make_op("dot"));
583
        mm->add_return({r});
584
585
586
587
588
589

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
590
        auto* mm = p.get_main_module();
591
592
593
        migraphx::shape sa{migraphx::shape::float_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
        migraphx::shape sc{migraphx::shape::float_type, {2, 8}};
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
        auto pa      = mm->add_parameter("a", sa);
        auto pb      = mm->add_parameter("b", sb);
        auto zp_a    = mm->add_literal(static_cast<int8_t>(0));
        auto scale_a = mm->add_literal(10.0f);
        scale_a      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), scale_a);
        zp_a = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}),
                                   zp_a);
        auto qa  = mm->add_instruction(migraphx::make_op("quantizelinear"), pa, scale_a, zp_a);
        auto dqa = mm->add_instruction(migraphx::make_op("dequantizelinear"), qa, scale_a, zp_a);

        auto zp_b    = mm->add_literal(static_cast<int8_t>(0));
        auto scale_b = mm->add_literal(10.0f);
        scale_b      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}), scale_b);
        zp_b = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}),
                                   zp_b);
        auto qb  = mm->add_instruction(migraphx::make_op("quantizelinear"), pb, scale_b, zp_b);
        auto dqb = mm->add_instruction(migraphx::make_op("dequantizelinear"), qb, scale_b, zp_b);

614
        auto r = migraphx::add_apply_alpha_beta(*mm, {dqa, dqb}, migraphx::make_op("dot"));
615
        mm->add_return({r});
616
617
618
619

        return p;
    };

620
    auto create_int8_optimized_prog = [] {
621
        migraphx::program p;
622
        auto* mm = p.get_main_module();
623
624
625
        migraphx::shape sa{migraphx::shape::float_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
        migraphx::shape sc{migraphx::shape::float_type, {2, 8}};
626
627
        auto pa      = mm->add_parameter("a", sa);
        auto pb      = mm->add_parameter("b", sb);
628
629
630
631
632
633
634
635
636
637
638
        auto zp      = mm->add_literal(static_cast<int8_t>(0));
        auto scale   = mm->add_literal(10.0f);
        auto scale_a = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), scale);
        auto zp_a =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), zp);
        auto quant_a = mm->add_instruction(migraphx::make_op("quantizelinear"), pa, scale_a, zp_a);
        auto scale_b = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}), scale);
        auto zp_b =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}), zp);
639
640
641
642
643
644
        auto quant_b  = mm->add_instruction(migraphx::make_op("quantizelinear"), pb, scale_b, zp_b);
        auto quant    = mm->add_instruction(migraphx::make_op("quant_dot"), quant_a, quant_b);
        auto scale_mb = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", quant->get_shape().lens()}}), scale);
        auto out_scale = mm->add_instruction(migraphx::make_op("mul"), scale_mb, scale_mb);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), quant, out_scale);
645
        mm->add_return({r});
646
647
648
649

        return p;
    };

650
651
652
653
654
655
656
657
    const std::vector<std::pair<float, float>> quant_params = {
        {0.1f, 0.0f}, {0.1f, 0.0f}, {0.1f, 100.0f}};
    auto p                  = create_program();
    std::size_t param_index = 0;
    migraphx::run_passes(p, {migraphx::capture_arguments_pass{{"dot"}, {}, &param_index}});
    migraphx::run_passes(
        p,
        {migraphx::quantize_int8_pass{{"dot"}, quant_params}, migraphx::dead_code_elimination{}});
658
    auto qp = create_int8_quantized_prog();
659

660
    EXPECT(p == qp);
661
662
663
664

    optimize_prog_int8(p);
    auto op = create_int8_optimized_prog();
    EXPECT(p == op);
665
666
}

667
TEST_CASE(dot_double_2args)
668
669
670
{
    auto create_program = [] {
        migraphx::program p;
671
        auto* mm = p.get_main_module();
672
673
674
675
        migraphx::shape sa{migraphx::shape::double_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::double_type, {16, 8}};
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
676
        auto r  = migraphx::add_apply_alpha_beta(*mm, {pa, pb}, migraphx::make_op("dot"));
677
        mm->add_return({r});
678
679
680
681
682
683

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
684
        auto* mm = p.get_main_module();
685
686
        migraphx::shape sa{migraphx::shape::double_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::double_type, {16, 8}};
687
688
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
689

690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
        auto zp_a    = mm->add_literal(static_cast<int8_t>(0));
        auto scale_a = mm->add_literal(10.0);
        scale_a      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), scale_a);
        zp_a = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}),
                                   zp_a);
        auto qa   = mm->add_instruction(migraphx::make_op("quantizelinear"), pa, scale_a, zp_a);
        auto dqa  = mm->add_instruction(migraphx::make_op("dequantizelinear"), qa, scale_a, zp_a);
        auto zp_b = mm->add_literal(static_cast<int8_t>(0));
        auto scale_b = mm->add_literal(5.0);
        scale_b      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}), scale_b);
        zp_b = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}),
                                   zp_b);
        auto qb  = mm->add_instruction(migraphx::make_op("quantizelinear"), pb, scale_b, zp_b);
        auto dqb = mm->add_instruction(migraphx::make_op("dequantizelinear"), qb, scale_b, zp_b);
706
        auto r   = migraphx::add_apply_alpha_beta(*mm, {dqa, dqb}, migraphx::make_op("dot"));
707
        mm->add_return({r});
708
709
710
        return p;
    };

711
    auto create_int8_optimized_prog = [] {
712
        migraphx::program p;
713
        auto* mm = p.get_main_module();
714
715
        migraphx::shape sa{migraphx::shape::double_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::double_type, {16, 8}};
716
717
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
718

719
720
721
722
        auto scale_a_lit = mm->add_literal(10.0);
        auto zp          = mm->add_literal(static_cast<int8_t>(0));
        auto scale_a     = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), scale_a_lit);
723
724
        auto zp_a =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), zp);
725
726
727
728
        auto qa = mm->add_instruction(migraphx::make_op("quantizelinear"), pa, scale_a, zp_a);
        auto scale_b_lit = mm->add_literal(5.0);
        auto scale_b     = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}), scale_b_lit);
729
730
        auto zp_b =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}), zp);
731
732
733
734
735
736
737
738
739
740
        auto qb   = mm->add_instruction(migraphx::make_op("quantizelinear"), pb, scale_b, zp_b);
        auto qdot = mm->add_instruction(migraphx::make_op("quant_dot"), qa, qb);
        auto scale_a_mb = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", qdot->get_shape().lens()}}),
            scale_a_lit);
        auto scale_b_mb = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", qdot->get_shape().lens()}}),
            scale_b_lit);
        auto out_scale = mm->add_instruction(migraphx::make_op("mul"), scale_a_mb, scale_b_mb);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), qdot, out_scale);
741
        mm->add_return({r});
742
743
744
745
        return p;
    };

    auto p = create_program();
746
747
748
749
750
751
752
753
754
755
    const std::vector<std::pair<float, float>>& quant_params{{0.1f, 0.0f}, {0.2f, 0.0f}};
    std::size_t param_index = 0;
    migraphx::run_passes(p, {migraphx::capture_arguments_pass{{"dot"}, {}, &param_index}});
    migraphx::run_passes(
        p,
        {migraphx::quantize_int8_pass{{"dot"}, quant_params}, migraphx::dead_code_elimination{}});
    EXPECT(p == create_int8_quantized_prog());

    optimize_prog_int8(p);
    EXPECT(p == create_int8_optimized_prog());
756
757
}

758
TEST_CASE(dot_half_1arg)
759
760
761
{
    auto create_program = [] {
        migraphx::program p;
762
        auto* mm = p.get_main_module();
763
764
        migraphx::shape s{migraphx::shape::half_type, {9, 9}};
        auto x = mm->add_parameter("x", s);
765
        auto r = mm->add_instruction(migraphx::make_op("dot"), x, x);
766
        mm->add_return({r});
767
768
769
770
771
772

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
773
        auto* mm = p.get_main_module();
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
        migraphx::shape sa{migraphx::shape::half_type, {9, 9}};
        auto x = mm->add_parameter("x", sa);

        auto zp_a    = mm->add_literal(static_cast<int8_t>(0));
        auto scale_a = mm->add_literal(migraphx::literal({sa.type()}, {10.0}));
        scale_a      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), scale_a);
        zp_a = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}),
                                   zp_a);
        auto qa   = mm->add_instruction(migraphx::make_op("quantizelinear"), x, scale_a, zp_a);
        auto dqa  = mm->add_instruction(migraphx::make_op("dequantizelinear"), qa, scale_a, zp_a);
        auto zp_b = mm->add_literal(static_cast<int8_t>(0));
        auto scale_b = mm->add_literal(migraphx::literal({sa.type()}, {10.0}));
        scale_b      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), scale_b);
        zp_b = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}),
                                   zp_b);
        auto qb  = mm->add_instruction(migraphx::make_op("quantizelinear"), x, scale_b, zp_b);
        auto dqb = mm->add_instruction(migraphx::make_op("dequantizelinear"), qb, scale_b, zp_b);
793
        auto r   = mm->add_instruction(migraphx::make_op("dot"), dqa, dqb);
794
795
796
        mm->add_return({r});
        return p;
    };
797

798
799
800
801
802
803
    auto create_int8_optimized_prog = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape sa{migraphx::shape::half_type, {9, 9}};
        auto x = mm->add_parameter("x", sa);

804
805
806
807
        auto zp        = mm->add_literal(static_cast<int8_t>(0));
        auto scale_lit = mm->add_literal(migraphx::literal({sa.type()}, {10.0}));
        auto scale     = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), scale_lit);
808
809
        zp =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), zp);
810
811
812
813
        auto qx        = mm->add_instruction(migraphx::make_op("quantizelinear"), x, scale, zp);
        auto qdot      = mm->add_instruction(migraphx::make_op("quant_dot"), qx, qx);
        auto out_scale = mm->add_instruction(migraphx::make_op("mul"), scale, scale);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), qdot, out_scale);
814
        mm->add_return({r});
815
816
817
818
        return p;
    };

    auto p = create_program();
819
820
821
822
823
824
825
826
827
828
    const std::vector<std::pair<float, float>>& quant_params{{0.1f, 0.0f}, {0.1f, 0.0f}};
    std::size_t param_index = 0;
    migraphx::run_passes(p, {migraphx::capture_arguments_pass{{"dot"}, {}, &param_index}});
    migraphx::run_passes(
        p,
        {migraphx::quantize_int8_pass{{"dot"}, quant_params}, migraphx::dead_code_elimination{}});
    EXPECT(p == create_int8_quantized_prog());

    optimize_prog_int8(p);
    EXPECT(p == create_int8_optimized_prog());
829
830
}

831
832
TEST_CASE(conv_float)
{
Shucai Xiao's avatar
Shucai Xiao committed
833
    auto create_program = [] {
834
        migraphx::program p;
835
        auto* mm = p.get_main_module();
836
        auto input =
837
            mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
838
        auto weights =
839
            mm->add_parameter("w", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
840
841
        auto r = mm->add_instruction(migraphx::make_op("convolution"), input, weights);
        mm->add_return({r});
842
843
844
845
846
847

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
848
        auto* mm = p.get_main_module();
849
850
        migraphx::shape sx{migraphx::shape::float_type, {4, 3, 3, 3}};
        migraphx::shape sw{migraphx::shape::float_type, {4, 3, 3, 3}};
851
852
        auto px = mm->add_parameter("x", sx);
        auto pw = mm->add_parameter("w", sw);
853

854
855
856
857
        auto zp        = mm->add_literal(static_cast<int8_t>(0));
        auto scale_lit = mm->add_literal(10.0f);
        auto scale     = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sx.lens()}}), scale_lit);
858
859
860
861
862
863
864
        zp =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sx.lens()}}), zp);
        auto quant_x = mm->add_instruction(migraphx::make_op("quantizelinear"), px, scale, zp);
        auto quant_w = mm->add_instruction(migraphx::make_op("quantizelinear"), pw, scale, zp);

        auto quant = mm->add_instruction(migraphx::make_op("quant_convolution"), quant_x, quant_w);

865
866
867
868
869
        auto scale_mb = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", quant->get_shape().lens()}}),
            scale_lit);
        auto out_scale = mm->add_instruction(migraphx::make_op("mul"), scale_mb, scale_mb);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), quant, out_scale);
870
        mm->add_return({r});
871
872
873
874
875
876

        return p;
    };

    auto p = create_program();
    const std::vector<std::pair<float, float>>& quant_params{{0.1f, 0.0f}, {0.1f, 0.0f}};
877
878
879
880
    std::size_t param_index = 0;
    migraphx::run_passes(p, {migraphx::capture_arguments_pass{{"convolution"}, {}, &param_index}});
    migraphx::run_passes(p, {migraphx::quantize_int8_pass{{"convolution"}, quant_params}});
    optimize_prog_int8(p);
881
882
883
884
885
    auto qp = create_int8_quantized_prog();

    EXPECT(p == qp);
}

886
TEST_CASE(conv_float_throw)
887
{
Shucai Xiao's avatar
Shucai Xiao committed
888
    auto create_program = [] {
889
        migraphx::program p;
890
        auto* mm = p.get_main_module();
891
        auto input =
892
            mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
893
        auto weights =
894
895
896
            mm->add_parameter("w", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
        auto r = mm->add_instruction(migraphx::make_op("convolution"), input, weights);
        mm->add_return({r});
897
898
899
900
901
902

        return p;
    };

    auto p = create_program();
    const std::vector<std::pair<float, float>>& quant_params{{0.1f, 0.0f}, {0.1f, 0.0f}};
903
904
905
    test::throws([&] {
        migraphx::run_passes(p, {migraphx::quantize_int8_pass{{"add"}, quant_params}});
    });
906
907
908
909
}

TEST_CASE(conv_half)
{
Shucai Xiao's avatar
Shucai Xiao committed
910
    auto create_program = [] {
911
        migraphx::program p;
912
        auto* mm = p.get_main_module();
913
        auto input =
914
            mm->add_parameter("x", migraphx::shape{migraphx::shape::half_type, {4, 3, 3, 3}});
915
        auto weights =
916
            mm->add_parameter("w", migraphx::shape{migraphx::shape::half_type, {4, 3, 3, 3}});
917
918
        auto r = mm->add_instruction(migraphx::make_op("convolution"), input, weights);
        mm->add_return({r});
919
920
921
922
923
924

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
925
        auto* mm = p.get_main_module();
926
927
        migraphx::shape sx{migraphx::shape::half_type, {4, 3, 3, 3}};
        migraphx::shape sw{migraphx::shape::half_type, {4, 3, 3, 3}};
928
929
        auto px = mm->add_parameter("x", sx);
        auto pw = mm->add_parameter("w", sw);
930

931
932
933
934
        auto zp        = mm->add_literal(static_cast<int8_t>(0));
        auto scale_lit = mm->add_literal(migraphx::literal({sx.type()}, {10.0}));
        auto scale     = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sx.lens()}}), scale_lit);
935
936
937
938
939
940
        zp =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sx.lens()}}), zp);
        auto quant_x = mm->add_instruction(migraphx::make_op("quantizelinear"), px, scale, zp);
        auto quant_w = mm->add_instruction(migraphx::make_op("quantizelinear"), pw, scale, zp);

        auto quant = mm->add_instruction(migraphx::make_op("quant_convolution"), quant_x, quant_w);
941
942
943
944
945
        auto scale_mb = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", quant->get_shape().lens()}}),
            scale_lit);
        auto out_scale = mm->add_instruction(migraphx::make_op("mul"), scale_mb, scale_mb);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), quant, out_scale);
946
        mm->add_return({r});
947
948
949
950
951
952

        return p;
    };

    auto p = create_program();
    const std::vector<std::pair<float, float>>& quant_params{{0.1f, 0.0f}, {0.1f, 0.0f}};
953
954
955
956
    std::size_t param_index = 0;
    migraphx::run_passes(p, {migraphx::capture_arguments_pass{{"convolution"}, {}, &param_index}});
    migraphx::run_passes(p, {migraphx::quantize_int8_pass{{"convolution"}, quant_params}});
    optimize_prog_int8(p);
957
958
959
960
961
    auto qp = create_int8_quantized_prog();

    EXPECT(p == qp);
}

962
963
964
965
966
967
template <class T>
auto get_hash(const T& x)
{
    return std::hash<T>{}(x);
}

968
969
970
971
TEST_CASE(target_copy)
{
    auto run_prog = [](migraphx::program p,
                       const migraphx::target& t,
972
                       migraphx::parameter_map& m_in,
973
974
                       std::vector<float>& res) {
        p.compile(t);
975
        migraphx::parameter_map m;
976
977
978
979
980
981
982
983
984
985
986
987
        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);
            }
        }

988
        auto result = t.copy_from(p.eval(m).back());
989
990
991
992
993
        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
    };

    auto create_program = [] {
        migraphx::program p;
994
        auto* mm = p.get_main_module();
995
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
996
997
        auto p1 = mm->add_parameter("x", s);
        auto p2 = mm->add_parameter("y", s);
998
        mm->add_instruction(migraphx::make_op("add"), p1, p2);
999
1000
1001
1002
1003
1004

        return p;
    };

    {
        auto p = create_program();
1005
        migraphx::parameter_map m;
1006
1007
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
        m["x"] = migraphx::generate_argument(s);
1008
        std::vector<float> ref_result;
1009
        migraphx::target ref_t = migraphx::make_target("ref");
1010
        run_prog(p, ref_t, m, ref_result);
1011
1012

        std::vector<float> orig_result;
1013
        run_prog(p, ref_t, m, orig_result);
1014

1015
        EXPECT(migraphx::verify::verify_rms_range(ref_result, orig_result));
1016
1017
1018
    }
}

1019
TEST_CASE(int8_quantization_dot)
1020
1021
1022
{
    auto run_prog = [](migraphx::program p,
                       const migraphx::target& t,
1023
                       migraphx::parameter_map& m_in,
1024
1025
                       std::vector<float>& res,
                       bool b_quantize = false) {
Shucai Xiao's avatar
Shucai Xiao committed
1026
        if(b_quantize)
1027
        {
1028
            std::vector<migraphx::parameter_map> cali_data;
1029
1030
1031
1032
            cali_data.push_back(m_in);
            migraphx::quantize_int8(p, t, cali_data);
        }
        p.compile(t);
1033
        migraphx::parameter_map m;
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
        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);
            }
        }

1046
        auto result = t.copy_from(p.eval(m).back());
1047
1048
1049
1050
1051
        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
    };

    auto create_program = [] {
        migraphx::program p;
1052
        auto* mm = p.get_main_module();
1053
1054
1055
        migraphx::shape sa{migraphx::shape::float_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
        migraphx::shape sc{migraphx::shape::float_type, {2, 8}};
1056
1057
1058
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
        auto pc = mm->add_parameter("c", sc);
1059
1060
        auto r =
            migraphx::add_apply_alpha_beta(*mm, {pa, pb, pc}, migraphx::make_op("dot"), 1.0f, 1.0f);
1061
        mm->add_return({r});
1062
1063
1064
1065
1066
        return p;
    };

    {
        auto p = create_program();
1067
        migraphx::parameter_map m;
1068
        migraphx::shape sa{migraphx::shape::float_type, {2, 16}};
1069
1070
1071
        migraphx::shape sb{migraphx::shape::float_type, {16, 8}};
        m["a"] = migraphx::generate_argument(sa, get_hash(std::string("a")));
        m["b"] = migraphx::generate_argument(sb, get_hash(std::string("b")));
1072
        std::vector<float> quant_result;
1073
        migraphx::target ref_t = migraphx::make_target("ref");
1074
        run_prog(p, ref_t, m, quant_result, true);
1075
1076

        std::vector<float> no_quant_result;
1077
        run_prog(p, ref_t, m, no_quant_result);
1078

1079
1080
1081
1082
        EXPECT(migraphx::verify::verify_range_with_tolerance(
            quant_result,
            migraphx::verify::expected{no_quant_result},
            migraphx::verify::tolerance{0.003}));
1083
1084
1085
    }
}

1086
1087
1088
1089
1090
1091
1092
1093
TEST_CASE(int8_quantization_conv)
{
    auto run_prog = [](migraphx::program p,
                       const migraphx::target& t,
                       std::vector<float>& res,
                       bool b_quantize = false) {
        if(b_quantize)
        {
1094
            std::vector<migraphx::parameter_map> cali_data;
1095
1096
1097
            migraphx::quantize_int8(p, t, cali_data);
        }
        p.compile(t);
1098
        migraphx::parameter_map m;
1099

1100
        auto result = t.copy_from(p.eval(m).back());
1101
1102
1103
1104
1105
        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
    };

    auto create_program = [] {
        migraphx::program p;
1106
        auto* mm = p.get_main_module();
1107
1108
1109
        migraphx::shape sx{migraphx::shape::float_type, {4, 2, 2, 2}};
        migraphx::shape sw{migraphx::shape::float_type, {4, 2, 2, 2}};
        std::vector<float> v(sx.elements(), 0.5f);
1110
1111
        auto input   = mm->add_literal(migraphx::literal(sx, v));
        auto weights = mm->add_literal(migraphx::literal(sw, v));
1112
1113
        auto r       = mm->add_instruction(migraphx::make_op("convolution"), input, weights);
        mm->add_return({r});
1114
1115
1116
1117
1118
1119
1120

        return p;
    };

    {
        auto p = create_program();
        std::vector<float> quant_result;
1121
        migraphx::target ref_t = migraphx::make_target("ref");
1122
        run_prog(p, ref_t, quant_result, true);
1123
1124

        std::vector<float> no_quant_result;
1125
        run_prog(p, ref_t, no_quant_result);
1126

1127
        EXPECT(migraphx::verify::verify_rms_range(quant_result, no_quant_result));
1128
1129
1130
    }
}

1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
TEST_CASE(int8_subgraph)
{
    auto create_program = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape sx{migraphx::shape::float_type, {2, 2, 4, 8}};
        migraphx::shape sy{migraphx::shape::float_type, {2, 2, 8, 6}};
        migraphx::shape sc{migraphx::shape::bool_type};
        auto cond = mm->add_parameter("cond", sc);
        auto a    = mm->add_parameter("a", sx);
        auto b    = mm->add_parameter("b", sy);

        migraphx::shape sd{migraphx::shape::float_type, {2, 2, 4, 6}};
        migraphx::shape sw{migraphx::shape::float_type, {2, 2, 1, 1}};
        auto x = mm->add_parameter("x", sd);
        auto w = mm->add_parameter("w", sw);

        auto* then_mod = p.create_module("If_6_if");
1149
        auto out1 = migraphx::add_apply_alpha_beta(*then_mod, {a, b}, migraphx::make_op("dot"));
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
        then_mod->add_return({out1});

        auto* else_mod = p.create_module("If_6_else");
        auto out2      = else_mod->add_instruction(migraphx::make_op("convolution"), x, w);
        else_mod->add_return({out2});

        auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
        mm->add_return({ret});

        return p;
    };

    auto create_int8_program = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape sx{migraphx::shape::float_type, {2, 2, 4, 8}};
        migraphx::shape sy{migraphx::shape::float_type, {2, 2, 8, 6}};
        migraphx::shape sout{migraphx::shape::float_type, {2, 2, 4, 6}};
        migraphx::shape sc{migraphx::shape::bool_type};
        auto cond = mm->add_parameter("cond", sc);
        auto a    = mm->add_parameter("a", sx);
        auto b    = mm->add_parameter("b", sy);

        // then submod
        auto* then_mod = p.create_module("If_6_if");
        auto zp1       = then_mod->add_literal(static_cast<int8_t>(0));
        auto s1        = then_mod->add_literal(10.0f);
        auto sa        = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sx.lens()}}), s1);
        auto zpa = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sx.lens()}}), zp1);
        auto qa = then_mod->add_instruction(migraphx::make_op("quantizelinear"), a, sa, zpa);
        auto sb = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sy.lens()}}), s1);
        auto zpb = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sy.lens()}}), zp1);
1187
1188
1189
1190
1191
1192
        auto qb    = then_mod->add_instruction(migraphx::make_op("quantizelinear"), b, sb, zpb);
        auto qdot  = then_mod->add_instruction(migraphx::make_op("quant_dot"), qa, qb);
        auto s1_mb = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", qdot->get_shape().lens()}}), s1);
        auto so = then_mod->add_instruction(migraphx::make_op("mul"), s1_mb, s1_mb);
        auto r  = then_mod->add_instruction(migraphx::make_op("dequantizelinear"), qdot, so);
1193
1194
1195
1196
1197
1198
1199
1200
        then_mod->add_return({r});

        migraphx::shape sd{migraphx::shape::float_type, {2, 2, 4, 6}};
        migraphx::shape sw{migraphx::shape::float_type, {2, 2, 1, 1}};
        auto x = mm->add_parameter("x", sd);
        auto w = mm->add_parameter("w", sw);
        // else submod
        auto* else_mod = p.create_module("If_6_else");
1201
        auto sax_lit   = else_mod->add_literal(2.0f);
1202
        auto zp        = else_mod->add_literal(static_cast<int8_t>(0));
1203
1204
        auto sax       = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sd.lens()}}), sax_lit);
1205
1206
        auto zpx = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sd.lens()}}), zp);
1207
1208
1209
1210
        auto qx      = else_mod->add_instruction(migraphx::make_op("quantizelinear"), x, sax, zpx);
        auto ssw_lit = else_mod->add_literal(1.66667f);
        auto ssw     = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sw.lens()}}), ssw_lit);
1211
1212
        auto zpw = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sw.lens()}}), zp);
1213
1214
1215
1216
1217
1218
1219
        auto qw     = else_mod->add_instruction(migraphx::make_op("quantizelinear"), w, ssw, zpw);
        auto qconv  = else_mod->add_instruction(migraphx::make_op("quant_convolution"), qx, qw);
        auto ssw_mb = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", qconv->get_shape().lens()}}),
            ssw_lit);
        auto so1 = else_mod->add_instruction(migraphx::make_op("mul"), sax, ssw_mb);
        auto r1  = else_mod->add_instruction(migraphx::make_op("dequantizelinear"), qconv, so1);
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
        else_mod->add_return({r1});

        auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
        mm->add_return({ret});

        return p;
    };

    auto p1 = create_program();
    const std::vector<std::pair<float, float>>& quant_params{
        {0.5f, 0.0f}, {0.6f, 0.0f}, {0.1f, 0.0f}, {0.1f, 0.0f}};
    std::size_t param_index = 0;
    migraphx::run_passes(
        p1, {migraphx::capture_arguments_pass{{"convolution", "dot"}, {}, &param_index}});
    migraphx::run_passes(p1, {migraphx::quantize_int8_pass{{"convolution", "dot"}, quant_params}});
    optimize_prog_int8(p1);

    auto p2 = create_int8_program();
    EXPECT(p1 == p2);
}

TEST_CASE(test_op_capture)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s1{migraphx::shape::float_type, {3, 3}};
    migraphx::shape s2{migraphx::shape::float_type, {3, 6}};
    std::vector<float> d1(s1.elements());
    std::vector<float> d2(s2.elements());
    std::iota(d1.begin(), d1.end(), 0.0f);
    std::iota(d2.begin(), d2.end(), 0.0f);

    auto p1 = mm->add_literal(s1, d1);
    auto p2 = mm->add_literal(s1, d1);
    auto pb = mm->add_literal(s2, d2);
    auto pc = mm->add_literal(s2, d2);
    auto pa = mm->add_instruction(migraphx::make_op("add"), p1, p2);
1257
1258
    auto ps =
        migraphx::add_apply_alpha_beta(*mm, {pa, pb, pc}, migraphx::make_op("dot"), 1.0f, 1.0f);
1259
1260
1261
1262
1263
    mm->add_instruction(migraphx::make_op("dot"), pa, ps);

    auto calc = [](std::size_t, const std::vector<migraphx::argument>&) {};

    migraphx::program capture_p = p;
1264
    migraphx::target t          = migraphx::make_target("ref");
1265
1266
1267
1268
    std::size_t param_index     = 0;
    migraphx::run_passes(capture_p,
                         {migraphx::capture_arguments_pass{{"dot"}, calc, &param_index}});

1269
1270
    p.compile(migraphx::make_target("ref"));
    capture_p.compile(migraphx::make_target("ref"));
1271
1272
1273
1274
1275
1276
1277
1278
1279

    auto cap_res = capture_p.eval({}).back();
    auto res     = p.eval({}).back();

    std::vector<float> vec;
    std::vector<float> cap_vec;
    cap_res.visit([&](auto output) { cap_vec.assign(output.begin(), output.end()); });
    res.visit([&](auto output) { vec.assign(output.begin(), output.end()); });

1280
    EXPECT(migraphx::verify::verify_rms_range(vec, cap_vec));
1281
1282
}

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