Skip to content

[CPU/WebGPU] Support empty MatMulNBits weights - #33559

Merged
Jiajia Qin (qjia7) merged 4 commits into
microsoft:mainfrom
qjia7:feat/matmul-nbits-empty-weights
Oct 28, 2026
Merged

Jiajia Qin (qjia7) merged 4 commits into
microsoft:mainfrom
qjia7:feat/matmul-nbits-empty-weights

Conversation

@qjia7

@qjia7 Jiajia Qin (qjia7) commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Support empty packed weights in CPU and WebGPU MatMulNBits. With weight_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 MatMulNBits graph 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 --check passed.

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.

🟡 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

Comment thread onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc Outdated
Comment thread onnxruntime/test/contrib_ops/matmul_nbits_empty_weights_test.cc

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.

🔵 Needs a closer look

Cross-provider prepacking, fused execution, and graph-capture changes warrant final human review of native-platform validation.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@xiaofeihan1 Xiaofei Han (xiaofeihan1) 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.

LGTM. One non-blocking question about model portability below.

Comment thread onnxruntime/core/graph/contrib_ops/contrib_defs.cc
@qjia7
Jiajia Qin (qjia7) merged commit 03a19a7 into microsoft:main Oct 28, 2026
105 of 107 checks passed
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