Skip to content

[rl] SGLang generator: reject SGLANG_RETURN_ORIGINAL_LOGPROB - #6375

Merged
tianyu-l merged 1 commit into
mainfrom
sglang-reject-original-logprob
Nov 25, 2026
Merged

tianyu-l merged 1 commit into
mainfrom
sglang-reject-original-logprob

Conversation

@tianyu-l

Copy link
Copy Markdown
Contributor

Summary (human-written)

Why

The trainer scores each token under softmax(logits / T), the distribution the generator sampled from, and DAPOLoss/GRPOLoss use the generator's logprob as the old policy: the importance ratio is p / q. SGLang's SGLANG_RETURN_ORIGINAL_LOGPROB=1 (documented by other RL frameworks for their own needs) makes SGLang's sampler return log_softmax(logits), before temperature. Nothing in torchtitan checks it, so with the variable set every SGLang-generated ratio is silently wrong whenever T != 1.

Example. SGLangEngine, SamplingConfig(temperature=0.8), a token with logits [2.0, 1.0, 0.0], sampled token 0:

q the trainer expects     = softmax([2, 1, 0] / 0.8)[0] = 0.73
q SGLang returns (env=1)  = softmax([2, 1, 0])[0]       = 0.67
main:     ratio p/q is off by 10% on this token; every token is off, no error
this PR:  ValueError at SGLangEngine init: unset SGLANG_RETURN_ORIGINAL_LOGPROB

What changed

  • SGLangEngine.__init__ raises ValueError if SGLANG_RETURN_ORIGINAL_LOGPROB is set. It reads sglang.srt.layers.sampler.SGLANG_RETURN_ORIGINAL_LOGPROB, the module constant SGLang's sampler itself branches on, so the check can't disagree with SGLang's parsing of the variable.

Alternatives considered

  • Recompute the returned logprobs from logits / T (as the batch-invariant TrainerLogprobsSampler does): extra work on every decode step to support a setting nobody needs.
  • Check at config time: the variable is read in the generator processes, which may have a different environment than the controller.

Testing

  • No new test: the check reads SGLang's own constant, so a test would only re-read an environment variable. Verified the import resolves against the SGLang commit the README pins (.venv-sglang).
  • pytest tests/rl/unit_tests/cpu: 741 passed, 7 skipped (GB300, CPU only).

Known issues / follow-ups

First PR of the score centering stack (arXiv 2609.20807, takes over #5760): score centering also reads SGLang's top-k logprobs, which this variable would turn pre-temperature too. Training with temperature <= 0 is handled separately by #6356.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 25, 2026
@tianyu-l

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 1m 13s —— View job


Verdict: Approve (please fill in the human-written summary before merging) -- review

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

Verdict: Approve -- please address the items below before merging -- this is a 7-line raise that turns an env var that silently breaks the importance ratio into a clear error at engine init, and it brings SGLang in line with the vLLM engine.

What this PR does. SGLangEngine.__init__ now raises ValueError when SGLang's SGLANG_RETURN_ORIGINAL_LOGPROB is on. With that variable set, SGLang returns log_softmax(logits) instead of the logprob of the temperature-scaled distribution it actually samples from. The trainer uses that value as the old-policy q in p / q.

Example. SamplingConfig(temperature=0.8), logits [2, 1, 0], token 0 sampled, SGLANG_RETURN_ORIGINAL_LOGPROB=1 exported (as slime/verl setup guides suggest):

main:    q = softmax([2,1,0])[0]     = 0.665   (trainer expects softmax([2,1,0]/0.8)[0] = 0.731)
         ratio p/q off by ~10% on every token, no error
this PR: ValueError at SGLangEngine init

Must fix

  1. PR description: "Summary (human-written)" is empty. The author needs to fill it in before merge.
Details
  • Gate. The problem is real on main. Nothing in torchtitan/rl/ reads this variable, and SGLang's default sampler honors it. This is the right layer. SGLangEngine runs SGLang's Scheduler in-process (engine.py:117-124, no scheduler subprocesses), so the module constant imported at engine.py:26 is the same value the sampler branches on in the same process. Config-time validation can't see the generator process's environment, so __init__ is the earliest point that works.
  • Symmetry. The vLLM engine already pins logprobs_mode="processed_logprobs" (vllm/engine.py:366), so this closes the same gap on the SGLang side. No one-sided design concern.
  • Batch-invariant mode. There, TrainerLogprobsSampler (batch_invariance.py:56-60) overwrites next_token_logprobs from logits / T, so the sampled-token logprob would already be correct. The env var would still affect top-k logprobs, which the planned score-centering work reads. Banning it unconditionally is simpler than exempting that mode, and it is the right call.
  • Upstream symbol. sglang.srt.layers.sampler.SGLANG_RETURN_ORIGINAL_LOGPROB is a module-level constant, not a stable API. If SGLang renames or moves it, the import fails loudly at module load rather than silently skipping the check, which is the behavior we want.
  • Tests. No test needed. A test would only set an env var and re-read it.
  • Overlap: #6356 (non-positive temperature) and #5760 (score centering) are complementary, not superseding.

Not verified: that the import resolves at the SGLang commit the README pins. SGLang is not installed in this runner, so this relies on the author's check against .venv-sglang.

@tianyu-l
tianyu-l force-pushed the sglang-reject-original-logprob branch from 1451dd8 to 4988ee4 Compare November 25, 2026 05:34
@tianyu-l
tianyu-l added this pull request to stack #6377 November 25, 2026 05:35
@tianyu-l
tianyu-l marked this pull request as ready for review November 25, 2026 05:42
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 25, 2026
@github-actions
github-actions Bot requested a review from pzhan9 November 25, 2026 05:42
@tianyu-l
tianyu-l force-pushed the sglang-reject-original-logprob branch from 4988ee4 to d8473f2 Compare November 25, 2026 05:47

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

it is unclear to me how SGLANG_RETURN_ORIGINAL_LOGPROB is defined. It seems to import from sglang itself? So it doesn't seem to be a config. Is it an envvar?

@tianyu-l
tianyu-l merged commit 354482e into main Nov 25, 2026
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants