Skip to content

Add direct CANN FAI decode attention for Qwen3#755

Open
ChaoWao wants to merge 1 commit into
hw-native-sys:mainfrom
openpto:codex/qwen3-decode-cce-attention
Open

Add direct CANN FAI decode attention for Qwen3#755
ChaoWao wants to merge 1 commit into
hw-native-sys:mainfrom
openpto:codex/qwen3-decode-cce-attention

Conversation

@ChaoWao

@ChaoWao ChaoWao commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Replace Qwen3-14B decode attention with a direct mixed CCEC kernel derived from CANN FusedInferAttentionScore and invoked through pl.jit.extern.
  • Preserve the serving ABI and vLLM physical layouts: active TND query/output and paged BSND KV cache. KV reshaping is a zero-copy view inside the compiled graph; no cache layout conversion is materialized.
  • Select the 19-core flash-decode path for long B1 attention and the 24-core regular path for true B16 and short B1 through runtime tiling.
  • Support runtime execution on A2/A3 for active batches 1 and 16 only. A2/A3 simulation is compile-only; B2-B15 and A5 are explicitly unsupported.
  • Declare cache layout, supported batches, and runtime/compile platforms in the Qwen3 contract.
  • Adapt prefill cache writes to BSND while preserving the four manual scopes from Fix: restore Qwen3 prefill ring isolation #753.
  • Vendor and attribute the required CANN kernel subset. The external mixed-kernel and include_dirs support are available in merged PyPTO #2009.
  • Keep the HOST tuple return compatible with current PyPTO specialization and run the B16/4096-token serving warmup with the 2 GiB ring capacity validated by Fix: restore Qwen3 prefill ring isolation #753.

Testing

  • python -m pytest -q tests/golden tests/contract: 189 passed.
  • decode_fwd.py -p a2a3sim --smoke: all 37 generated functions compiled.
  • Current PyPTO b2f347f compiled the complete serving HOST decode wrapper to DistributedCompiledProgram.
  • A2/A3 hardware correctness passed at sequence length 3338 for B1 and true B16 with independent paged KV ranges.
  • Hardware CI-shape accuracy with 2 GiB heap and 16K task/dep pools passed under task-submit task task_20260713_013155_194847010745.
  • Full serving hardware golden passed all 8 expected tokens through pypto-serving Fix: correct RMSNorm to use rsqrt and keep projections in FP32 #69.
  • True B16 serving with 16 distinct prompts matched 16 independent B1 references.
  • A 3338-token, 40-layer B1 serving run completed 19 decode steps without device deadlock. Decode TPOT was 31.2 ms, kernel wall average was 30.82 ms, and device Effective was 28.53 ms.
  • L2 timing at the same B1 shape measured a 42.39 us median slowest-AIV span inside the mixed launch and a 166.12 us median scheduler-visible attention stage from tiler dispatch through mixed completion. These are intentionally reported as different timing boundaries.

Compatibility

Merge order

  1. Squash-merge this PR first. Its required checks and all kernel checks pass.
  2. Update pypto-serving Fix: correct RMSNorm to use rsqrt and keep projections in FP32 #69's submodule to the resulting squash SHA.
  3. Rerun and merge pypto-serving Fix: correct RMSNorm to use rsqrt and keep projections in FP32 #69.

The non-required serving check currently fails before NPU execution because it intentionally tests pypto-serving main, whose old HOST tuple wrapper is incompatible with current PyPTO specialization. The companion fix and full hardware validation are in pypto-serving #69.

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@ChaoWao, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 15 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 75d3d7a4-bb8d-4ba3-8dd9-39cbae9d2020

📥 Commits

Reviewing files that changed from the base of the PR and between b918a0e and 10d43e5.

⛔ Files ignored due to path filters (1)
  • models/qwen3/14b/kernels/paged_attention_cce/generated/kernel_tiling/kernel_tiling.h is excluded by !**/generated/**
📒 Files selected for processing (72)
  • .github/workflows/ci.yml
  • LICENSE
  • NOTICE
  • models/qwen3/14b/contract.py
  • models/qwen3/14b/decode_fwd.py
  • models/qwen3/14b/kernels/paged_attention_cce/attention/entry.cpp
  • models/qwen3/14b/kernels/paged_attention_cce/kernel/fai_body.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/kernel/metadata_layout.h
  • models/qwen3/14b/kernels/paged_attention_cce/tiling/entry.cpp
  • models/qwen3/14b/kernels/paged_attention_cce/tiling/qwen_fai_runtime_tiler.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/arch/arch.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/arch/cross_core_sync.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/arch/local_tensor_buffer.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/arch/resource.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/base_defs.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/coord.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/detail/alignment.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/detail/dependent_false.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/detail/macros.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/block/CombineScale.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/block/block_epilogue.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/block/block_epilogue_init_outputs.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/block/block_epilogue_online_softmax.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/block/block_epilogue_online_softmax_low_prec.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/block/block_epilogue_rescale_o.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/block/block_epilogue_rescale_o_low_prec.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/dispatch_policy.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/copy_gm_to_ub.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/copy_ub_to_gm.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_broadcast_inplace_by_column.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_broadcast_inplace_by_row.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_broadcast_mul.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_broadcast_one_blk.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_cast.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_copy.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_elemwise_add.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_elemwise_mul.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_elemwise_muls.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/epilogue/tile_common/tile_swizzle.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/block/block_mmad.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/block/block_mmad_pv.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/block/block_mmad_pv_decode.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/block/block_mmad_qk.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/block/block_mmad_qk_decode.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/dispatch_policy.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/gemm_type.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/helper.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/copy_gm_to_l1.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/copy_gm_to_ub.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/copy_l0c_to_gm.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/copy_l1_to_bt.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/copy_l1_to_l0a.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/copy_l1_to_l0b.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/copy_ub_to_gm.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/tile_copy.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/tile_copy_tla.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm/tile_common/tile_mmad.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/gemm_coord.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/layout/layout.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/layout/matrix.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/layout/vector.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/attn_infra/matrix_coord.hpp
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/flash_attention_regular.h
  • models/qwen3/14b/kernels/paged_attention_cce/vendor/fused_infer_attention_score/kernel_common.hpp
  • models/qwen3/14b/paged_attention_cce.py
  • models/qwen3/14b/prefill_fwd.py
  • models/qwen3/14b/test_paged_attention_cce.py
  • tests/contract/test_qwen3_14b_contract.py
  • tests/golden/test_qwen3_decode_cce_source.py
  • tests/golden/test_qwen3_greedy_source.py
  • tests/golden/test_qwen3_prefill_source.py
  • tests/golden/test_qwen_fai_runtime_tiler.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@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 restructures the Qwen3-14B decode layer to minimize critical-path latency. Key optimizations include consolidating projection tasks to SPMD, merging zero-fill seeds, implementing early dispatch and resolution flags, and splitting the out_proj tasks. Additionally, the custom flash-decoding attention implementation is replaced with a direct external CCE paged attention kernel (paged_attention_cce) matching vLLM's physical BSND cache layout. The review feedback highlights a critical precision requirement on Ascend hardware: when casting from FP32 to BF16 via pl.cast, the mode="rint" parameter should be explicitly specified to ensure standard round-to-nearest-even behavior and avoid potential precision regressions in the cache and query assemblies.

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 thread models/qwen3/14b/decode_layer.py Outdated
Comment thread models/qwen3/14b/decode_layer.py Outdated
Comment thread models/qwen3/14b/decode_layer.py Outdated
@ChaoWao
ChaoWao force-pushed the codex/qwen3-decode-cce-attention branch 2 times, most recently from 7a7122d to b7c2d5b Compare July 12, 2026 06:40
@ChaoWao

ChaoWao commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed the numerical review feedback in b7c2d5b: the new Q/K/V FP32-to-BF16 conversions now use explicit mode="rint". The same update also restores Huawei attribution and adds the CANN OSLA v2 license and provenance notice.

@ChaoWao

ChaoWao commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator Author

Latest CI triage (run 29183004763): serving-qwen3-14b, sim (a2a3sim), and sim (a5sim) all fail at import time with the same error:

TypeError: _ExternKernelDecorator.__call__() got an unexpected keyword argument 'dual_aiv_dispatch'

Those jobs install PyPTO from current main, which does not yet contain the stacked API from hw-native-sys/pypto#2009. No CCEC kernel or numerical check runs before the failure. Please rerun these jobs after #2009 merges; the PR remains Draft until that dependency is available.

@ChaoWao
ChaoWao force-pushed the codex/qwen3-decode-cce-attention branch from b7c2d5b to a3dea66 Compare July 13, 2026 01:42
@ChaoWao ChaoWao changed the title Add Qwen3 decode attention with external CCEC Use CANN FAI for Qwen3 decode attention Jul 13, 2026
@ChaoWao
ChaoWao force-pushed the codex/qwen3-decode-cce-attention branch from a3dea66 to 1435da5 Compare July 13, 2026 02:03
@ChaoWao
ChaoWao force-pushed the codex/qwen3-decode-cce-attention branch 2 times, most recently from 1fb03e2 to ee40267 Compare July 13, 2026 06:56
@ChaoWao ChaoWao changed the title Use CANN FAI for Qwen3 decode attention Add direct CANN FAI decode attention for Qwen3 Jul 13, 2026
@ChaoWao
ChaoWao force-pushed the codex/qwen3-decode-cce-attention branch 2 times, most recently from 216e9fa to ed4377c Compare July 13, 2026 08:35
@ChaoWao
ChaoWao marked this pull request as ready for review July 13, 2026 09:01
- Call CANN FusedInferAttentionScore through pl.jit.extern while
  preserving the fused QKV, QK-norm, RoPE, projection, MLP, and
  FP32 LM-head paths.
- Keep active-TND query/output and paged-BSND KV storage without a
  materialized cache layout conversion.
- Specialize attention for B1 and true B16, and fingerprint the BSND,
  batch, and A2/A3 platform contract.
- Vendor the required CANN kernel subset with runtime tiling and retain
  the four-scope prefill capacity boundary.
- Keep host tuple returns compatible with current PyPTO specialization
  and allocate 2 GiB for the Qwen serving CI warmup.
@ChaoWao
ChaoWao force-pushed the codex/qwen3-decode-cce-attention branch from ed4377c to 10d43e5 Compare July 13, 2026 09:18
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.

1 participant