Feat/shadownpu sparse attention - #703
Open
yinwangsong wants to merge 1 commit into
Open
Conversation
Implement an HMX-backed sparse prefill attention path for Qwen NPU models, including deterministic head scheduling, INT8 Top-K selection, sparse CPU value computation, and pipelined CPU/NPU execution. Add QNN integration and model-specific compatibility handling for Qwen2.5 and Qwen1.5, including activation scaling and final-token CPU refinement. Provide host correctness tests, profiling and calibration tools, HMX operator build utilities, Android benchmark runners, head profiles, and end-to-end reproduction documentation. Signed-off-by: yws <48912667+yinwangsong@users.noreply.github.com>
yinwangsong
requested review from
chenghuaWang,
liang1232018,
oreomaker,
xumengwei and
yirongjie
as code owners
August 24, 2026 04:55
Contributor
|
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: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
yinwangsong
marked this pull request as draft
August 24, 2026 04:56
yinwangsong
marked this pull request as ready for review
August 24, 2026 04:58
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.
This is an implementation of MobiSys 2026 paper ShadowNPU: System and Algorithm Co-design for NPU-Centric
On-Device LLM Inference, which is coauthored by PKU and BUPT team.
Summary
This PR adds a reproducible ShadowNPU-style sparse prefill attention path for
Qwen models on Qualcomm Hexagon NPU devices. It combines static HMX INT8 QK,
CPU INT8 Top-K selection, sparse QK/softmax/PV, and pipelined CPU/NPU
execution while keeping decode attention dense.
The PR packages one-command reproduction for Qwen2.5-1.5B and provides the
source compatibility paths required by Qwen1.5-1.8B and Qwen1.5-0.5B.
Motivation
Dense prefill attention becomes increasingly expensive as the prompt length
grows. This change introduces a static, model-calibrated sparse attention path
that reduces prefill attention work while preserving strict retrieval quality
for the supported Qwen2.5-1.5B and Qwen1.5-1.8B configurations.
What changed
Runtime and scheduling
and sparse attention workers.
QNN and model compatibility
Tooling and reproducibility
strict retrieval checks, and median aggregation.
in
docs/shadownpu_sparse_attention.md.Supported configurations
Device validation
houji, SM8650)7e16701819ed3540c1d6aa35a8bec0c731cb08de3b384c34525e68c423e336af50401df239101dbf7d739aad6cec09366c09fdd93afe71dbf766ce04a8ee27d0292db9d70fe0dd4fe224124d8a24fa41d492a16cc8f649bd5aa3ffbbThe measurement commit and the canonical PR commit have an identical Git
tree. The original measurement commit is retained above for provenance
because the branch was squashed after the device runs.
Each formal log contains exactly one
BENCH_RESULT,QUALITY_RESULT, andQWEN_ATTN_PROFILE, observes the FastRPC redirect, and contains no QNN orFastRPC error marker. Attention mode, chunking, actual retention, CPU
placement, model compatibility settings, and process exit status were also
validated.
Performance
TTFT values are medians of three runs. Speedup is dense TTFT divided by sparse
TTFT within the same row.
The Qwen1.5-0.5B 2048- and 4096-token rows are retained as diagnostic timing
only because strict retrieval fails in both dense and sparse modes. Absolute
latency is temperature-sensitive. Historical preserved rows in the document
also include legacy fixed-output banks, whereas the current reproduction uses
the recommended target-requant banks.
Reproduction
Build the Android/QNN benchmark and follow
docs/shadownpu_sparse_attention.mdfor artifact provisioning. The packagedQwen2.5 A/B runner is:
Test plan
benchmark_qwen_npuwith the Android/QNN configuration.MLLM_TESTsuite on the squashed commit.Known limitations
profile, and operator-bank artifacts.
benchmarks; those checks require a manually triggered self-hosted runner or
maintainer device reproduction.