[WebGPU] Support odd-N subgroup matrix MatMul weights - #29893
Merged
Conversation
Adds odd-N support to WebGPU subgroup-matrix MatMul by padding constant FP16 weights to an even row stride and caching the result.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
Author
Copilot started reviewing on behalf of
Hariharan Seshadri (hariharans29)
July 28, 2026 05:57
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the WebGPU subgroup-matrix MatMul fast path to support odd N when B is a constant FP16 initializer, by padding B to an even physical row stride (N_b = N + 1) once and reusing the cached padded buffer for subsequent runs. This keeps the optimized Intel subgroup-matrix load path correct while preserving the logical output width N.
Changes:
- Add a lazily-built, cached GPU-side padding step for constant odd-
NFP16B, and plumb a newN_buniform through the subgroup-matrix MatMul shader. - Introduce a dedicated WGSL pad kernel template to materialize the padded
Bbuffer ([... , K, N] -> [..., K, N_b]with zero-filled extra column). - Add a large (currently disabled) test that marks
Bas an initializer to exercise the padded constant-weight path.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| onnxruntime/test/providers/webgpu/matmul_large_test.cc | Adds ability to mark B as an initializer and introduces an odd-N constant-weight test case (disabled). |
| onnxruntime/core/providers/webgpu/math/subgroup_matrix_matmul.h | Extends subgroup-matrix MatMul uniforms with N_b (physical B stride). |
| onnxruntime/core/providers/webgpu/math/subgroup_matrix_matmul.cc | Implements cached padding of constant odd-N B and routes the subgroup kernel to use padded B + N_b. |
| onnxruntime/core/providers/webgpu/math/subgroup_matrix_matmul_pad_b.wgsl.template | New WGSL template to copy+pad B to an even stride. |
| onnxruntime/core/providers/webgpu/math/subgroup_matrix_matmul_8x16x16.wgsl.template | Updates B indexing/loads to use uniforms.N_b and clarifies preconditions. |
| onnxruntime/core/providers/webgpu/math/matmul.h | Tracks whether input B is a constant initializer via TryGetConstantInput and exposes IsBConstant(). |
Use WORKGROUP_SIZE consistently for the B-padding dispatch instead of a hard-coded 64 that relied on the program's default workgroup size, so the dispatch count and workgroup size can't drift apart. Short-circuit the pad pass when the padded tensor is empty (output_size == 0): just cache the empty tensor rather than issue a zero-workgroup dispatch, which some drivers reject. Add odd-N cases to MathOpTest.MatMulSubgroupMatrix so the constant-weight even-stride padding path is exercised by the normal (non-disabled) test suite.
Member
|
Let's please wait for Jiajia Qin (@qjia7) to take a look at this PR when she is back |
Contributor
Author
|
Sounds good, thanks! |
Jiajia Qin (qjia7)
approved these changes
Aug 6, 2026
Ananya Anand (4n4ny4)
added a commit
to 4n4ny4/onnxruntime
that referenced
this pull request
Aug 13, 2026
The in-tree golden test compares the entire generated tree, so goldens cannot be updated for one template in isolation. Rebasing onto main pulled in subgroup_matrix_matmul_pad_b.wgsl.template and a rewrite of subgroup_matrix_matmul_8x16x16.wgsl.template (microsoft#29893, microsoft#29813), neither of which regenerated the goldens, so the golden test fails on main today. Regenerating for this branch necessarily picks those up. The remaining churn is __str_N renumbering: the generator emits one globally numbered string table, so new literals shift every downstream index. pad.h, oihw_to_ohwi.h and im2col_matmul.h are unchanged after normalizing those indices. Generated with: UPDATE_WGSL_GOLDEN=1 python wgsl_template/test/run_tests.py Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Ananya Anand (4n4ny4)
added a commit
to 4n4ny4/onnxruntime
that referenced
this pull request
Aug 14, 2026
The in-tree golden test compares the entire generated tree, so goldens cannot be updated for one template in isolation. Rebasing onto main pulled in subgroup_matrix_matmul_pad_b.wgsl.template and a rewrite of subgroup_matrix_matmul_8x16x16.wgsl.template (microsoft#29893, microsoft#29813), neither of which regenerated the goldens, so the golden test fails on main today. Regenerating for this branch necessarily picks those up. The remaining churn is __str_N renumbering: the generator emits one globally numbered string table, so new literals shift every downstream index. pad.h, oihw_to_ohwi.h and im2col_matmul.h are unchanged after normalizing those indices. Generated with: UPDATE_WGSL_GOLDEN=1 python wgsl_template/test/run_tests.py Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds odd-N support to WebGPU subgroup-matrix MatMul by padding constant FP16 weights to an even row stride and caching the result.