quantization.cpp 52.4 KB
Newer Older
Shucai Xiao's avatar
Shucai Xiao committed
1
2
3
4
5
#include <iostream>
#include <vector>
#include <migraphx/literal.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/instruction.hpp>
6
#include <migraphx/generate.hpp>
7
#include <migraphx/ref/target.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
8
#include <migraphx/verify.hpp>
9
#include <migraphx/common.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
10
#include <migraphx/quantization.hpp>
11
12
#include <migraphx/quantize_int8.hpp>
#include <migraphx/quantize_fp16.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
13
#include <migraphx/dead_code_elimination.hpp>
14
15
#include <migraphx/simplify_reshapes.hpp>
#include <migraphx/eliminate_common_subexpression.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
16
#include <migraphx/propagate_constant.hpp>
17
#include <migraphx/simplify_qdq.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
18
19
#include <migraphx/pass_manager.hpp>
#include <migraphx/onnx.hpp>
20
21
#include <migraphx/make_op.hpp>
#include <migraphx/serialize.hpp>
22
23
24
#include <migraphx/argument.hpp>
#include <migraphx/program.hpp>
#include <migraphx/shape.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
25
26
27
#include "test.hpp"
#include <migraphx/half.hpp>

28
static void optimize_prog_int8(migraphx::program& prog)
kahmed10's avatar
kahmed10 committed
29
{
30
31
32
33
    migraphx::run_passes(prog,
                         {migraphx::simplify_qdq{},
                          migraphx::eliminate_common_subexpression{},
                          migraphx::dead_code_elimination{}});
kahmed10's avatar
kahmed10 committed
34
35
}

Shucai Xiao's avatar
Shucai Xiao committed
36
37
TEST_CASE(param_add)
{
38
    auto create_program_float = [](bool add_return = false) {
Shucai Xiao's avatar
Shucai Xiao committed
39
        migraphx::program p;
40
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
41
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
42
43
        auto p1  = mm->add_parameter("x", s);
        auto p2  = mm->add_parameter("y", s);
44
        auto sum = mm->add_instruction(migraphx::make_op("add"), p1, p2);
45
46
        if(add_return)
        {
47
            mm->add_return({sum});
48
        }
Shucai Xiao's avatar
Shucai Xiao committed
49
50
51
52

        return p;
    };

53
    auto create_program_half = [](bool add_return = false) {
Shucai Xiao's avatar
Shucai Xiao committed
54
        migraphx::program p;
55
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
56
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
57
58
        auto p1  = mm->add_parameter("x", s);
        auto p2  = mm->add_parameter("y", s);
59
60
        auto hp1 = mm->add_instruction(migraphx::make_op("convert"), p1);
        auto hp2 = mm->add_instruction(migraphx::make_op("convert"), p2);
61
62
63
64
65
        auto hs  = mm->add_instruction(migraphx::make_op("add"), hp1, hp2);
        auto res = mm->add_instruction(
            migraphx::make_op("convert",
                              {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
            hs);
66
67
        if(add_return)
        {
68
            mm->add_return({res});
69
        }
Shucai Xiao's avatar
Shucai Xiao committed
70
71
72
73
74
75
76
77

        return p;
    };

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

Shucai Xiao's avatar
Shucai Xiao committed
78
        migraphx::quantize_fp16(p1);
Shucai Xiao's avatar
Shucai Xiao committed
79
80
81
82
83
84
85
        EXPECT(p1 == p2);
    }

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

Shucai Xiao's avatar
Shucai Xiao committed
86
        migraphx::quantize_fp16(p1, {"add"});
Shucai Xiao's avatar
Shucai Xiao committed
87
88
        EXPECT(p1 == p2);
    }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    {
        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
105
106
107
108
109
110
}

TEST_CASE(param_add_sub)
{
    auto create_program_float = [] {
        migraphx::program p;
111
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
112
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
113
114
        auto p1   = mm->add_parameter("x", s);
        auto p2   = mm->add_parameter("y", s);
115
116
        auto sum  = mm->add_instruction(migraphx::make_op("add"), p1, p2);
        auto diff = mm->add_instruction(migraphx::make_op("sub"), sum, p2);
117
118
        auto r    = mm->add_instruction(migraphx::make_op("add"), diff, p1);
        mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
119
120
121
122
123
124

        return p;
    };

    auto create_program_half_add = [] {
        migraphx::program p;
125
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
126
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
127
128
        auto p1  = mm->add_parameter("x", s);
        auto p2  = mm->add_parameter("y", s);
129
130
131
132
        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);
133
134
        auto hsum = mm->add_instruction(migraphx::make_op("add"), hp1, hp2);
        auto sum  = mm->add_instruction(
135
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), hsum);
136
        auto diff  = mm->add_instruction(migraphx::make_op("sub"), sum, p2);
137
        auto hdiff = mm->add_instruction(
138
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), diff);
139
        auto res = mm->add_instruction(migraphx::make_op("add"), hdiff, hp1);
140
141
142
        auto r   = mm->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), res);
        mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
143
144
145
146
147
148

        return p;
    };

    auto create_program_half_sub = [] {
        migraphx::program p;
149
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
150
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
151
152
        auto p1   = mm->add_parameter("x", s);
        auto p2   = mm->add_parameter("y", s);
153
154
        auto sum  = mm->add_instruction(migraphx::make_op("add"), p1, p2);
        auto hsum = mm->add_instruction(
155
156
157
            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);
158
159
        auto hdiff = mm->add_instruction(migraphx::make_op("sub"), hsum, hp2);
        auto diff  = mm->add_instruction(
160
161
162
            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});
163
164
165
166

        return p;
    };

Shucai Xiao's avatar
Shucai Xiao committed
167
168
169
170
    {
        auto p1 = create_program_float();
        auto p2 = create_program_half_add();

Shucai Xiao's avatar
Shucai Xiao committed
171
        migraphx::quantize_fp16(p1, {"add"});
Shucai Xiao's avatar
Shucai Xiao committed
172
173
174
175
176
177
178
        EXPECT(p1 == p2);
    }

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

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

Shucai Xiao's avatar
Shucai Xiao committed
181
182
        EXPECT(p1 == p2);
    }
183
184

    {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
        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();
        migraphx::run_passes(p0, {migraphx::quantize_fp16_pass{{"all"}}});
        EXPECT(p0 == create_program_fp16());
240

241
        auto p1 = create_program_float();
Shucai Xiao's avatar
Shucai Xiao committed
242
        migraphx::quantize_fp16(p1);
243
        EXPECT(p1 == create_program_quant_fp16());
244
    }
Shucai Xiao's avatar
Shucai Xiao committed
245
246
247
248
249
250
}

TEST_CASE(literal_add)
{
    auto create_program_float = [] {
        migraphx::program p;
251
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
252
253
254
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
        std::vector<float> data(2 * 3);
        std::iota(data.begin(), data.end(), 1.0f);
255
256
        auto l1 = mm->add_literal(migraphx::literal(s, data));
        auto l2 = mm->add_literal(migraphx::literal(s, data));
257
        mm->add_instruction(migraphx::make_op("add"), l1, l2);
Shucai Xiao's avatar
Shucai Xiao committed
258
259
260
261
262
263

        return p;
    };

    auto create_program_half = [] {
        migraphx::program p;
264
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
265
266
267
        migraphx::shape s{migraphx::shape::half_type, {2, 3}};
        std::vector<migraphx::half> data(2 * 3);
        std::iota(data.begin(), data.end(), 1.0f);
268
269
        auto l1 = mm->add_literal(migraphx::literal(s, data));
        auto l2 = mm->add_literal(migraphx::literal(s, data));
270
271
272
273
274
        auto hs = mm->add_instruction(migraphx::make_op("add"), l1, l2);
        mm->add_instruction(
            migraphx::make_op("convert",
                              {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
            hs);
Shucai Xiao's avatar
Shucai Xiao committed
275
276
277
278
279
280
281
282

        return p;
    };

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

Shucai Xiao's avatar
Shucai Xiao committed
283
        migraphx::quantize_fp16(p1, {"all"});
284
        migraphx::run_passes(*p1.get_main_module(),
Shucai Xiao's avatar
Shucai Xiao committed
285
                             {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
286
        migraphx::run_passes(*p2.get_main_module(),
Shucai Xiao's avatar
Shucai Xiao committed
287
                             {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
Shucai Xiao's avatar
Shucai Xiao committed
288
289
290
291
292
293
294
295

        EXPECT(p1 == p2);
    }

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

Shucai Xiao's avatar
Shucai Xiao committed
296
        migraphx::quantize_fp16(p1, {"add"});
297
        migraphx::run_passes(*p1.get_main_module(),
Shucai Xiao's avatar
Shucai Xiao committed
298
                             {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
299
        migraphx::run_passes(*p2.get_main_module(),
Shucai Xiao's avatar
Shucai Xiao committed
300
                             {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
Shucai Xiao's avatar
Shucai Xiao committed
301
302
303
304
        EXPECT(p1 == p2);
    }
}

305
TEST_CASE(fp16_subgraph)
306
{
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
    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();
        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 hl1       = then_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), l1);
        auto mhl1 = then_mod->add_instruction(
            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 hl2 = then_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), l2);
        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");
        auto hl3       = else_mod->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), l3);
        auto mhl3 = else_mod->add_instruction(
            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;
412
413
    };

414
415
416
417
418
419
420
421
422
423
    auto p1 = create_program();
    migraphx::quantize_fp16(p1);

    auto p2 = create_fp16_program();

    EXPECT(p1 == p2);
}

TEST_CASE(op_capture)
{
424
425
    auto create_program_float = [] {
        migraphx::program p;
426
        auto* mm = p.get_main_module();
427
428
429
        migraphx::shape s1{migraphx::shape::float_type, {3, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {3, 6}};

430
431
432
433
        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);
434
435
436
        auto pa = mm->add_instruction(migraphx::make_op("add"), p1, p2);
        auto ps = mm->add_instruction(migraphx::make_op("dot"), pa, pb, pc);
        mm->add_instruction(migraphx::make_op("dot"), pa, ps);
437
438
439
440
441
442

        return p;
    };

    auto create_program_op = [&] {
        migraphx::program p;
443
        auto* mm = p.get_main_module();
444
445
446
        migraphx::shape s1{migraphx::shape::float_type, {3, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {3, 6}};

447
448
449
450
        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);
451
        auto pa  = mm->add_instruction(migraphx::make_op("add"), p1, p2);
452
453
454
        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);
        auto opc = mm->add_instruction(migraphx::make_op("capture", {{"ins_index", 2}}), pc);
455
        auto ps  = mm->add_instruction(migraphx::make_op("dot"), opa, opb, opc);
456
457
458
        auto opm = mm->add_instruction(migraphx::make_op("capture", {{"ins_index", 3}}), pa);
        auto ops = mm->add_instruction(migraphx::make_op("capture", {{"ins_index", 4}}), ps);
        mm->add_instruction(migraphx::make_op("dot"), opm, ops);
459
460
461
462
463

        return p;
    };

    {
464
465
466
467
468
469
        auto p                  = create_program_float();
        auto op_capture_p       = create_program_op();
        migraphx::target t      = migraphx::ref::target{};
        std::size_t param_index = 0;
        migraphx::run_passes(
            p, {migraphx::capture_arguments_pass{{"dot", "convolution"}, {}, &param_index}});
470
471
472
473
        EXPECT(p == op_capture_p);
    }
}

474
TEST_CASE(op_capture_subgraph)
475
476
477
{
    auto create_program = [] {
        migraphx::program p;
478
        auto* mm = p.get_main_module();
479
480
481
482
483
484
        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);
485

486
487
488
489
        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);
490

491
492
493
        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});
494

495
496
497
        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});
498

499
500
        auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
        mm->add_return({ret});
501
502
503
504

        return p;
    };

505
    auto create_program_op = [&] {
506
        migraphx::program p;
507
        auto* mm = p.get_main_module();
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
        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});
534
535
536
537

        return p;
    };

538
539
540
541
542
543
544
    {
        auto p                  = create_program();
        auto op_capture_p       = create_program_op();
        migraphx::target t      = migraphx::ref::target{};
        std::size_t param_index = 0;
        migraphx::run_passes(
            p, {migraphx::capture_arguments_pass{{"dot", "convolution"}, {}, &param_index}});
545

546
547
        EXPECT(p == op_capture_p);
    }
548
549
}

550
TEST_CASE(dot_float)
551
552
553
{
    auto create_program = [] {
        migraphx::program p;
554
        auto* mm = p.get_main_module();
555
556
557
        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}};
558
559
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
560

561
        auto r = migraphx::add_dot_apply_alpha_beta(*mm, {pa, pb});
562
        mm->add_return({r});
563
564
565
566
567
568

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
569
        auto* mm = p.get_main_module();
570
571
572
        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}};
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
        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);

593
        auto r = migraphx::add_dot_apply_alpha_beta(*mm, {dqa, dqb});
594
        mm->add_return({r});
595
596
597
598

        return p;
    };

599
    auto create_int8_optimized_prog = [] {
600
        migraphx::program p;
601
        auto* mm = p.get_main_module();
602
603
604
        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}};
605
606
        auto pa      = mm->add_parameter("a", sa);
        auto pb      = mm->add_parameter("b", sb);
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
        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);
        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", {{"alpha", 1}, {"beta", 0}}), quant_a, quant_b);
        std::vector<float> vec(sc.elements(), 100.0f);
        auto dc = mm->add_literal(100.0f);
        auto mdc =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sc.lens()}}), dc);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), quant, mdc);
        mm->add_return({r});
627
628
629
630

        return p;
    };

631
632
633
634
635
636
637
638
    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{}});
639
    auto qp = create_int8_quantized_prog();
640

641
    EXPECT(p == qp);
642
643
644
645

    optimize_prog_int8(p);
    auto op = create_int8_optimized_prog();
    EXPECT(p == op);
646
647
}

648
TEST_CASE(dot_double_2args)
649
650
651
{
    auto create_program = [] {
        migraphx::program p;
652
        auto* mm = p.get_main_module();
653
654
655
656
        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);
657
        auto r  = migraphx::add_dot_apply_alpha_beta(*mm, {pa, pb});
658
        mm->add_return({r});
659
660
661
662
663
664

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
665
        auto* mm = p.get_main_module();
666
667
        migraphx::shape sa{migraphx::shape::double_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::double_type, {16, 8}};
668
669
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
670

671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
        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);
687
        auto r   = migraphx::add_dot_apply_alpha_beta(*mm, {dqa, dqb});
688
        mm->add_return({r});
689
690
691
        return p;
    };

692
    auto create_int8_optimized_prog = [] {
693
        migraphx::program p;
694
        auto* mm = p.get_main_module();
695
696
        migraphx::shape sa{migraphx::shape::double_type, {2, 16}};
        migraphx::shape sb{migraphx::shape::double_type, {16, 8}};
697
698
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
699

700
701
702
703
704
705
706
707
708
709
710
711
712
        auto scale_a = mm->add_literal(10.0);
        auto zp      = mm->add_literal(static_cast<int8_t>(0));
        scale_a      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), scale_a);
        auto zp_a =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), zp);
        auto qa      = mm->add_instruction(migraphx::make_op("quantizelinear"), pa, scale_a, zp_a);
        auto scale_b = mm->add_literal(5.0);
        scale_b      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}), scale_b);
        auto zp_b =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sb.lens()}}), zp);
        auto qb   = mm->add_instruction(migraphx::make_op("quantizelinear"), pb, scale_b, zp_b);
713
714
        auto qdot = mm->add_instruction(
            migraphx::make_op("quant_dot", {{"alpha", 1}, {"beta", 0}}), qa, qb);
715
716
717
718
719
        auto scale = mm->add_literal(50.0);
        scale      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", qdot->get_shape().lens()}}), scale);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), qdot, scale);
        mm->add_return({r});
720
721
722
723
        return p;
    };

    auto p = create_program();
724
725
726
727
728
729
730
731
732
733
    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());
734
735
}

736
TEST_CASE(dot_half_1arg)
737
738
739
{
    auto create_program = [] {
        migraphx::program p;
740
        auto* mm = p.get_main_module();
741
742
        migraphx::shape s{migraphx::shape::half_type, {9, 9}};
        auto x = mm->add_parameter("x", s);
743
        auto r = migraphx::add_dot_apply_alpha_beta(*mm, {x, x});
744
        mm->add_return({r});
745
746
747
748
749
750

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
751
        auto* mm = p.get_main_module();
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
        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);
771
        auto r   = migraphx::add_dot_apply_alpha_beta(*mm, {dqa, dqb});
772
773
774
        mm->add_return({r});
        return p;
    };
775

776
777
778
779
780
781
782
783
784
785
786
787
788
    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);

        auto zp    = mm->add_literal(static_cast<int8_t>(0));
        auto scale = mm->add_literal(migraphx::literal({sa.type()}, {10.0}));
        scale = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}),
                                    scale);
        zp =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sa.lens()}}), zp);
        auto qx   = mm->add_instruction(migraphx::make_op("quantizelinear"), x, scale, zp);
789
        auto qdot = mm->add_instruction(
790
791
792
793
794
795
796
797
            migraphx::make_op("quant_dot", {{"alpha", 1}, {"beta", 0}}), qx, qx);

        auto dq_scale = mm->add_literal(migraphx::literal({sa.type()}, {100.0}));
        dq_scale      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", qdot->get_shape().lens()}}),
            dq_scale);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), qdot, dq_scale);
        mm->add_return({r});
798
799
800
801
        return p;
    };

    auto p = create_program();
802
803
804
805
806
807
808
809
810
811
    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());
812
813
}

814
815
TEST_CASE(conv_float)
{
Shucai Xiao's avatar
Shucai Xiao committed
816
    auto create_program = [] {
817
        migraphx::program p;
818
        auto* mm = p.get_main_module();
819
        auto input =
820
            mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
821
        auto weights =
822
            mm->add_parameter("w", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
823
824
        auto r = mm->add_instruction(migraphx::make_op("convolution"), input, weights);
        mm->add_return({r});
825
826
827
828
829
830

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
831
        auto* mm = p.get_main_module();
832
833
        migraphx::shape sx{migraphx::shape::float_type, {4, 3, 3, 3}};
        migraphx::shape sw{migraphx::shape::float_type, {4, 3, 3, 3}};
834
835
        auto px = mm->add_parameter("x", sx);
        auto pw = mm->add_parameter("w", sw);
836

837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
        auto zp    = mm->add_literal(static_cast<int8_t>(0));
        auto scale = mm->add_literal(10.0f);
        scale = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sx.lens()}}),
                                    scale);
        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);

        migraphx::shape sc{migraphx::shape::float_type, {4, 4, 1, 1}};
        std::vector<float> vec(sc.elements(), 100.0f);
        migraphx::shape s_scale{migraphx::shape::float_type, sc.lens()};
        auto d_scale = mm->add_literal(100.0f);
        d_scale      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {4, 4, 1, 1}}}), d_scale);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), quant, d_scale);
        mm->add_return({r});
856
857
858
859
860
861

        return p;
    };

    auto p = create_program();
    const std::vector<std::pair<float, float>>& quant_params{{0.1f, 0.0f}, {0.1f, 0.0f}};
862
863
864
865
    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);
866
867
868
869
870
    auto qp = create_int8_quantized_prog();

    EXPECT(p == qp);
}

871
TEST_CASE(conv_float_throw)
872
{
Shucai Xiao's avatar
Shucai Xiao committed
873
    auto create_program = [] {
874
        migraphx::program p;
875
        auto* mm = p.get_main_module();
876
        auto input =
877
            mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
878
        auto weights =
879
880
881
            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});
882
883
884
885
886
887

        return p;
    };

    auto p = create_program();
    const std::vector<std::pair<float, float>>& quant_params{{0.1f, 0.0f}, {0.1f, 0.0f}};
888
889
890
    test::throws([&] {
        migraphx::run_passes(p, {migraphx::quantize_int8_pass{{"add"}, quant_params}});
    });
891
892
893
894
}

TEST_CASE(conv_half)
{
Shucai Xiao's avatar
Shucai Xiao committed
895
    auto create_program = [] {
896
        migraphx::program p;
897
        auto* mm = p.get_main_module();
898
        auto input =
899
            mm->add_parameter("x", migraphx::shape{migraphx::shape::half_type, {4, 3, 3, 3}});
900
        auto weights =
901
            mm->add_parameter("w", migraphx::shape{migraphx::shape::half_type, {4, 3, 3, 3}});
902
903
        auto r = mm->add_instruction(migraphx::make_op("convolution"), input, weights);
        mm->add_return({r});
904
905
906
907
908
909

        return p;
    };

    auto create_int8_quantized_prog = [] {
        migraphx::program p;
910
        auto* mm = p.get_main_module();
911
912
        migraphx::shape sx{migraphx::shape::half_type, {4, 3, 3, 3}};
        migraphx::shape sw{migraphx::shape::half_type, {4, 3, 3, 3}};
913
914
        auto px = mm->add_parameter("x", sx);
        auto pw = mm->add_parameter("w", sw);
915

916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
        auto zp    = mm->add_literal(static_cast<int8_t>(0));
        auto scale = mm->add_literal(migraphx::literal({sx.type()}, {10.0}));
        scale = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", sx.lens()}}),
                                    scale);
        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);
        auto d_scale = mm->add_literal(migraphx::literal({sx.type()}, {100.0}));
        d_scale      = mm->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {4, 4, 1, 1}}}), d_scale);
        auto r = mm->add_instruction(migraphx::make_op("dequantizelinear"), quant, d_scale);
        mm->add_return({r});
931
932
933
934
935
936

        return p;
    };

    auto p = create_program();
    const std::vector<std::pair<float, float>>& quant_params{{0.1f, 0.0f}, {0.1f, 0.0f}};
937
938
939
940
    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);
941
942
943
944
945
    auto qp = create_int8_quantized_prog();

    EXPECT(p == qp);
}

946
947
948
949
950
951
template <class T>
auto get_hash(const T& x)
{
    return std::hash<T>{}(x);
}

952
953
954
955
TEST_CASE(target_copy)
{
    auto run_prog = [](migraphx::program p,
                       const migraphx::target& t,
956
                       migraphx::parameter_map& m_in,
957
958
                       std::vector<float>& res) {
        p.compile(t);
959
        migraphx::parameter_map m;
960
961
962
963
964
965
966
967
968
969
970
971
        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);
            }
        }

972
        auto result = t.copy_from(p.eval(m).back());
973
974
975
976
977
        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
    };

    auto create_program = [] {
        migraphx::program p;
978
        auto* mm = p.get_main_module();
979
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
980
981
        auto p1 = mm->add_parameter("x", s);
        auto p2 = mm->add_parameter("y", s);
982
        mm->add_instruction(migraphx::make_op("add"), p1, p2);
983
984
985
986
987
988

        return p;
    };

    {
        auto p = create_program();
989
        migraphx::parameter_map m;
990
991
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
        m["x"] = migraphx::generate_argument(s);
992
993
994
        std::vector<float> ref_result;
        migraphx::target ref_t = migraphx::ref::target{};
        run_prog(p, ref_t, m, ref_result);
995
996

        std::vector<float> orig_result;
997
        run_prog(p, ref_t, m, orig_result);
998

999
        EXPECT(migraphx::verify_range(ref_result, orig_result));
1000
1001
1002
    }
}

1003
TEST_CASE(int8_quantization_dot)
1004
1005
1006
{
    auto run_prog = [](migraphx::program p,
                       const migraphx::target& t,
1007
                       migraphx::parameter_map& m_in,
1008
1009
                       std::vector<float>& res,
                       bool b_quantize = false) {
Shucai Xiao's avatar
Shucai Xiao committed
1010
        if(b_quantize)
1011
        {
1012
            std::vector<migraphx::parameter_map> cali_data;
1013
1014
1015
1016
            cali_data.push_back(m_in);
            migraphx::quantize_int8(p, t, cali_data);
        }
        p.compile(t);
1017
        migraphx::parameter_map m;
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
        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);
            }
        }

1030
        auto result = t.copy_from(p.eval(m).back());
1031
1032
1033
1034
1035
        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
    };

    auto create_program = [] {
        migraphx::program p;
1036
        auto* mm = p.get_main_module();
1037
1038
1039
        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}};
1040
1041
1042
        auto pa = mm->add_parameter("a", sa);
        auto pb = mm->add_parameter("b", sb);
        auto pc = mm->add_parameter("c", sc);
1043
        auto r  = migraphx::add_dot_apply_alpha_beta(*mm, {pa, pb, pc}, 1, 1);
1044
        mm->add_return({r});
1045
1046
1047
1048
1049
        return p;
    };

    {
        auto p = create_program();
1050
        migraphx::parameter_map m;
1051
        migraphx::shape sa{migraphx::shape::float_type, {2, 16}};
1052
1053
1054
        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")));
1055
        std::vector<float> quant_result;
1056
1057
        migraphx::target ref_t = migraphx::ref::target{};
        run_prog(p, ref_t, m, quant_result, true);
1058
1059

        std::vector<float> no_quant_result;
1060
        run_prog(p, ref_t, m, no_quant_result);
1061

1062
        EXPECT(migraphx::verify_range(quant_result, no_quant_result, 30000));
1063
1064
1065
    }
}

1066
1067
1068
1069
1070
1071
1072
1073
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)
        {
1074
            std::vector<migraphx::parameter_map> cali_data;
1075
1076
1077
            migraphx::quantize_int8(p, t, cali_data);
        }
        p.compile(t);
1078
        migraphx::parameter_map m;
1079

1080
        auto result = t.copy_from(p.eval(m).back());
1081
1082
1083
1084
1085
        result.visit([&](auto v) { res.assign(v.begin(), v.end()); });
    };

    auto create_program = [] {
        migraphx::program p;
1086
        auto* mm = p.get_main_module();
1087
1088
1089
        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);
1090
1091
        auto input   = mm->add_literal(migraphx::literal(sx, v));
        auto weights = mm->add_literal(migraphx::literal(sw, v));
1092
1093
        auto r       = mm->add_instruction(migraphx::make_op("convolution"), input, weights);
        mm->add_return({r});
1094
1095
1096
1097
1098
1099
1100

        return p;
    };

    {
        auto p = create_program();
        std::vector<float> quant_result;
1101
1102
        migraphx::target ref_t = migraphx::ref::target{};
        run_prog(p, ref_t, quant_result, true);
1103
1104

        std::vector<float> no_quant_result;
1105
        run_prog(p, ref_t, no_quant_result);
1106
1107
1108
1109
1110

        EXPECT(migraphx::verify_range(quant_result, no_quant_result));
    }
}

1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
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");
1129
        auto out1      = migraphx::add_dot_apply_alpha_beta(*then_mod, {a, b});
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
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
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
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
1257
1258
1259
1260
1261
        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);
        auto qb = then_mod->add_instruction(migraphx::make_op("quantizelinear"), b, sb, zpb);
        auto qdot =
            then_mod->add_instruction(migraphx::make_op("quant_dot", {{"beta", 0}}), qa, qb);
        auto so = then_mod->add_literal(100.0f);
        so      = then_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sout.lens()}}), so);
        auto r = then_mod->add_instruction(migraphx::make_op("dequantizelinear"), qdot, so);
        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");
        auto sax       = else_mod->add_literal(2.0f);
        auto zp        = else_mod->add_literal(static_cast<int8_t>(0));
        sax            = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sd.lens()}}), sax);
        auto zpx = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sd.lens()}}), zp);
        auto qx  = else_mod->add_instruction(migraphx::make_op("quantizelinear"), x, sax, zpx);
        auto ssw = else_mod->add_literal(1.66667f);
        ssw      = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sw.lens()}}), ssw);
        auto zpw = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sw.lens()}}), zp);
        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 so1   = else_mod->add_literal(3.33333f);
        so1        = else_mod->add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", sout.lens()}}), so1);
        auto r1 = else_mod->add_instruction(migraphx::make_op("dequantizelinear"), qconv, so1);
        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);
    auto ps = mm->add_instruction(migraphx::make_op("dot"), pa, pb, pc);
    mm->add_instruction(migraphx::make_op("dot"), pa, ps);

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

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

    p.compile(migraphx::ref::target{});
    capture_p.compile(migraphx::ref::target{});

    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()); });

    EXPECT(migraphx::verify_range(vec, cap_vec));
}

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