Optimize Deepseek V4 prepare decode#728
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to speed up Deepseek V4 decode metadata preparation by reducing small GPU kernel launch overhead and overlapping host→device copies with CPU-side plan building.
Changes:
- Add a NumPy implementation of the sliding-window
window_topkbuilder to avoid multiple small GPU ops for small decode batches. - Overlap decode-time H2D staging on a dedicated CUDA stream (
prep_stream) with CPU work (_build_compress_plans). - Vectorize part of the CPU-side HCA paged-offset construction and adjust
_attach_v4_per_fwd_metato consume CPUpositions_np.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+913
to
+919
| prep_stream.wait_stream(current_stream) | ||
| with torch.cuda.stream(prep_stream): | ||
| positions = var["positions"].copy_to_gpu(sum_scheduled_tokens) | ||
| cu_seqlens_q_gpu = var["cu_seqlens_q"].copy_to_gpu(scheduled_bs + 1) | ||
| context_lens_gpu = var["context_lens"].copy_to_gpu(scheduled_bs) | ||
| block_tables_gpu = var["block_tables"].copy_to_gpu(scheduled_bs) | ||
| state_slot_gpu = ss_buf.copy_to_gpu(scheduled_bs) |
valarLip
approved these changes
May 9, 2026
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.
Motivation
1. Stream-overlapped H2D transfers
Adopts the same
prep_streampattern used byAiterMLAMetadataBuilder: fires 5 H2D copies onmodel_runner.async_execute_stream2. Replace GPU window_topk with CPU numpy (
_build_window_topk_np)The existing
_build_window_topk_batcheduses ~15 PyTorch elementwise GPU ops (arange, where, clamp, mod, etc.) on small decode tensors (e.g. [128, 128]). Each op launches a separate GPU kernel3. Vectorize HCA paged-offset for-loop
Replaces the per-token Python for-loop in
_attach_v4_paged_decode_meta(O(T) iterations with slice writes) with vectorized numpy usingnp.repeat/np.cumsum/ fancy indexing — eliminates 256loop iterations for bs=128 MTP-1.
before:

after:

Technical Details
Test Plan
Test Result
Submission Checklist