fix(qwen3-14b): guard negative slot_mapping KV writes - #806
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughQwen3 14B prefill and decode paths now treat negative slot mappings as invalid and skip KV-cache data and scale writes. Reference implementations and the generated RoPE/QKV kernel mirror the guarded behavior while preserving required synchronization. ChangesKV-cache slot validity
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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 |
There was a problem hiding this comment.
Code Review
This pull request introduces a non-negative sentinel guard (slot >= 0) on slot mappings across multiple Qwen3 14B model files, including decode, prefill, and generated kernels, to prevent writing to the KV cache when the slot index is negative. The feedback highlights potential unbound variable issues in qwen3_14b_decode_ssn_draft.py and qwen3_14b_decode_tq_draft.py where block and offset variables are conditionally defined, and suggests initializing them to default values to prevent static analysis or compiler errors.
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.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@models/qwen3/14b/qwen3_14b_decode_ssn_draft.py`:
- Around line 332-334: Remove the conditional guards around the block/offset
calculations in models/qwen3/14b/qwen3_14b_decode_ssn_draft.py#L332-L334 and
models/qwen3/14b/qwen3_14b_decode_tq_draft.py#L1008-L1010. Compute
rp_slot_block/rp_slot_offset and slot_block/slot_offset unconditionally, while
preserving the existing downstream guards around memory stores.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6a48f9e3-3727-4dde-affc-c2edc7fac0b5
📒 Files selected for processing (7)
models/qwen3/14b/decode_layer_a8w8.pymodels/qwen3/14b/kernels/paged_attention_cce/kernel/rope_qkv_generated.hppmodels/qwen3/14b/prefill_fwd.pymodels/qwen3/14b/prefill_fwd_a8w8.pymodels/qwen3/14b/qwen3_14b_decode_ssn_draft.pymodels/qwen3/14b/qwen3_14b_decode_tq_draft.pymodels/qwen3/14b/qwen3_14b_prefill_tq_draft.py
Compute slot block and offset outside the write guard so tracing sees the values consistently. Keep KV cache writes guarded by slot >= 0 for negative slot_mapping sentinels.
Initialize slot block and offset before guarded writes, but compute real cache addresses only for non-negative slot_mapping values. This keeps review comments addressed without evaluating cache address math for the -1 warmup sentinel.
Revert the review-only slot block/offset predefinitions because serving warmup still fails with AICore 507018 when this DSL control flow is changed. Keep the earlier tested shape where address math is evaluated only inside the slot >= 0 guard.
Guard Qwen INT32 slot_mapping writes with the serving -1 sentinel directly instead of relying on signed relational comparisons. Apply the same sentinel check to the generated decode CCE KV write guards.
This reverts commit 0b6763d.
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a0ec4722a9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Update on slot_mapping=-1 / scratch-page repro
I rechecked the isolated prefill contract under the serving runtime environment.
This test only switches the pypto-lib checkout through `PYTHONPATH` / `--repo`.
Versions:
- Upstream control:
- `hw-native-sys/pypto-lib@68889bc7e74a6e249e03758bc26d382859f74268`
- Current PR head:
- `Leaf-Salix/pypto-lib@88b8223eacc5f5ca16ef57bc0e677e67ddf73f45`
- Previous fix point:
- `Leaf-Salix/pypto-lib@65ae4d5681723a67d71df6ce3d693b21b3d9f859`
- Serving/runtime components:
- `hw-native-sys/pypto-serving@db8937717193312680aa079e31c5fba9a8c2555c`
- `hw-native-sys/pypto@2b3f017c3eed1d010bcd7301e1aeda8e2a8b2f65`
- `hw-native-sys/simpler@a756969cfb4b25756792e6fbcb198d08beb6eea0`
Runtime environment:
```text
python: /data/sunkaixuan/yj_subdir/.venv_serving/bin/python
PTOAS_ROOT=/usr/local/bin/ptoas-bin
ASCEND_HOME_PATH=/usr/local/Ascend/cann-9.0.0
PTO_ISA_ROOT=/data/sunkaixuan/yj_subdir/pr806_repro_20260721_1918/pypto/runtime/build/pto-isaTest parameters: Results: Logs: Conclusion: |
Summary
The serving CI for Qwen3-14B can fail during model initialization/warmup with AICore
507018.After isolating the failure, the warmup path in
pypto-servingintentionally fills Qwen prefill/decodeslot_mappingwith-1:slot_mapping=-1-1However, the Qwen3-14B kernels were still deriving physical KV cache rows from that negative slot and could issue invalid K/V cache writes. DeepSeek already treats
slot_mapping=-1as a no-write sentinel and skips cache writes whenslot < 0, so this PR makes Qwen follow the same contract.What Changed
slot >= 0.slot < 0no-write convention to adjacent Qwen A8W8/TQ/SSN draft paths for consistency with the main kernels and DeepSeek.Tests
All tests used the same clean
pypto-servingcheckout and only switched thepypto-libcheckout throughPYPTO_ROOT/PYTHONPATH.slot_mapping=-1, scratch 1 page:68889bc:0/5PASS,5/5AICore50701865ae4d5:5/5PASSslot_mapping=arange, scratch 32 pages:68889bc:3/3PASS65ae4d5:3/3PASStests/test_qwen3_accuracy.py:68889bc:3/5PASS,2/5AICore50701865ae4d5:5/5PASStests/test_qwen3_serving.py:68889bc:1/5PASS,4/5AICore50701865ae4d5:5/5PASSFull-serving run details for fix branch
65ae4d5:4 passed, 5 warnings in 119.13s4 passed, 5 warnings in 118.92s4 passed, 5 warnings in 121.34s4 passed, 5 warnings in 120.75s4 passed, 5 warnings in 118.40sNotes
slot_mapping=-1contract is the strongest attribution evidence: upstream fails deterministically, the fix passes deterministically, and valid positive slots pass on both branches.slot_mapping=-1; Qwen kernels should honor that convention just like DeepSeek.