perf(npu): vectorize ring attention LSE extractio - #10002
Open
Ruihan11 wants to merge 1 commit into
Open
Conversation
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.
PR type
PR information
Summary
Vectorize second-half LSE extraction in the Ascend NPU ring-attention backward path.
Motivation
_get_second_half_lsepreviously iterated over every packed sequence and called.item()for each sequence boundary. Whencu_seqlensis an NPU tensor, these per-sequence scalar reads introduce repeated NPU-to-CPU synchronizations.The overhead grows with the number of packed sequences and becomes significant for padding-free batches containing many short sequences.
Changes
repeat_interleaveto build source and destination offsets.index_selectto gather the second-half LSE values.Tests
Added:
float32bfloat16int32andint64cu_seqlensValidation:
Experiment results
Environment:
Helper-level benchmark:
Synthetic 2-rank HCCL ring-attention forward and backward benchmark:
The helper-level improvement is larger than the end-to-end improvement because the complete step also includes native attention kernels, HCCL communication, and other backward operations.
Reproduction
Run the NPU helper benchmark:
SWIFT_RUN_NPU_BENCHMARK=1 \ python -m pytest -q -s \ tests/sequence_parallel/test_zigzag_ring_attn_npu.py \ -k benchmarkRun the distributed NPU E2E test:
SWIFT_RUN_NPU_E2E=1 \ ASCEND_RT_VISIBLE_DEVICES=0,1 \ python -m torch.distributed.run \ --standalone \ --nproc_per_node=2 \ tests/sequence_parallel/test_zigzag_ring_attn_npu_e2e.pyLimitations