Skip to content

Added a dimension check in MatMulComputeHelper to ensure the inner dimension K_#28053

Open
yuslepukhin wants to merge 8 commits intomainfrom
yuslepukhin/msrc_111715
Open

Added a dimension check in MatMulComputeHelper to ensure the inner dimension K_#28053
yuslepukhin wants to merge 8 commits intomainfrom
yuslepukhin/msrc_111715

Conversation

@yuslepukhin
Copy link
Copy Markdown
Member

@yuslepukhin yuslepukhin commented Apr 13, 2026

This pull request improves the handling and testing of 1D vector dot products in the MatMul implementation. It adds stricter validation for the K dimension in 1D vector cases and introduces new regression tests to ensure correct behavior for both valid and invalid input shapes.

Validation Enhancement:

  • Added a check in MatMulComputeHelper to ensure that when both inputs are 1D vectors, their K dimensions match, and to provide a clear error message if they do not.

Testing Improvements:

  • Added new tests in matmul_integer_test.cc to verify:
    • Correct computation for 1D vector dot products with matching K dimensions, for both uint8_t and int8_t types.
    • Proper failure and error messaging when 1D vectors with mismatched K dimensions are provided, for both uint8_t and int8_t types.

@yuslepukhin yuslepukhin requested a review from Copilot April 13, 2026 22:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a defensive dimension check to prevent out-of-bounds reads in MatMulInteger for 1D-vector dot products with mismatched inner dimensions, and introduces regression tests to cover valid and invalid 1D shape combinations.

Changes:

  • Add an explicit K_ vs right_shape[0] validation for the 1D-vector dot-product path in MatMulComputeHelper.
  • Add regression tests for successful 1D dot product and failure on mismatched 1D shapes for both uint8_t and int8_t.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
onnxruntime/core/providers/cpu/math/matmul_helper.h Adds a new inner-dimension check for 1D-vector dot products to prevent OOB reads.
onnxruntime/test/providers/cpu/math/matmul_integer_test.cc Adds regression tests for 1D dot product success and mismatched-dimension failures.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/test/providers/cpu/math/matmul_integer_test.cc Outdated
Comment thread onnxruntime/test/providers/cpu/math/matmul_integer_test.cc Outdated
Comment thread onnxruntime/core/providers/cpu/math/matmul_helper.h Outdated
Comment thread onnxruntime/test/providers/cpu/math/matmul_integer_test.cc Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/test/providers/cpu/math/matmul_integer_test.cc Outdated
Comment thread onnxruntime/test/providers/cpu/math/matmul_integer_test.cc
Comment thread onnxruntime/test/providers/cpu/math/matmul_integer_test.cc
@yuslepukhin yuslepukhin requested a review from Copilot April 14, 2026 17:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/test/providers/cpu/math/matmul_integer_test.cc Outdated
Comment thread onnxruntime/test/providers/cpu/math/matmul_integer_test.cc
@vraspar
Copy link
Copy Markdown
Contributor

vraspar commented Apr 14, 2026

By default, OpTester sets add_shape_to_tensor_data_ = true, which embeds the input shapes ([4] and [1]) into the graph's type metadata. During session.Initialize() → graph.Resolve(), ONNX's MatMulShapeInference (in onnx/defs/math/utils.cc:193) already validates inner dimensions and rejects the graph with "Incompatible dimensions for matrix multiplication".

So I believe to ensure the tests actually prove the runtime check works, the mismatch tests should disable shape metadata so inference is bypassed: test.SetAddShapeToTensorData(false);

Nits:

  • Should we also add a reverse mismatch test (A=[1], B=[4]) to cover both directions ?
  • The new error message ("Incompatible dimensions...") differs from the established "MatMul dimension mismatch" used in every other branch of the same helper and happens to collide with the ONNX shape inference message, making it harder to distinguish which check fired.

@yuslepukhin yuslepukhin requested a review from Copilot April 14, 2026 22:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@yuslepukhin yuslepukhin requested a review from vraspar April 14, 2026 22:56
@vraspar
Copy link
Copy Markdown
Contributor

vraspar commented Apr 14, 2026

Review generated with assistance from GitHub Copilot CLI.

Looks good overall — the fix is correct and the updated tests properly bypass ONNX shape inference via AddShapeToTensorData(false). A few minor nits:

1. Consider adding a plain MatMul (float) negative test — The fix is in the shared MatMulComputeHelper, which is also used by MatMul and QLinearMatMul. A single 1D mismatch test in matmul_test.cc (with AddShapeToTensorData(false)) would confirm the runtime guard works for all consumers of the helper, not just MatMulInteger.

2. Reverse mismatch direction — Both mismatch tests use left-longer-than-right (A=[4],B=[1]). Consider adding one reverse case (A=[1],B=[4]) to prove the check is symmetric.

3. PerRow_A_ZeroPoint_Rejected asserts on empty string

test.Run(OpTester::ExpectResult::kExpectFailure, "");

This passes on any failure. A more specific substring would strengthen the signal.

None of these are blocking — the security fix and core regression tests look solid.

vraspar
vraspar previously approved these changes Apr 14, 2026
Copy link
Copy Markdown
Contributor

@vraspar vraspar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yuslepukhin
Copy link
Copy Markdown
Member Author

/azp run Windows ARM64 QNN CI Pipeline

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants