Refactor: initialize DeepSeek V4 scratch on allocation - #823
Conversation
- Size QKV scratch buffers from the padded runtime token count - Initialize split-K and prefill padding buffers during allocation - Remove explicit seed scopes for zero and sentinel metadata
📝 WalkthroughWalkthroughDeepSeek-V4 kernels replace explicit buffer-seeding scopes and loops with allocation-time initialization, remove the static ChangesDeepSeek-V4 initialization updates
Estimated code review effort: 2 (Simple) | ~10 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.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
models/deepseek/v4-flash/qkv_proj_rope.py (1)
41-41: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winKeep the Q KV scratch allocation capacity compile-time fixed.
t_matmul = pl.max(t_dim, MATMUL_T_TILE)makes the first dimension ofqr_fp32,qr_i8_matmul,q_proj_i32, andkv_fp32depend on the runtime number of tokens. Use a compile-time scratch capacity, such asMATMUL_T_TILE, and add apl.max(t_dim, MATMUL_T_TILE)range check if the dynamic token count cannot exceed it; the matmul kernels can still use the actualt_dimfor valid-shape masking.Also applies to: 154-158, 216-216, 286-290
🤖 Prompt for 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. In `@models/deepseek/v4-flash/qkv_proj_rope.py` at line 41, Keep scratch-buffer capacities in the Q/KV projection paths compile-time fixed by using MATMUL_T_TILE rather than the runtime t_dim-derived value for qr_fp32, qr_i8_matmul, q_proj_i32, and kv_fp32 allocations. Add a pl.max(t_dim, MATMUL_T_TILE) range check where needed to reject unsupported dynamic token counts, while preserving actual t_dim for matmul masking.Source: Learnings
🤖 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.
Outside diff comments:
In `@models/deepseek/v4-flash/qkv_proj_rope.py`:
- Line 41: Keep scratch-buffer capacities in the Q/KV projection paths
compile-time fixed by using MATMUL_T_TILE rather than the runtime t_dim-derived
value for qr_fp32, qr_i8_matmul, q_proj_i32, and kv_fp32 allocations. Add a
pl.max(t_dim, MATMUL_T_TILE) range check where needed to reject unsupported
dynamic token counts, while preserving actual t_dim for matmul masking.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 5412f19d-2f85-4382-ab16-3a166f7c8998
📒 Files selected for processing (4)
models/deepseek/v4-flash/hc_pre.pymodels/deepseek/v4-flash/prefill_attention_swa.pymodels/deepseek/v4-flash/prefill_compressor_ratio128.pymodels/deepseek/v4-flash/qkv_proj_rope.py
#829) ## Summary - #823 moved the zero-seed of the decode split-K accumulators (`qr_fp32`, `kv_fp32` in `qkv_proj_rope`, and the separate-path `mixes_raw` in `hc_pre`) from on-core memset scopes to `create_tensor(init_value=0)`, which pre-fills the buffer on the **AICPU** during orchestration. - The AICPU is also the task scheduler, so the per-dispatch fill serializes on the critical orch/sched path and roughly **doubles the decode `orch` window**; the on-core memset instead overlaps with compute across the cores. - This restores the on-core seed scopes while keeping #823's dynamic `t_matmul` sizing. ## Root cause (bench, a2a3 card 6, hca decode, `PYPTO_BENCH=1`, 100 rounds, median us) | window | pre-#823 | #823 | this PR | |---|---|---|---| | orch_us | 48.6 | 107.9 | 51.3 | | sched_us | 344 | 395 | 344 | | effective_us | 344 | 395 | 345 | `effective = orch ∪ sched`; the AICPU fill inflates `orch` and stalls the scheduler loop (`sched`), so effective regresses ~+51us. `min` barely moves while median/tail widen — the signature of added serial AICPU work, not extra compute.
Summary
pl.create_tensor, removing their explicit seed scopes-1invalid-index sentinel