Skip to content

Add more kernels for Qwen-3.5 ops - #32106

Merged
kunal-vaishnavi merged 20 commits into
mainfrom
copilot/add-cuda-ops-for-qwen-3-5
Aug 19, 2026
Merged

Add more kernels for Qwen-3.5 ops#32106
kunal-vaishnavi merged 20 commits into
mainfrom
copilot/add-cuda-ops-for-qwen-3-5

Conversation

Copilot AI commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Description

Adds Qwen-3.5-related contrib kernel coverage across CPU, WebGPU, and CUDA. The PR introduces CPU and WebGPU implementations for GatedAdd, LinearAttentionGate, and GatedRMSNorm, and enables CUDA BFloat16 registrations for CausalConvWithState, LinearAttention, and LpNormalization.

Changes

  • CPU: Added GatedAdd, LinearAttentionGate, and GatedRMSNorm contrib kernels and registered them in the CPU contrib kernel table.
  • WebGPU: Added GatedAdd, LinearAttentionGate, and GatedRMSNorm contrib kernels, registered them in the WebGPU contrib kernel table, and fixed LinearAttentionGate shader binding order.
  • CUDA: Added BFloat16 kernel registrations for CausalConvWithState and LinearAttention, plus LpNormalization BF16 support starting at opset 22.
  • Tests: Expanded contrib op coverage for GatedAdd, LinearAttentionGate, GatedRMSNorm, CausalConvWithState, and LinearAttention, and added a CUDA capability-gated BF16 LpNormalization test.

Motivation and Context

These changes add kernel and type coverage needed by Qwen-3.5-style model patterns across supported execution providers, including BF16 CUDA paths for the affected ops.

Copilot AI and others added 2 commits August 15, 2026 04:27
…nGate

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread onnxruntime/contrib_ops/cpu/bert/gated_add.cc Fixed
Comment thread onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.cc Fixed
Comment thread onnxruntime/contrib_ops/webgpu/bert/linear_attention_gates.cc Fixed

@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/cpu/bert/gated_add.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/gated_add.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/linear_attention_gates.cc Outdated
Copilot AI and others added 4 commits August 15, 2026 04:44
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Copilot AI and others added 2 commits August 15, 2026 04:51
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
@kunal-vaishnavi
kunal-vaishnavi marked this pull request as ready for review August 15, 2026 05:01
Copilot AI balanced review requested due to automatic review settings August 15, 2026 05:01

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 CPU and WebGPU execution support for Qwen-3.5 gated attention operators.

Changes:

  • Implements GatedAdd, GatedRMSNorm, and LinearAttentionGate kernels.
  • Registers the new CPU and WebGPU kernels.
  • Extends shared tests across CPU, CUDA, and WebGPU.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
onnxruntime/test/contrib_ops/linear_attention_gates_op_test.cc Runs gate and RMSNorm tests across supported EPs.
onnxruntime/test/contrib_ops/gated_add_op_test.cc Extends GatedAdd tests to CPU and WebGPU.
onnxruntime/contrib_ops/webgpu/webgpu_contrib_kernels.cc Registers WebGPU kernels.
onnxruntime/contrib_ops/webgpu/bert/linear_attention_gates.h Declares WebGPU gate programs and kernels.
onnxruntime/contrib_ops/webgpu/bert/linear_attention_gates.cc Implements WebGPU gate and RMSNorm shaders.
onnxruntime/contrib_ops/webgpu/bert/gated_add.h Declares the WebGPU GatedAdd kernel.
onnxruntime/contrib_ops/webgpu/bert/gated_add.cc Implements the WebGPU GatedAdd shader.
onnxruntime/contrib_ops/cpu/cpu_contrib_kernels.cc Registers CPU kernels.
onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.h Declares CPU gate kernels.
onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.cc Implements CPU gate and RMSNorm kernels.
onnxruntime/contrib_ops/cpu/bert/gated_add.h Declares the CPU GatedAdd kernel.
onnxruntime/contrib_ops/cpu/bert/gated_add.cc Implements the CPU GatedAdd kernel.
Suppressed comments (1)

onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.cc:157

  • Narrowing num_rows to int32_t is unsafe because TryBatchParallelFor supports std::ptrdiff_t. With more than INT32_MAX normalization groups, the cast can produce a non-positive total, so no work runs and the uninitialized output is returned as successful. Preserve the wider count.
      context->GetOperatorThreadPool(), static_cast<int32_t>(num_rows),

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread onnxruntime/contrib_ops/cpu/bert/gated_add.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/linear_attention_gates.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/gated_add.cc
Copilot AI and others added 3 commits August 15, 2026 05:11
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>

@tianleiwu Tianlei Wu (tianleiwu) 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.

Two deterministic, PR-caused CI failures remain: the Float16 GatedAdd test overrides the repository's reduced-precision tolerance, and the generated CPU kernel documentation is stale. Details are inline. The implementation review otherwise found the operator validation, QMoE checked arithmetic and tiling, CUDA registrations, and targeted coverage well structured.

Comment thread onnxruntime/test/contrib_ops/gated_add_op_test.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/cpu_contrib_kernels.cc
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Comment thread onnxruntime/contrib_ops/webgpu/bert/linear_attention_gates.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/linear_attention_gates.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/gated_add.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/gated_add.cc Outdated
…ache hints, unify GatedAdd path

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>

@tianleiwu Tianlei Wu (tianleiwu) 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

@kunal-vaishnavi
kunal-vaishnavi merged commit 0c32e39 into main Aug 19, 2026
119 of 126 checks passed
@kunal-vaishnavi
kunal-vaishnavi deleted the copilot/add-cuda-ops-for-qwen-3-5 branch August 19, 2026 01:50
Tianlei Wu (tianleiwu) added a commit to microsoft/onnxruntime-genai that referenced this pull request Aug 21, 2026
## Description

Fuse Qwen3.6 MoE shared-expert scaling and routed/shared addition into
the `com.microsoft::GatedAdd` contrib operator.

Qwen3.6 shared-expert Mul+Add is replaced by com.microsoft::GatedAdd for
CPU, CUDA, and WebGPU. Unsupported execution providers retain portable
ONNX `Mul` + `Add`. No `fuse_shared_expert_gate` option remains.

This PR is stacked on #2353, which is itself stacked on #2351.
Dependencies are microsoft/onnxruntime#31835 for CUDA and merged
microsoft/onnxruntime#32106 for CPU/WebGPU.

## Changes

- Return the shared-expert projection and scalar gate separately from
`make_shared_expert`.
- Emit one `GatedAdd` per MoE layer on CPU, CUDA, and WebGPU.
- Preserve the standard `Mul` + `Add` graph as an explicit fallback for
unsupported EPs.
- Add focused tests for fused and fallback graph construction.

## Performance

On Qwen3.6-35B-A3B-NVFP4 with N=3 MTP, the real exported graph replaces
40 main-model pairs plus one MTP pair. Counterbalanced H200 measurements
reduced median decode latency from 7.311 to 7.225 ms/round (-1.18%).
Graph-off Nsight measured 40.35 fewer launches/round and 1.30% lower GPU
kernel time.

## Validation

- `50 passed`: `test_precision.py` plus `test_qwen_gated_add.py`.
- Real graph census: 40 `GatedAdd` nodes in `text.onnx`, one in
`mtp.onnx`.
- After normalizing fused edge names, all other nodes, initializers,
graph inputs, and graph outputs are unchanged from the baseline export.
- Runtime float, FP16, and BF16 results are bit-exact with separate
`Mul` + `Add`.

## Stack note

The source commit `5c35bb02fc` also contained an unrelated MTP prefill
chunk default change in `src/mtp_generator.{h,cpp}`. Those files are
intentionally excluded here because they depend on runtime PR #2352
rather than builder PR #2353.
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.

6 participants