Skip to content

webgpu qmoe - #26489

Merged
Guenther Schmuelling (guschmue) merged 16 commits into
mainfrom
gs/webgpu-qmoe
Nov 13, 2025
Merged

webgpu qmoe#26489
Guenther Schmuelling (guschmue) merged 16 commits into
mainfrom
gs/webgpu-qmoe

Conversation

@guschmue

Copy link
Copy Markdown
Contributor

qmoe implementation for webgpu ep

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 implements the QMoE (Quantized Mixture of Experts) operator for the WebGPU execution provider, adding support for 4-bit and 8-bit quantized expert weights. The implementation includes gate computation with top-k selection, hidden state gathering, quantized matrix multiplication using existing MatMulNBits infrastructure, SwiGLU activation, and final output mixing.

Key Changes

  • Added QMoE kernel registration to webgpu_contrib_kernels.cc (MoE kernel itself remains commented out/unimplemented)
  • Implemented QMoE operator with support for 4-bit and 8-bit quantized expert weights
  • Added multiple WGSL shader templates for gate computation, hidden state gathering, zero initialization, SwiGLU activation, and final mixing

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
webgpu_contrib_kernels.cc Registers QMoE kernel while keeping MoE commented out
qmoe.h Defines QMoE class extending MoE base class with quantization parameters
qmoe.cc Implements QMoE compute logic with gate, gather, matmul, and activation steps
moe_base.h Defines base enums for activation types, quantization types, and parallelization modes
moe.h Defines base MoE class with common activation and routing parameters
moe.cc Stub implementation of base MoE class (not yet implemented)
gate.wgsl.template WGSL shader for gate computation using bubble sort for top-k selection
hidden_state_gather.wgsl.template WGSL shader for gathering hidden states assigned to each expert
zero_tensor.wgsl.template WGSL shader for zero-initializing output tensor
swiglu.wgsl.template WGSL shader implementing SwiGLU activation function
final_mix.wgsl.template WGSL shader for mixing expert outputs with router weights

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/contrib_ops/webgpu/moe/swiglu.wgsl.template Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/zero_tensor.wgsl.template Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.h
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.h
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/hidden_state_gather.wgsl.template Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/swiglu.wgsl.template
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc

@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/moe/moe.h Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/moe.h Fixed
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Fixed
Guenther Schmuelling and others added 7 commits November 4, 2025 08:41
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…mplate

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@guschmue
Guenther Schmuelling (guschmue) marked this pull request as ready for review November 4, 2025 16:47
Comment thread onnxruntime/contrib_ops/webgpu/moe/moe_base.h Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/gate.wgsl.template Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/final_mix.wgsl.template
@guschmue

Copy link
Copy Markdown
Contributor Author
  • some fixes around chunking tokens into chunks of max_tokens
  • merge main
  • reflect some changes around zero_points that came in with main

testing looks good on nvidia, apple gpu and intel integrated gpu.

Comment thread onnxruntime/contrib_ops/webgpu/moe/gate.wgsl.template Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/moe/qmoe.cc Outdated
@qjia7

Copy link
Copy Markdown
Contributor

Guenther Schmuelling (@guschmue), two additional points may need your attention in follow-ups aside from the existing TODOs in your PR:

Firstly, please refactor all your shaders using input.getByOffset/output.setByOffset. This approach helps avoid errors such as tensor size exceeding the maxStorageBufferBindingSize.

Secondly, consider optimizing the generation phase when moe_params.num_rows is 1. Your current solution is suitable for the prefill phase where moe_params.num_rows is greater than 1. I think your previous solution #26130 might be more effective for generation.

@guschmue
Guenther Schmuelling (guschmue) merged commit 5955798 into main Nov 13, 2025
116 of 158 checks passed
@guschmue
Guenther Schmuelling (guschmue) deleted the gs/webgpu-qmoe branch November 13, 2025 17:11
Rohanjames1997 pushed a commit to Rohanjames1997/onnxruntime that referenced this pull request Dec 4, 2025
qmoe implementation for webgpu ep

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sumit Kumar (Sumit2318) pushed a commit that referenced this pull request Jan 6, 2026
qmoe implementation for webgpu ep

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@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