1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
| // Copyright (c) Microsoft Corporation. All rights reserved.
| // Licensed under the MIT License.
|
| #include "gtest/gtest.h"
| #include "test/common/cuda_op_test_utils.h"
| #include "test/common/quantization_test_utils.h"
| #include "test/providers/provider_test_utils.h"
|
| #include "core/common/common.h"
| #include "core/framework/op_kernel.h"
| #include "core/mlas/inc/mlas.h"
| #include "core/session/onnxruntime_session_options_config_keys.h"
| #include "core/util/math_cpuonly.h"
| #include "core/util/qmath.h"
|
| #include <algorithm>
| #include <random>
|
| namespace onnxruntime {
| namespace test {
|
| TEST(MatmulIntegerOpTest, MatMulInteger_2D) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<uint8_t>("T1", {4, 3}, {11, 7, 3, 10, 6, 2, 9, 5, 1, 8, 4, 0});
| test.AddInput<uint8_t>("T2", {3, 2}, {1, 4, 2, 5, 3, 6});
| test.AddInput<uint8_t>("a_zero_point", {}, {12});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| test.AddOutput<int32_t>("T3", {4, 2}, {-38, -83, -44, -98, -50, -113, -56, -128});
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_2D_empty_input) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<uint8_t>("T1", {0, 3}, {});
| test.AddInput<uint8_t>("T2", {3, 2}, {1, 4, 2, 5, 3, 6});
| test.AddInput<uint8_t>("a_zero_point", {}, {12});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| test.AddOutput<int32_t>("T3", {0, 2}, {});
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MissingAZeroPointWithBZeroPointCUDA) {
| auto cuda_ep = DefaultCudaExecutionProvider();
| if (!cuda_ep) {
| GTEST_SKIP() << "CUDA execution provider is not available.";
| }
|
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("A", {1, 1}, {3});
| test.AddInput<int8_t>("B", {1, 1}, {4});
| test.AddOptionalInputEdge<int8_t>();
| test.AddInput<int8_t>("b_zero_point", {}, {1});
| test.AddOutput<int32_t>("Y", {1, 1}, {9});
|
| SessionOptions options;
| ASSERT_STATUS_OK(options.config_options.AddConfigEntry(kOrtSessionOptionsDisableCPUEPFallback, "1"));
| std::vector<std::unique_ptr<IExecutionProvider>> providers;
| providers.push_back(std::move(cuda_ep));
| test.Run(options, OpTester::ExpectResult::kExpectSuccess, "", {}, nullptr, &providers);
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<uint8_t>("T1", {1, 1}, {11});
| test.AddInput<uint8_t>("T2", {1, 1}, {13});
| test.AddInput<uint8_t>("a_zero_point", {}, {12});
| test.AddInput<uint8_t>("b_zero_point", {}, {12});
| test.AddOutput<int32_t>("T3", {1, 1}, {-1});
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_t) {
| if (DefaultCudaExecutionProvider() &&
| !HasCudaEnvironment(530 /*min_cuda_architecture*/)) {
| return;
| }
|
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 4},
| {-3, 7, 5, -6,
| 4, -5, 8, 7});
| test.AddInput<int8_t>("T2",
| {4, 4},
| {5, -3, 7, 8,
| -6, -8, -3, 6,
| 7, 9, 9, -5,
| 8, 7, -6, 7});
| test.AddInput<int8_t>("a_zero_point", {}, {5});
| test.AddInput<int8_t>("b_zero_point", {}, {5});
| test.AddOutput<int32_t>("T3",
| {2, 4},
| {-55, 16, 89, -44,
| 122, 154, 68, -39});
|
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_t_A_ND) {
| if (DefaultCudaExecutionProvider() &&
| !HasCudaEnvironment(530 /*min_cuda_architecture*/)) {
| return;
| }
|
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 2, 4},
| {-3, 7, 5, -6,
| 4, -5, 8, 7,
|
| 7, -4, 3, 6,
| -4, -5, 5, 7});
|
| test.AddInput<int8_t>("T2",
| {4, 3},
| {5, -3, 7,
| 8, -6, -8,
| -3, 6, 7,
| 9, 9, -5});
|
| test.AddInput<int8_t>("a_zero_point", {}, {3});
| test.AddInput<int8_t>("b_zero_point", {}, {4});
| test.AddOutput<int32_t>("T3",
| {2, 2, 3},
| {-49, -39, 21,
| -46, 103, 78,
|
| -9, 57, 69,
| -33, 153, 45});
|
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_t_B_ND) {
| if (DefaultCudaExecutionProvider() &&
| !HasCudaEnvironment(530 /*min_cuda_architecture*/)) {
| return;
| }
|
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 4},
| {-3, 7, 5, -6,
| 4, -5, 8, 7});
| test.AddInput<int8_t>("T2",
| {2, 4, 3},
| {5, -3, 7,
| 8, -6, -8,
| -3, 6, 7,
| 9, 9, -5,
|
| 5, -3, 7,
| 8, -6, -8,
| -3, 6, 7,
| 9, 9, -5});
| test.AddInput<int8_t>("a_zero_point", {}, {1});
| test.AddInput<int8_t>("b_zero_point", {}, {2});
| test.AddOutput<int32_t>("T3",
| {2, 2, 3},
| {-45, -61, -11,
| -20, 103, 68,
|
| -45, -61, -11,
| -20, 103, 68});
|
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_t_A_ND_B_ND) {
| if (DefaultCudaExecutionProvider() &&
| !HasCudaEnvironment(530 /*min_cuda_architecture*/)) {
| return;
| }
|
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 2, 4},
| {-3, 7, 5, -6,
| 4, -5, 8, 7,
|
| -3, 7, 5, -6,
| 4, -5, 8, 7});
| test.AddInput<int8_t>("T2",
| {2, 4, 4},
| {5, -3, 7, 8,
| -6, -8, -3, 6,
| 7, 9, 9, -5,
| 8, 7, -6, 7,
|
| 5, -3, 7, 8,
| -6, -8, -3, 6,
| 7, 9, 9, -5,
| 8, 7, -6, 7});
| test.AddInput<int8_t>("a_zero_point", {}, {5});
| test.AddInput<int8_t>("b_zero_point", {}, {5});
| test.AddOutput<int32_t>("T3",
| {2, 2, 4},
| {-55, 16, 89, -44,
| 122, 154, 68, -39,
|
| -55, 16, 89, -44,
| 122, 154, 68, -39});
|
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_t_A_Has_Zero_Point) {
| if (DefaultCudaExecutionProvider() &&
| !HasCudaEnvironment(530 /*min_cuda_architecture*/)) {
| return;
| }
|
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 2, 4},
| {-3, 7, 5, -6,
| 4, -5, 8, 7,
|
| -3, 7, 5, -6,
| 4, -5, 8, 7});
| test.AddInput<int8_t>("T2",
| {2, 4, 4},
| {0, -8, 2, 3,
| -11, -13, -8, 1,
| 2, 4, 4, -10,
| 3, 2, -11, 2,
|
| 0, -8, 2, 3,
| -11, -13, -8, 1,
| 2, 4, 4, -10,
| 3, 2, -11, 2});
| test.AddInput<int8_t>("a_zero_point", {}, {5});
| test.AddOutput<int32_t>("T3",
| {2, 2, 4},
| {-55, 16, 89, -44,
| 122, 154, 68, -39,
|
| -55, 16, 89, -44,
| 122, 154, 68, -39});
|
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_t_No_Zero_Point) {
| if (DefaultCudaExecutionProvider() &&
| !HasCudaEnvironment(530 /*min_cuda_architecture*/)) {
| return;
| }
|
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 2, 4},
| {-8, 2, 0, -11,
| -1, -10, 3, 2,
|
| -8, 2, 0, -11,
| -1, -10, 3, 2});
| test.AddInput<int8_t>("T2",
| {2, 4, 4},
| {0, -8, 2, 3,
| -11, -13, -8, 1,
| 2, 4, 4, -10,
| 3, 2, -11, 2,
|
| 0, -8, 2, 3,
| -11, -13, -8, 1,
| 2, 4, 4, -10,
| 3, 2, -11, 2});
| test.AddOutput<int32_t>("T3",
| {2, 2, 4},
| {-55, 16, 89, -44,
| 122, 154, 68, -39,
|
| -55, 16, 89, -44,
| 122, 154, 68, -39});
|
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_WithZero_ZeroPoint) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<uint8_t>("T1", {4, 3}, {11, 7, 3, 10, 6, 2, 9, 5, 1, 8, 4, 0});
| test.AddInput<uint8_t>("T2", {3, 2}, {1, 4, 2, 5, 3, 6});
| test.AddInput<uint8_t>("a_zero_point", {}, {0});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| test.AddOutput<int32_t>("T3", {4, 2}, {34, 97, 28, 82, 22, 67, 16, 52});
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_PerColumn_ND) {
| // TODO: Unskip when fixed #41968513
| if (DefaultDmlExecutionProvider().get() != nullptr) {
| GTEST_SKIP() << "Skipping because of the following error: AbiCustomRegistry.cpp(507): The parameter is incorrect.";
| }
|
| OpTester test("MatMulInteger", 10);
| test.AddInput<uint8_t>("T1",
| {2, 2, 4},
| {125, 135, 133, 122,
| 132, 123, 136, 135,
|
| 125, 135, 133, 122,
| 132, 123, 136, 135});
| test.AddInput<int8_t>("T2",
| {2, 4, 4},
| {0, -8, 2, 3,
| -11, -13, -8, 1,
| 2, 4, 4, -10,
| 3, 2, -11, 2,
|
| 0, -8, 2, 3,
| -11, -13, -8, 1,
| 2, 4, 4, -10,
| 3, 2, -11, 2});
| test.AddInput<uint8_t>("a_zero_point", {}, {133});
| test.AddInput<int8_t>("b_zero_point",
| {2, 1, 4},
| {1, -2, 2, -1,
| 2, -4, -1, 0});
| test.AddOutput<int32_t>("T3",
| {2, 2, 4},
| {-38, -18, 123, -61,
| 128, 142, 80, -45,
|
| -21, -52, 72, -44,
| 134, 130, 62, -39});
|
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_uint8_2D) {
| OpTester test("MatMulInteger", 10);
|
| test.AddInput<int8_t>("T1",
| {2, 3},
| {-3, 7, 5,
| 4, -5, 8});
|
| test.AddInput<uint8_t>("T2",
| {3, 2},
| {5, 12,
| 7, 2,
| 9, 6});
|
| test.AddInput<int8_t>("a_zero_point", {}, {0});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
|
| test.AddOutput<int32_t>("T3",
| {2, 2},
| {79, 8,
| 57, 86});
|
| test.Run();
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_uint8_PerColumn_ND) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 2, 2},
| {1, -2,
| 3, 4,
|
| -1, 5,
| 2, -3});
|
| test.AddInput<uint8_t>("T2",
| {2, 2, 3},
| {10, 20, 30,
| 1, 2, 3,
|
| 4, 5, 6,
| 7, 8, 9});
|
| test.AddInput<int8_t>("a_zero_point", {}, {0});
|
| test.AddInput<uint8_t>("b_zero_point",
| {2, 1, 3},
| {1, 2, 3,
| 4, 5, 6});
|
| test.AddOutput<int32_t>("T3",
| {2, 2, 3},
| {9, 18, 27,
| 27, 54, 81,
|
| 15, 15, 15,
| -9, -9, -9});
|
| // TODO: Unskip when fixed #41968513
| test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
| }
|
| // [M x N] = [M x K] x [K x N] = [batch_seq x input_dim] x [input_dim x embed_dim]
| template <typename WeightType>
| void RunMatMulIntegerU8X8Test(const int M, const int N, const int K, bool B_is_initializer) {
| OpTester test("MatMulInteger", 10);
| static std::default_random_engine e(123);
| static std::uniform_int_distribution<int> n_unsigned(0, 127);
| static std::uniform_int_distribution<int> n_xint8(std::numeric_limits<WeightType>::min(), std::numeric_limits<WeightType>::max());
|
| Eigen::MatrixXi matrix_a = Eigen::MatrixXi::Random(K, M)
| .unaryExpr([](int) { return n_unsigned(e); });
| std::vector<uint8_t> matrix_a_data = ToVector<uint8_t>(matrix_a.data(), M * K);
| uint8_t a_zero_point = 0;
| Eigen::MatrixXi matrix_a_offset = matrix_a - a_zero_point * Eigen::MatrixXi::Ones(K, M);
|
| Eigen::MatrixXi matrix_b = Eigen::MatrixXi::Random(N, K)
| .unaryExpr([](int) { return n_xint8(e); });
| std::vector<WeightType> matrix_b_data = ToVector<WeightType>(matrix_b.data(), N * K);
| WeightType b_zero_point = 0;
| std::vector<WeightType> b_zp_per_column(N, b_zero_point);
| Eigen::MatrixXi b_zp_matrix = b_zero_point * Eigen::MatrixXi::Ones(N, K);
| Eigen::MatrixXi matrix_c = ((matrix_b - b_zp_matrix) * matrix_a_offset).eval();
|
| test.AddInput<uint8_t>("T1", {M, K}, std::move(matrix_a_data));
| test.AddInput<WeightType>("T2", {K, N}, std::move(matrix_b_data), B_is_initializer);
|
| test.AddOutput<int32_t>("T3", {M, N}, ToVector<int32_t>(matrix_c.data(), M * N));
| test.Run();
| }
|
| void RunMatMulIntegerU8X8TestBatch(const int M, const int N, const int K) {
| RunMatMulIntegerU8X8Test<int8_t>(M, N, K, false);
| RunMatMulIntegerU8X8Test<int8_t>(M, N, K, true);
| RunMatMulIntegerU8X8Test<uint8_t>(M, N, K, false);
| RunMatMulIntegerU8X8Test<uint8_t>(M, N, K, true);
| RunMatMulIntegerU8X8Test<int8_t>(M, N, K, false);
| RunMatMulIntegerU8X8Test<int8_t>(M, N, K, true);
| RunMatMulIntegerU8X8Test<uint8_t>(M, N, K, false);
| RunMatMulIntegerU8X8Test<uint8_t>(M, N, K, true);
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_Uint8_Int8_Scalar) {
| RunMatMulIntegerU8X8TestBatch(1, 1, 32);
| RunMatMulIntegerU8X8TestBatch(1, 1, 260);
| RunMatMulIntegerU8X8TestBatch(1, 1, 288);
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_Uint8_Int8_GEMV) {
| RunMatMulIntegerU8X8TestBatch(1, 2, 16);
| RunMatMulIntegerU8X8TestBatch(1, 2, 64);
| RunMatMulIntegerU8X8TestBatch(1, 8, 36);
| RunMatMulIntegerU8X8TestBatch(1, 8, 68);
| RunMatMulIntegerU8X8TestBatch(1, 8, 400);
| RunMatMulIntegerU8X8TestBatch(1, 512, 1024);
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_Uint8_Int8_GEMM) {
| RunMatMulIntegerU8X8TestBatch(2, 2, 40);
| RunMatMulIntegerU8X8TestBatch(2, 48, 33);
| RunMatMulIntegerU8X8TestBatch(2, 51, 40);
| RunMatMulIntegerU8X8TestBatch(4, 8, 68);
| }
|
| #ifndef ENABLE_TRAINING
| // Prepacking is disabled in full training build so no need to test the feature in a training build.
| TEST(MatmulIntegerOpTest, SharedPrepackedWeights) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<uint8_t>("T1", {1, 1}, {11});
| test.AddInput<uint8_t>("T2", {1, 1}, {13}, true); // Trigger pre-packing
| test.AddInput<uint8_t>("a_zero_point", {}, {12});
| test.AddInput<uint8_t>("b_zero_point", {}, {12});
| test.AddOutput<int32_t>("T3", {1, 1}, {-1});
|
| std::vector<uint8_t> t2_init_values(1, 13);
|
| OrtValue t2;
| Tensor::InitOrtValue(DataTypeImpl::GetType<uint8_t>(), TensorShape({1, 1}),
| t2_init_values.data(), OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator), t2);
|
| SessionOptions so;
| // Set up T2 as a shared initializer to be shared between sessions
| ASSERT_EQ(so.AddInitializer("T2", &t2), Status::OK());
|
| // We want all sessions running using this OpTester to be able to share pre-packed weights if applicable
| test.EnableSharingOfPrePackedWeightsAcrossSessions();
|
| // Pre-packing is limited just to the CPU EP for now and we will only test the CPU EP
| // and we want to ensure that it is available in this build
| auto cpu_ep = []() -> std::vector<std::unique_ptr<IExecutionProvider>> {
| std::vector<std::unique_ptr<IExecutionProvider>> execution_providers;
| execution_providers.push_back(DefaultCpuExecutionProvider());
| return execution_providers;
| };
|
| size_t number_of_pre_packed_weights_counter_session_1 = 0;
| size_t number_of_shared_pre_packed_weights_counter = 0;
|
| // Session 1
| {
| auto ep_vec = cpu_ep();
| test.Run(so, OpTester::ExpectResult::kExpectSuccess, "", {}, nullptr,
| &ep_vec, {}, &number_of_pre_packed_weights_counter_session_1, &number_of_shared_pre_packed_weights_counter);
| // Assert that no pre-packed weights have been shared thus far
| ASSERT_EQ(number_of_shared_pre_packed_weights_counter, static_cast<size_t>(0));
| }
|
| auto number_of_elements_in_shared_prepacked_buffers_container =
| test.GetNumPrePackedWeightsShared();
| // Assert that the number of elements in the shared container
| // is the same as the number of weights that have been pre-packed
| ASSERT_EQ(number_of_pre_packed_weights_counter_session_1, number_of_elements_in_shared_prepacked_buffers_container);
|
| // On some platforms/architectures MLAS may choose to not do any pre-packing and the number of elements
| // that have been pre-packed will be zero in which case we do not continue with the testing
| // of "sharing" of pre-packed weights as there are no pre-packed weights to be shared at all.
| if (number_of_pre_packed_weights_counter_session_1 == 0)
| return;
|
| // Session 2
| {
| size_t number_of_pre_packed_weights_counter_session_2 = 0;
| auto ep_vec = cpu_ep();
| test.Run(so, OpTester::ExpectResult::kExpectSuccess, "", {}, nullptr,
| &ep_vec, {}, &number_of_pre_packed_weights_counter_session_2, &number_of_shared_pre_packed_weights_counter);
|
| // Assert that the same number of weights were pre-packed in both sessions
| ASSERT_EQ(number_of_pre_packed_weights_counter_session_1, number_of_pre_packed_weights_counter_session_2);
|
| // Assert that the number of pre-packed weights that were shared equals
| // the number of pre-packed weights in the second session
| ASSERT_EQ(number_of_pre_packed_weights_counter_session_2,
| static_cast<size_t>(number_of_shared_pre_packed_weights_counter));
| }
| }
| #endif
|
| // Regression test: 1D vector dot product with matching K dimension should succeed.
| // A=[K], B=[K] -> scalar output (dot product).
| TEST(MatmulIntegerOpTest, MatMulInteger_1D_Vector_DotProduct) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<uint8_t>("T1", {4}, {1, 2, 3, 4});
| test.AddInput<uint8_t>("T2", {4}, {5, 6, 7, 8});
| test.AddInput<uint8_t>("a_zero_point", {}, {0});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| // dot product: 1*5 + 2*6 + 3*7 + 4*8 = 5 + 12 + 21 + 32 = 70
| test.AddOutput<int32_t>("T3", {}, {70});
| test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
| }
|
| // Same 1D vector dot product test with int8_t types.
| TEST(MatmulIntegerOpTest, MatMulInteger_1D_Vector_DotProduct_int8) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1", {3}, {1, -2, 3});
| test.AddInput<int8_t>("T2", {3}, {4, 5, -6});
| test.AddInput<int8_t>("a_zero_point", {}, {0});
| test.AddInput<int8_t>("b_zero_point", {}, {0});
| // dot product: 1*4 + (-2)*5 + 3*(-6) = 4 - 10 - 18 = -24
| test.AddOutput<int32_t>("T3", {}, {-24});
| test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
| }
|
| // Regression test: 1D vectors with mismatched K dimension must fail safely.
| // Covers prior invalid-shape handling for A=[K], B=[1] where K > 1.
| TEST(MatmulIntegerOpTest, MatMulInteger_1D_Vector_KDimensionMismatch) {
| OpTester test("MatMulInteger", 10);
| test.AddShapeToTensorData(false);
| test.AddInput<uint8_t>("T1", {4}, {1, 1, 1, 1});
| test.AddInput<uint8_t>("T2", {1}, {5});
| test.AddInput<uint8_t>("a_zero_point", {}, {0});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| test.AddOutput<int32_t>("T3", {}, {0});
| test.Run(OpTester::ExpectResult::kExpectFailure, "MatMul dimension mismatch", {kDmlExecutionProvider});
| }
|
| // Same regression test with int8_t types.
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_1D_Vector_KDimensionMismatch) {
| OpTester test("MatMulInteger", 10);
| test.AddShapeToTensorData(false);
| test.AddInput<int8_t>("T1", {8}, {1, 1, 1, 1, 1, 1, 1, 1});
| test.AddInput<int8_t>("T2", {1}, {5});
| test.AddInput<int8_t>("a_zero_point", {}, {0});
| test.AddInput<int8_t>("b_zero_point", {}, {0});
| test.AddOutput<int32_t>("T3", {}, {0});
| test.Run(OpTester::ExpectResult::kExpectFailure, "MatMul dimension mismatch", {kDmlExecutionProvider});
| }
|
| // 1D dot product: uint8 A x int8 B (mixed types).
| TEST(MatmulIntegerOpTest, MatMulInteger_1D_Vector_DotProduct_uint8_int8) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<uint8_t>("T1", {3}, {10, 20, 30});
| test.AddInput<int8_t>("T2", {3}, {1, -1, 2});
| test.AddInput<uint8_t>("a_zero_point", {}, {0});
| test.AddInput<int8_t>("b_zero_point", {}, {0});
| // dot product: 10*1 + 20*(-1) + 30*2 = 10 - 20 + 60 = 50
| test.AddOutput<int32_t>("T3", {}, {50});
| test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
| }
|
| // 1D dot product: int8 A x uint8 B (mixed types).
| TEST(MatmulIntegerOpTest, MatMulInteger_1D_Vector_DotProduct_int8_uint8) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1", {3}, {-1, 2, -3});
| test.AddInput<uint8_t>("T2", {3}, {10, 20, 30});
| test.AddInput<int8_t>("a_zero_point", {}, {0});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| // dot product: (-1)*10 + 2*20 + (-3)*30 = -10 + 40 - 90 = -60
| test.AddOutput<int32_t>("T3", {}, {-60});
| test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
| }
|
| // int8 A x uint8 B, 2D with zero points.
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_uint8_2D_WithZeroPoints) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 3},
| {-3, 7, 5,
| 4, -5, 8});
| test.AddInput<uint8_t>("T2",
| {3, 2},
| {15, 22,
| 17, 12,
| 19, 16});
| test.AddInput<int8_t>("a_zero_point", {}, {2});
| test.AddInput<uint8_t>("b_zero_point", {}, {10});
| // A_offset = A - 2: {-5, 5, 3, 2, -7, 6}
| // B_offset = B - 10: {5, 12, 7, 2, 9, 6}
| // C[0,0] = (-5)*5 + 5*7 + 3*9 = -25 + 35 + 27 = 37
| // C[0,1] = (-5)*12 + 5*2 + 3*6 = -60 + 10 + 18 = -32
| // C[1,0] = 2*5 + (-7)*7 + 6*9 = 10 - 49 + 54 = 15
| // C[1,1] = 2*12 + (-7)*2 + 6*6 = 24 - 14 + 36 = 46
| test.AddOutput<int32_t>("T3",
| {2, 2},
| {37, -32,
| 15, 46});
| test.Run();
| }
|
| // int8 A x uint8 B, A=ND B=ND with broadcasting.
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_uint8_A_ND_B_ND) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 2, 3},
| {1, -2, 3,
| -1, 4, -2,
|
| 1, -2, 3,
| -1, 4, -2});
| test.AddInput<uint8_t>("T2",
| {2, 3, 2},
| {10, 20,
| 30, 40,
| 50, 60,
|
| 10, 20,
| 30, 40,
| 50, 60});
| test.AddInput<int8_t>("a_zero_point", {}, {0});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| // batch 0: [[1,-2,3],[−1,4,−2]] x [[10,20],[30,40],[50,60]]
| // [0,0] = 1*10+(-2)*30+3*50 = 10-60+150 = 100
| // [0,1] = 1*20+(-2)*40+3*60 = 20-80+180 = 120
| // [1,0] = (-1)*10+4*30+(-2)*50 = -10+120-100 = 10
| // [1,1] = (-1)*20+4*40+(-2)*60 = -20+160-120 = 20
| test.AddOutput<int32_t>("T3",
| {2, 2, 2},
| {100, 120,
| 10, 20,
|
| 100, 120,
| 10, 20});
| test.Run();
| }
|
| // int8 A x uint8 B, A=ND B=2D (broadcast B across batches).
| TEST(MatmulIntegerOpTest, MatMulInteger_int8_uint8_A_ND_B_2D) {
| OpTester test("MatMulInteger", 10);
| test.AddInput<int8_t>("T1",
| {2, 2, 3},
| {1, -2, 3,
| -1, 4, -2,
|
| 2, 0, -1,
| 0, 3, 1});
| test.AddInput<uint8_t>("T2",
| {3, 2},
| {10, 20,
| 30, 40,
| 50, 60});
| test.AddInput<int8_t>("a_zero_point", {}, {0});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| // batch 0: same as above = {100,120,10,20}
| // batch 1: [[2,0,-1],[0,3,1]] x [[10,20],[30,40],[50,60]]
| // [0,0] = 2*10+0*30+(-1)*50 = 20-50 = -30
| // [0,1] = 2*20+0*40+(-1)*60 = 40-60 = -20
| // [1,0] = 0*10+3*30+1*50 = 90+50 = 140
| // [1,1] = 0*20+3*40+1*60 = 120+60 = 180
| test.AddOutput<int32_t>("T3",
| {2, 2, 2},
| {100, 120,
| 10, 20,
|
| -30, -20,
| 140, 180});
| test.Run();
| }
|
| // [M x N] = [M x K] x [K x N] with int8 A, parameterized on B type.
| template <typename WeightType>
| void RunMatMulIntegerS8X8Test(const int M, const int N, const int K, bool B_is_initializer) {
| OpTester test("MatMulInteger", 10);
| static std::default_random_engine e(456);
| static std::uniform_int_distribution<int> n_signed(-64, 63);
| static std::uniform_int_distribution<int> n_xint8(std::numeric_limits<WeightType>::min(), std::numeric_limits<WeightType>::max());
|
| Eigen::MatrixXi matrix_a = Eigen::MatrixXi::Random(K, M)
| .unaryExpr([](int) { return n_signed(e); });
| std::vector<int8_t> matrix_a_data = ToVector<int8_t>(matrix_a.data(), M * K);
| int8_t a_zero_point = 0;
| Eigen::MatrixXi matrix_a_offset = matrix_a - a_zero_point * Eigen::MatrixXi::Ones(K, M);
|
| Eigen::MatrixXi matrix_b = Eigen::MatrixXi::Random(N, K)
| .unaryExpr([](int) { return n_xint8(e); });
| std::vector<WeightType> matrix_b_data = ToVector<WeightType>(matrix_b.data(), N * K);
| WeightType b_zero_point = 0;
| Eigen::MatrixXi b_zp_matrix = b_zero_point * Eigen::MatrixXi::Ones(N, K);
| Eigen::MatrixXi matrix_c = ((matrix_b - b_zp_matrix) * matrix_a_offset).eval();
|
| test.AddInput<int8_t>("T1", {M, K}, std::move(matrix_a_data));
| test.AddInput<WeightType>("T2", {K, N}, std::move(matrix_b_data), B_is_initializer);
|
| test.AddOutput<int32_t>("T3", {M, N}, ToVector<int32_t>(matrix_c.data(), M * N));
| test.Run();
| }
|
| void RunMatMulIntegerS8X8TestBatch(const int M, const int N, const int K) {
| RunMatMulIntegerS8X8Test<int8_t>(M, N, K, false);
| RunMatMulIntegerS8X8Test<int8_t>(M, N, K, true);
| RunMatMulIntegerS8X8Test<uint8_t>(M, N, K, false);
| RunMatMulIntegerS8X8Test<uint8_t>(M, N, K, true);
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_Int8_X8_Scalar) {
| RunMatMulIntegerS8X8TestBatch(1, 1, 32);
| RunMatMulIntegerS8X8TestBatch(1, 1, 260);
| RunMatMulIntegerS8X8TestBatch(1, 1, 288);
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_Int8_X8_GEMV) {
| RunMatMulIntegerS8X8TestBatch(1, 2, 16);
| RunMatMulIntegerS8X8TestBatch(1, 2, 64);
| RunMatMulIntegerS8X8TestBatch(1, 8, 36);
| RunMatMulIntegerS8X8TestBatch(1, 8, 68);
| RunMatMulIntegerS8X8TestBatch(1, 8, 400);
| }
|
| TEST(MatmulIntegerOpTest, MatMulInteger_Int8_X8_GEMM) {
| RunMatMulIntegerS8X8TestBatch(2, 2, 40);
| RunMatMulIntegerS8X8TestBatch(2, 48, 33);
| RunMatMulIntegerS8X8TestBatch(2, 51, 40);
| RunMatMulIntegerS8X8TestBatch(4, 8, 68);
| }
|
| // Per-row A zero point is not supported by the CPU implementation.
| // Verify it is rejected gracefully.
| TEST(MatmulIntegerOpTest, MatMulInteger_PerRow_A_ZeroPoint_Rejected) {
| OpTester test("MatMulInteger", 10);
| test.AddShapeToTensorData(false);
| test.AddInput<uint8_t>("T1",
| {2, 3},
| {11, 7, 3,
| 10, 6, 2});
| test.AddInput<uint8_t>("T2",
| {3, 2},
| {1, 4, 2, 5, 3, 6});
| // per-row A zero point: shape {2, 1} — not scalar
| test.AddInput<uint8_t>("a_zero_point", {2, 1}, {12, 10});
| test.AddInput<uint8_t>("b_zero_point", {}, {0});
| test.AddOutput<int32_t>("T3", {2, 2}, {0, 0, 0, 0});
| test.Run(OpTester::ExpectResult::kExpectFailure, "", {kDmlExecutionProvider});
| }
|
| } // namespace test
| } // namespace onnxruntime
|
|