pack_int8_args.cpp 19.6 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.
 */
Umang Yadav's avatar
Umang Yadav committed
24
#include <migraphx/instruction_ref.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
25
26
27
#include <migraphx/gpu/context.hpp>
#include <migraphx/gpu/lowering.hpp>
#include <migraphx/gpu/target.hpp>
28
#include <migraphx/gpu/allocation_model.hpp>
29
#include <migraphx/apply_alpha_beta.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
30
31
32
#include <migraphx/adjust_allocation.hpp>
#include <migraphx/gpu/pack_int8_args.hpp>
#include <migraphx/gpu/rocblas.hpp>
33
#include <migraphx/gpu/device_name.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
34
35
#include <migraphx/auto_contiguous.hpp>
#include <migraphx/dead_code_elimination.hpp>
36
#include <migraphx/replace_allocate.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
37
38
39
40
41
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/make_op.hpp>
#include <test.hpp>
42
43
44
45
#include "make_precompile_op.hpp"

// Treat some operators as compilable to enable lowering
MIGRAPHX_GPU_TEST_PRECOMPILE("add", "mul", "convert")
Shucai Xiao's avatar
Shucai Xiao committed
46

47
void run_passes(migraphx::module& m, migraphx::gpu::context& ctx)
Shucai Xiao's avatar
Shucai Xiao committed
48
49
50
51
52
{
    migraphx::run_passes(m,
                         {migraphx::auto_contiguous{},
                          migraphx::gpu::lowering{&ctx, false},
                          migraphx::dead_code_elimination{},
53
54
                          migraphx::replace_allocate{migraphx::gpu::gpu_allocation_model{}},
                          migraphx::dead_code_elimination{},
Shucai Xiao's avatar
Shucai Xiao committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
                          migraphx::gpu::pack_int8_args{},
                          migraphx::dead_code_elimination{}});
}

TEST_CASE(quant_dot)
{
    auto create_module = [] {
        migraphx::module m("test");
        migraphx::shape m1_shape{migraphx::shape::int8_type, {5, 8}};
        migraphx::shape m2_shape{migraphx::shape::int8_type, {8, 7}};
        migraphx::shape m3_shape{migraphx::shape::int32_type, {5, 7}};

        auto l1 = m.add_parameter("a", m1_shape);
        auto l2 = m.add_parameter("b", m2_shape);
        auto l3 = m.add_parameter("c", m3_shape);
70
71
        auto r =
            migraphx::add_apply_alpha_beta(m, {l1, l2, l3}, migraphx::make_op("quant_dot"), 1, 1);
Shucai Xiao's avatar
Shucai Xiao committed
72
73
74
75
76
77
78
79
80
81
        m.add_return({r});
        return m;
    };

    auto create_optimized_int8_x4 = [](bool int8_x4) {
        migraphx::module m("test");
        migraphx::shape m1_shape{migraphx::shape::int8_type, {5, 8}};
        migraphx::shape m2_shape{migraphx::shape::int8_type, {8, 7}};
        migraphx::shape m3_shape{migraphx::shape::int32_type, {5, 7}};

82
83
84
85
86
87
88
        auto l1         = m.add_parameter("a", m1_shape);
        auto l2         = m.add_parameter("b", m2_shape);
        auto l3         = m.add_parameter("c", m3_shape);
        auto beta       = m.add_literal(1);
        auto output     = m.add_parameter("test:#output_0", m3_shape);
        auto gemm_alloc = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(m3_shape)}}));
Shucai Xiao's avatar
Shucai Xiao committed
89
90
91
92
93
94
95
96

        auto packa = l2;
        if(int8_x4)
        {
            auto alloc = m.add_instruction(
                migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(m2_shape)}}));
            packa = m.add_instruction(migraphx::make_op("gpu::int8_gemm_pack_a"), l2, alloc);
        }
97
98
99
100
101
102
103
        auto gemm = m.add_instruction(
            migraphx::make_op("gpu::quant_gemm",
                              {{"int8_x4_format", int8_x4},
                               {"compute_fp32", migraphx::gpu::get_compute_fp32_flag()}}),
            l1,
            packa,
            gemm_alloc);
104
105
106
107
108
109
110
111
112

        auto beta_broadcast = m.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", m3_shape.lens()}}), beta);
        auto beta_alloc = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(m3_shape)}}));
        auto beta_contiguous =
            m.add_instruction(migraphx::make_op("gpu::contiguous"), beta_broadcast, beta_alloc);
        auto mul_alloc = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(m3_shape)}}));
113
114
        auto m3_beta = m.add_instruction(make_precompile_op("mul"), l3, beta_contiguous, mul_alloc);
        auto gemm_add = m.add_instruction(make_precompile_op("add"), gemm, m3_beta, output);
115
        m.add_return({gemm_add});
Shucai Xiao's avatar
Shucai Xiao committed
116
117
118
119

        return m;
    };

120
121
122
    auto m1  = create_module();
    auto ctx = migraphx::gpu::context{};
    run_passes(m1, ctx);
Shucai Xiao's avatar
Shucai Xiao committed
123

124
125
    bool int8_x4 = migraphx::gpu::get_int8_x4_format(ctx);
    auto m2      = create_optimized_int8_x4(int8_x4);
Shucai Xiao's avatar
Shucai Xiao committed
126
127
128
129
130
131
132
133
134
135
    EXPECT(m1 == m2);
}

TEST_CASE(quant_dot_trans)
{
    auto create_module = [] {
        migraphx::module m("test");
        migraphx::shape s1{migraphx::shape::int8_type, {3, 2, 8, 5}};
        migraphx::shape s2{migraphx::shape::int8_type, {3, 2, 7, 8}};

136
137
138
139
140
141
        auto l1 = m.add_parameter("a", s1);
        auto tl1 =
            m.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l1);
        auto l2 = m.add_parameter("b", s2);
        auto tl2 =
            m.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l2);
142
        auto r = migraphx::add_apply_alpha_beta(m, {tl1, tl2}, migraphx::make_op("quant_dot"), 3);
Shucai Xiao's avatar
Shucai Xiao committed
143
144
145
146
147
148
149
150
151
152
153
154
        m.add_return({r});
        return m;
    };

    auto create_optimized_int8_x4 = [](bool int8_x4) {
        migraphx::module m("test");
        migraphx::shape s1{migraphx::shape::int8_type, {3, 2, 8, 5}};
        migraphx::shape s2{migraphx::shape::int8_type, {3, 2, 7, 8}};
        migraphx::shape s3{migraphx::shape::int32_type, {3, 2, 5, 7}};

        auto l1     = m.add_parameter("a", s1);
        auto l2     = m.add_parameter("b", s2);
155
        auto alpha  = m.add_literal(3);
Shucai Xiao's avatar
Shucai Xiao committed
156
157
        auto output = m.add_parameter("test:#output_0", s3);

158
159
        auto tl1 =
            m.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
160
161
162
163
164
        migraphx::shape ts1{migraphx::shape::int8_type, {3, 2, 5, 8}};
        auto alloca = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ts1)}}));
        auto conta = m.add_instruction(migraphx::make_op("gpu::contiguous"), tl1, alloca);

165
166
        auto tl2 =
            m.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l2);
Shucai Xiao's avatar
Shucai Xiao committed
167
168
169
170
171
        migraphx::shape ts2{migraphx::shape::int8_type, {3, 2, 8, 7}};
        auto allocb = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ts2)}}));
        auto contb = m.add_instruction(migraphx::make_op("gpu::contiguous"), tl2, allocb);

172
173
174
175
176
177
178
179
180
181
182
183
        auto alpha_broadcast = m.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", conta->get_shape().lens()}}), alpha);
        auto alpha_alloc = m.add_instruction(migraphx::make_op(
            "hip::allocate",
            {{"shape",
              migraphx::to_value(migraphx::shape(migraphx::shape::int32_type, {3, 2, 5, 8}))}}));
        auto alpha_contiguous =
            m.add_instruction(migraphx::make_op("gpu::contiguous"), alpha_broadcast, alpha_alloc);
        // alpha = int32 and tl1 = int8, convert tl1 to int32 for multiplication and then convert
        // back result to int8
        auto tl1_convert_alloc = m.add_instruction(migraphx::make_op(
            "hip::allocate", {{"shape", migraphx::to_value(alpha_contiguous->get_shape())}}));
184
185
186
187
188
189
        auto tl1_convert =
            m.add_instruction(make_precompile_op(migraphx::make_op(
                                  "convert", {{"target_type", alpha->get_shape().type()}})),
                              conta,
                              tl1_convert_alloc);
        auto mul_alloc = m.add_instruction(migraphx::make_op(
190
            "hip::allocate", {{"shape", migraphx::to_value(tl1_convert->get_shape())}}));
191
192
        auto tl1_alpha_int32 =
            m.add_instruction(make_precompile_op("mul"), alpha_contiguous, tl1_convert, mul_alloc);
193
194
195
        // convert mul_res to int8
        auto tl1_alpha_int8_alloc = m.add_instruction(migraphx::make_op(
            "hip::allocate", {{"shape", migraphx::to_value(conta->get_shape())}}));
196
197
198
199
200
        auto tl1_alpha_int8 =
            m.add_instruction(make_precompile_op(migraphx::make_op(
                                  "convert", {{"target_type", conta->get_shape().type()}})),
                              tl1_alpha_int32,
                              tl1_alpha_int8_alloc);
201

Shucai Xiao's avatar
Shucai Xiao committed
202
203
204
205
206
207
208
        auto packb = contb;
        if(int8_x4)
        {
            auto allocpb = m.add_instruction(
                migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ts2)}}));
            packb = m.add_instruction(migraphx::make_op("gpu::int8_gemm_pack_a"), contb, allocpb);
        }
209

210
211
212
213
214
215
216
        auto gemm = m.add_instruction(
            migraphx::make_op("gpu::quant_gemm",
                              {{"int8_x4_format", int8_x4},
                               {"compute_fp32", migraphx::gpu::get_compute_fp32_flag()}}),
            tl1_alpha_int8,
            packb,
            output);
Shucai Xiao's avatar
Shucai Xiao committed
217
218
219
220
221
        m.add_return({gemm});

        return m;
    };

222
223
224
    auto m1  = create_module();
    auto ctx = migraphx::gpu::context{};
    run_passes(m1, ctx);
Shucai Xiao's avatar
Shucai Xiao committed
225

226
227
    bool int8_x4 = migraphx::gpu::get_int8_x4_format(ctx);
    auto m2      = create_optimized_int8_x4(int8_x4);
Shucai Xiao's avatar
Shucai Xiao committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

    EXPECT(m1 == m2);
}

TEST_CASE(quant_dot_pad)
{
    auto create_module = [] {
        migraphx::module m("test");
        migraphx::shape s1{migraphx::shape::int8_type, {5, 6}};
        migraphx::shape s2{migraphx::shape::int8_type, {6, 7}};
        migraphx::shape s3{migraphx::shape::int32_type, {5, 7}};

        auto l1 = m.add_parameter("a", s1);
        auto l2 = m.add_parameter("b", s2);
        auto l3 = m.add_parameter("c", s3);
243
244
        auto r =
            migraphx::add_apply_alpha_beta(m, {l1, l2, l3}, migraphx::make_op("quant_dot"), 1, 1);
Shucai Xiao's avatar
Shucai Xiao committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
        m.add_return({r});
        return m;
    };

    auto create_optimized_int8_x4 = [](bool int8_x4) {
        migraphx::module m("test");
        migraphx::shape s1{migraphx::shape::int8_type, {5, 6}};
        migraphx::shape ps1{migraphx::shape::int8_type, {5, 8}};
        migraphx::shape s2{migraphx::shape::int8_type, {6, 7}};
        migraphx::shape ps2{migraphx::shape::int8_type, {8, 7}};
        migraphx::shape s3{migraphx::shape::int32_type, {5, 7}};

        auto l1     = m.add_parameter("a", s1);
        auto l2     = m.add_parameter("b", s2);
        auto l3     = m.add_parameter("c", s3);
260
        auto beta   = m.add_literal(1);
Shucai Xiao's avatar
Shucai Xiao committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
        auto output = m.add_parameter("test:#output_0", s3);

        auto pl1   = l1;
        auto packa = l2;
        migraphx::instruction_ref pl2{};
        if(int8_x4)
        {
            auto po1 = m.insert_instruction(
                l1, migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ps1)}}));
            pl1 = m.add_instruction(
                migraphx::make_op("gpu::pad", {{"mode", 0}, {"pads", {0, 2, 0, 0}}, {"value", 0}}),
                l1,
                po1);

            auto po2 = m.insert_instruction(
                l2, migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ps2)}}));
            pl2 = m.insert_instruction(
                std::next(l2),
                migraphx::make_op("gpu::pad", {{"mode", 0}, {"pads", {2, 0, 0, 0}}, {"value", 0}}),
                l2,
                po2);
        }

284
285
286
        auto gemm_alloc = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(s3)}}));

Shucai Xiao's avatar
Shucai Xiao committed
287
288
289
290
291
292
293
        if(int8_x4)
        {
            auto alloc = m.add_instruction(
                migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ps2)}}));
            packa = m.add_instruction(migraphx::make_op("gpu::int8_gemm_pack_a"), pl2, alloc);
        }

294
295
296
297
298
299
300
        auto gemm = m.add_instruction(
            migraphx::make_op("gpu::quant_gemm",
                              {{"int8_x4_format", int8_x4},
                               {"compute_fp32", migraphx::gpu::get_compute_fp32_flag()}}),
            pl1,
            packa,
            gemm_alloc);
301
302
303
304
305
306
307
308
309

        auto beta_broadcast =
            m.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s3.lens()}}), beta);
        auto beta_alloc = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(s3)}}));
        auto beta_contiguous =
            m.add_instruction(migraphx::make_op("gpu::contiguous"), beta_broadcast, beta_alloc);
        auto mul_alloc = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(s3)}}));
310
311
        auto m3_beta = m.add_instruction(make_precompile_op("mul"), l3, beta_contiguous, mul_alloc);
        auto gemm_add = m.add_instruction(make_precompile_op("add"), gemm, m3_beta, output);
312
        m.add_return({gemm_add});
Shucai Xiao's avatar
Shucai Xiao committed
313
314
315
        return m;
    };

316
317
318
    auto m1  = create_module();
    auto ctx = migraphx::gpu::context{};
    run_passes(m1, ctx);
Shucai Xiao's avatar
Shucai Xiao committed
319

320
321
    bool int8_x4 = migraphx::gpu::get_int8_x4_format(ctx);
    auto m2      = create_optimized_int8_x4(int8_x4);
Shucai Xiao's avatar
Shucai Xiao committed
322
323
324
325
326
327
328
329
330
331
332

    EXPECT(m1 == m2);
}

TEST_CASE(quant_dot_trans_pad)
{
    auto create_module = [] {
        migraphx::module m("test");
        migraphx::shape s1{migraphx::shape::int8_type, {3, 2, 9, 5}};
        migraphx::shape s2{migraphx::shape::int8_type, {3, 2, 7, 9}};

333
334
335
336
337
338
        auto l1 = m.add_parameter("a", s1);
        auto tl1 =
            m.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l1);
        auto l2 = m.add_parameter("b", s2);
        auto tl2 =
            m.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l2);
339
        auto r = migraphx::add_apply_alpha_beta(m, {tl1, tl2}, migraphx::make_op("quant_dot"), 3);
Shucai Xiao's avatar
Shucai Xiao committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
        m.add_return({r});
        return m;
    };

    auto create_optimized_int8_x4 = [](bool int8_x4) {
        migraphx::module m("test");
        migraphx::shape s1{migraphx::shape::int8_type, {3, 2, 9, 5}};
        migraphx::shape ps1{migraphx::shape::int8_type, {3, 2, 5, 12}};
        migraphx::shape s2{migraphx::shape::int8_type, {3, 2, 7, 9}};
        migraphx::shape ps2{migraphx::shape::int8_type, {3, 2, 12, 7}};
        migraphx::shape s3{migraphx::shape::int32_type, {3, 2, 5, 7}};

        auto l1     = m.add_parameter("a", s1);
        auto l2     = m.add_parameter("b", s2);
354
        auto alpha  = m.add_literal(3);
Shucai Xiao's avatar
Shucai Xiao committed
355
356
        auto output = m.add_parameter("test:#output_0", s3);

357
358
        auto tl1 =
            m.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
359
360
361
362
363
        migraphx::shape ts1{migraphx::shape::int8_type, {3, 2, 5, 9}};
        auto ta = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ts1)}}));
        auto conta = m.add_instruction(migraphx::make_op("gpu::contiguous"), tl1, ta);

364
365
        auto tl2 =
            m.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l2);
Shucai Xiao's avatar
Shucai Xiao committed
366
367
368
        migraphx::shape ts2{migraphx::shape::int8_type, {3, 2, 9, 7}};
        auto tb = m.add_instruction(
            migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ts2)}}));
369

Shucai Xiao's avatar
Shucai Xiao committed
370
371
372
373
374
375
376
        migraphx::instruction_ref ptb{};
        if(int8_x4)
        {
            ptb = m.add_instruction(
                migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ps2)}}));
        }
        auto contb = m.add_instruction(migraphx::make_op("gpu::contiguous"), tl2, tb);
377
        auto pb    = contb;
Shucai Xiao's avatar
Shucai Xiao committed
378
379
        if(int8_x4)
        {
380
            pb = m.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
381
382
383
                migraphx::make_op("gpu::pad", {{"mode", 0}, {"pads", {0, 0, 3, 0, 0, 0, 0, 0}}}),
                contb,
                ptb);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
        }

        auto alpha_broadcast = m.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", conta->get_shape().lens()}}), alpha);
        auto alpha_alloc = m.add_instruction(
            migraphx::make_op("hip::allocate",
                              {{"shape",
                                migraphx::to_value(migraphx::shape(migraphx::shape::int32_type,
                                                                   conta->get_shape().lens()))}}));
        auto alpha_contiguous =
            m.add_instruction(migraphx::make_op("gpu::contiguous"), alpha_broadcast, alpha_alloc);

        // alpha = int32 and tl1 = int8, convert tl1 to int32 for multiplication and then convert
        // back result to int8
        auto tl1_convert_alloc = m.add_instruction(migraphx::make_op(
            "hip::allocate", {{"shape", migraphx::to_value(alpha_contiguous->get_shape())}}));
400
401
402
403
404
405
        auto tl1_convert =
            m.add_instruction(make_precompile_op(migraphx::make_op(
                                  "convert", {{"target_type", alpha->get_shape().type()}})),
                              conta,
                              tl1_convert_alloc);
        auto mul_alloc = m.add_instruction(migraphx::make_op(
406
            "hip::allocate", {{"shape", migraphx::to_value(tl1_convert->get_shape())}}));
407
408
        auto tl1_alpha_int32 =
            m.add_instruction(make_precompile_op("mul"), alpha_contiguous, tl1_convert, mul_alloc);
409
410
411
412
413
414
415
416
417
418
        // convert mul_res to int8
        auto tl1_alpha_int8_alloc = m.add_instruction(migraphx::make_op(
            "hip::allocate", {{"shape", migraphx::to_value(conta->get_shape())}}));

        migraphx::instruction_ref pta{};
        if(int8_x4)
        {
            pta = m.add_instruction(
                migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ps1)}}));
        }
Shucai Xiao's avatar
Shucai Xiao committed
419

420
421
422
423
424
        auto tl1_alpha_int8 =
            m.add_instruction(make_precompile_op(migraphx::make_op(
                                  "convert", {{"target_type", conta->get_shape().type()}})),
                              tl1_alpha_int32,
                              tl1_alpha_int8_alloc);
425
426
427
428
429
430
431
432
433
434
435
436
437

        auto pa = tl1_alpha_int8;
        if(int8_x4)
        {
            pa = m.add_instruction(
                migraphx::make_op("gpu::pad", {{"mode", 0}, {"pads", {0, 0, 0, 3, 0, 0, 0, 0}}}),
                tl1_alpha_int8,
                pta);
        }

        auto packb = pb;
        if(int8_x4)
        {
Shucai Xiao's avatar
Shucai Xiao committed
438
439
440
441
            auto allocpb = m.add_instruction(
                migraphx::make_op("hip::allocate", {{"shape", migraphx::to_value(ps2)}}));
            packb = m.add_instruction(migraphx::make_op("gpu::int8_gemm_pack_a"), pb, allocpb);
        }
442

Shucai Xiao's avatar
Shucai Xiao committed
443
        auto gemm = m.add_instruction(
444
445
446
447
448
449
            migraphx::make_op("gpu::quant_gemm",
                              {{"int8_x4_format", int8_x4},
                               {"compute_fp32", migraphx::gpu::get_compute_fp32_flag()}}),
            pa,
            packb,
            output);
Shucai Xiao's avatar
Shucai Xiao committed
450
451
452
453
454
        m.add_return({gemm});

        return m;
    };

455
456
457
    auto m1  = create_module();
    auto ctx = migraphx::gpu::context{};
    run_passes(m1, ctx);
Shucai Xiao's avatar
Shucai Xiao committed
458

459
460
    bool int8_x4 = migraphx::gpu::get_int8_x4_format(ctx);
    auto m2      = create_optimized_int8_x4(int8_x4);
Shucai Xiao's avatar
Shucai Xiao committed
461
462
463
464
465

    EXPECT(m1 == m2);
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }