Skip to content

[WebGPU] Support odd-N subgroup matrix MatMul weights - #29893

Merged
Jiajia Qin (qjia7) merged 2 commits into
microsoft:mainfrom
jchen10:sgmm_padding
Aug 6, 2026
Merged

[WebGPU] Support odd-N subgroup matrix MatMul weights#29893
Jiajia Qin (qjia7) merged 2 commits into
microsoft:mainfrom
jchen10:sgmm_padding

Conversation

@jchen10

Copy link
Copy Markdown
Contributor

Adds odd-N support to WebGPU subgroup-matrix MatMul by padding constant FP16 weights to an even row stride and caching the result.

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

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@jchen10

Copy link
Copy Markdown
Contributor Author

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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-N FP16 B, and plumb a new N_b uniform through the subgroup-matrix MatMul shader.
  • Introduce a dedicated WGSL pad kernel template to materialize the padded B buffer ([... , K, N] -> [..., K, N_b] with zero-filled extra column).
  • Add a large (currently disabled) test that marks B as 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().

Comment thread onnxruntime/core/providers/webgpu/math/subgroup_matrix_matmul.cc
Comment thread onnxruntime/test/providers/webgpu/matmul_large_test.cc
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.
@hariharans29

Copy link
Copy Markdown
Member

Let's please wait for Jiajia Qin (@qjia7) to take a look at this PR when she is back

@jchen10

Copy link
Copy Markdown
Contributor Author

Sounds good, thanks!

@edgchen1 Edward Chen (edgchen1) added the ep:WebGPU ort-web webgpu provider label Jul 30, 2026
Comment thread onnxruntime/core/providers/webgpu/math/subgroup_matrix_matmul.cc
@qjia7
Jiajia Qin (qjia7) merged commit 1fe713b into microsoft:main Aug 6, 2026
86 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ep:WebGPU ort-web webgpu provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants