Repository navigation
Conversation
|
@claude review |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. The engine change is small and correct: it passes num_topk_logprobs through as SGLang's top_logprobs_num and concatenates the streamed rows. The remaining items are in the description and the new test.
What this PR does. It removes the SGLangEngine guard that rejected score centering. Each request now asks SGLang for top_logprobs_num = num_topk_logprobs. _OutputCollector concatenates SGLang's streamed top-k increments into [num_tokens, k] arrays on FinishedRequest, matching what VLLMEngine returns. In batch-invariant mode, TrainerLogprobsSampler also recomputes the top-k values at SGLang's chosen ids with the trainer's compute_topk_logprobs, the same thing the vLLM hook does (vllm/batch_invariance.py:53).
Example.
GRPOLoss.Config(score_centering=ScoreCentering.Config()) # k = 32, generator = SGLangEngine
before: ValueError at SGLangEngine.__init__ ("returns no top-k logprobs yet")
after: training request -> top_logprobs_num=32 -> FinishedRequest.topk_token_ids/topk_logprobs [n, 32]
validation request -> top_logprobs_num=0 -> topk_* = None
batch-invariant: SGLang's ids, trainer's compute_topk_logprobs values -> coefficients == 0
Must fix
- PR description. "Summary (human-written)" is empty. The testing section also says "every class passes", but its table covers 7 of the 11 parity classes and leaves out
GDNHiCache,KDADPEP,DistMoEandDistMoEDPEP. Please fill in the summary and either add those rows or say they were skipped (e.g. nodist_moepackage). tests/rl/unit_tests/gpu/test_sglang_bitwise_parity.py:319--_trainer_topk_logprobsskips the Dist-MoE classes' per-call token-count setup, so on those classes the new test depends on state left by an earlier test (inline).
Should consider
tests/rl/unit_tests/gpu/test_sglang_bitwise_parity.py:292-- fold the top-k check intotest_trainer_vs_sglang_decoderather than adding a second generate to every class (inline).torchtitan/rl/generator/sglang/engine.py:940-- the shape check mostly repeatsbatcher.py:1015(inline).
Questions
- With k = 32, SGLang's
get_top_logprobsdoes a.tolist()of[B, 32]every decode step, and in batch-invariant mode the new sampler adds one small host-to-device copy per row. Have you measured decode throughput with score centering on vs. off forrl_grpo_qwen3_0_6b_varlen_sglang? The batch-invariant path is debug-only, so its cost matters less; the non-invariant cost is what users will see.
Details
- Gate: the problem is real (the guard is removed at
engine.py:236). The work belongs in the engine, and the trainer side already exists in #6378. The vLLM side is already done, so this isn't one-sided. I found no overlapping open PR. - Mixed batches: validation requests (k = 0) and training requests (k > 0) can share a decode step.
TrainerLogprobsSamplerselects only the rows withlen(ids) > 0, and the collector keys onoutput_top_logprobs_val is not Noneper batch, with an empty list per k = 0 request. A k = 0 request ends withtopk_logprobs == [], sotopk_arraysreturnsNone. That's correct. - Aborted requests (
AbortReqpath) carry no top-k. That's fine:token_env.py:237turns them intoERROR_INFRAbefore they reach the batcher. - I could not read the SGLang source at the README commit. The claims that the top-k ids come from the same temperature-scaled logprobs as the sampled token, and that the CUDA-tensor rows written by
TrainerLogprobsSamplerreach_OutputCollectorin a formnp.asarrayaccepts, rest on the author's GB300 run.
Not verified: no GPU or SGLang in this environment, so none of the parity tests were run here.
| def _trainer_topk_logprobs(self, sequences, *, prompt_lengths, topk_token_ids): | ||
| """The trainer's ``compute_topk_logprobs`` of ``topk_token_ids`` at each | ||
| completion position, from one packed prefill of ``sequences``.""" | ||
| with self.parallelism_context.activate_spmd(), torch.no_grad(): | ||
| packed = torch.cat( | ||
| [torch.tensor(ids, device=self.device) for ids in sequences] | ||
| ) | ||
| positions = torch.cat( | ||
| [torch.arange(len(ids), device=self.device) for ids in sequences] | ||
| ) | ||
| logits = self.model( | ||
| packed, | ||
| attention_metadata=self.model._get_attention_metadata(positions), | ||
| positions=positions, | ||
| ) |
There was a problem hiding this comment.
This calls self.model(...) directly, so it skips the _trainer_logprobs override in TestSGLangBitwiseParityDistMoE (line ~822), which calls trainer_dist_moe_runtime.set_num_local_input_tokens_per_call(...) before every trainer forward. The test is in the base class, so it also runs on DistMoE and DistMoEDPEP. There, the trainer forward uses whatever count the previous test left behind, or the runtime's initial value if you run the test alone with -k topk. It passes only if that count happens to match this batch. The PR's results table doesn't list either Dist-MoE class (or GDNHiCache/KDADPEP), so I can't tell whether this path has run at all.
Smallest fix: move the Dist-MoE count setup into a hook that both helpers call. For example, add def _before_trainer_forward(self, sequences): pass to the base class, call it from _trainer_logprobs and _trainer_topk_logprobs, and have the Dist-MoE class override the hook instead of _trainer_logprobs.
| def test_trainer_vs_sglang_topk_logprobs(self): | ||
| """Score centering's top-k logprobs: SGLang's at decode equal the trainer's | ||
| ``compute_topk_logprobs`` at the same ids, so its coefficients are 0.""" | ||
| results = self._generate( | ||
| self.prompt_ids, num_topk_logprobs=self.NUM_TOPK_LOGPROBS |
There was a problem hiding this comment.
The new test adds one more SGLang generate plus a trainer prefill to each of about 11 parity classes, several of them 4-GPU. Instead, test_trainer_vs_sglang_decode could request num_topk_logprobs=self.NUM_TOPK_LOGPROBS in _check_trainer_vs_sglang_decode and check the top-k from the same results. That gives:
- one generate instead of two;
- a check that the sampled-token logprobs stay bitwise equal when k > 0 (the new test doesn't check that);
- top-k coverage on the shared-prompt, cached-KV request in that test, which is the path real GRPO groups take.
_trainer_topk_logprobs also repeats the packing and forward in _varlen_prefill_logprobs (test_bitwise_parity.py:383). If the forward is shared, the Dist-MoE issue above goes away too.
| num_tokens = len(self.token_ids) | ||
| if ( | ||
| topk_logprobs.ndim != 2 | ||
| or topk_logprobs.shape[0] != num_tokens | ||
| or topk_token_ids.shape != topk_logprobs.shape | ||
| ): | ||
| raise ValueError( | ||
| f"{request_id}: top-k rows of shape {topk_logprobs.shape} for " | ||
| f"{num_tokens} tokens" | ||
| ) |
There was a problem hiding this comment.
Two of these checks are already handled elsewhere:
- A row count that doesn't match the token count is caught by the batcher at
batcher.py:1015, which checks rows against loss tokens. - Ragged rows already make
np.asarray(..., dtype=...)raise.
The only new invariant this checks is that ids and logprobs have the same k. Unlike vLLM's flat layout (vllm/engine.py:828), SGLang can't shift rows here: it returns one row per token, at the same offsets as the token logprobs. Consider dropping the check, or cutting it down to the ids-vs-logprobs shape comparison. Without it, topk_arrays becomes two np.asarray calls that could be inlined at the single call site.
3cd40b3 to
ce52246
Compare
ce52246 to
9db6867
Compare
9db6867 to
9e4151c
Compare
|
@claude review again |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. The new commit (9e4151c, replacing 3cd40b3) has the same diff as the one I reviewed before: all four files and every hunk match. So both earlier must-fix items are still open, and there are no new findings.
Still open from the last review
- PR description. "Summary (human-written)" is still the template placeholder. The testing section still says "every class passes", but the table lists only 7 of the 11 parity classes.
GDNHiCache,KDADPEP,DistMoEandDistMoEDPEPare missing. Add those rows, or say they were skipped and why. tests/rl/unit_tests/gpu/test_sglang_bitwise_parity.py:333--_trainer_topk_logprobsstill callsself.model(...)directly. The Dist-MoE classes put theirset_num_local_input_tokens_per_callsetup in a_trainer_logprobsoverride (line 822). This helper doesn't go through that override, so onDistMoEandDistMoEDPEPthe new test depends on whatever token count an earlier test left behind. Running it alone with-k topkwould use the runtime's initial count instead. The fix is a_before_trainer_forward(sequences)hook that both helpers call and the Dist-MoE class overrides. The inline comment from the last review still applies.
Should consider (still open)
test_sglang_bitwise_parity.py:292-- fold the top-k check intotest_trainer_vs_sglang_decodeto save one generate per class. That would also check that the sampled-token logprobs stay bitwise equal when k > 0, and would cover the cached-KV shared-prompt request.torchtitan/rl/generator/sglang/engine.py:940-- most of the shape check repeats the batcher's row-count check (batcher.py:1015). Only the ids-vs-logprobs shape comparison is new.
Questions (still open)
- Have you measured decode throughput with score centering on vs. off on SGLang (k = 32)? This is about the cost of the per-step top-k
.tolist()outside batch-invariant mode.
A maintainer still needs to review this PR.
Not verified: there is no GPU or SGLang in this environment, so I didn't run the parity tests.
9e4151c to
2b31156
Compare
2b31156 to
911ffb8
Compare
|
@claude review again |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. The new commit (911ffb8) fixes the Dist-MoE test setup and fills in the results table. The only must-fix left is the empty human-written summary.
Resolved since the last review
- Dist-MoE token count in the top-k test: fixed.
_before_trainer_forward(sequences)(test_sglang_bitwise_parity.py:258) now runs at the start of both_trainer_logprobs(:263) and_trainer_topk_logprobs(:328). The Dist-MoE class overrides the hook (:829) and no longer overrides_trainer_logprobs. No otherself.model(...)call skips the hook. - Results table: it now lists all 11 parity classes, including
GDNHiCache,KDADPEP,DistMoEandDistMoEDPEP.
Must fix
- PR description. "Summary (human-written)" is still the template placeholder. The author needs to write it.
Should consider (still open, not blocking)
test_sglang_bitwise_parity.py:298: you could fold the top-k check intotest_trainer_vs_sglang_decode. That saves one generate per class, checks that the sampled-token logprobs stay bitwise equal when k > 0, and covers the cached-KV shared-prompt request.torchtitan/rl/generator/sglang/engine.py:940: most of the shape check repeatsbatcher.py:1015. Only the ids-vs-logprobs shape comparison is new.
Questions (still open)
- Have you measured decode throughput on SGLang with score centering on vs. off (k = 32)? This is about the cost of SGLang's per-step top-k
.tolist()outside batch-invariant mode.
The diff touches the RL generator, so a maintainer still needs to review.
Not verified: there is no GPU or SGLang here, so I didn't run the parity tests. The passing results come from the author's GB300 run.
911ffb8 to
d75b362
Compare
d75b362 to
2d5b048
Compare
2d5b048 to
450724e
Compare
450724e to
7462582
Compare
7462582 to
1288014
Compare
1288014 to
8809d49
Compare

Summary (human-written)
Why
Score centering (#6378) needs the generator's top-k logprobs per generated token. With
SGLangEngineit raises today.Example.
rl_grpo_qwen3_0_6b_varlen_sglangwithGRPOLoss.Config(..., score_centering=ScoreCentering.Config()):What changed
SGLangEngine.add_requestspassesSamplingConfig.num_topk_logprobsas SGLang'stop_logprobs_num(validation requests ask for 0). SGLang computes them from the same temperature-scaled full-vocabulary logprobs as the sampled token's (SGLANG_RETURN_ORIGINAL_LOGPROBis rejected since [rl] SGLang generator: reject SGLANG_RETURN_ORIGINAL_LOGPROB #6375), and its top-k doesn't depend on the sampled token. SGLang needs no engine-wide bound, somax_num_topk_logprobsis unused._OutputCollectorconcatenates SGLang's streamedoutput_top_logprobs_{val,idx}increments per request (the same offsets as the token logprobs) and checks they form[num_tokens, k].TrainerLogprobsSampleralso recomputes the top-k values at SGLang's ids with the trainer'scompute_topk_logprobs, so they match the trainer's bitwise (before, only the sampled token's logprob was recomputed).Testing
test_sglang_bitwise_parity.py: newtest_trainer_vs_sglang_topk_logprobsin the shared base class, so every parity class runs it: SGLang's top-k logprobs at decode equal the trainer'scompute_topk_logprobsof the same ids, bitwise. Catches a misaligned row (streamed increments) or a top-k computed outside the trainer's code path; either would make score centering's coefficients nonzero in batch-invariant mode..venv-sglangat the README's SGLang commit: all 11 classes that CI runs pass, each including the new top-k check (the Dist-MoE classes set their per-call token count through a_before_trainer_forwardhook that both trainer helpers call):Varlen(Qwen3-0.6B, TP 2)MoEEPGDNGDNHiCacheSlidingWindowMLADistMoE(EP 2 over TP 2)KDAMoEDPEPKDADPEPDistMoEDPEP(DP 2 x TP 2, EP 4)max_num_topk_logprobsis accepted and ignored.Known issues / follow-ups
Stacked on #6378. Next: Verifiers (#6380).