Skip to content

feat(archon): add NPU support for varlen attention - #1686

Open
262913 wants to merge 1 commit into
areal-project:ascend-v1.0.5from
262913:feat/archon-varlen-npu
Open

feat(archon): add NPU support for varlen attention#1686
262913 wants to merge 1 commit into
areal-project:ascend-v1.0.5from
262913:feat/archon-varlen-npu

Conversation

@262913

@262913 262913 commented Sep 8, 2026

Copy link
Copy Markdown

Description

Enable archon's varlen attention to run on Ascend NPU via torch_npu, complementing the existing CUDA Flash Attention backend within the archon module.

Key changes:

  • add device dispatch in _varlen_attn: CUDA uses Flash Attention, NPU uses npu_fusion_attention
  • implement _varlen_attn_npu and _varlen_attn_backward_npu with torch_npu APIs, preserving the same interface as CUDA
  • add _make_causal_mask_npu to build explicit bool causal masks for NPU (sparse_mode=1, allMask)
  • unify return signature across CUDA and NPU backends

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📝 Documentation update
  • ♻️ Refactoring
  • ⚡ Performance improvement
  • ✅ Test coverage improvement

Checklist

  • I have read the Contributing Guide
  • Pre-commit hooks pass (pre-commit run --all-files)
  • Relevant tests pass; new tests added for new functionality
  • Documentation updated (not applicable)
  • Branch is up to date with main
  • Self-reviewed via /review-pr command
  • This PR was created by a coding agent via /create-pr
  • This PR is a breaking change

Additional Context

_varlen_attn uniformly returns four tensors (output, lse_or_max, softmax_sum, aux) to satisfy the fixed return count required by custom_op. The shape of output is always [T_q, H, D]. The differences are: on CUDA, lse_or_max is softmax_lse with shape [H, T_q], and both softmax_sum and aux are placeholder zeros of shape [2]; on NPU, lse_or_max is softmax_max with shape [T_q, H, S], softmax_sum also has shape [T_q, H, S], and aux is [seed, offset] as shape [2] of int64.

Enable varlen attention to run on Ascend NPU via torch_npu,
complementing the existing CUDA Flash Attention backend.

Key changes:
- add device dispatch in _varlen_attn: CUDA uses Flash Attention,
  NPU uses npu_fusion_attention
- implement _varlen_attn_npu and _varlen_attn_backward_npu with
  torch_npu APIs, preserving the same interface as CUDA
- add _make_causal_mask_npu to build explicit bool causal masks for
  NPU (sparse_mode=1, allMask)
- unify return signature across CUDA and NPU backends

Signed-off-by: xixianwu <49093929+262913@users.noreply.github.com>
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