Skip to content

[Model Loading] Overlap checkpoint staging with CUDA graph capture during startup - #32017

Merged
alexnails merged 17 commits into
sgl-project:mainfrom
yh0903:startup-weight-load-overlap
Aug 13, 2026
Merged

alexnails merged 17 commits into
sgl-project:mainfrom
yh0903:startup-weight-load-overlap

Conversation

@yh0903

@yh0903 yh0903 commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

The native/default model-loading path currently finishes loading and post-processing the real weights before CUDA graph capture begins. For large checkpoints, checkpoint I/O and CUDA graph capture are both material parts of engine startup, but they run serially.

This PR introduces an opt-in startup path that stages safetensors checkpoint pages while the runtime initializes and CUDA graphs are captured. It then loads and post-processes the real weights in place, without changing the tensor storage referenced by the captured graphs.

The existing behavior remains the default:

--startup-weight-load-mode serial  # default

This PR does not enable overlap by default.

Modifications

Startup flow

Existing serial startup

flowchart LR
    S1["Load and post-process<br/>real weights"]
    S2["Initialize runtime and<br/>capture CUDA graphs"]
    S3["Ready to serve"]
    S1 --> S2 --> S3
Loading

Opt-in overlap startup

flowchart TB
    O1["Allocate final GPU storage<br/>and resolve safetensors shards"]
    O2["Initialize capture-safe weights<br/>and required post-processing"]
    O3["Explicitly start checkpoint prefetch"]
    O4["Storage-to-OS-page-cache staging"]
    O5["Initialize memory pools and attention backends<br/>and capture CUDA graphs"]
    O6["Load and post-process real weights<br/>in the existing storage"]
    O7["CUDA synchronize and verify storage identity,<br/>stop/join prefetch, and run the TP barrier"]
    O8["Ready to serve"]

    O1 --> O2 --> O3 --> O5 --> O6 --> O7 --> O8
    O3 --> O4
    O4 -. "runs concurrently" .-> O5
    O4 -. "warms pages consumed by" .-> O6
Loading

The overlapped work is checkpoint staging from storage into the OS page cache. Real checkpoint iteration, GPU weight commit, and post-load processing still happen after CUDA graph capture.

  • Add --startup-weight-load-mode {serial,overlap}, defaulting to serial.
  • Split the exact DefaultModelLoader startup lifecycle into model/storage initialization, checkpoint resolution, capture-safe initialization, and post-capture in-place commit.
  • Reuse the existing coordinated checkpoint page-cache prefetcher through a cancellable lifecycle handle and reuse the resolved checkpoint source during commit.
  • Verify that graph-visible tensor object identity, data pointer, shape, stride, dtype, device, and storage offset remain unchanged after the real-weight commit.
  • Start background prefetch only after ModelRunner/TP-worker construction completes; scheduler startup and sglang.benchmark.one_batch explicitly own the active lifetime.
  • Propagate explicit start/finalize handling through ModelRunner and TP workers.
  • Match the existing process-terminal startup contract: prepare, graph-capture, commit, and barrier failures are not recovered, so no failure-only cancellation or cleanup path is added. On the successful path, finalize stops and joins checkpoint prefetch before the TP barrier and serving.
  • Keep --weight-loader-prefetch-checkpoints as an independent serial-path control and avoid starting a second prefetch when overlap already owns staging.

Scope and rollout plan

This is PR 1 of a staged three-PR rollout. The overlap path is intentionally opt-in and explicitly gated here. Its initial supported scope is:

  • CUDA graph capture with the exact DefaultModelLoader
  • auto/safetensors loading with mmap enabled
  • Native dense Llama, Qwen2, and Qwen3 generation models
  • FP16/BF16, TP1/TP2, and a single primary checkpoint source

Unsupported configurations raise an explicit error instead of silently changing startup behavior. The existing serial path remains unchanged.

Planned follow-ups:

  • PR 2 — generalize and harden: broaden the validated support matrix, add startup-phase timing/diagnostics, and factor shared lifecycle pieces so the native/default-loader and checkpoint-engine paths can converge where their semantics match. The feature remains opt-in while this coverage expands.
  • PR 3 — default rollout: introduce an auto policy and make it the default after broader CI and benchmark burn-in. Validated configurations select overlap automatically; unsupported configurations retain the serial path, with an explicit serial escape hatch.

Accuracy Tests

  • Added a registered 1-GPU end-to-end test comparing default serial and overlap output token IDs, generated text, and output logprobs (1e-5 tolerance).
  • Added registered CPU unit coverage for compatibility selection, explicit rejection, successful lifecycle transitions, graph-visible storage invariants, checkpoint-source reuse, prefetch cancel/wait behavior, serial/overlap scheduler routing, server arguments, and MLX routing.
  • Manually validated the overlap safetensors Engine path and sglang.benchmark.one_batch overlap path on H100. A focused latest-code smoke produced identical serial/overlap input IDs, prefill logits, and decoded text through real decode CUDA graphs; this smoke is not used for performance claims.
  • Qwen3-32B benchmark runs produced matching output IDs, text, and logprobs across all three startup modes.
  • scripts/ci/check_registered_tests.py, Python compilation, formatting/lint, and git diff --check pass.

Speed Tests and Profiling

Configuration: Qwen3-32B BF16 on H100, Triton decode CUDA graphs through batch size 256, prefill CUDA graphs disabled, three timed samples per configuration with A/B/C scheduled in Latin-square order, and warmed compiler/graph caches. Reported values are mean ± sample standard deviation. Checkpoint files were present on the local NVMe filesystem before timing; before every run, POSIX_FADV_DONTNEED was applied and mincore verified zero resident checkpoint pages. Prefetch used four threads per rank unless otherwise noted. These results were collected against commit 0dbcd4199.

Primary result: incremental overlap benefit

The primary comparison is B → C. Both configurations use the same coordinated page-cache prefetch mechanism; C changes the startup order so runtime initialization and CUDA graph capture occur before the real-weight commit. This is the closest measured control for the additional overlap benefit.

Storage TP B: Serial + prefetch C: Overlap Wall time saved B → C reduction Speedup
Local NVMe 1 38.94 ± 0.56 s 35.60 ± 0.83 s 3.34 s 8.6% 1.09x
Local NVMe 2 51.12 ± 1.16 s 45.16 ± 0.29 s 5.96 s 11.7% 1.13x

Across the refreshed local-NVMe configurations, overlap reduced startup by 8.6–11.7% relative to serial startup with coordinated prefetch. Each of the three paired B → C samples favored C for both TP1 and TP2. With three repeats per configuration, these results characterize the observed startup behavior but are not intended to establish tight statistical confidence.

A latest-code TP2 NFS rerun passed output parity but was excluded from the performance table because shared-storage contention made the samples unsuitable for a stable mean comparison: default-serial startup ranged from 148.70 to 1093.41 s, and overlap ranged from 53.72 to 118.67 s. No NFS speed claim is made from that run.

End-to-end context

For context, the complete opt-in path also includes the existing coordinated-prefetch gain:

Storage TP A: Default serial B: Serial + prefetch C: Overlap A → B reduction A → C reduction
Local NVMe 1 84.83 ± 0.30 s 38.94 ± 0.56 s 35.60 ± 0.83 s 54.1% 58.0% (2.38x)
Local NVMe 2 78.60 ± 0.36 s 51.12 ± 1.16 s 45.16 ± 0.29 s 35.0% 42.5% (1.74x)

Most of the A → C reduction comes from coordinated checkpoint prefetching. A → C describes the end-to-end opt-in startup path; B → C is the main result attributable to the new startup ordering. The sequential percentage reductions are not additive because they use different baselines.

These measurements do not establish monotonic scaling with TP size. TP1 and TP2 were measured in separate executions on different nodes. Matching eight aggregate prefetch threads on TP1 produced means of 43.25 s for B and 36.71 s for C, a 15.1% reduction (1.18x), compared with 11.7% (1.13x) on TP2 using four threads per rank, also eight aggregate threads. Each TP rank captures its own CUDA graph concurrently, so capture time is not summed across ranks. The results demonstrate a benefit at TP1 and TP2, but do not imply increasing percentage savings as TP rank grows.

Checklist

  • Format the code according to the contribution guide.
  • Add registered unit and end-to-end tests.
  • Update user-facing argument help for the new control.
  • Provide accuracy and speed benchmark results.
  • Preserve the existing serial startup behavior by default.

CI States

Latest PR Test (Base): ✅ Run #31526564021
Latest PR Test (Extra): ⏳ Run #32449179602
Latest PR Test (AMD ROCm 7.2): ➖ No AMD PR run found for this commit.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@yh0903

yh0903 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@yh0903
yh0903 marked this pull request as ready for review July 22, 2026 05:03
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@ch-wan ch-wan self-assigned this Jul 22, 2026
@yh0903 yh0903 closed this Jul 22, 2026
@yh0903 yh0903 reopened this Jul 22, 2026
@yh0903 yh0903 closed this Jul 22, 2026
@yh0903 yh0903 reopened this Jul 22, 2026
Comment thread python/sglang/srt/server_args.py Resolved
Comment thread python/sglang/srt/managers/scheduler.py Resolved
@yh0903
yh0903 force-pushed the startup-weight-load-overlap branch from 3b71cb6 to 7206fe9 Compare August 4, 2026 07:02
@yh0903
yh0903 requested a review from mmangkad as a code owner August 4, 2026 07:02
@yh0903
yh0903 force-pushed the startup-weight-load-overlap branch 2 times, most recently from 72c28fb to c5753d9 Compare August 4, 2026 17:13
yh0903 and others added 4 commits August 4, 2026 23:42
…e explicit

Start checkpoint prefetch only after constructor work completes, so Scheduler and one_batch explicitly own the active lifetime. Replace broad exception handlers with fail-closed states and cleanup-only finally blocks.

Co-authored-by: Wenhui Zhu <[email protected]>
…near

Route overlap with explicit mode checks and remove failure-only cancellation plumbing. Startup failures already terminate Scheduler and one_batch, while successful finalization still joins prefetch before serving.

Co-authored-by: Wenhui Zhu <[email protected]>

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

do a look over on the comments and docstrings, other than that LGTM

…_mode a namespace

The ServerArgs namespace coverage lint landed after this branch was cut, so the
new field was missing its NS(...) marker. Use NS("model"), matching the other
weight-loading fields in the same block.

Co-authored-by: Wenhui Zhu <[email protected]>
Keep the storage-ordering and rollout constraints concise while preserving the reasons behind both invariants. Remove process-specific PR follow-up wording from the production comment.

Co-authored-by: Wenhui Zhu <[email protected]>
@yh0903

yh0903 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @alexnails! I did another pass over the comments/docstrings and pushed the cleanup. The PR-related CI paths, including the overlap E2E test, are green. The only Base root failure was the upstream post-capture KV sizing CI regression, which was fixed after this run in #33847; the remaining Base failures are fast-fail cascades. Could you take one final look and refresh your approval when you get a chance?

@yh0903

yh0903 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@yh0903

yh0903 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@yh0903

yh0903 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

Resolve the ServerArgs migration test conflict by preserving both the image processor backend coverage and startup weight-load mode coverage.

Co-authored-by: Wenhui Zhu <[email protected]>
@yh0903

yh0903 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

1 similar comment
@yh0903

yh0903 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@alexnails
alexnails merged commit 6b94d39 into sgl-project:main Aug 13, 2026
270 of 299 checks passed
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
@ch-wan ch-wan added the release-highlight Candidate PR for release note highlight label Aug 21, 2026
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apple-silicon release-highlight Candidate PR for release note highlight run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants