Skip to content

[Sampling] Support sampling masks with overlap scheduling - #36631

Merged
ByronHsu merged 24 commits into
mainfrom
codex/sampling-mask-overlap
Sep 10, 2026
Merged

ByronHsu merged 24 commits into
mainfrom
codex/sampling-mask-overlap

Conversation

@ByronHsu

@ByronHsu ByronHsu commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Sampling masks currently do not support overlap scheduling because materializing the masks inside the sampler synchronizes the GPU with the CPU.

Modifications

  1. Remove CPU synchronization from sampling-mask capture. Keep sampling-mask outputs on the GPU inside the sampler, then copy them asynchronously to the CPU and materialize the response data during result processing.
  2. Introduce --sampling-mask-max-tokens (default: 4096) to set the maximum number of tokens per sampling mask.
  3. Reject sampling-mask requests upfront if top_k exceeds the configured mask capacity.
  4. Abort the request with an overflow error if the realized mask exceeds the capacity, including when cutoff ties produce more tokens than top_k even though top_k is within the limit.
  5. Mark sampling results as invalid if validation fails, such as when the selected-token logprob is non-finite (NaN or infinity). Abort the request before committing the failed step's sampled token.
  6. Replace SGLANG_DISAGGREGATION_SAMPLING_MASK_MAX_TOKENS with the server argument --sampling-mask-max-tokens for both ordinary and disaggregated serving. PD metadata remains off by default: set SGLANG_ENABLE_DISAGG_SAMPLING_MASK=1 on both prefill and decode servers. Without it, requests asking for masks receive HTTP 400 with the required setting; unused metadata buffers are neither allocated nor transferred. Startup rejects the retired SGLANG_DISAGGREGATION_SAMPLING_MASK_MAX_TOKENS variable with explicit migration instructions.

Accuracy Tests

Current head d73bc1bd7f; review fixes verified at 1d542447e5 on BJ job 3439034 (NVIDIA H200, lmsysorg/sglang:dev-cu13):

  • 240 affected unit tests and 35 subtests passed. Before-fix tests reproduced premature KV slot reuse during offload, lost error types/cleanup interruption on prefill abort, and all eight legacy-env configurations silently starting.
  • Live TP2/PP1 and TP1/PP2 sampling-mask tests passed with Qwen/Qwen2.5-0.5B-Instruct: masks with and without ordinary logprobs, followed by ordinary generation; four tokens per request. Checks cover selected-token membership, support IDs, aligned lengths, capacity, and finite sampling logprobs.
  • TP2 uses overlap and the real status collective. PP2 uses the existing pipeline scheduler (which automatically disables ordinary overlap) and real cross-stage send/receive.
  • After consolidating the distributed cases into the existing sampling-mask file, all 29 tests passed in 249.286 seconds on BJ with a runtime-only Qwen/Qwen2.5-0.5B-Instruct override for the existing fixtures. The normal run passed both distributed tests and ten capture/packing tests; three existing fixtures could not access the gated Llama model (HTTP 401). The committed default model is unchanged.
  • All 24 current feature files differing from merged main were SHA-256 matched to the BJ source tree.
  • Ruff configured lint/format, isort, whitespace, and registered-test validation passed. Current-head CI is complete: 106 passed, 25 failed, 45 skipped, 2 cancelled. CUDA Base passed after one failed-jobs-only retry; the NPU reference-image retry passed. Remaining failures are the separate MLX fixture issue, shared AMD/NVFP4 failures, Xeon package/test mismatch, and NPU Wan timing plus dependent-job failures; no confirmed sampling-mask regression remains. Monitoring is paused.

Run the distributed tests with:

PYTHONPATH=python python3 test/registered/sampling/test_sampling_mask.py TestDistributedSamplingMask -v

Earlier verification also covered FlashInfer/PyTorch HTTP requests with overlap on/off, seeded parity, and two live Mooncake P/D cases (disabled/error and enabled metadata handoff).

Upstream main 5177a3ec08 remains merged through 92774ddb48; no history was rebased. spec/ remains removed.

Review follow-up commits:

  • 90eab32d48: defer abort release until pending KV offload completes; preserve the optional backend hook and leave base classes unchanged.
  • 084b80efce: reuse prefill abort retirement and preserve err_type despite sender exceptions.
  • 520e7f7541: reject the retired environment variable at startup.
  • f79c5a9466: revert the unrelated MLX test repair. That fix is split into Fix MLX shutdown test mock after request-ingestion refactor #38739; main still reproduces its two shutdown-fixture failures, so MLX CI may remain red here until the separate repair lands.
  • 1d542447e5: live TP2/PP2 coverage.
  • d73bc1bd7f: merge distributed coverage into the existing sampling-mask test file; use the two-GPU CUDA runner and preserve the one-GPU AMD suite with only the distributed class skipped.

The earlier three review fixes remain separate commits: e511bfc998 (opt-in PD metadata), 6f53732040 (bulk CPU copy), and a723a1a55a (capture-only helper allocation).

Speed Tests and Profiling

Qwen3-8B BF16, TP1 on one NVIDIA H200, with FlashInfer sampling and attention and CUDA graphs enabled. Each request uses 128 input tokens, 512 output tokens, temperature=1, top_k=4096, top_p=1, and return_sampling_mask=true. The PR uses --sampling-mask-max-tokens=8192 to accommodate cutoff ties; observed masks contained 4096–4396 tokens.

Before: upstream 756d0e0a85, overlap disabled. After: PR cb30f7c6a9, overlap enabled. This measures the combined implementation and scheduling change.

Median steady decode throughput from three measured runs after one warmup per configuration; parentheses show the run range.

Batch size Before (tokens/s) After (tokens/s) Change
1 155.47 (154.90–155.56) 182.37 (181.99–182.40) +17.30%
16 1659.03 (1650.52–1665.77) 2216.61 (2216.46–2219.79) +33.61%
64 3179.15 (3174.73–3201.97) 4826.58 (4637.89–4837.76) +51.82%

Throughput uses scheduler decode logs over full-batch intervals. Prefill, warmup, and final HTTP response serialization are excluded; sampling-mask capture, copying, and decode result processing are included. All 18 measured trials and six warmups completed with aligned masks/logprobs and no overflow or invalid-result failures.

Reproducer, exact commands, and full per-run evidence.


CI States

Latest PR Test (Base): 🚫 Run #34503593751
Latest PR Test (Extra): ✅ Run #35022699343
Latest PR Test (AMD ROCm 10): ➖ No AMD PR run found for this commit.

Co-authored-by: Nan Jiang [email protected]

@ByronHsu
ByronHsu force-pushed the codex/faithful-sampling-mask-capture branch from 05b18a3 to 6ee0895 Compare September 1, 2026 21:41
Base automatically changed from codex/faithful-sampling-mask-capture to main September 3, 2026 00:13
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Sep 5, 2026
@ByronHsu

ByronHsu commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Sep 5, 2026
@ByronHsu

ByronHsu commented Sep 5, 2026

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 Sep 5, 2026
@ByronHsu

ByronHsu commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

1 similar comment
@ByronHsu

ByronHsu commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

Byron Hsu added 2 commits September 8, 2026 16:57
Addresses review comment 3962601377. Copy once before slicing individual request rows in the non-overlap path.
Addresses review comment 3962601385. Build the row-selection partial only for requests that opt into mask capture.
@ByronHsu
ByronHsu force-pushed the codex/sampling-mask-overlap branch from 275aa5a to a723a1a Compare September 8, 2026 23:57

@nanjiangwill nanjiangwill 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.

Thanks—the three earlier performance comments are addressed, and removing the spec/ tree helped. The remaining inline comments are lifecycle/configuration issues I think should be resolved before merge. Two cleanup and verification items: 4964140 is an unrelated MLX test repair and should be split out, and the new distributed paths still need one real TP2 sampling-mask run plus one live PP run. The Mooncake P/D run is useful, but TP1 and unit-level PP reconstruction do not exercise the status collective or actual PP send/recv.

Comment thread python/sglang/srt/environ.py Resolved

@nanjiangwill nanjiangwill 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.

The lifecycle and configuration fixes look good. I left two focused test-structure comments to keep the added coverage aligned with SGLang’s CI contract and remove an unused mock.

)

register_cuda_ci(est_time=139, stage="base-b", runner_config="1-gpu-small")
register_cuda_ci(est_time=250, stage="base-b", runner_config="2-gpu-large")

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.

Could we keep this file on 1-gpu-small and move only TestDistributedSamplingMask into a focused two-GPU test file? These cases genuinely need a different CI contract, which is the explicit new-file exception in unit-test-admission.md. In this CI run the distributed cases took about 136 seconds, while the remaining roughly 119 seconds held a second large GPU unnecessarily. Splitting them preserves the existing suite’s cost and makes the two-GPU requirement explicit.



def validate_sampling_mask_max_tokens(server_args: Any):
if envs.SGLANG_DISAGGREGATION_SAMPLING_MASK_MAX_TOKENS.is_set():

@nanjiangwill nanjiangwill Sep 10, 2026 •

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.

maybe we have a follow up pr to deprecate SGLANG_DISAGGREGATION_SAMPLING_MASK_MAX_TOKENS entirely in sglang, but merge it later

@ByronHsu
ByronHsu merged commit fd7743e into main Sep 10, 2026
160 of 201 checks passed
@ByronHsu
ByronHsu deleted the codex/sampling-mask-overlap branch September 10, 2026 21:52
@ByronHsu ByronHsu added the release-highlight Candidate PR for release note highlight label Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apple-silicon documentation Improvements or additions to documentation release-highlight Candidate PR for release note highlight 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.

2 participants