Repository navigation
[CPU/WebGPU] Support empty MatMulNBits weights - #33559
Merged
Jiajia Qin (qjia7) merged 4 commits intoOct 28, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Existing WebGPU fusions bypass the empty-weight handling, and the new FP16 tests need device-feature guards.
2 open issues
What changed in this PR
Adds empty packed-weight support to CPU and WebGPU MatMulNBits, allowing optional projections to use zero-sized defaults while preserving logical output shapes.
Changes:
- Validates empty packed shapes and returns zeros or broadcast bias without processing unused quantization data.
- Records WebGPU fill dispatches for graph replay and skips unused CPU prepacking.
- Documents the behavior and adds tests for empty weights, runtime overrides, malformed shapes, and graph capture.
| File | Description |
|---|---|
| onnxruntime/test/contrib_ops/matmul_nbits_empty_weights_test.cc | Adds CPU/WebGPU correctness, override, and replay tests. |
| onnxruntime/core/graph/contrib_ops/contrib_defs.cc | Documents empty-weight semantics in the operator schema. |
| onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc | Adds a replayable zero/bias fill path. |
| onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc | Skips unused prepacking and fills empty-weight outputs. |
| onnxruntime/contrib_ops/cpu/quantization/matmul_nbits_helper.h | Adds shared empty-weight shape validation. |
| docs/ContribOperators.md | Updates the generated operator documentation. |
Review effort: Balanced
Jiajia Qin (qjia7)
marked this pull request as ready for review
October 26, 2026 01:54
Jiajia Qin (qjia7)
requested review from
Edward Chen (edgchen1) and
Xiaofei Han (xiaofeihan1)
October 26, 2026 01:58
Xiaofei Han (xiaofeihan1)
previously approved these changes
Oct 27, 2026
Xiaofei Han (xiaofeihan1)
left a comment
Contributor
There was a problem hiding this comment.
LGTM. One non-blocking question about model portability below.
Xiaofei Han (xiaofeihan1)
approved these changes
Oct 27, 2026
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
Support empty packed weights in CPU and WebGPU
MatMulNBits. Withweight_prepacked=0, one or more dimensions of B's packed shape may be zero; the other dimensions must retain their normal values.The output keeps the logical shape determined by A, K and N, and contains zeros or the broadcast bias. Unused quantization parameters may also be empty. CPU skips unused parameter packing and matrix multiplication; WebGPU records a fill dispatch so graph replay rewrites the output correctly. QKV/MLP fusion remains enabled: when a projection has empty weights, the fused operator uses the shared zero/bias fallback while computing the nonempty projections.
Includes operator documentation and focused tests for FP16/FP32, 2/4/8-bit weights, constant/runtime inputs, bias, malformed shapes, empty/nonempty overrides, and graph capture.
Motivation and Context
Optional projections, such as disabled LoRA adapters, should not require full-size zero weight tensors. Zero-sized defaults let the same raw
MatMulNBitsgraph run on CPU and WebGPU, with nonempty weights supplied when needed.Split from #33370. That PR retains these changes until this PR merges; QLoRA-specific operators, fusion and shaders remain there.
Validation
Built the standalone branch against main with built-in WebGPU on Windows x64. All 16 empty-weight tests, 28 existing QKV/MLP fusion regressions, and 8 existing nonempty-weight regressions passed; 4 Arm64-specific tests were skipped. Coverage includes fused empty/nonempty overrides, MLP bias, residual outputs, and capture replay. FP16 WebGPU cases require confirmed ShaderF16 support. Lint and
git diff --checkpassed.