Skip to content

Commit 9f5b222

Browse files
bmehta001Copilot
andcommitted
Use 1D shapes in MatMulInteger tests with shape inference disabled
Address review feedback: tests now use actual 1D inputs to exercise the vector x vector code path this PR fixes. AddShapeToTensorData(false) skips ONNX shape inference which rejects 1D MatMulInteger inputs at graph build time. Also removed unnecessary static_cast in matmul_helper.h to match the style of other K-dimension checks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent cbbcb33 commit 9f5b222

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

onnxruntime/core/providers/cpu/math/matmul_helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class MatMulComputeHelper {
170170
ORT_RETURN_IF_NOT(M_ == 1 && N_ == 1, "M_ == 1 && N_ == 1 was false");
171171
// Both inputs are 1D vectors: left(K) dot right(K) => scalar.
172172
// Validate K dimensions match to prevent out-of-bounds reads.
173-
ORT_RETURN_IF_NOT(K_ == static_cast<ptrdiff_t>(right_shape[0]),
173+
ORT_RETURN_IF_NOT(K_ == right_shape[0],
174174
"MatMul dimension mismatch");
175175
} else {
176176
if (left_num_dims == 1) {

onnxruntime/test/providers/cpu/math/matmul_integer_test.cc

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,29 +504,42 @@ TEST(MatmulIntegerOpTest, SharedPrepackedWeights) {
504504
}
505505
#endif
506506

507-
// Regression test: 1D inputs with mismatched K dimensions must fail
507+
// Regression test: 1D×1D inputs with mismatched K dimensions must fail
508508
// instead of causing an out-of-bounds read in the MLAS backend.
509509
TEST(MatmulIntegerOpTest, MatMulInteger_1D_DimensionMismatch) {
510510
OpTester test("MatMulInteger", 10);
511-
// A is 1D with K=5, B is 1D with K=1 — dimensions don't match.
511+
// Omit shape info so ONNX shape inference won't reject 1D inputs at graph build time.
512+
test.AddShapeToTensorData(false);
513+
// A is [5] (K=5), B is [1] (K=1) — vector×vector K dimensions don't match.
512514
test.AddInput<uint8_t>("T1", {5}, {11, 7, 3, 10, 6});
513515
test.AddInput<uint8_t>("T2", {1}, {1});
514516
test.AddInput<uint8_t>("a_zero_point", {}, {0});
515517
test.AddInput<uint8_t>("b_zero_point", {}, {0});
516518
test.AddOutput<int32_t>("T3", {}, {0});
517-
test.Run(OpTester::ExpectResult::kExpectFailure, "MatMul dimension mismatch");
519+
520+
// Restrict to CPU — other EPs may not support 1D quantized inputs.
521+
std::vector<std::unique_ptr<IExecutionProvider>> cpu_only;
522+
cpu_only.push_back(DefaultCpuExecutionProvider());
523+
test.Run(OpTester::ExpectResult::kExpectFailure, "MatMul dimension mismatch",
524+
{}, nullptr, &cpu_only);
518525
}
519526

520-
// Valid 1D × 1D dot product: both vectors have the same K.
527+
// Valid 1D×1D dot product: both vectors have the same K, producing scalar output.
521528
TEST(MatmulIntegerOpTest, MatMulInteger_1D_Valid) {
522529
OpTester test("MatMulInteger", 10);
523-
// A=[2,3], B=[4,5] => dot = 2*4 + 3*5 = 23
530+
// Omit shape info so ONNX shape inference won't reject 1D inputs at graph build time.
531+
test.AddShapeToTensorData(false);
532+
// A=[2,3], B=[4,5] => scalar dot = 2*4 + 3*5 = 23
524533
test.AddInput<uint8_t>("T1", {2}, {2, 3});
525534
test.AddInput<uint8_t>("T2", {2}, {4, 5});
526535
test.AddInput<uint8_t>("a_zero_point", {}, {0});
527536
test.AddInput<uint8_t>("b_zero_point", {}, {0});
528537
test.AddOutput<int32_t>("T3", {}, {23});
529-
test.Run();
538+
539+
// Restrict to CPU — other EPs may not support 1D quantized inputs.
540+
std::vector<std::unique_ptr<IExecutionProvider>> cpu_only;
541+
cpu_only.push_back(DefaultCpuExecutionProvider());
542+
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {}, nullptr, &cpu_only);
530543
}
531544

532545
} // namespace test

0 commit comments

Comments
 (0)