Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Play 2048 with GRPO","local":"play-2048-with-grpo","sections":[{"title":"1. Install dependencies","local":"1-install-dependencies","sections":[],"depth":2},{"title":"2. Start the 2048 server","local":"2-start-the-2048-server","sections":[],"depth":2},{"title":"3. Define the environment class","local":"3-define-the-environment-class","sections":[],"depth":2},{"title":"4. Train with GRPO","local":"4-train-with-grpo","sections":[],"depth":2},{"title":"5. Save the model","local":"5-save-the-model","sections":[],"depth":2},{"title":"Next steps","local":"next-steps","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/openenv/main/en/_app/immutable/entry/start.BVsaIFPz.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/chunks/DOr2Gjb3.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/chunks/DfHjNWj2.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/entry/app.Bi0HOH_m.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/chunks/Ba1drWuH.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/chunks/Cs50_Xpk.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/chunks/PGLvW8OU.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/nodes/0.uj0bKvXN.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/chunks/CFswWuIO.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/nodes/20.B8UbdS5G.js" rel="modulepreload"> | |
| <link href="/docs/openenv/main/en/_app/immutable/chunks/DCZF09PK.js" rel="modulepreload"> | |
| <!--1ekap7h--><meta name="hf:doc:metadata" content="{"title":"Play 2048 with GRPO","local":"play-2048-with-grpo","sections":[{"title":"1. Install dependencies","local":"1-install-dependencies","sections":[],"depth":2},{"title":"2. Start the 2048 server","local":"2-start-the-2048-server","sections":[],"depth":2},{"title":"3. Define the environment class","local":"3-define-the-environment-class","sections":[],"depth":2},{"title":"4. Train with GRPO","local":"4-train-with-grpo","sections":[],"depth":2},{"title":"5. Save the model","local":"5-save-the-model","sections":[],"depth":2},{"title":"Next steps","local":"next-steps","sections":[],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/openenv/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="play-2048-with-grpo" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#play-2048-with-grpo"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>Play 2048 with GRPO</span></h1><!--]--><!----> <p>Train a language model to play 2048 with GRPO. The game runs in the OpenSpiel environment, and TRL’s <code>GRPOTrainer</code> drives it through <code>environment_factory</code>: the model plays by calling a <code>move</code> tool, and the game score is the reward.</p> <p>The recipe follows TRL’s <a href="https://github.com/huggingface/trl/tree/main/examples/grpo_2048" rel="nofollow"><code>examples/grpo_2048</code></a>, which implements the game in plain Python. Here the game logic lives in an OpenEnv server instead.</p> <blockquote class="tip"><p>Unsloth maintains its own <a href="https://github.com/unslothai/notebooks/blob/main/nb/OpenEnv_gpt_oss_%2820B%29_Reinforcement_Learning_2048_Game.ipynb" rel="nofollow">2048 tutorial with OpenEnv</a>, which trains <code>gpt-oss-20b</code> with Unsloth on the same OpenSpiel server to write a 2048 strategy instead of playing move by move.</p></blockquote> <blockquote class="note"><p><strong>Difficulty</strong>: Advanced | <strong>GPU Required</strong>: Yes</p></blockquote> <!--[1--><h2 class="relative group"><a id="1-install-dependencies" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1-install-dependencies"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>1. Install dependencies</span></h2><!--]--><!----> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!---->!pip install -Uq <span class="hljs-string">"trl[peft]"</span> trackio open_spiel | |
| !pip install -q <span class="hljs-string">"openenv-openspiel-env @ git+https://github.com/huggingface/OpenEnv.git#subdirectory=envs/openspiel_env"</span><!----></pre></div><!----> <p><code>open_spiel</code> ships the game engine (wheels for Linux and macOS). <code>openenv-openspiel-env</code> installs the client (<code>OpenSpielEnv</code>) and the server.</p> <!--[1--><h2 class="relative group"><a id="2-start-the-2048-server" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2-start-the-2048-server"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>2. Start the 2048 server</span></h2><!--]--><!----> <p>An OpenSpiel server serves one game, chosen with the <code>OPENSPIEL_GAME</code> variable when it starts. Run one locally for 2048:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> os, subprocess, sys, time | |
| <span class="hljs-keyword">import</span> requests | |
| server = subprocess.Popen( | |
| [sys.executable, <span class="hljs-string">"-m"</span>, <span class="hljs-string">"uvicorn"</span>, <span class="hljs-string">"openspiel_env.server.app:app"</span>, <span class="hljs-string">"--port"</span>, <span class="hljs-string">"8000"</span>], | |
| env={**os.environ, <span class="hljs-string">"OPENSPIEL_GAME"</span>: <span class="hljs-string">"2048"</span>}, | |
| ) | |
| <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">60</span>): | |
| <span class="hljs-keyword">try</span>: | |
| <span class="hljs-keyword">if</span> requests.get(<span class="hljs-string">"http://localhost:8000/health"</span>).ok: | |
| <span class="hljs-keyword">break</span> | |
| <span class="hljs-keyword">except</span> requests.ConnectionError: | |
| time.sleep(<span class="hljs-number">1</span>)<!----></pre></div><!----> <p>The hosted <a href="https://huggingface.co/spaces/openenv/openspiel_env" rel="nofollow"><code>openenv/openspiel_env</code></a> Space serves Catch. To host 2048 instead, duplicate it, set the <code>OPENSPIEL_GAME=2048</code> variable in the Space settings, and use its <code>https://<user>-openspiel-env.hf.space</code> URL as <code>base_url</code> below.</p> <p>The server accepts 8 concurrent sessions by default (<code>MAX_CONCURRENT_ENVS</code>), one per rollout in a generation batch with the settings below.</p> <!--[1--><h2 class="relative group"><a id="3-define-the-environment-class" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3-define-the-environment-class"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>3. Define the environment class</span></h2><!--]--><!----> <p><code>environment_factory</code> takes a class the trainer instantiates once per rollout. Its public methods with docstrings become tools, <code>reset()</code> returns the start of the conversation, and <code>get_reward()</code> scores the finished rollout.</p> <p>OpenSpiel’s 2048 observation is the board as 16 tile values (<code>info_state</code>), the step reward is the points gained by merging tiles, and the actions are <code>0</code> (up), <code>1</code> (right), <code>2</code> (down) and <code>3</code> (left).</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> openspiel_env <span class="hljs-keyword">import</span> OpenSpielAction, OpenSpielEnv | |
| PROMPT = <span class="hljs-string">"Play 2048 on a 4x4 board. Use the tool `move` with one of: up, down, left, right. Maximize the score."</span> | |
| MOVES = {<span class="hljs-string">"up"</span>: <span class="hljs-number">0</span>, <span class="hljs-string">"right"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"down"</span>: <span class="hljs-number">2</span>, <span class="hljs-string">"left"</span>: <span class="hljs-number">3</span>} | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">Game2048Env</span>: | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-variable language_">self</span>.client = OpenSpielEnv(base_url=<span class="hljs-string">"http://localhost:8000"</span>).sync() | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">reset</span>(<span class="hljs-params">self, **kwargs</span>) -> <span class="hljs-built_in">str</span>: | |
| result = <span class="hljs-variable language_">self</span>.client.reset() | |
| <span class="hljs-variable language_">self</span>.observation = result.observation | |
| <span class="hljs-variable language_">self</span>.score = <span class="hljs-number">0.0</span> | |
| <span class="hljs-variable language_">self</span>.done = result.done | |
| <span class="hljs-keyword">return</span> <span class="hljs-string">f"<span class="hljs-subst">{PROMPT}</span>\n\n<span class="hljs-subst">{self._render()}</span>"</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">move</span>(<span class="hljs-params">self, direction: <span class="hljs-built_in">str</span></span>) -> <span class="hljs-built_in">str</span>: | |
| <span class="hljs-string">""" | |
| Play one move in 2048. | |
| Args: | |
| direction: One of "up", "down", "left", "right". | |
| Returns: | |
| The board and score after the move. | |
| """</span> | |
| <span class="hljs-keyword">if</span> <span class="hljs-variable language_">self</span>.done: | |
| <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">"Game over."</span>) | |
| action_id = MOVES.get(direction.strip().lower()) | |
| <span class="hljs-keyword">if</span> action_id <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> <span class="hljs-variable language_">self</span>.observation.legal_actions: | |
| <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">f"Illegal move: <span class="hljs-subst">{direction}</span>."</span>) | |
| result = <span class="hljs-variable language_">self</span>.client.step(OpenSpielAction(action_id=action_id, game_name=<span class="hljs-string">"2048"</span>)) | |
| <span class="hljs-variable language_">self</span>.observation = result.observation | |
| <span class="hljs-variable language_">self</span>.score += result.reward <span class="hljs-keyword">or</span> <span class="hljs-number">0.0</span> | |
| <span class="hljs-variable language_">self</span>.done = result.done | |
| <span class="hljs-keyword">return</span> <span class="hljs-variable language_">self</span>._render() | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">get_reward</span>(<span class="hljs-params">self</span>) -> <span class="hljs-built_in">float</span>: | |
| <span class="hljs-keyword">return</span> <span class="hljs-variable language_">self</span>.score | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_render</span>(<span class="hljs-params">self</span>) -> <span class="hljs-built_in">str</span>: | |
| tiles = [<span class="hljs-built_in">int</span>(v) <span class="hljs-keyword">for</span> v <span class="hljs-keyword">in</span> <span class="hljs-variable language_">self</span>.observation.info_state] | |
| rows = [<span class="hljs-string">" "</span>.join(<span class="hljs-string">f"<span class="hljs-subst">{v:4d}</span>"</span> <span class="hljs-keyword">for</span> v <span class="hljs-keyword">in</span> tiles[i : i + <span class="hljs-number">4</span>]) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, <span class="hljs-number">16</span>, <span class="hljs-number">4</span>)] | |
| <span class="hljs-keyword">return</span> <span class="hljs-string">f"score=<span class="hljs-subst">{self.score}</span>\n"</span> + <span class="hljs-string">"\n"</span>.join(rows) + <span class="hljs-string">f"\ndone=<span class="hljs-subst">{self.done}</span>"</span><!----></pre></div><!----> <p>An error raised inside a tool is sent back to the model as the tool response, so an illegal move costs a turn without ending the game.</p> <!--[1--><h2 class="relative group"><a id="4-train-with-grpo" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4-train-with-grpo"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>4. Train with GRPO</span></h2><!--]--><!----> <p>The environment generates the starting board and owns the reward, so the trainer needs no dataset and no reward function. <code>max_steps</code> sets the length of the run.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> LoraConfig | |
| <span class="hljs-keyword">from</span> trl <span class="hljs-keyword">import</span> GRPOConfig, GRPOTrainer | |
| trainer = GRPOTrainer( | |
| model=<span class="hljs-string">"Qwen/Qwen3-4B"</span>, | |
| args=GRPOConfig( | |
| max_steps=<span class="hljs-number">200</span>, | |
| chat_template_kwargs={<span class="hljs-string">"enable_thinking"</span>: <span class="hljs-literal">False</span>}, | |
| logging_steps=<span class="hljs-number">1</span>, | |
| log_completions=<span class="hljs-literal">True</span>, | |
| num_completions_to_print=<span class="hljs-number">2</span>, | |
| report_to=<span class="hljs-string">"trackio"</span>, | |
| trackio_space_id=<span class="hljs-string">"openenv-2048"</span>, | |
| max_completion_length=<span class="hljs-number">2048</span>, | |
| per_device_train_batch_size=<span class="hljs-number">4</span>, | |
| gradient_accumulation_steps=<span class="hljs-number">2</span>, | |
| ), | |
| environment_factory=Game2048Env, | |
| peft_config=LoraConfig(), | |
| ) | |
| trainer.train()<!----></pre></div><!----> <p>Each generation batch holds <code>per_device_train_batch_size × gradient_accumulation_steps = 8</code> rollouts, all from the same starting prompt (<code>num_generations</code> defaults to 8). The trainer keeps one <code>Game2048Env</code> instance per rollout and reuses them across batches. A rollout ends when the model stops calling <code>move</code> or reaches <code>max_completion_length</code>, so longer completions let the model play longer games.</p> <p>In the Trackio dashboard, <code>reward</code> (also logged as <code>rewards/Game2048Env/mean</code>) is the mean game score and <code>tools/call_frequency</code> is how often the model calls <code>move</code>. Expect the call frequency to rise first, then the score.</p> <!--[1--><h2 class="relative group"><a id="5-save-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5-save-the-model"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>5. Save the model</span></h2><!--]--><!----> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!---->trainer.push_to_hub() | |
| server.terminate()<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>Next steps</span></h2><!--]--><!----> <ul><li>The <a href="end-to-end-walkthrough">end-to-end walkthrough</a> covers the same <code>environment_factory</code> pattern on a single-step task, and <a href="wordle-grpo">Wordle GRPO</a> on another multi-turn game.</li> <li>The <a href="../guides/training">Training overview</a> lists the other frameworks that train on OpenEnv environments.</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/openenv/blob/main/docs/source/tutorials/rl-training-2048.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_8eecdv = { | |
| base: "/docs/openenv/main/en", | |
| assets: "/docs/openenv/main/en" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/openenv/main/en/_app/immutable/entry/start.BVsaIFPz.js"), | |
| import("/docs/openenv/main/en/_app/immutable/entry/app.Bi0HOH_m.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 20], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 32 kB
- Xet hash:
- ec2964ee649d71d5e6a2ba9613069128a46aeb7f248ff5e861c5d4f968f3db21
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.