Skip to content

refactor(dynamo): move native token response handling to Gym - #3763

Open
jthomson04 wants to merge 6 commits into
NVIDIA-NeMo:mainfrom
jthomson04:jthomson04/dynamo-gym-token-response-cleanup
Open

jthomson04 wants to merge 6 commits into
NVIDIA-NeMo:mainfrom
jthomson04:jthomson04/dynamo-gym-token-response-cleanup

Conversation

@jthomson04

@jthomson04 jthomson04 commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Move native Dynamo response-token interpretation from the NeMo-RL token wrapper into NeMo-Gym.

  • Gym config: for the Dynamo backend, configure the Gym policy model to request nvext.engine_data and set derive_prefix_token_ids_from_messages: true.
    • Existing extra_body, nvext and extra_fields values are kept, and the request field is added once.
    • A null extra_body or nvext is treated as empty. A non-mapping or non-list value raises at setup.
  • Wrapper:
    • NeMo-RL keeps exact prompt rendering, prefix splicing, metadata stripping and nvext.token_data request preparation.
    • The wrapper uses Gym's required_prefix_token_ids instead of deriving its own.
    • It applies Gym's newest-assistant rule: a tokenized newest assistant message requires a prefix, and a prefix requires one. Older tokenized turns are ignored.
  • Responses: successful Dynamo responses are returned unchanged. Gym validates the engine data, builds the training token bundle, and strips nvext.engine_data and choice.logprobs.
  • Tests: a 2-GPU Dynamo + Gym GRPO smoke, tests/functional/grpo_dynamo_gym.sh, is added to L1_Functional_Tests_Dynamo.sh.
  • Docs: the Dynamo design document and the Dynamo upgrade checklist are updated.

Dependency

This PR depends on NeMo-Gym PR 1784, which stacks on NeMo-Gym PR 2836. The Gym submodule is pinned to PR 1784's exact head, 2a80b8dfefd1e40a32a6fc55cad19199bb64daf4, so CI tests both sides together. That head is based on Gym main at 75bb53e6.

Before this PR merges, the submodule must move to the exact Gym main commit that contains both PR 2836 and PR 1784.

Gym contract

  • Opt-in derivation: Gym derives required_prefix_token_ids only when derive_prefix_token_ids_from_messages is true, and this PR sets it for backend: dynamo. Gym config validation requires return_token_id_information: true and rejects the flag together with supply_prefix_token_ids or use_completions_api.
  • Newest assistant only: Gym reads only the newest assistant message. If it has no token bundle, Gym sends no prefix and the wrapper renders the whole prompt.
  • Missing engine data: when the request asks for engine_data and the response lacks it, Gym raises instead of falling back to /tokenize. Message-level token metadata is cross-checked against the engine data, including log probabilities.

Compatibility

  • The request wire fields remain nvext.token_data and nvext.extra_fields=["engine_data"].
  • Dynamo is unchanged.
  • This PR leaves the vLLM backend's Gym config unchanged. perf(vllm): optimize native HTTP request processing #3892 enables the same Gym flag for the native vLLM server.
  • The cleaned-up wrapper requires the updated Gym implementation.
  • uv.lock changes because of the Gym bump:
    • aiohttp splits into 3.14.5 (Gym needs >=3.14.4) and 3.14.3 (kept for sglang).
    • pyjwt moves from 2.14.0 to 2.15.1.
    • Gym moved mlflow, wandb, gitpython, pyarrow and sqlparse into optional extras.

Validation

  • NEMO_RL_PY_EXECUTABLES_SYSTEM=1 .venv/bin/python -m pytest tests/unit/models/generation/test_dynamo_token_wrapper.py tests/unit/models/generation/test_swe1_dynamo_config.py tests/unit/environments/test_nemo_gym.py tests/unit/environments/test_nemo_gym_utils.py -k "dynamo or setup_nemo_gym_config" (33 passed)
  • NEMO_RL_PY_EXECUTABLES_SYSTEM=1 .venv/bin/python -m pytest tests/unit/test_config_validation.py tests/unit/utils/test_outdated_config_checks.py (1161 passed, 350 skipped)
  • NEMO_RL_PY_EXECUTABLES_SYSTEM=1 .venv/bin/python -m pytest tests/unit/environments/test_nemo_gym_token_capture.py tests/unit/environments/test_nemo_gym.py tests/unit/environments/test_nemo_gym_utils.py -m nemo_gym (28 passed). test_nemo_gym_sanity and test_vllm_http_logprobs_contract need GPUs and were not run locally.
  • pre-commit run --files passes on all touched files.
  • NeMo-Gym PR 1784 CI is green at the pinned Gym SHA.

The managed Dynamo + Gym round trip is covered by tests/functional/grpo_dynamo_gym.sh in L1_Functional_Tests_Dynamo. It runs in full mode (L1) and is skipped under FAST=1 (Lfast).

🤖 Generated with Claude Code

@jthomson04
jthomson04 requested review from a team as code owners August 21, 2026 23:08
@copy-pr-bot

copy-pr-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Aug 21, 2026
@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test 05a5360

@github-actions

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: 05a5360 (PR #3763 from jthomson04/dynamo-gym-token-response-cleanup)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

@jthomson04 jthomson04 added the CI:L1 Run doctests, unit tests, and functional tests label Aug 21, 2026
@jthomson04
jthomson04 force-pushed the jthomson04/dynamo-gym-token-response-cleanup branch from 05a5360 to 245575e Compare August 22, 2026 23:00
@copy-pr-bot

copy-pr-bot Bot commented Aug 22, 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.

@github-actions

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: 245575e (PR #3763 from jthomson04/dynamo-gym-token-response-cleanup)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test 245575e

@jthomson04 jthomson04 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Team review of the Dynamo→Gym response-handling move: 6 agents (RL expert, Gym submodule expert, bug finder, design reviewer, test agent, devil's advocate), 18 findings raised, 7 surviving after an adversarial pass.

The refactor itself is a clear win and worth saying so. Gym's replacement isn't just a relocation of _validate_engine_data — it cross-checks the Dynamo bundle against message-level metadata and against vLLM's own response token IDs, neither of which the wrapper ever did. Concrete payoff: adding routed_experts to the native bundle used to mean editing two repos; now it's one entry in _DYNAMO_ENGINE_DATA_FIELD_MAPPING.

The one item I'd genuinely act on before merge is the test-discrimination gap — the suite currently passes with the deleted derivation restored. Everything else is low-severity or documentation.

pre-commit run --all-files is clean. Two items were deliberately not filed here: the missing-engine_data fallback belongs on NeMo-Gym PR 1784, and the "replace the overloaded engine_data flag with a named derive_required_prefix_token_ids boolean" idea belongs in a tracking issue against Gym.

Three findings were dropped by the adversarial pass and are worth naming so they don't get re-raised later: an assert on the hardcoded vllm_model key (the fix would break grpo_nanov3.yaml, and vllm_model_for_training.yaml pins both names anyway), a comment about OmegaConf list replacement (unreachable), and routed_experts not being stripped (router replay is rejected outright on the Dynamo backend).

Generated by Claude Code

Comment thread 3rdparty/Gym-workspace/Gym Resolved
Comment thread docs/design-docs/dynamo-integration.md Resolved
Comment thread nemo_rl/environments/nemo_gym.py Resolved
Comment thread nemo_rl/environments/nemo_gym.py Resolved
@github-actions

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: d3a007e (PR #3763 from jthomson04/dynamo-gym-token-response-cleanup)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test d3a007e

@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test 34cf872

@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: 34cf872 (PR #3763 from jthomson04/dynamo-gym-token-response-cleanup)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

jthomson04 and others added 4 commits October 12, 2026 09:36
Main renamed dtensor_cfg to automodel_cfg and removed _v2, so the
outdated-config check rejected grpo_dynamo_gym_smoke.yaml at startup.
Main's setup_nemo_gym_config backend test also built a config without
env, which the Dynamo branch now writes to.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Gym PR 1784 now stacks on Gym PR 2836. Gym derives
required_prefix_token_ids only when derive_prefix_token_ids_from_messages
is set, and only from the newest assistant message.

- Pin Gym to 2a80b8dfefd1e40a32a6fc55cad19199bb64daf4 and relock.
- Set derive_prefix_token_ids_from_messages for the Dynamo backend next
  to the engine_data request.
- Make the token wrapper read only the newest assistant message, so it
  agrees with Gym about when a prefix must be present.
- Name the Gym flag in the missing-prefix error.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
grpo_dynamo_gym.sh repeats the Dynamo and vLLM version and backport
assertions from grpo_dynamo.sh, so a Dynamo upgrade must update both.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jthomson04
jthomson04 force-pushed the jthomson04/dynamo-gym-token-response-cleanup branch from 34cf872 to 4ea2d1d Compare October 12, 2026 17:22
@jthomson04

Copy link
Copy Markdown
Contributor Author

/ok to test 4ea2d1d

@github-actions

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: 4ea2d1d (PR #3763 from jthomson04/dynamo-gym-token-response-cleanup)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

jthomson04 and others added 2 commits October 12, 2026 11:26
The Dynamo setup built the policy_model overlay with a setdefault chain,
so `policy_model: null` (or `nemo_gym: null`) raised AttributeError, and
a sharded job without a top-level policy_model got an overlay that no
shard claimed, which parse_shard_plan rejects.

Build the overlay with _gym_policy_model_overlay, which treats null
levels as empty, rejects non-mapping levels, and writes to
common_overrides for an unclaimed sharded job so every shard gets it.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sibling PRs add their own keys to the same Gym vllm_model overlay, so
whole-overlay equality checks would break when they land. Assert the
keys this change owns, the preserved user keys, and that other servers
stay unchanged. The vLLM-backend test no longer assumes the overlay is
untouched; it checks that no Dynamo extra_body was added.

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

Copy link
Copy Markdown
Contributor Author

/ok to test 3001240

jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 12, 2026
Move the shard-aware lookup of the Gym policy model's vllm_model overlay
into _gym_policy_model_overlay, byte-identical to the copy that NVIDIA-NeMo#3856 and
NVIDIA-NeMo#3763 add, so the three PRs merge onto one helper.
_enable_gym_prefix_derivation now only decides the flag.

The helper treats a null overlay level as empty and rejects a non-mapping
one with a ValueError. Before, an unsharded policy_model: null (or a null
nested level) failed with an AttributeError.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 12, 2026
The dynamo case asserted that setup_nemo_gym_config leaves env.nemo_gym
untouched. The Dynamo setup in NVIDIA-NeMo#3856 and NVIDIA-NeMo#3763 writes its own policy model
overlay there, and NVIDIA-NeMo#3763 sets derive_prefix_token_ids_from_messages for
Dynamo too, so neither that assertion nor a narrower one on the flag would
survive those PRs. Pin only megatron, whose Gym overlay nothing touches.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: 3001240 (PR #3763 from jthomson04/dynamo-gym-token-response-cleanup)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 13, 2026
Move the shard-aware lookup of the Gym policy model's vllm_model overlay
into _gym_policy_model_overlay, byte-identical to the copy that NVIDIA-NeMo#3856 and
NVIDIA-NeMo#3763 add, so the three PRs merge onto one helper.
_enable_gym_prefix_derivation now only decides the flag.

The helper treats a null overlay level as empty and rejects a non-mapping
one with a ValueError. Before, an unsharded policy_model: null (or a null
nested level) failed with an AttributeError.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 13, 2026
The dynamo case asserted that setup_nemo_gym_config leaves env.nemo_gym
untouched. The Dynamo setup in NVIDIA-NeMo#3856 and NVIDIA-NeMo#3763 writes its own policy model
overlay there, and NVIDIA-NeMo#3763 sets derive_prefix_token_ids_from_messages for
Dynamo too, so neither that assertion nor a narrower one on the flag would
survive those PRs. Pin only megatron, whose Gym overlay nothing touches.

Signed-off-by: jthomson04 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@terrykong terrykong removed the Documentation Improvements or additions to documentation label Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants