Skip to content

[NPU] Add causal conv1d#28267

Open
zhaozx-cn wants to merge 14 commits into
sgl-project:mainfrom
zhaozx-cn:add_causal_conv1d
Open

[NPU] Add causal conv1d#28267
zhaozx-cn wants to merge 14 commits into
sgl-project:mainfrom
zhaozx-cn:add_causal_conv1d

Conversation

@zhaozx-cn

@zhaozx-cn zhaozx-cn commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Motivation

apply custom causal_conv1d on npu, sgl-project/sgl-kernel-npu#555 is required

Modifications

apply custom causal_conv1d on npu.

Accuracy Tests

Model Task Score Official
Qwen/Qwen3.5-35B-A3B CEVAL 0.9057 0.902
Qwen/Qwen3.5-397B-A17B CEVAL 0.922 0.93

Speed Tests and Profiling

Model: Qwen/Qwen3.5-35B-A3B

Stage Input / Output Batch Size Metric Before this PR After this PR
Prefill 8k / 1 bs1 TTFT (ms) 531 446
Decode 8k / 2k bs48 TPOT (ms) 37.81 33.49
MTP 8k / 2k bs48 TPOT (ms) 16.30 16.08

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ❌ Run #29193910582
Latest PR Test (Extra): ❌ Run #29193910464

Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request refactors the Ascend GDN backend to use torch.ops.npu.causal_conv1d for both decode and extend forward passes, and introduces a device_print utility in utils.py for debugging on NPU. Key feedback includes caching transposed and contiguous convolution weights on the layer object to avoid performance overhead in hot paths, correcting the side-effect registration in _mark_op_side_effectful by adding ops to torch.fx.node._side_effectful_ops instead of calling has_side_effect, and removing unused imports (dataclass and List).

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +226 to +229
mixed_qkv = torch.ops.npu.causal_conv1d(
mixed_qkv,
layer.conv_weights.transpose(0, 1).contiguous(),
conv_states=conv_states,

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.

high

Calling layer.conv_weights.transpose(0, 1).contiguous() on every forward pass introduces unnecessary overhead. Cache the transposed and contiguous weights on the layer object.

Suggested change
mixed_qkv = torch.ops.npu.causal_conv1d(
mixed_qkv,
layer.conv_weights.transpose(0, 1).contiguous(),
conv_states=conv_states,
if not hasattr(layer, "_conv_weights_npu_cache"):
layer._conv_weights_npu_cache = layer.conv_weights.transpose(0, 1).contiguous()
mixed_qkv = torch.ops.npu.causal_conv1d(
mixed_qkv,
layer._conv_weights_npu_cache,
conv_states=conv_states,

Comment on lines +251 to +254
mixed_qkv = torch.ops.npu.causal_conv1d(
mixed_qkv,
layer.conv_weights,
layer.bias,
activation=layer.activation,
conv_states=conv_states_tmp,
has_initial_state=has_initial_states,
cache_indices=cache_indices,
layer.conv_weights.transpose(0, 1).contiguous(),
conv_states=conv_states_for_prefill,

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.

high

Calling layer.conv_weights.transpose(0, 1).contiguous() on every forward pass introduces unnecessary overhead. Cache the transposed and contiguous weights on the layer object.

Suggested change
mixed_qkv = torch.ops.npu.causal_conv1d(
mixed_qkv,
layer.conv_weights,
layer.bias,
activation=layer.activation,
conv_states=conv_states_tmp,
has_initial_state=has_initial_states,
cache_indices=cache_indices,
layer.conv_weights.transpose(0, 1).contiguous(),
conv_states=conv_states_for_prefill,
if not hasattr(layer, "_conv_weights_npu_cache"):
layer._conv_weights_npu_cache = layer.conv_weights.transpose(0, 1).contiguous()
mixed_qkv = torch.ops.npu.causal_conv1d(
mixed_qkv,
layer._conv_weights_npu_cache,
conv_states=conv_states_for_prefill,

Comment thread python/sglang/srt/hardware_backend/npu/utils.py Outdated
Comment thread python/sglang/srt/hardware_backend/npu/attention/ascend_gdn_backend.py Outdated
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Comment thread python/sglang/srt/hardware_backend/npu/attention/ascend_gdn_backend.py Outdated
Comment thread python/sglang/srt/hardware_backend/npu/attention/ascend_gdn_backend.py Outdated
Comment thread python/sglang/srt/hardware_backend/npu/attention/ascend_gdn_backend.py Outdated
zhaozx-cn and others added 9 commits June 25, 2026 17:42
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Signed-off-by: zhaozx-cn <zhaozx2116@163.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants