Skip to content

feat(rollouts): carry a training framework's sampling values to every model call - #5463

Merged
zyzhou5 merged 9 commits into
NVIDIA-NeMo:mainfrom
zyzhou5:feat-rollout-model-sampling
Nov 10, 2026
Merged

zyzhou5 merged 9 commits into
NVIDIA-NeMo:mainfrom
zyzhou5:feat-rollout-model-sampling

Conversation

@zyzhou5

@zyzhou5 zyzhou5 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Gym side of PR 1 in the design doc "RL sampling through Gym": single-agent paths. The RL side is NVIDIA-NeMo/RL#5030.

Problem

  • There is no clean interface for a training framework's sampling values. NeMo RL writes them into each row's responses_create_params, Gym's sampling_overrides pins values on the Model Server, and RL's vLLM checks every call. These paths can collide, and a pin cannot hold separate training and validation values.
  • Writing into the row does not reach every call. Harness agents reject the fields (Coding-agent servers reject generation controls sent by NeMo-RL #4656), CLI harnesses don't send them on every call, and multi-agent rows have no single responses_create_params.
  • Untrained agents cannot keep their own sampling when their calls reach RL's vLLM, whose check rejects anything but the training or validation values.

Where each agent's sampling comes from

Agent Sampling If nothing is set
Trained The training framework's values for the rollout, sent with each rollout. In NeMo RL: policy.generation, and the val_* values for validation Not applicable: the framework always sends values
Untrained, such as an LLM judge Optionally, sampling_overrides on its own Model Server It keeps whatever it sends

How the values travel

  1. The framework puts the values on each row as _ng_model_sampling, next to _ng_rollout_id. The task is unchanged.
  2. The collector copies them into the episode request. Flat rows reach the agent's /run as they are.
  3. The Environment Server adds /ng-sampling/<values> to the trained agent's activation URL.
  4. Gym's URL helpers copy the segment onto every model call the agent makes, including the base URL handed to a harness.
  5. The Model Server handles each call by its segment:
Request from URL What the Model Server does
Trained agent …/ng-sampling/<values>/v1/… Writes the values into the request. The output cap becomes the smaller of the call's own cap and the framework's.
Untrained caller …/ng-sampling/null/v1/… Keeps the caller's values, or applies its own sampling_overrides. It tells the generation server with the header x-ng-sampling: null, so NeMo RL's check skips the call.
No segment …/v1/… Refuses the call (422) if it is a trained agent's Model Server, listed in model_call_sampling.required_model_servers: something dropped the values. A listed name that is not a Model Server of the run fails the run at start-up.

Untrained callers get null without any configuration: in a run that sets model_call_sampling, resources servers (judges, tools), harbor's sidecars and the roles an agent runs but does not train mark their calls. Those roles are tau2's simulated user, pinchbench's and conversational_tool_use_policy_tool_generation's judges, and Hermes's compression helper; an agent marks one by building its URL with trained=False.

Scope

  • Covered: single_agent_turn, the legacy_agent relay and harbor, with any agent, including CLI harnesses. That includes /v1/messages for Claude Code.
  • Agents that build their model URLs with Gym's helpers need no change. Four did not: verifiers_agent, mini_swe_agent and harbor_agent built theirs by hand and now use the helpers, and swe_agents now points OpenHands at the per-run URL whenever the rollout carries values, not only under token capture.
  • Multi-agent environments come in PR 2. Until then, an Environment Server that does not apply the values, such as nemo_user_sim or swe_together, refuses an episode that carries them before it starts (applies_model_sampling).

Gym-only eval sends no values, so no call carries a segment and sampling is unchanged. One URL change: with observability on (the Slurm default), harbor_agent and mini_swe_agent now build their model URLs with Gym's helpers, which add /ng-rollout/<id> as other agents' URLs already do; Gym's Model Servers strip it.

Replaces #4867. Refs #4656.

Validation

  • New tests: tests/unit_tests/core/test_model_call_sampling.py, tests/unit_tests/server_runtime/test_model_sampling.py and the /ng-sampling/ cases in tests/unit_tests/core/test_correlation.py, including an end-to-end test across an agent, a resources server and a Model Server, plus tests in each touched server.
  • pytest tests/unit_tests on main at 50f0932: the 135 tests that fail here (sandbox, orchestration and Harbor task-folder tests that need packages or tools this environment lacks) fail the same way on main; everything else passes.
  • Touched server suites that pass: vllm_model, openai_model, switchyard_model, single_agent_turn, single_agent_turn_legacy, legacy_agent, pinchbench and the harbor environment server, apart from 3 jinja2 tests that also fail on main. The tau2, hermes_agent, conversational_tool_use_policy_tool_generation, verifiers_agent, mini_swe_agent, swe_agents and harbor_agent suites need their own packages, so they run in CI.
  • pre-commit run --files on the changed files: passed.

Rollouts

Validated end to end in NVIDIA-NeMo/RL#5030 at 8d07564. Later changes come from rebases and review fixes, covered by the tests above.

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Nov 2, 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.

@zyzhou5

zyzhou5 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Review concerns from #4867, and where they stand here

#4867 concern Here
The segment must keep rollout routing: Switchyard's session id and path-mounted relays (root_path) Segment after the rollout prefix. Tests: test_sampling_values_keep_the_session_id (Switchyard), test_a_relay_mount_is_kept. A rollout named ng-sampling is not mistaken for a segment. A segment before the rollout prefix (the old layout) gets a 422 instead of being dropped.
resolve_model_base_url overrides and monkeypatches (pi_sandboxed_agent, cvdp, anyterminal) must keep working Signature unchanged. The segment comes from the request context inside apply_rollout_prefix.
The cap must not raise a smaller harness limit min() over every limit name, per endpoint. Test: test_the_cap_never_raises_a_smaller_limit_the_harness_set.
top_p must be > 0 (Yu); max_output_tokens bound; strict types; URL length; duplicate keys; NaN/Infinity All in ModelCallSampling and decode_model_sampling, tested.
The middleware must run before policy admission and capture, and capture must record the applied values Outermost of the Model Server's middleware. Tests run through the real vllm_model app, with and without the checkpoint policy gate, and check the captured request.
The middleware ran on every Model Server and rewrote every body (Ananth) Only calls that carry values are buffered and rewritten. Without a segment, the middleware only matches the path.
A pin silently overrode the URL values (Ananth) Now an error, at startup for required servers and per call otherwise.
A conflict warning on every call (Yu) No warning. Conflicts are errors.
Five agents kept separate allow-lists for request sampling (Ananth) Dropped. Agents no longer take sampling in their requests; the values come with the rollout.
The training guide must state the rule (Ananth) external-agent-harnesses.mdx now says where the values come from with NeMo RL, with Gym on its own, and for untrained callers. A new section, "Send sampling values with each rollout", covers other training loops.
Stale "outermost" comment (Yu) Reworded.
Body rewriting speed orjson.
Test files named after their modules tests/unit_tests/core/test_model_call_sampling.py, tests/unit_tests/server_runtime/test_model_sampling.py.
vLLM token-limit fixes (Ananth asked for a split) Separate PR: #5462.

One deliberate change from #4867: /v1/messages now takes the values (with max_tokens as the cap) instead of returning a 422. Claude Code calls that endpoint, and in a training run its calls must carry the values.

@zyzhou5
zyzhou5 marked this pull request as ready for review November 3, 2026 01:18
@zyzhou5
zyzhou5 force-pushed the feat-rollout-model-sampling branch from 2b79be9 to fe7c39e Compare November 3, 2026 01:36
@zyzhou5

zyzhou5 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test fe7c39e

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

@svcnvidia-nemo-ci svcnvidia-nemo-ci added feature New capabilities, enhancements, or enablement work area:training Training framework integrations and training-data interfaces needs-review PR is ready for code review and waiting on a reviewer labels Nov 3, 2026
@zyzhou5

zyzhou5 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 51189ad

@zyzhou5

zyzhou5 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 478eb64

@zyzhou5
zyzhou5 force-pushed the feat-rollout-model-sampling branch from 478eb64 to 8d07564 Compare November 3, 2026 19:52
@zyzhou5

zyzhou5 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 8d07564

zyzhou5 and others added 9 commits November 10, 2026 20:46
… model call

A training framework that serves the policy needs every model call of the agents
it trains sampled with its own values: its training values, or its validation
values for a validation rollout. NeMo RL writes them into each row's
responses_create_params today. Harness agents reject those fields (NVIDIA-NeMo#4656), CLI
harnesses do not send them on every call, and multi-agent rows have no single
responses_create_params to write into.

The framework now sets the values on each row as _ng_model_sampling, next to
_ng_rollout_id, and leaves the task unchanged. Gym carries them into the URL of
every model call the rollout's agent makes, as /ng-sampling/<values> after any
rollout prefix. The Model Server writes them into the request before policy
admission and capture see it: temperature and top_p replace the call's, and
max_output_tokens caps the call's own limit under each endpoint's name.

- Typed rows: the collector copies the field into the episode request, and
  single_agent_turn and harbor add the segment to the agent's activation URL.
  single_agent_turn_legacy reads it from the flat row.
- Flat rows: the agent reads it from its /run body.
- Agents: an inbound segment becomes the request's, and Gym's URL helpers and
  ServerClient put it on every model URL the agent builds.
- In a run that sets the model_call_sampling block, resources servers send
  /ng-sampling/null with their model calls, so a verifier's LLM judge keeps its
  own sampling or its Model Server's sampling_overrides. vllm_model forwards
  null to its generation server as the header x-ng-sampling: null.
- A Model Server in model_call_sampling.required_model_servers refuses a
  generation call without a segment and cannot set sampling_overrides.

Gym-only evaluation sets neither the field nor the block, so its URLs and
requests are unchanged.

Replaces NVIDIA-NeMo#4867.

Signed-off-by: Zeyu Zhou <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Harbor's compose sidecars, such as a simulated user, call a model server
through URLs that harbor builds for them. They are never the agent being
trained, so in a run that sets model_call_sampling their URLs now carry
/ng-sampling/null. Their calls keep their own sampling instead of failing
policy_model's missing-segment check or the training framework's check on
the policy's values.

Signed-off-by: Zeyu Zhou <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… URLs

verifiers_agent, mini_swe_agent and harbor_agent build their model URLs
without Gym's URL helpers. In a training run, their calls therefore reached
the trained agent's model server without the rollout's sampling values and
were refused. NeMo RL's QA distillation recipe runs verifiers_agent this way.

- verifiers_agent uses its per-rollout client copy whenever the rollout
  carries values, not only when capture is on. The shared client's URL,
  built once for every rollout, still carries none.
- mini_swe_agent builds its model URL with base_url_for_run, as
  mini_swe_agent_2 already does. A caller's policy_base_url is still used
  as given.
- harbor_agent builds its model URL with base_url_for_run.

With observability or capture on, mini_swe_agent's and harbor_agent's calls
now also carry the rollout prefix, so they appear in per-rollout capture.

Signed-off-by: Zeyu Zhou <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e reports

NeMo RL's score centering pins logprobs and top_logprobs on policy_model, which
a training run lists as required, so score-centering runs failed at startup.
These fields change what the engine reports, not how it samples: both the
startup refusal and the per-call refusal now ignore them.

A real pin conflict is a configuration error, so the per-call refusal answers
409 instead of a plain error that surfaced as a retryable 500. A test also
checks that a null call's x-ng-sampling header reaches the HTTP request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
ServerClient gave the current segment only to calls aimed at a model server, so
an agent calling its own /v1/responses (langgraph, haystack, finance_agent)
started a request without it, and its model calls got a 422 from a required
policy_model. Generation calls to agents now carry it too; /run does not.

apply_rollout_prefix now keeps an explicitly passed segment even with
trace_context=False, as its docstring said. CallerSamplingMiddleware declares
applies_to_mcp_requests, so a resources server that exposes tools over MCP
starts in a training run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
…carries sampling

OpenHands ignores llm.base_url and reads the model server from the global config
unless the overlay redirects it, and the overlay was installed only under token
capture. With capture off, as in NeMo RL's SWE2 recipes, every policy call lost
the sampling segment and got a 422. Gym-only evaluation, which carries no
segment, keeps the bare URL.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
An agent's URL helpers gave every model URL it built the run's segment, so a
role the agent runs besides the one a training framework trains sampled with
the framework's values: tau2's simulated user, pinchbench's judge (when
judge_model_server is set) and conversational_tool_use_policy_tool_generation's
judge. On a Model Server with its own sampling_overrides, every such call
failed with 409.

url_path_for_run, base_url_for_run and resolve_model_base_url take trained=False
for such a role, which then gets null wherever the run carries a segment, and
those three agents pass it. A run without values is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
…t ignore them

An episode request carries a training framework's sampling values in
model_sampling, but only single_agent_turn (and its legacy variant) and harbor
put them in the trained agent's URLs. Every other environment server accepted
them and ran its agents without them: the agent the framework trains then got a
422 from its required Model Server, which agent sessions turn into failed, empty
turns, so an RL run continued, or failed much later with an unrelated error.

Environment servers now declare applies_model_sampling. An episode that carries
values is refused at admission, terminally, by a server that does not declare
it; Gym-only evaluation sends no values and is unaffected.
single_agent_turn and harbor declare it.

model_sampling_segment gives one participant's segment value: the episode's
values for a participant the framework trains, null for every other one, none
without values. single_agent_turn and harbor use it for their agent's path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
A trained agent's server may still pin logprobs and top_logprobs. The null
header is sent on Chat Completions requests. Only model calls a resources server
makes through Gym's server client while handling a request are marked null;
judges with their own clients and agent-driven untrained roles are not yet. The
middleware comment says where it runs instead of calling it outermost.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zeyu Zhou <[email protected]>
@zyzhou5

zyzhou5 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 2a3a42b

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/42902094007

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/42905415488

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/42908308447

@ananthsub

Copy link
Copy Markdown
Contributor

Two follow-ups on the per-rollout sampling values, both reproduced on main at b02f8d0.

Collating a dataset that declares a taskset drops _ng_model_sampling. _collate_samples_single_type turns each row into a task with materialize_task, which leaves every _ng_ key out of task_input. It then copies back only the task index, rollout index, rollout id and attempt index, so the prepared row loses _ng_model_sampling and _episode_request_body sends no model_sampling. NVIDIA-NeMo/RL#5030 is not affected, because it adds the values to rows that were already prepared. A caller that adds them before preparation would lose them, as #5958 proposes for gym eval. Please add MODEL_SAMPLING_KEY_NAME to the keys copied back after materialize_task, with a test that collates a taskset dataset and checks _episode_request_body.

A redirect drops the /ng-sampling/ segment. ModelSamplingMiddleware and ModelSamplingContextMiddleware remove the segment before routing, so a redirect such as FastAPI's 307 from /v1/responses/ to /v1/responses points at a path without it. The capture prefix and /ng-trace/ are restored on redirects (_preserve_capture_prefix_on_redirect, _restore_trace_segment_on_redirect), but the sampling segment is not. With token capture on, a POST to /ng-rollout/3-0/ng-agent-instance/policy/training-token-capture/ng-sampling/<values>/v1/responses/ redirects to /ng-rollout/3-0/ng-agent-instance/policy/training-token-capture/v1/responses. On a Model Server in required_model_servers, the follow-up gets a 422 saying the call carries no segment. On any other Model Server it runs with the caller's own values and no error. Gym's URL helpers never add a trailing slash, so only an external harness or proxy that does would hit this. Please have both middlewares put the segment back into a same-origin Location the way _restore_trace_segment_on_redirect does, with tests for a required Model Server, one that is not required, and an agent server.

This branch was previously deployed

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

Labels

area:training Training framework integrations and training-data interfaces feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants