Repository navigation
perf(mtp): Optimize Qwen3.8 Flash Next MTP - #51
sammysun0711 merged 9 commits into
Conversation
Refresh shared-index proposal metadata for all draft steps in one GPU launch and skip unused compressed-key and ring metadata. Fuse draft-extend layout with full metadata preparation and avoid scalar readbacks in eager metadata builders. Keep the PyHIP decode page-table width within its ABI limit. Add coverage for shared metadata, graph replay, padding, bucket changes, paged attention output, and eager readback removal.
Reuse target prompt embeddings for draft prefill and accelerate the MTP input broadcast add. Use ROCm max selection for topk=1 draft proposals without computing full-vocabulary softmax. Use q/k/v views for ROCm GDN target verification when ReplaySSM buffers are absent. This verification optimization also serves other speculative models; it is grouped with the MTP execution-path changes. Split the MTP and speculative execution changes from fe69548 (add c8 opt).
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
3 open issues
What changed in this PR
This PR targets improved MTP performance for Qwen3.8 Flash Next / Qwen QSA paths, focusing on faster draft-extend / decode metadata handling, broader PyHIP utilization, and reduced redundant embedding work.
Changes:
- Expand PyHIP QSA usage (including draft-extend indexer and additional mode gating) and add shared-metadata fast paths for MTP decode.
- Introduce/adjust Triton and graph-metadata kernels to avoid syncs and improve CUDA graph replay behavior.
- Add/extend kernel tests covering shared metadata refresh, graph replay, and eager paths without CPU scalar readbacks.
| File | Description |
|---|---|
| test/registered/kernels/test_qsa_mtp_shared_indexer.py | Updates test setup for MTP sparse-index capture expectations. |
| test/registered/kernels/test_qsa.py | Adds coverage for shared draft metadata refresh + CPU-mirror paths. |
| test/registered/kernels/test_pyhip_qsa.py | Extends PyHIP QSA tests to draft worker + draft-extend indexer eager/graph behavior. |
| python/sglang/srt/speculative/eagle_worker_v2.py | Optimizes topk=1 sampling on ROCm; reuses/reshapes target embeddings for draft extend. |
| python/sglang/srt/models/qwen4_exp_mtp.py | Adds Triton kernel to speed broadcast-add of embeddings into HC streams; adjusts embedding prep logic. |
| python/sglang/srt/models/qwen4_exp.py | Plumbs text-only prefill embeddings into forward_batch for MTP reuse. |
| python/sglang/srt/layers/attention/qwen_sparse_attn_backend.py | Adds shared-decode metadata fast path; refactors graph-mode constants; avoids sync-heavy scalar reads. |
| python/sglang/srt/layers/attention/qsa/pyhip_backend.py | Enables PyHIP indexer path for draft-extend; tightens decode support constraints. |
| python/sglang/srt/layers/attention/qsa/graph_metadata.py | Adds new Triton kernels for combined draft-extend metadata + shared decode metadata refresh. |
| python/sglang/srt/layers/attention/qsa/config.py | Introduces internal graph-mode constants. |
| python/sglang/srt/layers/attention/linear/gdn_backend.py | Avoids fused split path when target_verify reads can consume views directly. |
Review effort: Lite
| "token-slot bound reads them back once per forward" | ||
| ) | ||
| return max(1, int(sequence_lengths.max())) | ||
| return self.req_to_token.shape[1] |
There was a problem hiding this comment.
Why not use return max(1, int(sequence_lengths.max().item())) to avoid large table.
There was a problem hiding this comment.
item() will add a gpu->cpu sync point which will slow down the gpu execution.
| max_length = int(forward_batch.seq_lens_cpu[:batch_size].max()) | ||
| else: | ||
| max_length = int(sequence_lengths.max()) | ||
| max_length = self.req_to_token.shape[1] |
There was a problem hiding this comment.
Why not use max_length = max(1, int(sequence_lengths.max().item())) to avoid large table.
|
Reverting change 7 in 026932d —it got a bit too complex and the gains aren't really clear. I'll consider a separate PR for it down the road if we still need it. |
d0a5b96
into
qwen38-flash-next-amd-current-fix-install
* use pyhip qsa prefill kerenl for draft extend * support pyhip indexer for draft_extend_v2 * support cudagraph for draft extend * perf(qsa): reduce MTP draft replay metadata preparation Refresh shared-index proposal metadata for all draft steps in one GPU launch and skip unused compressed-key and ring metadata. Fuse draft-extend layout with full metadata preparation and avoid scalar readbacks in eager metadata builders. Keep the PyHIP decode page-table width within its ABI limit. Add coverage for shared metadata, graph replay, padding, bucket changes, paged attention output, and eager readback removal. * perf(mtp): optimize draft processing and target verification Reuse target prompt embeddings for draft prefill and accelerate the MTP input broadcast add. Use ROCm max selection for topk=1 draft proposals without computing full-vocabulary softmax. Use q/k/v views for ROCm GDN target verification when ReplaySSM buffers are absent. This verification optimization also serves other speculative models; it is grouped with the MTP execution-path changes. Split the MTP and speculative execution changes from fe69548 (add c8 opt). * replace number with qsa graph mode * update zero mm_input_embeds; update tests * update condition of extend_seq_lens_cpu * revert(qsa): restore full MTP replay metadata preparation (cherry picked from commit d0a5b96)
* perf(mtp): Optimize Qwen3.8 Flash Next MTP (#51) * use pyhip qsa prefill kerenl for draft extend * support pyhip indexer for draft_extend_v2 * support cudagraph for draft extend * perf(qsa): reduce MTP draft replay metadata preparation Refresh shared-index proposal metadata for all draft steps in one GPU launch and skip unused compressed-key and ring metadata. Fuse draft-extend layout with full metadata preparation and avoid scalar readbacks in eager metadata builders. Keep the PyHIP decode page-table width within its ABI limit. Add coverage for shared metadata, graph replay, padding, bucket changes, paged attention output, and eager readback removal. * perf(mtp): optimize draft processing and target verification Reuse target prompt embeddings for draft prefill and accelerate the MTP input broadcast add. Use ROCm max selection for topk=1 draft proposals without computing full-vocabulary softmax. Use q/k/v views for ROCm GDN target verification when ReplaySSM buffers are absent. This verification optimization also serves other speculative models; it is grouped with the MTP execution-path changes. Split the MTP and speculative execution changes from fe69548 (add c8 opt). * replace number with qsa graph mode * update zero mm_input_embeds; update tests * update condition of extend_seq_lens_cpu * revert(qsa): restore full MTP replay metadata preparation (cherry picked from commit d0a5b96) * fix(qsa): adapt MTP backport to qwen4_v0.5.21 Keep the current 1M bounds, capability-based GDN routing, and fused topk1 paths. Reuse the per-request Eagle embedding shift once and initialize each tail before the draft repairs it. Update draft PyHIP dispatch tests for the new contract, migrate the prefill-anchor case to the current QSA test file, and cover embedding alignment and broadcast-add graph replay. --------- Co-authored-by: Xiake Sun <[email protected]>


Motivation
This PR optimizes Qwen3.8 Flash Next MTP perf. Modifications include:
DRAFT_EXTEND_V2, utilize pyhip decode qsa indexerDRAFT_EXTEND_V2's cudagraphDRAFT_EXTEND_V2and proposal decodeCo-authored-by: Cheng Luo [email protected]
Modifications
Simplify and fuse metadata kernels in
DRAFT_EXTEND_V2and proposal decodeIn proposal decode


init_forward_metadata_out_graph, this pr replaces_qsa_graph_layout_kerneland_qsa_graph_row_metadata_kernelwith_qsa_mtp_shared_decode_metadata_kernel.before change, there are 4 kernels.
after change only 1 kernel
In
DRAFT_EXTEND_V2stage, this pr fuses_qsa_graph_layout_kerneland_qsa_graph_row_metadata_kernelinto_qsa_graph_draft_extend_metadata_kernelafter

Accuracy Tests
Speed Tests and Profiling
12K in 1conc
launch server with
before
After
Checklist
Review and Merge Process
/tag-and-rerun-ci,/tag-run-ci-label,/rerun-failed-ci