Skip to content

webgpu / nbitmm support for bias and weight_index - #26392

Merged
Guenther Schmuelling (guschmue) merged 9 commits into
mainfrom
gs/nbitmm-bias
Nov 2, 2025
Merged

Guenther Schmuelling (guschmue) merged 9 commits into
mainfrom
gs/nbitmm-bias

Conversation

@guschmue

Copy link
Copy Markdown
Contributor

add support for bias and weight_index, move subgroup_matrix_matmul_nbits to template and make program callable from other ops.

…its to template and make program callable from other ops.
@guschmue Guenther Schmuelling (guschmue) added the ep:WebGPU ort-web webgpu provider label Oct 23, 2025

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

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

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

Adds WebGPU support for bias and weight_index parameters to N-bit matrix multiplication operations, enabling features like stacked weights and bias addition in quantized operations.

Key changes:

  • Extended matmul_nbits operations to support optional bias parameter across multiple implementations (DP4A, wide tile, subgroup matrix)
  • Added weight_index uniform variable to enable weight stacking and offset computation in quantized matmul
  • Refactored Apple-specific shader generation to use WGSL templates instead of inline string concatenation

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
matmul_4bits_test.cc Added test case for WebGPU with bias support
subgroup_matrix_matmul_nbits_apple.wgsl.template New template file consolidating Apple shader generation with bias support
subgroup_matrix_matmul_nbits.h Added has_bias and weight_idx parameters to program interface
subgroup_matrix_matmul_nbits.cc Refactored to use template system and added bias/weight_index support
matmul_nbits_zero_pt.wgsl.template Added has_bias parameter declaration
matmul_nbits_wide_tile.wgsl.template Implemented bias addition and weight offset calculations
matmul_nbits.h Added has_bias parameter and exposed ApplyMatMulNBits function
matmul_nbits.cc Removed bias constraint, added ApplyMatMulNBits function with extensive documentation
dp4a_matmul_small_m.wgsl.template Added bias support with offset calculations
dp4a_matmul_nbits.h Added has_bias and weight_index parameters to program interfaces
dp4a_matmul_nbits.cc Integrated bias support across DP4A implementations
dp4a_matmul_common.wgsl.template Added has_bias parameter declaration
dp4a_matmul.wgsl.template Implemented bias addition with vectorized operations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc Outdated
Guenther Schmuelling and others added 2 commits October 23, 2025 09:49
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@guschmue
Guenther Schmuelling (guschmue) marked this pull request as ready for review October 23, 2025 23:50

@qjia7 Jiajia Qin (qjia7) 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.

In current change, it seems that ApplyMatMulNBits calculate a with one weight index of b. For QMoE case, it only compute one expert. I remember you said you need 4 selected experts. So will ApplyMatMulNBits be called 4 times to get the up projection results? Why not directly generate the result by calling ApplyMatMulNBits once?

Comment thread onnxruntime/contrib_ops/webgpu/quantization/dp4a_matmul_common.wgsl.template Outdated
Comment thread onnxruntime/contrib_ops/webgpu/quantization/dp4a_matmul_small_m.wgsl.template Outdated
Comment thread onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc
Comment thread onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc
Comment thread onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc
Comment thread onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/quantization/matmul_nbits.cc Outdated
Comment thread onnxruntime/test/contrib_ops/matmul_4bits_test.cc
@guschmue

Copy link
Copy Markdown
Contributor Author

In current change, it seems that ApplyMatMulNBits calculate a with one weight index of b. For QMoE case, it only compute one expert. I remember you said you need 4 selected experts. So will ApplyMatMulNBits be called 4 times to get the up projection results? Why not directly generate the result by calling ApplyMatMulNBits once?
Earlier on I has a QMoE implementation that does 4 experts in 1 shot but in that case I need to go token by token and the prefill would get pretty costly.
The current implementation (

for (int token_offset = 0; token_offset < moe_params.num_rows; token_offset += max_tokens) {
)
does it different. I looks at all tokens and assigns them to experts, than walks over the experts and runs them 1 by one if there are tokens assigned to them. I think this is more in line with other implementations and should be better for prefill performance.
If you have say 1000 tokens it is most likely we need to run all experts, for generation we'd run 4.
Also helps with memory usage.

@qjia7 Jiajia Qin (qjia7) 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.

The current code changes are clean. No more questions for the nbitmm.

If you have say 1000 tokens it is most likely we need to run all experts, for generation we'd run 4.

Does that mean that for generation, we can do specific optimization to calculate the four experts in one ApplyMatMulNBits?

Comment thread onnxruntime/contrib_ops/webgpu/quantization/dp4a_matmul.wgsl.template Outdated

@qjia7 Jiajia Qin (qjia7) 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.

The code changes in matmulnbits are clean for me. One unclear place is that whether we should treat prefill and generation different for QMoE which may impact the interface of ApplyMatMulNBits. But we can revisit this part again if needed.

@guschmue
Guenther Schmuelling (guschmue) merged commit 423a03f into main Nov 2, 2025
92 checks passed
@guschmue
Guenther Schmuelling (guschmue) deleted the gs/nbitmm-bias branch November 2, 2025 19:27
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.

3 participants