Skip to content

[Spec][PD] Enable fused TopK for GLM-5.2 MTP IndexShare - #31477

Merged
Fridge003 merged 8 commits into
mainfrom
codex/feat/pd-decode-fused-topk
Aug 5, 2026
Merged

Fridge003 merged 8 commits into
mainfrom
codex/feat/pd-decode-fused-topk

Conversation

@HanHan009527

@HanHan009527 HanHan009527 commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

GLM-5.2 MTP reuses DSA IndexShare top-k seeds across draft-decode steps to avoid redundant indexer work. The seed-transfer path added in PR #30839 preserves allocator independence by transporting request-relative positions. Because the existing fused TopK consumes allocator-local physical KV slots, this path currently falls back to an unfused page-table transform during draft decode.

This PR optimizes GLM-5.2 MTP IndexShare specifically under PD disaggregation. It converts the transferred seed once at the Decode consumer and then uses the existing fused TopK. The wire representation remains unchanged, while the Decode-local remap is required because the Prefill and Decode allocators are independent.

Modifications

  • Keep GLM-5.2 MTP IndexShare seeds in request-relative position space during transfer.
  • For the supported Decode consumer path (SGLANG_DSA_FUSE_TOPK=1, non-HiSparse, and dcp_size == 1), remap the seed once through the local req_to_token table before it enters the EAGLE draft loop.
  • Fail invalid seed rows closed instead of consuming stale, out-of-range, or unallocated slots.
  • Enable the existing fused DSA TopK after the local remap.
  • Add one focused CPU unit test covering fused-path selection and position-to-local-slot remapping.

Accuracy Tests

The real PD seed was produced on Prefill, transferred to Decode, remapped locally, and consumed by fused TopK.

  • MTP acceptance: 66% -> 66%
  • Decoded tokens per iteration: 2.96 -> 2.96
  • No request errors, OOMs, worker restarts, or long-context CUDA Graph fallback
  • Targeted CPU suite: 19 passed, 17 warnings, 2 subtests passed

A bounded LongBench-v2 short evaluation used the same first 64 sample IDs in both independent distributed runs, with temperature 0, reasoning_effort=none, max_tokens=1024, and concurrency 1. This is a correctness/runtime guard, not a full LongBench-v2 score.

Metric fused off fused on
Accuracy 60.94% (39/64) 62.50% (40/64)
Invalid answers 1 2
Request errors 0 0
Natural / max-token stops 63 / 1 62 / 2
Input tokens, p50 / p90 / max 24584 / 47323 / 76495 24584 / 47323 / 76495

Answer choices agreed on 51/64 samples. Independent distributed runs are not bitwise deterministic, so aggregate accuracy and request/runtime guardrails are used for the regression check rather than exact text equality.

Speed Tests and Profiling

Hardware and topology:

  • 4 nodes, 32 x H20-96G
  • Prefill: 2 nodes, TP16, EAGLE/MTP
  • Decode: 2 nodes, TP16, EAGLE/MTP, Full CUDA Graph
  • HiSparse disabled

Workload:

  • 131072 input tokens + 1536 output tokens
  • Concurrency 1, temperature 0, fixed seed
  • One excluded warmup followed by three independent cache-miss runs per arm
Metric fused off fused on Delta
TPOT 11.60 ms 11.22 ms -3.28%
ITL 34.37 ms 33.31 ms -3.08%
Decode forward 17828.26 ms 17274.41 ms -3.11%
Output throughput 26.23 tok/s 26.52 tok/s +1.11%
MTP acceptance 66% 66% unchanged
Decoded tokens / iteration 2.96 2.96 unchanged

TTFT and PD transfer time were unchanged within noise, as expected for a Decode-local optimization. Target verify, draft extend, and draft decode remained on Full CUDA Graph in all measured runs.

Dataset-derived Decode operator profile

One representative request from the same bounded LongBench-v2 subset was profiled through Router -> Prefill -> Decode: sample index 24, 76,489 dataset-prompt tokens and 76,503 API prompt tokens after chat wrapping. The capture covered 32 active Decode iterations with Full CUDA Graph enabled. Only Decode TP0 is summarized below; all eight rank traces per arm were checked and retained for audit.

Operator (Decode TP0) fused off fused on
transform_index_page_table_prefill_kernel 2528 calls 0 calls
transform_index_page_table_decode_kernel 32 calls 0 calls
Unfused topk_kernel 704 calls 0 calls
Fused topk_small_batch_kernel<true> 0 calls 704 calls
topk_plan 96 calls 96 calls
FP8 paged-MQA logits 704 calls 704 calls

This isolates the intended mechanism: recurring page-table conversion disappears, unfused TopK dispatch changes to the existing fused small-batch path, and indexer scoring/planning work is preserved. Raw profiler durations are intentionally not used for an A/B latency claim because capture perturbation differs between the two CUDA Graph paths; the profiler-off repeated 128K + 1.5K A/B above remains the performance evidence.

Checklist

  • The change is limited to the PD Decode DSA IndexShare path.
  • Formatting and repository pre-commit checks pass.
  • A focused unit test covers the new remap behavior.
  • Accuracy and real-hardware performance results are provided.
  • Dataset-derived Decode profiling confirms the recurring page-table transforms disappear.
  • No user-facing documentation change is required.

CI States

Latest PR Test (Base): 🚫 Run #30874159469
Latest PR Test (Extra): ✅ Run #30874159192

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to remap Parallel Disaggregation (PD) DSA seeds to local slots by utilizing the decode-local page table, ensuring that fused TopK remains functional under disaggregation mode. The changes include a new utility function, logic updates in the speculative disaggregation module, and corresponding unit tests. The reviewer suggests adding type hints for the 'server_args' parameter in the utility functions and refactoring the complex conditional logic in the disaggregation module to improve code readability and maintainability.

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.

Comment on lines +75 to +83
invalid_rows = torch.any(
(dsa_topk_indices < -1)
| (dsa_topk_indices >= batch.seq_lens[:, None])
| (dsa_topk_indices >= table_width)
# Slot 0 is the reserved padding sink; real KV allocations
# start at 1, and untouched req-to-token entries remain 0.
| (valid_positions & (local_slots <= 0)),
dim=1,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The calculation of invalid_rows is a bit dense and combines multiple conditions in a single torch.any call. For better readability and maintainability, consider breaking down the conditions into separate, descriptively named boolean tensors. This will make the logic easier to understand at a glance.

            # A seed is invalid if it references a position that is:
            #  - an invalid sentinel value (only -1 is valid)
            #  - out of bounds for the request's sequence length
            #  - out of bounds for the physical page table
            #  - a valid position that maps to an unallocated or padding slot
            is_invalid_sentinel = dsa_topk_indices < -1
            is_out_of_bounds_seq = dsa_topk_indices >= batch.seq_lens[:, None]
            is_out_of_bounds_table = dsa_topk_indices >= table_width
            is_unallocated_slot = valid_positions & (local_slots <= 0)
            invalid_mask = (
                is_invalid_sentinel
                | is_out_of_bounds_seq
                | is_out_of_bounds_table
                | is_unallocated_slot
            )
            invalid_rows = torch.any(invalid_mask, dim=1)

@HanHan009527 HanHan009527 changed the title [PD] Re-enable fused DSA TopK for Decode IndexShare seeds [Spec] Enable fused TopK for GLM-5.2 MTP IndexShare seeds Jul 17, 2026
@HanHan009527 HanHan009527 changed the title [Spec] Enable fused TopK for GLM-5.2 MTP IndexShare seeds [Spec][PD] Enable fused TopK for GLM-5.2 MTP IndexShare Jul 17, 2026
@HanHan009527
HanHan009527 marked this pull request as ready for review July 17, 2026 08:11
@HanHan009527

Copy link
Copy Markdown
Collaborator Author

@zRzRzRzRzRzRzR Hello, when you have time, could you please take a look and see if this modification matches your design?

@zRzRzRzRzRzRzR zRzRzRzRzRzRzR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, this only changes the PD Decode path with fused TopK enabled, HiSparse disabled, and dcp_size == 1, while all existing paths retain their current behavior, correct?

Copy link
Copy Markdown

Hi @HanHan009527 — I read your PD/MTP IndexShare optimization where request-relative seeds are remapped once to decode-local KV slots, eliminating the repeated page-table transforms and 704 unfused TopK calls in the profiled run. I’m Daniel, a user researcher with a product research team studying local AI systems for inference and kernel engineers. My main question is: what observability would make allocator-identity bugs across prefill, transfer, and decode easier to isolate without a large distributed reproduction? Would you be open to a Zoom interview of up to 30 minutes at your convenience? We’re preparing a prototype and may later provide test units or invite relevant participants as early users or technical advisors. This is research, not sales.

@HanHan009527 HanHan009527 added the run-ci CI: run the baseline test suite on this PR label Jul 26, 2026
@HanHan009527

Copy link
Copy Markdown
Collaborator Author

Just to confirm, this only changes the PD Decode path with fused TopK enabled, HiSparse disabled, and dcp_size == 1, while all existing paths retain their current behavior, correct?

Yes, I ensured this in should_remap_pd_dsa_seed_to_local_slots.

def should_remap_pd_dsa_seed_to_local_slots(server_args: "ServerArgs") -> bool:
    """Whether a PD seed should enter the allocator-local fused TopK domain."""
    return (
        envs.SGLANG_DSA_FUSE_TOPK.get()
        and server_args.disaggregation_mode == "decode"
        and not server_args.enable_hisparse
        and server_args.dcp_size == 1
    )

HiSparse consumes logical/request-relative positions and resolves GPU residency per layer through swap_in_selected_pages; eagerly remapping the seed through req_to_token would produce logical or stale/nonresident slots and bypass the required host-to-device resolution.

DCP shards KV across context ranks, while the current remap only produces a single-rank allocator-local slot and does not apply the DCP owner filter or global-to-local index conversion.

@HanHan009527

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci extra

@github-actions github-actions Bot added the run-ci-extra CI: also run the extra suite (requires run-ci) label Jul 27, 2026
@HanHan009527

Copy link
Copy Markdown
Collaborator Author

@Fridge003 Could you please review this PR?

@kpham-sgl kpham-sgl left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! Open question: how do we write better tests (outside of test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py)

@kpham-sgl

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py

@github-actions

github-actions Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py:

🚀 8-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/models_e2e/test_dsa_glm52_cache_layer_split.py

Keep request-relative seed positions on the PD wire, remap them once through the Decode-local req_to_token table, and then use the existing fused DSA TopK path.
@HanHan009527
HanHan009527 force-pushed the codex/feat/pd-decode-fused-topk branch from 5a05138 to 175d98b Compare August 3, 2026 11:15
@HanHan009527

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py

@github-actions

github-actions Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py:

🚀 8-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/models_e2e/test_dsa_glm52_cache_layer_split.py

@HanHan009527

Copy link
Copy Markdown
Collaborator Author

Thank you for the contribution! Open question: how do we write better tests (outside of test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py)

The review feedback has been processed. Regarding the testing issue, I think we might need to build a tiny model to test with 2 or 4 GPUs?

@kpham-sgl kpham-sgl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can revisit the testing question later

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py

@github-actions

github-actions Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py:

🚀 8-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/models_e2e/test_dsa_glm52_cache_layer_split.py

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py

@github-actions

github-actions Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_dsa_glm52_cache_layer_split.py:

🚀 8-gpu-b200 (1 test): ✅ View workflow run

cd test/ && python3 registered/models_e2e/test_dsa_glm52_cache_layer_split.py

@Fridge003
Fridge003 merged commit 9436de7 into main Aug 5, 2026
467 of 546 checks passed
@Fridge003
Fridge003 deleted the codex/feat/pd-decode-fused-topk branch August 5, 2026 21:17
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
Jianhong-Zhang pushed a commit to Jianhong-Zhang/sglang that referenced this pull request Aug 18, 2026
@tianxiaojiang4

Copy link
Copy Markdown
Contributor

Fused DSA top-k is still off on ROCm under PD: should_remap_pd_dsa_seed_to_local_slots() gates on is_cuda(), so this PR's remap never applies on gfx950, pd_index_share_seed stays true, and should_use_dsa_fused_topk() returns False.

Measured on GLM-5.2-MXFP4, 8×MI355X (gfx950, ROCm 7.2.0), 1P1D at TP2, decode step time p50:

fused 23.2 ms
unfused (current ROCm behaviour) 59.2 ms

2.55×, replicated across four independent run pairs. The gap is flat in context (39.7 ms at 37k prompt tokens, 41.8 ms at 177k) and both arms replay the draft CUDA graph (eager fraction 2e-4), so it is kernel work rather than launch overhead or a fallback.

Fusion itself is fine on gfx950. Enabling it by declining the seed passes needle-in-a-haystack at ~150k context (3/3 depths) with real acceptance 3.09, acceptance simulation off.

Is the is_cuda() gate intentional, or just untested on ROCm? Happy to test a patch on MI355X.

@kpham-sgl

Copy link
Copy Markdown
Collaborator

@tianxiaojiang4 , I meant to gate off NPU only but ROCm paths are untested. Can you submit a PR with tested results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants