[Prototype] Multi-turn GRPO for blackjack with OpenEnv #603
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.
Results
This PR adapts the GRPO pipeline to support multi-turn RL environments using Blackjack as a testbed.
Rollout example (green characters are marked for training):

Win rate (best theoretical win rate in blackjack is ~30-40%, so this is expected)

Key Changes:
TokenAccumulator: Utility for multi-turn token management. This is where i spend a lot of time to ensure correctness.NOTE: set
thinking=Falseto disable thinking in qwenBlackjackEnv: Lightweight wrapper over OpenEnv/OpenSpiel. Handles action parsing, observation formatting, and reward computation. Requires OpenEnv patches to expose game metadata (player total, dealer card).Token masking:Training mask is now computed from TokenAccumulator.response_mask. Since it is multi turn, we cannot just slice anymore, e.g.all_tokens[:response_len], we have to doall_tokens[loss_mask].Serial rollouts- Games execute serially (do_single_rollout) instead of parallel batched generation. Each game runs until done or truncated. TODO: enable better parallelism.Filtering- Drops groups with zero reward variance (can't compute advantages) and episodes with truncated responses (incomplete game trajectories).Loss debugging- Added extensive logging to simple_grpo_loss for debugging numerical issues. Should be cleaned up in future iteration.OpenEnv Patches
Python script to apply the patch is untested but you should be able to copy/paste your changes with easy.
Instructions
See apps/blackjack/openenv_patch/README.md for details.