feat(ascend): add prefix-shared attention Ascend C kernel - #340
Open
zhangj1an wants to merge 1 commit into
Open
Conversation
zhangj1an
requested review from
Flink-ddd,
KJLdefeated,
bitborne and
inaniloquentee
as code owners
August 25, 2026 09:45
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
zhangj1an
force-pushed
the
feat/ascend-prefix-shared-attention
branch
from
September 10, 2026 13:51
c70e632 to
2c5b851
Compare
Rebased onto latest test. setup.py conflict resolved by keeping test's version (CUDA prefix-shared attention already merged separately) plus the recursive **/*.asc glob so subdirectory .asc kernels compile. Signed-off-by: zhangj1an <jianmusings@gmail.com>
zhangj1an
force-pushed
the
feat/ascend-prefix-shared-attention
branch
from
September 10, 2026 14:00
2c5b851 to
5b7c27c
Compare
inaniloquentee
approved these changes
Sep 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Latest Status [25 Aug 2026]
Will fix CI error then mark as ready for review
Summary
Port of the CUDA prefix-shared fused attention (
csrc/cuda/attention/prefix_shared_attention.cu, the GRPO decode workload) to an Ascend NPU version:Forward is implemented as an Ascend C (CANN) kernel,
_C_npu.prefix_shared_attention_ascend:q [bs, G, len_q, 128],k/v [bs, len_kv, 128]— the G generated responses share one prompt-prefix KV sequence, stored once per batch instead of once per group (same surface as the CUDAPrefixSharedAttentionOp: bf16 only, D=128, non-causal, no key-padding mask, forward-only).(bs, g, 64-row query block)is processed end-to-end by one AI-core block, streaming the keys in a fixed 64-key tile order with fp32 online-softmax accumulation (per-row max / sum-exp rescaling per tile — the same flash-style single pass as the CUDA kernel).len_kv— outputs are batch-invariant: bitwise identical regardless of batch size, batch position, or how many blocks were launched (items are strided across blocks).Integration:
prefix_shared_attention(op classattention, reusing its tolerance contract) withpytorch/cuda/ascendcandidates and aGtestPrefixSharedAttentionOpgold that expands the shared K/V over G and reusesNativeAttentionOp.forward_fp32(non-causal, default scale).scripts/check_operator.pylearns--device npu(defensivetorch_npuprobe) so the ascend candidates run in the gtest harness on NPU hosts.setup.py.ascglob changed to recursive**/*.asc, allowing kernels to follow the CUDA-style directory structure undercsrc/ascend/attention/.Build notes (same pattern as PR #320)
Each
.ascsource file can define only onePYBIND11_MODULE, because linking multiple sources with Bisheng causes a duplicatePyInit__C_npuerror. Therefore pybind registrations are consolidated incsrc/ascend/ops_npu.asc(logp + prefix-shared attention), while individual kernel files contain only the kernel and host forward functions.Files
csrc/ascend/attention/prefix_shared_attention_ascend.asccsrc/ascend/ops_npu.asc_C_npupybind registration file (logp + prefix-shared attention). New.csrc/ascend/batch_invariant_logp_ascend.ascPYBIND11_MODULE, which is moved to the aggregated registration file. Kernel logic is unchanged.rl_engine/kernels/ops/ascend/attention/prefix_shared_attn.pyPrefixSharedAttentionAscendOpwrapper, same surface as the CUDAPrefixSharedAttentionOp(op(q, k, v) -> out) with bf16/D=128 validation. New.rl_engine/kernels/ops/ascend/attention/__init__.pyrl_engine/kernels/gtest/operator_specs.pyprefix_shared_attentionspec (pytorch/cuda/ascend candidates) and theGtestPrefixSharedAttentionOpgold.rl_engine/kernels/gtest/operator_inputs.pyscripts/check_operator.py--device npusupport (defensivetorch_npuprobe).tests/test_prefix_shared_attention_ascend.pytests/test_ws1_gtest_gpu.pyprefix_shared_attentionto the registered-ops coverage set.setup.py.ascglob to**/*.asc.Test
Build first (same as PR #320):
gtest
pytest
Test results
Environment: 8× Ascend 910, CANN 8.5.1 (Bisheng), torch 2.7.1 + torch_npu 2.7.1.
suite=prefix_shared_attention passed=True pass_rate=1.0000suite=prefix_shared_attention passed=True pass_rate=1.0000suite=batch_invariant_logp passed=True pass_rate=1.0000tests/test_prefix_shared_attention_ascend.pytests/test_batch_invariant_logp.py(regression)gtest: prefix_shared_attention ascend bf16 (raw)
gtest: prefix_shared_attention pytorch bf16 (raw)
gtest: batch_invariant_logp ascend bf16 regression (raw)
pytest: tests/test_prefix_shared_attention_ascend.py (raw)
pytest: tests/test_batch_invariant_logp.py regression (summary)
(all
TestAscend*classes passed; skips are CUDA/Triton-only tests on this NPU host)Notes
PrefixSharedAttentionOp.PrefixSharedAttentionOpis consumed directly (e.g.benchmarks/benchmark_attention.py) and is not registered either; the Ascend op follows the same surface.ruff checkpasses for all modified Python files.