Repository navigation
feat(checkpoint): export Blackjack game state for partial-rollout checkpoints - #3896
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
cf53406 to
b25ea94
Compare
6f93202 to
8b4bb07
Compare
b25ea94 to
c1d3223
Compare
8b4bb07 to
930c857
Compare
c1d3223 to
8cee79e
Compare
492a18b to
9a99133
Compare
8cee79e to
dc25a06
Compare
9a99133 to
eb5f7f4
Compare
01cfa55 to
c3fbf5f
Compare
eb5f7f4 to
f0c480a
Compare
c3fbf5f to
6bd5512
Compare
f0c480a to
72115fd
Compare
6bd5512 to
c9f11b1
Compare
72115fd to
7a09ddc
Compare
69a7f74 to
da0629a
Compare
9a9592c to
b5bdf33
Compare
620084f to
b813fdd
Compare
b5bdf33 to
b4bfe35
Compare
b813fdd to
936a132
Compare
b4bfe35 to
4d51770
Compare
936a132 to
e8f442b
Compare
2dd08b3 to
6bccbb0
Compare
e8f442b to
185993d
Compare
6bccbb0 to
4b8212a
Compare
185993d to
40d8ba5
Compare
4b8212a to
8a0c514
Compare
19d9fd8 to
ad43688
Compare
15bfbc3 to
cb18c76
Compare
ad43688 to
31ee9be
Compare
…r setup GymnasiumServer built its own FastAPI app and copied part of the shared resources server setup, so it never served /reverify_mode and, with checkpointing on, never installed the checkpoint participant. Split SimpleResourcesServer.setup_webserver into create_resources_app (middleware, checkpoint participant, /aggregate_metrics, /reverify_mode) and register_protocol_routes (/seed_session, /close_session, /verify). GymnasiumServer now overrides only the protocol routes with /reset and /step. GymnasiumServer declares /reset in checkpoint_session_starts, so a reset passes the same checkpoint admission boundary as /seed_session. A terminal /step reports the session's end through checkpoint_session_ended, as does OpenAir's optional /close. Gymnasium servers stay restart_only unless a subclass declares checkpoint_mode = "exported"; the base then exports and restores session_state per session through the overridable export_env_state and restore_env_state hooks. Signed-off-by: Ananth Subramaniam <[email protected]>
…ckpoints Blackjack declares checkpoint_mode = "exported". Each session's two hands and its random number generator state decide the rest of the game, so export_env_state writes them as JSON and restore_env_state validates them and rebuilds the generator. A restored game deals the same cards and ends with the same outcome as an uncheckpointed game with the same seed. Ported from #2946 onto the v2 checkpoint hooks. Signed-off-by: Ananth Subramaniam <[email protected]>
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/39395422621 |
|
Rollout evidence rerun on vLLM 0.29.0, the version Gym's The tree was Checkpointing off. 100 Blackjack games gave a mean reward of -0.07: 45 wins, 3 draws, 28 losses and 24 busts. Blackjack deals from an unseeded random number generator, so only the outcome distributions can be compared. A chi-square test gives p = 0.44 and p = 0.57 against the earlier Checkpoint, crash and restore. The same driver as in the description, built on
At the end, every participant was idle. The only tracebacks are in the agent log at the moment of the kill, where the agent was still waiting on the killed policy model. |
ananthsub/partial-ckpt-blackjackananthsub/partial-ckpt-gymnasium(fix(gymnasium): build Gymnasium servers on the shared resources server setup #3895, which targetsmain)feature,area:env-infraWhat changed and why
Blackjack is the first Gymnasium environment to opt in to checkpointing.
Its resources server now exports a live game, so an agent that can continue an episode can resume the game after a crash instead of dealing again.
gymnasium_agent, the agent Blackjack runs with today, declares no checkpoint session support, so it takes part as restart-only and a game a checkpoint catches still starts over from its input.The export becomes useful end to end once that agent can continue an episode.
The rest of a Blackjack game depends only on the player's hand, the dealer's hand, and the session's random number generator. Blackjack sets
checkpoint_mode = "exported"and overrides the two Gymnasium hooks:export_env_statewrites both hands and the generator state (random.Random.getstate()) as JSON.restore_env_statechecks that the hands are lists of card ranks, rebuilds the generator withsetstate, and raisesValueErrorfor any malformed state. Nothing is installed if any session is invalid.The Gymnasium base handles everything else: tracking sessions from
/resetto the terminal/step, exporting every live session at commit, and closing a retired session./stepcan reach the server; until then the retire's mark refuses it withstale_attempt. Blackjack answers a step for a session with no game with 404, instead of crashing on an empty hand. The same crash exists onmain.How it works
Where this PR sits in the overall flow
The highlighted part is what this PR adds.
flowchart LR C["Controller<br/>NeMo RL, or rollout collection"] CO["Coordination<br/>prepare, commit, restore, resume,<br/>retire, forget"] K["Control plane on every server<br/>phases, lease, storage,<br/>retire: refuse, stop, free;<br/>forget"] subgraph G["One participant per Gym server"] E["Environment server<br/>episode steps"] M["Policy model<br/>held responses, generation cuts"] A["Agent<br/>sessions parked at boundaries"] R["Resources server<br/>session state"] end W["Inference worker<br/>stages cut prefixes"] D[("Checkpoint directory<br/>records, then manifest")] L[("Capture ledger<br/>retire and delete from #3938, #3939")] C --> CO --> K K --> E & M & A & R M --> W M --> L G --> D classDef this fill:#fde68a,stroke:#b45309,stroke-width:2px,color:#1f2937 class R thisOne checkpoint, a crash, and the restore, end to end:
This PR
The game state that decides the rest of a hand, saved at a checkpoint and restored into a fresh server.
Where this sits in the stack
This PR uses the resources server checkpoint participant from #3886, which has merged, and needs nothing else from the open part of the partial-rollout checkpointing stack. It is one of five PRs that port the environments the old stack checkpointed onto the new hooks. Workplace Assistant and the Gymnasium fix target
main, and Blackjack builds on the Gymnasium fix. Indirect prompt injection and proof refinement are still based on the end-to-end suite (#3889):ananthsub/partial-ckpt-workplace-assistant): feat(checkpoint): checkpoint Workplace Assistant sessionsananthsub/partial-ckpt-gymnasium): fix(gymnasium): build Gymnasium servers on the shared resources server setupananthsub/partial-ckpt-blackjack): feat(checkpoint): export Blackjack game state for partial-rollout checkpoints (on the Gymnasium PR) (this PR)ananthsub/partial-ckpt-indirect-prompt-injection): feat(checkpoint): export indirect prompt injection session stateananthsub/partial-ckpt-proof-refinement): feat(checkpoint): continue proof refinement agent turns from their boundariesRelationship to the old stack
Ported from #2946 onto the v2 checkpoint hooks. The old version serialized the same three fields through the Gymnasium base's
serialize_session_stateanddeserialize_session_state, keyed by rollout attempt. Here the state is keyed by the cookie session ID, and restore also validates the hands and turns a bad generator state into a clear error.Issue
No separate issue. This is the Blackjack part of the partial-rollout checkpointing port.
Validation
Run on this branch, on the current
main:RAY_TMPDIR=/tmp python -m pytest -q resources_servers/blackjack/tests: passed. The new tests drive a checkpoint-enabled server through its control routes:RAY_TMPDIR=/tmp python -m pytest -q tests/unit_tests/test_checkpoint_*.py resources_servers/gymnasium/tests responses_api_agents/gymnasium_agent/tests: passed.ruff check,ruff format, andpre-commit run --fileson the changed files.Rollout evidence
Setup
The model was
Qwen/Qwen3-4B-Instruct-2507, served by vLLM 0.24.0 with thehermestool-call parser on one NVIDIA RTX 6000 Ada Generation (48 GB).Gym's
vllm_modelserver ran withuses_reasoning_parser=false, because the served model is an instruct model without a reasoning parser.Every rollout sampled at temperature 1.0.
Each tree ran in its own worktree, with server venvs created by
gym env start.Blackjack with checkpointing off
Blackjack ran through
gymnasium_agentwithresources_servers/blackjack/configs/blackjack.yamland its example data, onmainat717c04a3b3, on #3895, and with this change on #3895:gym env start --config resources_servers/blackjack/configs/blackjack.yaml --model-type vllm_model \ --model Qwen/Qwen3-4B-Instruct-2507 --model-url http://127.0.0.1:8100/v1 --model-api-key dummy \ ++policy_model.responses_api_models.vllm_model.uses_reasoning_parser=false gym eval run --no-serve --config resources_servers/blackjack/configs/blackjack.yaml --model-type vllm_model \ --model Qwen/Qwen3-4B-Instruct-2507 --model-url http://127.0.0.1:8100/v1 --model-api-key dummy \ ++policy_model.responses_api_models.vllm_model.uses_reasoning_parser=false \ --agent blackjack_gymnasium_agent --input resources_servers/blackjack/data/example.jsonl \ --output rollouts.jsonl --num-repeats 20 --concurrency 16 --temperature 1.0mainBlackjack deals from an unseeded random number generator, so each run plays different hands and only the distributions can be compared.
A chi-square test on the four outcomes gives p = 0.25 against
mainand p = 0.18 against #3895, so the runs are consistent with one another.A game takes one to four model turns in every run.
Every rollout ended with
terminatedset and no failure, and the server logs show no tracebacks or error responses.Blackjack with checkpointing on
gymnasium_agentdeclares no checkpoint session support, so it takes part in a checkpoint as a restart-only agent.A Blackjack game that a checkpoint catches mid-play is therefore a restart: after a crash it starts over from its input, and the game state this change exports is not used end to end yet.
The run below checks that behavior with real server processes.
The tree was the checkpoint stack (
ananthsub/partial-ckpt-e2e) with #3895 and this change cherry-picked on top; both applied without conflicts.A driver built on the process-level checkpoint suite (
tests/e2e/checkpoint/checkpoint_deployment.py) started the policy model, the Blackjack resources server,gymnasium_agentand the flat-row wrapper, each as its own process from its own venv.It sent 120 games to the wrapper's
/runand drove the checkpoint throughnemo_gym._checkpoint.coordination, as a training controller would.The Blackjack server held a dealt game for each of the 66.
gymnasium_agentreported all 66 as restarts.The policy model held their 66 model calls without reporting a blocker, and the Blackjack server reported no request in flight.
The Blackjack server's commit wrote no record, although it held 66 live sessions, and the agent and policy model wrote none either.
SIGKILL, fresh processes were started from the same configs, and restore and resume ran with the empty scope.Every participant then reported them as retired, and the Blackjack server held no sessions.
A
/runof a retired attempt sent straight to the agent was refused with HTTP 409stale_attempt.All returned HTTP 200 and ended with
terminatedset: 25 wins, 7 draws, 26 losses and 8 busts, for a mean reward of -0.14.The 54 games that finished before the checkpoint had a mean of -0.26, in the range of the uninterrupted runs above.
At the end, every participant was idle and the Blackjack server held no sessions.
While the checkpoint was open, the 66 restarted games did not advance: the policy model held their undelivered model responses until resume, as it does for every call, so none of them finished in the three seconds before the crash.
The agent log shows five truncated model responses at the moment of the kill, which come from killing the policy model while the agent still waited on it.
What is not covered
gymnasium_agentrestarts every game a checkpoint catches.The restored-state equivalence is covered by this change's unit test instead: a restored game deals the same cards and ends with the same outcome as an uncheckpointed game with the same seed.
Unit tests cover restore of malformed and invalid state and the release of a retired session.
Compatibility
Through
gymnasium_agent, which is restart-only, a game a checkpoint catches is still a restart and starts over after a crash.