Skip to content

feat(sglang): add token-exact SGLang model server for RL training - #1787

Open
Kh4L wants to merge 17 commits into
NVIDIA-NeMo:mainfrom
Kh4L:sglang-splice-fix
Open

Kh4L wants to merge 17 commits into
NVIDIA-NeMo:mainfrom
Kh4L:sglang-splice-fix

Conversation

@Kh4L

@Kh4L Kh4L commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Adds a text-only SGLang model server for NeMo Gym rollouts. It calls the native /generate endpoint to preserve sampled token IDs and logprobs, then splices those exact IDs into later turns. Changed rendering inputs fail explicitly, and a full context window ends generation without truncating the prompt.

The adapter uses Gym's current public package imports and shared HTTP client. Its component lock inherits the repository's resolver policy and pins Transformers 5.17.0. The tokenizer and chat template must match training. Serving can use its own environment because requests carry integer token IDs.

Validation

  • On 70056c72, all 298 adapter, shared HTTP client, dependency, and checkpoint-worker CPU tests passed. The documented command also passed all 74 adapter tests in a fresh locked environment. Four CPU replay checks with the Qwen3-0.6B tokenizer preserved the exact sampled prefix and expected continuation with tools and thinking independently enabled or disabled. The backend responses for those checks were fixtures. Pre-commit and the documentation link checks passed.
  • On Gym a73c3f9f, all 261 SGLang adapter, shared HTTP client, component dependency, and checkpoint-worker tests passed in a fresh locked environment. All 328 managed component locks passed, along with pre-commit and the feature diff whitespace check.
  • On Gym 666aee20, Qwen3-0.6B and Workplace Assistant completed 20 synchronous and 20 asynchronous GRPO steps in separate four-GB200 jobs, with 160 rollout samples in each mode. Both had mean reward 0.15 and zero gradients because every prompt group had identical rewards. Configurations, curves, and timings
  • The preceding validation head a73c3f9f merged Gym main e0b0444b. Within the SGLang component, the refresh updated the lock metadata for the optional Ray and E2B bounds. All 128 locked package versions and sources match the GPU-tested revision.

Supports NVIDIA-NeMo/RL#3127. The NeMo-RL dependency bump will follow in a separate PR after the adapter lands on Gym main. Episode truncation is tracked separately in NVIDIA-NeMo/RL#3063.

@copy-pr-bot

copy-pr-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@cwing-nvidia

Copy link
Copy Markdown
Contributor

@Kh4L can you also take a look at #1557

@Kh4L

Kh4L commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 261615e: added an over-context guard to the SGLang /generate path.

SGLang hard-rejects input >= context_length with a 500 (vLLM tolerates it). In a multi-turn rollout the spliced prompt can already fill the window; the 500 then corrupts the trajectory and trips the downstream contiguity assert, stalling the async step. The guard pre-checks the prompt against sglang_max_total_sequence_length and defensively catches the ClientResponseError, ending the turn cleanly with finish_reason="length" — mirroring the existing vLLM context-length handling.

The splice fix + this guard have now been exercised end-to-end by a 16-node R=32 SGLang async-GRPO run: 0 chat-500s, 15+ training steps with graceful trajectory truncation and no stall. Still draft pending a full convergence-vs-baseline comparison.

@Kh4L

Kh4L commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Convergence validation complete. The splice fix + over-context guard in this PR (as carried on the fork branch) have now been validated by a full-scale end-to-end run: Qwen3-30B-A3B-Thinking SWE-bench async-GRPO on SGLang, 16 nodes (8 train + 8 gen, 32 generation replicas), 63 training steps across four clean checkpoint+resume segments.

  • train/total_reward/mean rose 0.10 → 0.19 → 0.22 and then held ~0.22 for two independent segments — the model learns and saturates; no stalls.
  • Zero over-context 500s (the guard's pre-check + error catch both exercised).
  • Rare non-contiguous turns were handled gracefully throughout (handled on the NeMo-RL side in the fork; the assert-side resilience is a separate NeMo-RL change).

Remaining known caveat: this port of the fix onto refactored Gym main is still not GPU-tested in this exact form (the validated code is the fork's pre-refactor equivalent) — keeping the PR draft until that's run.

Kh4L added 8 commits July 23, 2026 14:56
SGLang /generate hard-rejects input tokens >= context_length (HTTP 500),
unlike vLLM which tolerates it. In a multi-turn SWE rollout the spliced
prompt can already fill the window; the 500 then corrupts the trajectory
and trips the downstream contiguity assert, stalling the whole async step.

Mirror the vLLM context-length handling: pre-check the prompt length
against sglang_max_total_sequence_length, and defensively catch the
ClientResponseError, ending the turn cleanly with finish_reason="length"
(empty completion) in both cases. Validated by a 16-node R=32 SGLang
GRPO run (0 chat-500s; 15+ steps, graceful truncation, no stall).

Signed-off-by: Serge Panev <[email protected]>
The SGLang engine path re-parses tool calls from raw generated text
client-side. The existing parser only handled the hermes JSON format
(Qwen3-thinking). Add a dependency-free parser for the qwen3_coder
XML-ish format (<tool_call><function=NAME><parameter=KEY>...) used by
e.g. Qwen3-Coder-style chat templates, with schema-aware argument
type coercion from the request's tools, selectable via a new
sglang_tool_format config field (default "hermes" = no behavior
change). Unit tests mirror the template's canonical example.

Signed-off-by: Serge Panev <[email protected]>
Chat templates iterate assistant tool-call arguments as a mapping
(tool_call.arguments|items), but OpenAI-format history carries
function.arguments as a JSON string - exactly what our /generate-path
parsers emit. When a request misses the token-splice cache and falls
back to a full template render of a tool-call-bearing conversation,
jinja raises TypeError('Can only get item pairs from a mapping.') and
the request 500s.

Normalize on the full-render path only: decode string arguments that
parse to a JSON object, leave everything else (and the splice path,
which never re-renders old assistant turns) untouched. Inputs are not
mutated - the splice session cache holds the original messages.

Signed-off-by: Serge Panev <[email protected]>
Mechanical ruff check --fix (I001 import order) + ruff format on the SGLang
proxy files; no logic changes. Fixes the failing Lint check on the PR.

Signed-off-by: Serge Panev <[email protected]>
@Kh4L
Kh4L force-pushed the sglang-splice-fix branch from c223c16 to 65ddaa2 Compare July 23, 2026 22:27
@copy-pr-bot

copy-pr-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@Kh4L Kh4L changed the title fix(sglang): keep multi-turn prompts prefix-stable via token-splicing feat(sglang): add token-exact SGLang model server for RL training Jul 23, 2026
The adapter renders the chat template and tokenizes locally before calling the
SGLang server, so its transformers version has to agree with the server's. It
was pinned to 5.6.0 to match the NeMo-RL sglang extra; that extra moves to
5.8.1 alongside sglang 0.5.13.post1, so this pin follows.

A mismatch here does not fail loudly at import. It surfaces as a rollout
contiguity failure when the locally rendered prefix stops matching what the
server tokenized, which is far harder to attribute, hence the comment on the
pin.

Signed-off-by: Serge Panev <[email protected]>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added area:model Model servers, inference providers, and model adapters feature New capabilities, enhancements, or enablement work labels Oct 20, 2026
@Kh4L

Kh4L commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 3122202

@Kh4L

Kh4L commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test caa72c5

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the risk:high Autobot PR risk: high label Nov 19, 2026
@Kh4L

Kh4L commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 32db78d

@Kh4L

Kh4L commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 666aee2

@Kh4L
Kh4L marked this pull request as ready for review November 20, 2026 19:42
@Kh4L Kh4L added the needs-review PR is ready for code review and waiting on a reviewer label Nov 20, 2026
@Kh4L
Kh4L requested review from a team November 20, 2026 19:42
@Kh4L

Kh4L commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test a73c3f9

@svcnvidia-nemo-ci svcnvidia-nemo-ci 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.

HOLD

I found two BLOCKERs: cache reuse can substitute a different assistant answer, and accepted generation controls can be silently ignored. The RISK findings cover template fidelity, lost splice state, sampling configuration, parsing and endpoint recovery. Two documentation findings cover published guidance and the test command.

I inspected all 14 changed files, including the component lock’s dependency metadata, and traced the affected shared contracts. This was a static review; I did not execute tests or independently verify the validation runs reported in the PR.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@Kh4L

Kh4L commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 70056c7

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 20, 2026 23:20
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

@Kh4L

Kh4L commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 987cdf5

@Kh4L

Kh4L commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

hey @NVIDIA-NeMo/gym_reviewers_model @NVIDIA-NeMo/gym_reviewers_server_runtime, could someone take a look at the SGLang adapter? the lock refresh is in and all required checks pass. this is what we need to land before we can bump Gym in NeMo-RL and finish NVIDIA-NeMo/RL#3127. thanks!

@Kh4L

Kh4L commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Resolved all 12 automated-review threads from a73c3f9f5: both blockers (sampled replay validation and unsupported-control 422 checks), all 9 risks (dynamic template terminator, fragment tool forwarding, 409 on lost splice state, inherited extra_body/sampling_overrides, Hermes arg/malformed-markup parsing, <think> dedup, endpoint health accounting, component-locked test command), and the Fern model-server guide were addressed in 70056c72d and are live on 987cdf590 with required checks green.

This branch was previously deployed

1 inactive (outdated) deployment
public — caa72c5f Deployed Nov 10, 2026 by copy-pr-bot[bot] via release / finalize / notify #11259
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model Model servers, inference providers, and model adapters feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer risk:high Autobot PR risk: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants