Repository navigation
[WebGPU] Share MatMulNBits input preparation and tune narrow DP4A - #33635
Open
Jiajia Qin (qjia7) wants to merge 6 commits into
Open
Jiajia Qin (qjia7) wants to merge 6 commits into
Jiajia Qin (qjia7) wants to merge 6 commits into
Conversation
Use rectangular tiles and K-parallel reduction for narrow projections, while retaining the existing full-width load schedule. Cover INT2/4/8 correctness and long-K dispatch, including the existing activation-quantization error in test tolerances.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new INT4 and INT8 regressions silently execute no cases in combined CUDA/WebGPU builds, leaving their intended coverage absent.
2 open issues
What changed in this PR
This PR extracts reusable WebGPU MatMulNBits optimizations from #33370, separating shared input preparation and narrow-projection tuning from the QLoRA implementation.
Changes:
- Adds invocation-local reuse of activation quantization and subgroup-matrix prepacking.
- Tunes narrow DP4A projections with rectangular tiles, wider INT8 loads, and K-parallel reduction while preserving full-width loading.
- Adds shared-input and INT2/4/8 regression tests covering tails, long K, zero points, and bias.
| File | Description |
|---|---|
onnxruntime/test/providers/webgpu/matmul_nbits_input_preparation_test.cc |
Tests shared versus fresh input preparation. |
onnxruntime/test/contrib_ops/matmul_8bits_test.cc |
Adds narrow INT8 projection regressions. |
onnxruntime/test/contrib_ops/matmul_4bits_test.cc |
Adds narrow INT4 regressions and updates a shader-name comment. |
onnxruntime/test/contrib_ops/matmul_2bits_test.cc |
Adds narrow INT2 regressions and FP16 WebGPU execution. |
onnxruntime/contrib_ops/webgpu/quantization/subgroup_matrix_matmul_nbits.h |
Declares reusable prepacking interfaces. |
onnxruntime/contrib_ops/webgpu/quantization/subgroup_matrix_matmul_nbits.cc |
Extracts tiling selection and accepts prepacked activations. |
onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.h |
Adds an optional quantized-input workspace parameter. |
onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc |
Forwards the workspace to DP4A execution. |
onnxruntime/contrib_ops/webgpu/quantization/dp4a_matmul.wgsl.template |
Supports rectangular tiles and wider K loads. |
onnxruntime/contrib_ops/webgpu/quantization/dp4a_matmul_nbits.h |
Defines shared workspace and configurable DP4A programs. |
onnxruntime/contrib_ops/webgpu/quantization/dp4a_matmul_nbits.cc |
Reuses quantization and selects narrow-projection strategies. |
onnxruntime/contrib_ops/webgpu/quantization/dp4a_matmul_k_parallel.wgsl.template |
Renames the reduction shader and bounds scale loading. |
Review effort: Balanced
Restore the original 64x64 DP4A program, dispatch and WGSL. Remove small-tile test cases while retaining shared input preparation and focused INT2/4/8 K-parallel coverage.
Jiajia Qin (qjia7)
marked this pull request as draft
October 27, 2026 05:47
Jiajia Qin (qjia7)
marked this pull request as ready for review
October 27, 2026 09:19
Jiajia Qin (qjia7)
requested review from
Edward Chen (edgchen1) and
Xiaofei Han (xiaofeihan1)
October 27, 2026 09:20
This branch has not been deployed
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.

Description
Extend the existing K-parallel DP4A dispatch to narrow INT2/4/8 projections (
N <= 32,K >= 2048), with FP32 accumulation of their partial sums. Allow projections to share activation quantization and subgroup-matrix input prepacking within one invocation.The fixed 64×64
DP4AMatMulNBitsProgram, its dispatch, and its WGSL shader are unchanged by this PR.Motivation and Context
Extract reusable kernel changes from #33370 so its QLoRA operator and fused shaders can be reviewed separately. #33370 will be rebased after this merges.
Performance
RTX 5080, Windows, block size 32, graph capture enabled. GPU timestamp medians include preparation and multiplication (two rounds, 100 replays each, warm-up samples discarded):
| M × K × N | Weights / activations | D3D12, main → PR | Vulkan, main → PR |
|---|---|---:|---:|
| 128 × 2048 × 32 | INT8 / FP16 | 47 → 10 µs | 52 → 14 µs |
| 512 × 4096 × 32 | INT8 / FP16 | 96 → 36 µs | 97.5 → 32 µs |
| 128 × 2048 × 4096 | INT4 / FP16 | 84.5 → 84 µs | 84 → 85 µs |
| 128 × 2048 × 4096 | INT4 / FP32 | 74 → 74 µs | 73.5 → 73 µs |
These measurements cover isolated GPU operators; timestamp resolution is 1 µs.
Validation
Windows Release build with built-in WebGPU, D3D12 and Vulkan; lint and
git diff --checkpassed.Provider regressions: 15 passed, five skipped (four Arm64/KleidiAI cases and subgroup-matrix reuse on D3D12). All 28 QKV/MLP checks passed.
All three shared-input tests passed with Vulkan, including subgroup-matrix prepacking.
Retained INT2/4/8 tests cover K-parallel dispatch thresholds, K tails, 16/32-column outputs, batching, zero points, bias, and FP32 inputs. Shared-input tests also cover FP16 when supported by the device. Tests specific to the removed tile variants are excluded.
Dedicated FP16 INT2/4/8 regressions cover cancellation after partial sums exceed the FP16 range, with the general FP32-accumulation option disabled. They check device capabilities and disable CPU fallback. All fail when the narrow-path FP32 override is removed and pass with it restored.