Repository navigation
feat(loss): add sampler replay for top-k/top-p training incl score centering - #5006
Conversation
…'s replay - Replaying top-k/top-p requires vLLM to report filtered logprobs (logprobs_mode=processed_logprobs); vllm_cfg overrides vllm_kwargs, as in the worker. - The vLLM HTTP server (NeMo-Gym rollouts) pins top_k to the train or validation value, min_p to 0 and repetition_penalty to 1. Unset, vLLM would fill them from the model's generation_config.json (Qwen2.5-Instruct ships repetition_penalty=1.05), and the sampler would apply processing the trainer does not replay. - Since the server pins top_k, the Gym path accepts top_k with the vLLM backend. The server tells validation requests apart by temperature/top_p only, so a val_top_k that differs from top_k while those match is rejected at setup (_validate_val_sampling), replacing the old "val_top_k unsupported" assert. Signed-off-by: Hayate Iso <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add SamplerSupportLogprob: given each position's captured sampler head ids, with -1 where the sampler filtered the id out, it returns the log-probs at those ids renormalized over the finite ones (the set the sampler drew from, S_q). It gathers the K head logits and takes a logsumexp over them; under tensor parallelism each rank gathers its shard and one all-reduce sums them. There is no vocabulary top-k, softmax or vocabulary-sized collective, and the backward is a local scatter into this rank's vocab shard. get_policy_logprobs_at_sampler_topk takes the sampling params and dispatches to it when they set sampler_replay (new TrainingSamplingParams field, default false). Under sampling filters the head is only defined over the sampler's support, so filters without replay raise there. sampler_support_ids marks the filtered head slots, sampled_logprobs_from_support reads the sampled token from its slot, and check_sampler_support returns where the head holds the whole support (finite mass 1). It raises on a corrupt capture: a head with no finite entry, or a sampled token missing from a complete head. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
…ore centering With policy.generation.sampler_replay.enabled=true the trainer renormalizes its logits over the support the sampler actually drew from, S_q, instead of recomputing the filters on its own logits (S_p). S_q is read from the captured vLLM processed top-K head: with top_logprobs >= top_k its finite entries are S_q, and filtered ids come back as -inf, so p and q share one support. This also lets score centering (NVIDIA-NeMo#4818), which required unfiltered sampling, compose with top-k/top-p. As in miles: - The actor loss scores the sampled token over S_q in one SamplerSupportLogprob pass, with or without score centering; with it, the same pass gives the centering head. Every sampled token is in the support, so nothing is dropped, p/q is defined at every token, and trainer/sampler numerics cannot move the boundary. - The prev-logprob pass (Megatron and Automodel LogprobsPostProcessor) uses the same renormalization; TQPolicy fetches the sampler head for it, and the SingleController sends it to training when either option is on. The reference KL stays on the full-vocabulary pass. - The estimator keeps NVIDIA-NeMo#4818's form. Head slots the sampler filtered out carry no coefficient, and with no trainer tail (the head holds all of p) rho is zero, so centering is exact for every IS mode and for token- and sequence-level loss. - Setup requires sampler replay for score centering under top-k/top-p, and for the flag: filtered sampling, top_k with top-p (so S_q is bounded), a captured head and top_logprobs >= top_k, Automodel TP=CP=1 and vocabulary logits. Legacy GRPO/PPO/distillation reject the flag; a missing head raises a clear error instead of a KeyError. A token whose head misses part of S_q (vLLM keeps ties at the k-th logit for small batches) is dropped from the loss and counted in policy_support_excluded_tokens; training raises only on a corrupt capture (a sampled token outside a complete head, or a head with no finite entry). - Extraction keeps -inf head candidates. With the flag off the trainer still recomputes top-k/top-p with NVIDIA-NeMo#4818's kernels. - The filtered Gym score-centering smoke runs top_k=12, top_p=0.9 with a 16-wide head and replay on, without force_on_policy_ratio, so it checks the replayed prev logprobs against vLLM's. - The option mirrors Router Replay: policy.generation.sampler_replay.enabled (SamplerReplayConfig) read through sampler_replay_enabled(). A short guide, docs/guides/sampler-replay.md, covers it; the score-centering guide keeps only the estimator-specific part. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
6258fe0 to
77d3b09
Compare
|
Auto-sync is disabled for ready for review pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
guyueh1
left a comment
There was a problem hiding this comment.
Found one score-centering regression in the new zero-tail branch.
Resolve the import conflict in single_controller_utils/config.py: keep main's RecoveryTargetLevel import and this branch's sampler_replay_enabled. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
The zero-tail branch set rho = 0 whenever the trainer tail was below 1e-6, including unfiltered capture, where a tiny trainer tail can still carry a large sampler tail. With a top-2 head, p = [0.6, 0.3999991, 9e-7] and q = [0.5, 0.3, 0.2], IS-off centering then left an expected tail-logit gradient of about 0.2 instead of the clamped estimate's 0.02. Only replay, where the trainer renormalizes over the sampler's support and the head holds all of p, has no tail. compute_score_centering_term now takes sampler_replay and zeroes rho only then; unfiltered capture is back to NVIDIA-NeMo#4818's estimate. ClippedPGLossFn derives the flag from policy_support_mask, which only filtering supplies, and filtered score centering always replays. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
Document why replay rejects top-p without top_k and top_logprobs < top_k: a head smaller than the kept set would drop the flattest positions from the loss, a systematic subset rather than a negligible one. Recommend capping top-p with top_k == top_logprobs, and point both setup errors to the section. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
|
/ok to test 941d761 |
terrykong
left a comment
There was a problem hiding this comment.
Please merge main again: main changed after your merge commit, and the PR now conflicts in loss_functions.py, score_centering.py, test_score_centering.py and grpo_score_centering_gym_single_controller.sh.
Thanks for fixing the zero-tail branch (af11112) and documenting the head-size limits (941d761). The design is clean. Replay rides on TrainingSamplingParams with one line per backend (Megatron, Automodel). One shared sampler_support_logprobs feeds the actor loss and both prev-logprob passes, so they can't drift. SamplerSupportLogprob gathers the K values before upcasting and guards all -inf rows, so no NaN gradients; on CPU it matches a float64 reference to ~2.5e-7 forward and backward, including a simulated TP=2 split. No vLLM monkey-patch, and the reference KL stays on the full vocab.
Please add to the PR body: every Gym run now forces repetition_penalty=1 and min_p=0 (here), with or without replay. Validation numbers on models whose generation_config.json sets them (e.g. Qwen2.5-Instruct's 1.05) may not match older runs.
Comments:
- (C1) A tied token the head leaves out, with probability below 1e-4, crashes the run as "corrupt capture" instead of being dropped.
- (C3)
presence_penalty,frequency_penaltyandlogit_biasstill reach vLLM unreplayed; reject them, and setgeneration_config="vllm". - (C5) Use the new
resolve_vllm_logprobs_modehere instead of the copied lookup. - (C6) Make
SamplerReplayConfigaBaseModel, per config-conventions. - (C9) The functional check can't tell replay from recompute; also check
policy_support_excluded_tokens. - (C13) Say "sampler replay requires vLLM" directly; today a non-vLLM user is sent to set
top_logprobsfirst.
No GPU was used for this review; GPU-dependent claims are marked as not run.
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5006-sampler-replay.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5006-tie-crash.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5006-zero-tail.html
Generated by Claude Code
Resolve the conflicts with NVIDIA-NeMo#5013 (adaptive-ppo-clip for score centering): compute_score_centering_term takes both sampler_replay and the adaptive-ppo-clip arguments, and ClippedPGLossFn passes all of them. Under replay rho is zero, so adaptive-ppo-clip drops the tail coefficient the way it does any infinite tail ratio; the alpha term is zero-sum over S_q either way. The replay unit tests now also cover adaptive-ppo-clip, and the functional score-centering smoke keeps both the replay and per-mode checks. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
vLLM's PyTorch top-k path, taken for fewer than 8 rows, keeps every token tied at the top_k-th logit, so with top_logprobs == top_k the captured head can leave out a kept token. When the left-out mass was under 1e-4 the head passed the mass check and a sampled tie raised "the capture is corrupt". A sampled token missing from a full head (no -inf slot) is now dropped and counted in policy_support_excluded_tokens; a miss in a head with a -inf slot still raises. The guide's tie sentence now says which positions are dropped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
…ot replay vLLM applies presence_penalty, frequency_penalty and logit_bias to the logits before it samples and before it computes processed logprobs, and the trainer replays none of them, so a Gym agent or extra_body that set one trained off-policy with no error. The HTTP chat server now rejects them, as it already rejects min_p and repetition_penalty; this applies to every Gym run. The captured-media test's fake request gains the fields these checks read (it lacked repetition_penalty already). Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
The score-centering check kept its own copy of the vllm_cfg/vllm_kwargs lookup; use the shared helper so the two cannot drift. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
…e vLLM first New user-facing config is a BaseModel: SamplerReplayConfig now declares enabled: bool = False, and sampler_replay_enabled reads the block through model_validate, since it is a dict until MasterConfig validates it. Tests build it with keyword arguments, and the PPO exemplar and its reference config carry the block like the GRPO ones. Replay on a non-vLLM backend now says it needs vLLM before asking for top_k or top_logprobs, which those backends reject anyway. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
token_mult_prob_error averages over kept tokens only, so it passes whether the trainer replays the sampler's support or recomputes the filters. policy_support_excluded_tokens tells them apart: recompute drops every sampled token outside the trainer's own support, replay only ties a head left out. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
Brings in NVIDIA-NeMo#5061 (vLLM fp32 LM head via head_dtype), NVIDIA-NeMo#5058 and NVIDIA-NeMo#4825. No conflicts; NVIDIA-NeMo#5061 changes the fp32_lm_head comment next to this branch's logprobs_mode comment in vllm/config.py. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
|
Merged main again: a10d634 resolves the #5013 conflicts, and 6c1ae9f brings in #5061. Under replay rho is 0, so adaptive-ppo-clip drops the tail term the same way it does for any infinite tail ratio. The replay unit tests now cover adaptive-ppo-clip too. Each comment is fixed in its own commit, with a reply on each thread. I added the Gym sampling note to the PR body. |
…config.json Set generation_config="vllm" on the engine unless vllm_kwargs sets it, so a request that leaves a sampling field unset gets vLLM's neutral value instead of the model file's. The trainer replays none of those values. This also covers fields the Gym server does not pin, such as the default max_tokens, and any field vLLM starts reading from the file later. EOS ids still load from the model file. The server's pins and request checks stay. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
|
/ok to test 047d20e |
Conflict in vllm_worker_async.py: main (NVIDIA-NeMo#4978) moved the chat route's sampling check into check_sampling_param, which forced top_k=-1. Sampler replay serves the matched profile's top_k, so check_sampling_param now returns that top_k (top_k or val_top_k, -1 when None) and rejects a request top_k that differs from it. The route keeps the branch's min_p, penalty and logit-bias rejections. test_check_sampling_param and the route's rejection test are updated to match. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hayate Iso <[email protected]>
|
/ok to test 37df8ca |
Brings in NVIDIA-NeMo#5006 (sampler replay). Both conflicts keep both sides: configure_generation_config runs NVIDIA-NeMo#4905's Dynamo checks and NVIDIA-NeMo#5006's sampler-replay checks, and test_grpo.py imports SamplerReplayConfig without main's DynamoConfig, which NVIDIA-NeMo#4905 removes. test_dynamo_config.py's helper now sets top_p/top_k, which NVIDIA-NeMo#5006's top-k/top-p check reads. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: jthomson04 <[email protected]>
Match extract_score_centering_samples after sampler replay (#5006): top-k/top-p sampling gives filtered head candidates a -inf processed logprob, which the flat path rejected as non-finite. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Zhaopeng Qiu <[email protected]>
Match extract_score_centering_samples after sampler replay (#5006): top-k/top-p sampling gives filtered head candidates a -inf processed logprob, which the flat path rejected as non-finite. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Zhaopeng Qiu <[email protected]>
Match extract_score_centering_samples after sampler replay (#5006): top-k/top-p sampling gives filtered head candidates a -inf processed logprob, which the flat path rejected as non-finite. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Zhaopeng Qiu <[email protected]>
…n04/dynamo-gym NVIDIA-NeMo#4905 split native_engine.py into the DynamoEngineAdapter base (engine_adapter.py) and DynamoVllmAdapter (vllm_adapter.py), and moved RolloutResult to rollout_result.py. The Gym path moves onto that structure: - Gym calls are one more entry in DynamoVllmAdapter._handlers(); the base's dispatch refuses every other request, so the generic OpenAI branch of _generate (n>1, best_of, prompt logprobs, queue checks) and its tests go (Terry's C21 on NVIDIA-NeMo#4979). - start_dynamo_endpoint registers chat and completions (C20) and takes the vLLM adapter's chat settings (template, parsers, structural tag). - Gym calls sample with the matched profile's top_k, as NVIDIA-NeMo#5006 made the native route do, and pin min_p and the repetition penalty and reject the presence and frequency penalties (C41's rebase break); vLLM's request errors keep their 4xx. - test_dynamo_native_engine_capture.py becomes test_dynamo_vllm_adapter_capture.py. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: jthomson04 <[email protected]>
…logprobs Main's sampler replay (#5006) refuses top_k/top_p filtering unless vLLM returns logprobs after the filter, since the trainer now replays it. The parity test samples with top_p=1e-6 to make regenerated tails comparable token for token, so it failed at startup. Set vllm_cfg.logprobs_mode=processed_logprobs, as main's score-centering functional test does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
…logprobs Main's sampler replay (#5006) refuses top_k/top_p filtering unless vLLM returns logprobs after the filter, since the trainer now replays it. The parity test samples with top_p=1e-6 to make regenerated tails comparable token for token, so it failed at startup. Set vllm_cfg.logprobs_mode=processed_logprobs, as main's score-centering functional test does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
…logprobs Main's sampler replay (#5006) refuses top_k/top_p filtering unless vLLM returns logprobs after the filter, since the trainer now replays it. The parity test samples with top_p=1e-6 to make regenerated tails comparable token for token, so it failed at startup. Set vllm_cfg.logprobs_mode=processed_logprobs, as main's score-centering functional test does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
…logprobs Main's sampler replay (#5006) refuses top_k/top_p filtering unless vLLM returns logprobs after the filter, since the trainer now replays it. The parity test samples with top_p=1e-6 to make regenerated tails comparable token for token, so it failed at startup. Set vllm_cfg.logprobs_mode=processed_logprobs, as main's score-centering functional test does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
…logprobs Main's sampler replay (#5006) refuses top_k/top_p filtering unless vLLM returns logprobs after the filter, since the trainer now replays it. The parity test samples with top_p=1e-6 to make regenerated tails comparable token for token, so it failed at startup. Set vllm_cfg.logprobs_mode=processed_logprobs, as main's score-centering functional test does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
…logprobs Main's sampler replay (#5006) refuses top_k/top_p filtering unless vLLM returns logprobs after the filter, since the trainer now replays it. The parity test samples with top_p=1e-6 to make regenerated tails comparable token for token, so it failed at startup. Set vllm_cfg.logprobs_mode=processed_logprobs, as main's score-centering functional test does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
Stacked on #4818 (score centering). Review the 3 commits after #4818's head
39c2642d8; each is one topic. Compareview of only these commits:
https://github.com/isomap/RL/compare/39c2642d8...sampler-replay.Adds optional sampler replay for top-k/top-p training. Instead of recomputing the filters on the trainer's logits,
the trainer renormalizes over the set the sampler actually drew from (S_q): the finite entries of the processed
top-K head that #4818 already captures. Every sampled token is inside the support, so no token is dropped and
trainer/sampler numerics cannot move the filter boundary. The feature is off by default and works with or without
score centering; score centering under top-k/top-p, which #4818 rejects, now requires it.
The capture path is #4818's as-is; this PR adds no new vLLM output, request field, or data-plane column.
Behavior and configuration
policy.generation.sampler_replay.enabled=true,token_capture.enabled=true, andpolicy.generation.top_logprobs >= top_k. Requires top-k/top-p sampling; top-p requirestop_k, which boundsS_q.
full vocabulary. With replay off, top-k/top-p training recomputes the filters with feat(loss): add score centering with sampler top-k capture #4818's kernels unchanged.
-inf) carry no coefficient, and the head holds all of p, so theestimator has no tail (
rho = 0) and is exact for every IS mode and both loss normalizations. The sampled tokenand the head come from one pass.
distillation trainers, Automodel TP/CP > 1, and Megatron fused linear logprobs.
Sampler support and transport
The vLLM HTTP server (NeMo-Gym rollouts) now pins
top_kto the train or validation value,min_pto 0, andrepetition_penaltyto 1. It rejects requests that settop_kto another value, or setmin_p,repetition_penalty,presence_penalty,frequency_penaltyorlogit_biasto a non-neutral value. The engine also runswith
generation_config="vllm"unlessvllm_kwargssets it, so vLLM takes no sampling defaults from the model'sgeneration_config.json(Qwen2.5-Instruct shipsrepetition_penalty: 1.05). vLLM applies these fields beforeprocessed logprobs, and the trainer never replays them. This applies to every Gym run, with or
without replay, so on models whose
generation_config.jsonsets these, training and validation numbers may notmatch runs from before this PR. A
val_top_kthe server cannot tell apart from training is rejected at setup.Captured head (
[T, K]int32 IDs, float32 logprobs;-infkept for filtered candidates) → canonical rolloutcolumns → training fetch, plus the prev-logprob fetch under replay →
SamplerSupportLogprob→ loss.SamplerSupportLogprobgathers the K head logits and takes a logsumexp over the finite ones. Under TP each rankgathers its vocabulary shard and one all-reduce of K values per chunk combines them; there is no vocabulary-wide
top-k, softmax, or collective, and the backward is a local scatter.
Documentation and tests
See
docs/guides/sampler-replay.mdfor motivation, mechanism, configuration, and cost. The score-centeringguide's "Sampling filters" section covers the estimator under replay.
SamplerSupportLogprobagainst a full-vocabulary reference in fp32 and bf16, and under gloo TP=2 with acollective spy (one K-sized all-reduce per chunk, nothing vocabulary-sized).
mode; replay without score centering (values and gradient over S_q, no vocabulary filter, no head).
top_k=12,top_p=0.9, 16-wide head) runs withoutforce_on_policy_ratio, so it checks the replayed prev logprobs against vLLM's (token_mult_prob_error < 1.05).It is registered in the fast SC3 functional plan.
Validation
test_vllm_policy_generation,which needs a GPU). GB200 unit tests all pass: 33 kernel and packed TP/CP, 289 algorithm and data-plane
(1 skipped), 5 vLLM parity including processed logprobs against a manual HF computation, 6 + 2 Megatron
prev-logprob replay and full-vocab sampling, 3 + 11 Automodel prev-logprob replay and logprob memory.
39c2642d8): CPU 1065 passed (the one error needs a GPU); GB200 unit testsall passed (33 kernel/TP-CP, 287 algorithm/data-plane, 5 vLLM parity, 6 + 2 Megatron, 3 + 11 Automodel).
grad norm 0.19-0.41,
token_mult_prob_error1.0087-1.0106, no excluded tokens.median
token_mult_prob_error1.009 vs 1.011; no excluded tokens vs about 250 per step from recompute.Remaining limits and evidence
SingleController + Gym + vLLM token capture is required, as for score centering. Replay without
top_k(top-palone) is unsupported.