-
Notifications
You must be signed in to change notification settings - Fork 191
feat: Implement Qwen NPU Decoding Support with Memory Management Fixes #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
oreomaker
merged 12 commits into
UbiquitousLearning:v2
from
jialilve:feature/qwen-npu-decoding
Nov 20, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1270b5f
backup WIP qwen-npu-decoding changes
jialilve 1d5d253
feat: implement Qwen NPU simple single chunk decoding support
jialilve b438b3d
implement Qwen NPU simple muti-chunk decoding support
jialilve d773b69
Merge remote-tracking branch 'upstream/v2' into feature/qwen-npu-deco…
jialilve e26b11b
fix: stabilize QNN multi-chunk decoding
jialilve 079a01e
update examples/qwen_npu/main.cpp
jialilve e777dbe
Merge remote-tracking branch 'upstream/v2' into feature/qwen-npu-deco…
jialilve cdb7dfa
Add code comments to QNN backend
jialilve 0524b6e
Merge remote-tracking branch 'upstream/v2' into feature/qwen-npu-deco…
jialilve 834db98
chore: update kleidiai submodule to v1.12.0
jialilve e5ac2d4
Merge remote-tracking branch 'upstream/v2' into feature/qwen-npu-deco…
jialilve 2f6077b
chore: bump kleidiai submodule to 84796ec
jialilve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
KV cache seq count is likely wrong in the decode loop (comment already says
[Maybe Wrong])In the per-chunk decode loop you recompute the absolute sequence length:
Using
chunk_starthere ignores already-consumed tokens in the current chunk and appears inconsistent with the comment and with the new KV cache APIs. This can misalign the KV cache during multi-step decode, especially after the first generated token within the last chunk.A more consistent approach would be to base the sequence count on the absolute number of valid tokens processed so far, e.g.:
or, if
setKVCacheSeqCntis defined to take “index of next write” rather than “current valid length”, adjust accordingly (e.g.absolute_seq_len - 1).Please double-check this against
nn::KVCache::setCurrentSeqCntsemantics; the current implementation is very likely incorrect for multi-step decode.Length of output: 2302
🏁 Script executed:
Length of output: 2654
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 0
Length of output: 3484
🏁 Script executed:
Length of output: 27
🏁 Script executed:
Length of output: 128
🏁 Script executed:
Length of output: 0
Length of output: 326
🏁 Script executed:
Length of output: 0
Length of output: 31
🏁 Script executed:
Length of output: 2657
Length of output: 3312
🏁 Script executed:
Length of output: 943
KV cache seq count must track absolute sequence length, not just chunk offset
The bug is confirmed. In the decode loop at line 176, you reset the KV cache seq count to
chunk_starton every iteration, butcurrent_chunk_lenincrements. This causes the cache to lose track of tokens decoded within the chunk:current_chunk_len = chunk_prompt_len + 1, but cache set tochunk_start→ off bychunk_prompt_lentokenscurrent_chunk_len = chunk_prompt_len + 2, but cache stillchunk_start→ off bychunk_prompt_len + 1tokensThe computed
absolute_seq_lenvariable (line 163) is never used, indicating incomplete implementation. The fix is to use it:Additionally, the prefill at line 97 should similarly use
chunk_start + chunk_prompt_len(the absolute valid length after prefill) instead of justchunk_start.📝 Committable suggestion
🤖 Prompt for AI Agents