Skip to content

Integrate Qwen3.8-Next A3 operator optimizations - #23914

Open
Lethobenthos20 wants to merge 1 commit into
vllm-project:mainfrom
Lethobenthos20:codex/qwen38next-a3-optimized-stack
Open

Lethobenthos20 wants to merge 1 commit into
vllm-project:mainfrom
Lethobenthos20:codex/qwen38next-a3-optimized-stack

Conversation

@Lethobenthos20

@Lethobenthos20 Lethobenthos20 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR rebases the validated Qwen3.8-Next A3 optimization stack onto the latest vLLM Ascend main branch.

Included work

Source PR Scope
#23824 QSA, GDN, and MoE hot-path operator optimizations
#23576 A3 fix for single-row QSA Triton lowering
#23760 QSA decode preparation, compression, and rotary-path optimizations
#23761 HyperConnection NZ decomposition and row-pair combination
Lethobenthos20#17 Fuse QSA and PCP hot paths across A3 and A5
Lethobenthos20#20 Restore official FLA compatibility and cumulative A5 Qwen3.8 fusions

Additional integration fixes:

  • Skip GBSA metadata preparation when uneven PCP decode partitioning leaves a rank with zero local query rows. The attention path already treats that shard as a no-op, while the metadata AICPU kernel rejects empty input.
  • Rename the GDN projection-preparation profiler symbol to fused_gdn_projection_prepare_kernel, distinguishing it from the separate ChunkGDN fused prepare path.

Validation

  • Rebased onto the latest main and resolved the GDN recurrent-fusion conflict.
  • Python compilation, merge-tree validation, and git diff --check passed.
  • A3 PP4 × PCP2 × TP2 service and profiling validation is being refreshed on the rebased stack.

vLLM baseline: c410543d9ffff03f5f6c4a8c0ae1d3ad082c4a15

@github-actions

Copy link
Copy Markdown
Contributor

👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:‌‌

  • A PR should do only one thing, smaller PRs enable faster reviews.
  • Every PR should include unit tests and end-to-end tests ‌to ensure it works and is not broken by other future PRs.
  • Write the commit message by fulfilling the PR description to help reviewer and future developers understand.

If CI fails, you can run linting and testing checks locally according Contributing and Testing.

🤖 How to trigger CI

CI runs automatically when the PR is opened or reopened. Pushing a new commit no longer re-runs the E2E CI (to save NPU runner resources), so use the comment commands below when needed. They are available to the PR author and users with triage permission or above:

  • /run — run the full PR CI gate on the latest commit.
  • /rerun — re-run the failed CI jobs (e.g. after a flaky failure).
  • /e2e — run selected E2E tests instead of the full gate.
  • /nightly / /presmoke / /weekly — run the corresponding large-scale tests (triage permission or above; blocked during the nightly maintenance window).
  • /cancel — cancel a running CI on this PR.

Note

Until the CI gate has run on a commit, the required ci-gate check stays pending and the PR cannot merge — remember to comment /run after pushing.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@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 introduces significant performance optimizations for Qwen models on Ascend NPUs, including in-place GBSA attention outputs, decomposed TextGR projections with prepacked BF16 NZ weights, fused Triton kernels for final combine and RMSNorm, a fused shared-expert gate-and-add kernel, and shared INT8 activation quantization. The review feedback highlights several critical issues that must be addressed, such as a runtime NameError in the QSA indexer, potential out-of-bounds memory accesses in multiple Triton kernels due to un-clamped indices or negative offsets, and opportunities to optimize C++ string allocations and modernise pointer casting in the PyTorch bindings.

Comment on lines +420 to +421
q = upstream_indexer.apply_qsa_rmsnorm(self.q_layernorm, q.reshape(-1, self.index_head_dim)).reshape_as(q)
q = upstream_indexer.apply_qsa_rope(self.rotary_emb, positions, q)

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.

critical

The variable upstream_indexer is used here but it is not defined or imported in this file. This will raise a NameError at runtime. It should be replaced with upstream_qsa, which is imported at the top of the file.

Suggested change
q = upstream_indexer.apply_qsa_rmsnorm(self.q_layernorm, q.reshape(-1, self.index_head_dim)).reshape_as(q)
q = upstream_indexer.apply_qsa_rope(self.rotary_emb, positions, q)
q = upstream_qsa.apply_qsa_rmsnorm(self.q_layernorm, q.reshape(-1, self.index_head_dim)).reshape_as(q)
q = upstream_qsa.apply_qsa_rope(self.rotary_emb, positions, q)

Comment on lines +173 to +177
tl.store(
injection + row * HC_COUNT + column - LOWRANK,
2.0 * sigmoid,
(column >= LOWRANK) & (column < LOWRANK + HC_COUNT),
)

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

In _hc_down_silu_inject_kernel, the pointer offset column - LOWRANK can be negative when column < LOWRANK. Performing pointer arithmetic with negative offsets on a tensor pointer can lead to out-of-bounds memory access or undefined behavior on some platforms, even if the store is masked. Clamping the offset to a minimum of 0 using tl.maximum ensures safe address generation.

Suggested change
tl.store(
injection + row * HC_COUNT + column - LOWRANK,
2.0 * sigmoid,
(column >= LOWRANK) & (column < LOWRANK + HC_COUNT),
)
tl.store(
injection + row * HC_COUNT + tl.maximum(0, column - LOWRANK),
2.0 * sigmoid,
(column >= LOWRANK) & (column < LOWRANK + HC_COUNT),
)

Comment on lines +849 to +850
gate_columns = tl.arange(0, 16)
gates = tl.load(injection_ptr + row * stride_injection_row + gate_columns, gate_columns < HC_COUNT, 0.0)

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

In _hc_combine_final_norm_segmented_kernel, gate_columns is initialized with a block size of 16 (tl.arange(0, 16)). If HC_COUNT is less than 16 (e.g., 4 or 8), loading from injection_ptr with gate_columns can result in out-of-bounds memory access on the right side of the row, even if masked. Clamping the columns to HC_COUNT - 1 prevents illegal memory access.

Suggested change
gate_columns = tl.arange(0, 16)
gates = tl.load(injection_ptr + row * stride_injection_row + gate_columns, gate_columns < HC_COUNT, 0.0)
gate_columns = tl.arange(0, 16)
safe_gate_columns = tl.minimum(gate_columns, HC_COUNT - 1)
gates = tl.load(injection_ptr + row * stride_injection_row + safe_gate_columns, gate_columns < HC_COUNT, 0.0)

Comment on lines +50 to +52
s = tl.load(shared + row[:, None] * shared_stride + col[None, :], mask, 0).to(tl.float32)
r = tl.load(routed + row[:, None] * routed_stride + col[None, :], mask, 0).to(tl.float32)
g = tl.load(gate + row * gate_stride, row < rows, 0).to(tl.float32)

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

In _shared_expert_gate_add_rows_kernel, the row and column indices can exceed rows and width respectively when BLOCK_M or BLOCK_N does not perfectly divide the dimensions. Loading from shared, routed, or gate with out-of-bounds indices can cause memory faults or undefined behavior, even if masked. Clamping the indices to rows - 1 and width - 1 ensures safe memory access.

Suggested change
s = tl.load(shared + row[:, None] * shared_stride + col[None, :], mask, 0).to(tl.float32)
r = tl.load(routed + row[:, None] * routed_stride + col[None, :], mask, 0).to(tl.float32)
g = tl.load(gate + row * gate_stride, row < rows, 0).to(tl.float32)
safe_row = tl.minimum(row, rows - 1)
safe_col = tl.minimum(col, width - 1)
s = tl.load(shared + safe_row[:, None] * shared_stride + safe_col[None, :], mask, 0).to(tl.float32)
r = tl.load(routed + safe_row[:, None] * routed_stride + safe_col[None, :], mask, 0).to(tl.float32)
g = tl.load(gate + safe_row * gate_stride, row < rows, 0).to(tl.float32)

Comment thread csrc/torch_binding.cpp
Comment on lines +2974 to +2975
char *layout_q_ptr = const_cast<char *>(layout_q_str.c_str());
char *layout_kv_ptr = const_cast<char *>(layout_kv_str.c_str());

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

Using const_cast<char *> on std::string::c_str() is verbose and can be avoided. In C++17, std::string::data() has a non-const overload that returns char * directly. This is cleaner and safer.

    char *layout_q_ptr = layout_q_str.data();
    char *layout_kv_ptr = layout_kv_str.data();

Comment thread csrc/torch_binding.cpp
Comment on lines +2959 to +2973
std::vector<int64_t> lse_shape{0};
if (return_softmax_lse) {
const auto layout_q_str = std::string(layout_q);
if (layout_q_str == "TND") {
lse_shape = {q.size(0), q.size(1), 1};
} else if (layout_q_str == "BNSD") {
lse_shape = {q.size(0), q.size(1), q.size(2), 1};
} else {
lse_shape = {q.size(0), q.size(2), q.size(1), 1};
}
}
auto softmax_lse = at::empty(lse_shape, q.options().dtype(at::kFloat));

std::string layout_q_str(layout_q);
std::string layout_kv_str(layout_kv);

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

layout_q_str is constructed twice from layout_q (once inside the if (return_softmax_lse) block and once in the outer block). We can construct it once at the beginning of the function and reuse it to avoid redundant string allocations and constructions.

    std::string layout_q_str(layout_q);
    std::vector<int64_t> lse_shape{0};
    if (return_softmax_lse) {
        if (layout_q_str == "TND") {
            lse_shape = {q.size(0), q.size(1), 1};
        } else if (layout_q_str == "BNSD") {
            lse_shape = {q.size(0), q.size(1), q.size(2), 1};
        } else {
            lse_shape = {q.size(0), q.size(2), q.size(1), 1};
        }
    }
    auto softmax_lse = at::empty(lse_shape, q.options().dtype(at::kFloat));

    std::string layout_kv_str(layout_kv);

@Lethobenthos20
Lethobenthos20 force-pushed the codex/qwen38next-a3-optimized-stack branch 5 times, most recently from 42a4ff6 to 6b8a9fa Compare November 30, 2026 11:24
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

CaimYin3 pushed a commit to CaimYin3/vllm-ascend that referenced this pull request Dec 2, 2026
Rebase the combined stack onto source PR vllm-project#23914 at 4886362 and fork PR vllm-project#17 at e52ce81. Retain the norm-scatter regression cases, interface documentation and release note from the previous integration merge, and remove an unused GDN local restored by the newer base. Pinned Ruff checks and formatting pass; DP4 TP2 real-weight profiling validation follows on r207.

Signed-off-by: yincaiming <y00913110@china.huawei.com>
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Preserve the validated QSA, GDN, MoE, HC, and W4A8 operator paths while resolving the upstream GDN preparation overlap.

Signed-off-by: Lethobenthos20 <167174511+Lethobenthos20@users.noreply.github.com>
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant