Skip to content

feat(capture): run server-side spec capture as an SGLang plugin - #945

Merged
jiapingW merged 9 commits into
mainfrom
maocheng/sglang-capture-plugin
Oct 18, 2026
Merged

jiapingW merged 9 commits into
mainfrom
maocheng/sglang-capture-plugin

Conversation

@maocheng23

@maocheng23 maocheng23 commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Online disaggregated training needs SGLang capture servers that write prefill hidden states straight into Mooncake. Today that requires a patched SGLang:

  • there are three patch targets (v0.5.18, the Kimi K3 fork, and an Ascend overlay);
  • every SGLang bump means re-porting the patch;
  • the v0.5.18 patch no longer applies to SGLang main.

sgl-project/sglang#43448, sgl-project/sglang#43449 and sgl-project/sglang#43450 add generic, off-by-default extension points to SGLang. This PR runs the same capture as an SGLang plugin on top of them, so capture servers no longer need a patched SGLang.

Modifications

plugins/sglang-spec-capture. A new, separately installed distribution, specforge-sglang-capture. Install it into the server environment with pip install --no-deps. Entry point sglang.srt.plugins: specforge_spec_capture; it is inert unless SPECFORGE_SPEC_CAPTURE=1.

Piece SGLang hook it uses What it does
CaptureObserver ModelRunner.forward_observer Picks capture requests' prefill rows: the aux-layer concatenation and the post-norm last hidden, with the logits_mup_width_multiplier restoration. Copies rows out of CUDA-graph outputs and rejects cached prefixes, partial prefills and subset outputs.
CaptureDeviceOutput / CaptureHostOutput DeviceAuxiliaryOutput / HostAuxiliaryOutput Copies rows to pinned host memory with the generation result, or keeps device tensors for RDMA device publication. Hands each finished request to the background Mooncake sink and holds its response (Req.defer_output).
PendingCaptures DeferredOutputSource Releases requests in order once their objects are written. Backpressure is SGLANG_SPEC_CAPTURE_MAX_PENDING_BATCHES.
Streamer subclass Scheduler.get_output_streamer_class + build_additional_customized_info Returns the result in meta_info["spec_capture"].
sink.py none The patch's Mooncake writer, moved here, including the Ascend location="cpu" mount.
install() AFTER hook on Scheduler.__init__ Fails scheduler startup without --aux-hidden-state-capture, --return-hidden-states-mode full or --chunked-prefill-size -1, or on an SGLang without the extension points. Errors raised in register() are only logged by SGLang, so validation lives here.

SpecForge wiring.

  • Config: deployment.disaggregated.server_capture: patch | plugin. The default is patch, so existing recipes are unchanged. External producers can also set DISAGG_SERVER_CAPTURE=plugin.
  • Adapter: in plugin mode each spec travels as one JSON string in sampling_params.custom_params["spec_capture"], because custom_params values must be JSON scalars. The adapter already unwrapped list-shaped results.
  • Launch plan: managed-local servers get --speculative-algorithm <METHOD> --enable-return-aux-hidden-states --return-aux-hidden-state-layer-ids … --return-hidden-states-mode full plus SPECFORGE_SPEC_CAPTURE=1. Preflight checks the plugin and SGLang hook modules instead of the patch. The new flags and env are launcher-owned, so passthrough args cannot repeat them.
  • Live gate: test_server_capture_gate.py runs against whichever backend is installed (SPECFORGE_SERVER_CAPTURE=patch|plugin to force one).
  • Docs: the SGLang inventory, the config README and the plugin README.

Related Issues

Accuracy Test

GPU. One GB300 (CUDA 13), image lmsysorg/sglang:nightly-dev-cu13-20261006-f3fe7534 with SGLang main 8c273d6813 + sgl-project/sglang#43448, sgl-project/sglang#43449 and sgl-project/sglang#43450 (maocheng23/sglang spec-capture/integration) overlaid; real mooncake_master, TCP.

  • Live gate, plugin mode (SPECFORGE_RUN_SERVER_CAPTURE_TESTS=1 SPECFORGE_SERVER_CAPTURE=plugin, tiny Llama target): 3/3 pass.
    • EAGLE3 zero-copy: aux states match an independent HF forward within bf16 tolerance; lm_head of the captured last hidden state reproduces HF's logits; one EAGLE3 train step on the server-captured features gives a finite loss.
    • DFlash on the same server: aux states match HF.
    • DFlash without teacher metrics: the sink writes no last-hidden object.
  • Qwen3.8-27B, TP1, DFlash recipe layers 5/19/33/47/61, through this adapter and MooncakeFeatureStore:
    • shapes are correct and input_ids pass through for prompt lengths 7 to 4096;
    • the captured aux rows equal SGLang's own return_hidden_states output exactly (max abs diff 0.0);
    • lm_head of the captured last hidden state picks the served greedy token.
  • Unit tests on the GPU image: 57 passed (plugin, plugin integration, adapter). The SGLang hook suites: 75 passed.
  • TP=2 (Qwen3.8-27B, with [Feat] scheduler: Add forward observer and deferred responses for plugins sglang#43450's idle fix): rank 0 writes and rank 1 does not. Captures still equal return_hidden_states exactly and lm_head picks the served token. 16 /flush_cache calls during 64 concurrent captures gave no errors, and the same prompt captured bit-identically afterwards.

CPU (macOS, torch CPU):

  • test_sglang_capture_plugin.py: 26 unit tests with SGLang fakes.
  • test_sglang_capture_plugin_integration.py: drives the plugin through SGLang's real generation-result copy, consume, deferred release, output streamer and tokenizer meta_info accumulation into this adapter's result parsing. The objects written to the Mooncake stand-in match the captured rows byte for byte. It is skipped on SGLang builds without the extension points.
  • Six new plugin-mode adapter tests and four new launch-plan tests.
  • tests/test_config, tests/test_runtime, tests/test_scripts and tests/test_application: 929 passed, with no new failures compared with main on the same machine. The remaining failures are local SGLang-version mismatches that main also has.

Benchmark & Profiling

  • Capture throughput. Qwen3.8-27B on one GB300, TP1, Mooncake over local TCP.
    • 64 × 2048-token prompts (8 per request, 4 producer threads) captured in 5.75 s: 11.1 samples/s, 22.8K prompt tok/s.
    • The sink writes a 7-sample batch (28 objects, 881 MB) in about 96 ms; batch_put_from takes about 88 ms of that. It runs on the sink thread, not the scheduler.
  • SGLang with the plugin installed but inactive. Parity with stock main for decode and prefill; see the bench_serving A/B in [Feat] scheduler: Add forward observer and deferred responses for plugins sglang#43450.
  • Not measured yet: plugin vs. patch capture throughput on the same hardware. The v0.5.18 patch does not apply to SGLang main, so the patch arm needs its own image.

Checklist

🤖 Generated with Claude Code

maocheng23 and others added 2 commits October 6, 2026 01:27
Add `plugins/sglang-spec-capture` (`specforge-sglang-capture`), an SGLang
plugin that performs the spec-capture patch's work on SGLang builds with the
generic forward-observer, deferred-output and --aux-hidden-state-capture
extension points, so capture servers no longer need a patched SGLang:

- the forward observer selects capture requests' prefill rows (aux
  concatenation and post-norm last hidden, with the muP restoration);
- the auxiliary output copies them with the generation result, hands each
  finished request to the background Mooncake sink (ported from the patch,
  including the Ascend mount) and holds its response;
- the deferred-output source releases requests in order once their objects
  are durable, and a streamer subclass returns the result in
  `meta_info["spec_capture"]`;
- the scheduler refuses to start without --aux-hidden-state-capture,
  --return-hidden-states-mode full and --chunked-prefill-size -1.

`deployment.disaggregated.server_capture: plugin` selects it (default
`patch`, unchanged): the adapter sends each spec as JSON in
`sampling_params.custom_params`, managed-local servers are launched with the
plugin flags and SPECFORGE_SPEC_CAPTURE=1, and preflight checks the plugin and
SGLang hooks instead of the patch. External producers can also set
DISAGG_SERVER_CAPTURE=plugin. The live server-capture gate runs against
whichever backend is installed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Port the NvlinkArena from the v0.5.18 capture patch (#938
at 9ad9ff8) into the plugin's sink. With MOONCAKE_PROTOCOL=nvlink the sink
copies each batch into one fabric-memory arena that the Mooncake
TransferEngine allocates on the writer GPU, sized by the required
SGLANG_SPEC_CAPTURE_NVLINK_ARENA_BYTES. Results gain
"nvlink": {"session", "control"} and a per-feature "address". Clients free
objects by writing JSON key lists to the control TCP connection, which the
writer thread drains before it allocates. NVLink always selects device
publication, and SGLANG_SPEC_CAPTURE_GPU_PUT=0 is rejected.

The scheduler now checks the arena size at startup, like the plugin's flag
checks, instead of failing every capture batch. The tcp and rdma paths are
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
maocheng23 and others added 3 commits October 15, 2026 00:03
sgl-project/sglang#43449 renamed the server argument. The plugin, the
launcher, the schema's reserved-flag list, the docs and the tests follow.
SpecForge's own model.aux_hidden_state_layer_ids config field is unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
sgl-project/sglang#43449 replaced --aux-hidden-state-capture <method> with a
bool flag; --speculative-algorithm now names the capture convention and SGLang
resets it, so no draft runs. The launcher renders the new pair, both flags are
launcher-owned, and the plugin's messages, docs and tests follow. The plugin
still reads the resolved method from the non-CLI aux_hidden_state_capture field.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@jiapingW

Copy link
Copy Markdown
Collaborator

Perhaps we should nudge the sglang team to handle these PRs? @maocheng23

@jiapingW
jiapingW self-requested a review October 18, 2026 11:04

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

LGTM.

@jiapingW
jiapingW merged commit 717b109 into main Oct 18, 2026
6 checks passed
@jiapingW
jiapingW deleted the maocheng/sglang-capture-plugin branch October 18, 2026 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants