fix(rl): unbreak MultiSteps + add Hydra integration test for train()#23
Merged
Conversation
train_selfplay's production optimizer wraps the base optax chain in optax.MultiSteps (see checkpoints.CheckpointManager.start). MultiSteps' update path does a tree.map(updates, state.acc_grads). Since PR #6 nnx.pure'd opt_state but left params Param-wrapped, updates (= grads from grad_fn) carried Param nodes that state.acc_grads (now bare) didn't — causing a Custom node type mismatch at trace time. The existing test_trainer_step and test_train_integration both use raw optax.sgd (no MultiSteps wrapper) and never hit this path, so the regression sat latent until the Hydra-composed integration test in the follow-up commit surfaced it. Pure params too so the jit body sees a uniform bare-array pytree. Write the bare results back into the model's Param containers via nnx.replace_by_pure_dict (same pattern league.restore uses), then the existing nnx.update flow merges with non_params and writes to model. Verified by running train_selfplay.py end-to-end on the colab tic_tac_toe profile for 1 iteration — full pipeline succeeds. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
End-to-end test that composes the real config/train_selfplay.yaml (experiment=tic_tac_toe/colab + tiny overrides), then calls train_selfplay.train(...) for 2 iterations. Asserts the loop completes without exception and writes a well-formed checkpoint (metadata.json, state/, league.json, league_states/). Closes the coverage gap called out in #19: the existing test_train_integration.py exercises underlying pieces directly but bypasses Hydra config composition AND the production-shaped optimizer (MultiSteps + adamw + LR schedule). This test catches both surfaces. Runs in <30s on CPU. Patches HydraConfig.get() so the test reaches the train() body without needing a real Hydra runtime context. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Discovers and fixes a latent production regression while closing the orchestrator-coverage gap called out in #19.
The bug
CheckpointManager.start()always wraps the base optimizer in `optax.MultiSteps`. PR #6 nnx.pure'd `opt_state` for optax interop but left `params` Param-wrapped. MultiSteps' update path does `jax.tree.map(updates, state.acc_grads)` — and since `updates = grad_fn(params)` inherits params' Param wrappers while `state.acc_grads` was stripped by `nnx.pure`, the tree shapes don't align. The result: a `ValueError: Custom node type mismatch` at trace time on every `train_selfplay.py` run.This sat latent because:
Confirmed independently by running `train_selfplay.py experiment=tic_tac_toe/colab` against `main` — same crash.
The fix
`nnx.pure` params alongside opt_state so the jit body sees a uniform bare-array pytree across params, grads, and opt_state's accumulators. Write the bare results back into model's Param containers via `nnx.replace_by_pure_dict` (same pattern `league.restore` uses).
`src/jaxpot/rl/trainer.py`, ~15 lines net change.
The test
`tests/test_train_hydra_integration.py` composes the real `config/train_selfplay.yaml` with `experiment=tic_tac_toe/colab` + tiny-scale overrides (16 envs, 2 iters), then calls `train_selfplay.train(...)` end-to-end. Asserts the loop completes and writes a well-formed checkpoint.
Runs in ~10s on CPU. Patches `HydraConfig.get()` to provide an output dir without a real Hydra runtime context.
Test plan
Commits
The fix lands first so the test's regression-gate value is real: without commit 1, commit 2's test would have failed. With both, the test is now a permanent gate against future drift between params/opt_state structures.
🤖 Generated with Claude Code