Repository navigation
Conversation
…raws Add Tinker's StableReinforce loss to the slime backend: a centered score z(a) - mean_k z(v_k) over K sampler draws per generated token, plus a sampler-learner KL weighted by beta0 / sqrt(token age). Rollout gateway: SamplingBackend.generate accepts num_auxiliary_token_samples, requested per session with create_session(sid, num_auxiliary_token_samples=K); draws reach TraceRecord.auxiliary_token_samples on trained tokens. SGLang: a sglang.srt.plugins entry point, enabled by AGENTCORE_SGLANG_TOKEN_SAMPLES=1, returns the sampled token and K draws in place of the top-K logprobs. SglangHttpBackend requests and validates them. slime: score_centering.py provides the loss and a --custom-megatron-init-path hook. Draws travel in Sample.multimodal_train_inputs; the hook takes them out of the model's forward kwargs and records the step of each weight push.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds score function centering (SFC) to the slime backend, following Tinker's StableReinforce loss. It keeps policy-gradient training stable when rollouts come from an older version of the model, as in asynchronous training. To support it, the rollout gateway can ask SGLang for
Kextra random draws from the sampling distribution at every generated token.At each generated token
awith drawsv_1 … v_Kfrom the same sampler distribution:ageis the number of optimizer steps since the weights that sampled the token were pushed to SGLang. The centered score needs onlyK + 1logit gathers per position; the loss has no importance ratio and no entropy term.Usage
cd src/agentcore_rl_toolkit/backends/slime/examples/math_agent SLIME_DIR=... MODEL_DIR=... TRAIN_DATA_PATH=... MODEL_TYPE=... bash train_sfc.shtrain_sfc.shistrain.shwith SFC enabled and asynchronous training: it runs slime'strain_async.pyon separate trainer and rollout GPUs (TRAINER_GPUS,ROLLOUT_GPUS, 4 each by default), so SGLang generates the next rollout while Megatron trains on the current one. It launches throughpython3 -m agentcore_rl_toolkit.backends.slime.train ${SLIME_DIR}/train_async.py, which registers the SFC flags with slime's argument parser, and passes:It also exports
AGENTCORE_SGLANG_TOKEN_SAMPLES=1to the SGLang engines. Sampling must be untruncated (--rollout-top-p 1.0,--rollout-top-k -1), and--entropy-coefmust be 0; both are checked at startup.Changes
Rollout gateway
SamplingBackend.generatetakes an optionalnum_auxiliary_token_samples; sessions request it withcreate_session(sid, num_auxiliary_token_samples=K), and backends without the parameter are rejected at session creation. Backends return the draws asAuxiliaryTokenSamplesonTurnRecord, and they reachTraceRecord.auxiliary_token_sampleson trained tokens.sglang_token_samples, registered as asglang.srt.pluginsentry point. WithAGENTCORE_SGLANG_TOKEN_SAMPLES=1, atop_logprobs_num=Krequest returns the sampled token followed byKrandom draws instead of the top-Ktokens. The top-logprobs path is reused as transport because it already carries per-position token ids end to end; while the variable is set, it changes the meaning oftop_logprobs_numfor every request on that server.SglangHttpBackendrequests the draws, records SGLang's weight version, and fails the turn if a position doesn't carry exactly the sampled token plusKdraws (plugin missing or truncated sampling).slime backend
integration/score_centering.py: the loss, and a--custom-megatron-init-pathhook. Draws travel to the train actors inSample.multimodal_train_inputs, the per-sample dict slime forwards unchanged; the hook takes them out of the model's forward kwargs, counts optimizer steps, and records the step of each weight push for token ages.integration/rollout.py: requests draws for training rollouts (not evaluation) and attaches them to each sample.train.py: entrypoint that runs slime'strain.pyortrain_async.pywith the--sfc-*flags registered (parse_args(add_custom_arguments=...)); slime's own entrypoints drop flags they don't define.examples/math_agent/train_sfc.sh: GSM8K recipe with SFC enabled.Results
GSM8K with Qwen3-0.6B, rollouts on AgentCore Runtime,
train_async.pywith 2 trainer and 2 SGLang GPUs, 64 prompts × 4 samples per rollout, 16 optimizer steps per rollout, lr 1e-6, token-sum aggregation, β₀ = 0.3, K = 8. Rollouts are generated by weights 4 versions behind the trainer (64–79 optimizer steps): slime's async loop gives one version of lag, and an experiment-only hook pushes weight snapshots three versions old. The baseline is ratio-free REINFORCE (−A · log π, same aggregation), matching verl's bypass-modeloss_type=reinforcewithrollout_is=null. One seed per run; runs are still in progress.ppo_kl)As in the verl experiments, REINFORCE collapses under stale rollouts, with responses running to the length limit, while SFC + KL trains steadily. SFC starts slower here because the lag is enforced from step 0: for the first 64 steps the sampler is the initial policy, and the KL anchors the learner to it.
Testing
REINFORCE with async training and staleness=4 collapsed, while SFC is stable.