Buckets:

download
raw
7.38 kB
import"../chunks/DsnmJJEf.js";import{i as b,h as y,C as v,H as t,a as h,E as T,s as f}from"../chunks/CKEMnrQO.js";import{p as J,o as U,s as e,f as R,a as g,b as j,d as u,n as C}from"../chunks/Bb-LL0eD.js";const G='{"title":"Training a Real Coding Agent with GRPO (OpenCode)","local":"training-a-real-coding-agent-with-grpo-opencode","sections":[{"title":"How It Works","local":"how-it-works","sections":[],"depth":2},{"title":"Run The Recipe","local":"run-the-recipe","sections":[],"depth":2},{"title":"Full Recipe","local":"full-recipe","sections":[],"depth":2}],"depth":1}';var W=u('<meta name="hf:doc:metadata"/>'),E=u(`<p></p> <!> <!> <p>This tutorial covers the black-box training path: training the actual <a href="https://opencode.ai" rel="nofollow"><code>opencode</code></a> coding agent, with its own planner, tools,
context management, and stop condition, using TRL’s experimental <code>AsyncGRPOTrainer</code>. The agent owns its loop, and OpenEnv captures what it did.</p> <blockquote class="note"><p>Three GRPO patterns, three tutorials. For a standard <code>reset()</code> / <code>step()</code> flow where TRL drives the episode, see the <a href="wordle-grpo">Wordle GRPO tutorial</a>. For harness rollouts where the
trainer still generates each turn (white-box), see the <a href="browsergym-harness">BrowserGym harness tutorial</a>. Use this page when you
want to train a production agent as-is, without reimplementing its loop.</p></blockquote> <!> <p>The full recipe lives in TRL. The moving pieces:</p> <ol><li>Each rollout runs the agent inside an OpenEnv session created by <code>OpenCodeSessionFactory</code> from <a href="https://github.com/huggingface/OpenEnv/tree/main/envs/opencode_env" rel="nofollow"><code>opencode_env</code></a>,
in <code>transparent_proxy</code> mode. A small proxy inside the sandbox forwards the
agent’s <code>/v1/chat/completions</code> calls to your vLLM server and records each
turn’s token ids and logprobs to a trace.</li> <li>When the agent stops, TRL’s <code>HarnessRolloutWorker</code> reads the trace, rebuilds
the per-turn training rows from the recorded ids, and scores the final
workspace with the session’s <code>verify()</code> method (a held-out verifier the
agent never sees).</li> <li><code>AsyncGRPOTrainer</code> trains on those rows, propagating the rollout reward to
every trained token through the group-relative advantage. NCCL weight sync
keeps the vLLM server on the current policy, so the agent always samples
from the model being trained.</li></ol> <p>Each rollout gets its own isolated session: one sandbox, one proxy port, one
agent process. Three small functions adapt the recipe to your task: <code>rollout_reward_fn</code> (outcome to scalar reward), <code>train_turn_fn</code> (which turns
receive gradient), and <code>agent_turn_fn</code> (which trace entries are real agent
turns rather than auxiliary calls like title generation). All three are
documented in <a href="https://huggingface.co/docs/trl/openenv#training-on-harnesses-training-a-real-coding-agent-opencode" rel="nofollow">TRL’s harness training guide</a>.</p> <!> <p>The reference script trains on competitive-coding problems from <code>agentica-org/DeepCoder-Preview-Dataset</code>. The agent writes <code>solution.py</code>, and
the verifier runs it against held-out tests, returning the fraction passed.</p> <!> <p>Serve the policy with tool calling, token ids, and NCCL weight sync enabled
(one GPU), then train (a second GPU):</p> <!> <p>The script is self-contained and runs the agent in a local subprocess sandbox,
so no container setup is needed. The recipe has been validated end to end on
Qwen3 (see <a href="https://github.com/huggingface/trl/pull/6420" rel="nofollow">huggingface/trl#6420</a>).</p> <!> <ul><li><a href="https://huggingface.co/docs/trl/openenv#training-on-harnesses-training-a-real-coding-agent-opencode" rel="nofollow">Training on harnesses</a> in TRL’s OpenEnv docs: rollout semantics, the reward path, turn selection,
and the trace contract.</li> <li><a href="https://github.com/huggingface/trl/blob/main/examples/scripts/openenv/opencode.py" rel="nofollow"><code>examples/scripts/openenv/opencode.py</code></a> in TRL: the complete, runnable script.</li> <li><a href="https://github.com/huggingface/OpenEnv/tree/main/envs/opencode_env" rel="nofollow"><code>envs/opencode_env</code></a>:
the OpenEnv side, including the session factory, sandbox backends, and the
transparent interception proxy.</li></ul> <!> <p></p>`,1);function x(w,m){J(m,!1),U(()=>{new URLSearchParams(window.location.search).get("fw")}),b();var n=E();y("ekqdbr",d=>{var p=W();f(p,"content",G),g(d,p)});var o=e(R(n),2);v(o,{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"});var a=e(o,2);t(a,{title:"Training a Real Coding Agent with GRPO (OpenCode)",local:"training-a-real-coding-agent-with-grpo-opencode",headingTag:"h1"});var r=e(a,6);t(r,{title:"How It Works",local:"how-it-works",headingTag:"h2"});var l=e(r,8);t(l,{title:"Run The Recipe",local:"run-the-recipe",headingTag:"h2"});var s=e(l,4);h(s,{code:"cGlwJTIwaW5zdGFsbCUyMHRybCUyMHRyYWNraW8lMjBkYXRhc2V0cyUwQXBpcCUyMGluc3RhbGwlMjAlMjJvcGVuZW52LW9wZW5jb2RlLWVudiUyMCU0MCUyMGdpdCUyQmh0dHBzJTNBJTJGJTJGZ2l0aHViLmNvbSUyRmh1Z2dpbmdmYWNlJTJGT3BlbkVudi5naXQlMjNzdWJkaXJlY3RvcnklM0RlbnZzJTJGb3BlbmNvZGVfZW52JTIy",highlighted:`pip install trl trackio datasets
pip install <span class="hljs-string">&quot;openenv-opencode-env @ git+https://github.com/huggingface/OpenEnv.git#subdirectory=envs/opencode_env&quot;</span>`,lang:"bash",wrap:!1});var i=e(s,4);h(i,{code:"JTIzJTIwVGVybWluYWwlMjAxJTBBQ1VEQV9WSVNJQkxFX0RFVklDRVMlM0QwJTIwVkxMTV9TRVJWRVJfREVWX01PREUlM0QxJTIwdmxsbSUyMHNlcnZlJTIwUXdlbiUyRlF3ZW4zLTRCLUluc3RydWN0LTI1MDclMjAlNUMlMEElMjAlMjAlMjAlMjAtLWhvc3QlMjAwLjAuMC4wJTIwLS1wb3J0JTIwODAwMCUyMCU1QyUwQSUyMCUyMCUyMCUyMC0tZW5hYmxlLWF1dG8tdG9vbC1jaG9pY2UlMjAtLXRvb2wtY2FsbC1wYXJzZXIlMjBoZXJtZXMlMjAlNUMlMEElMjAlMjAlMjAlMjAtLWxvZ3Byb2JzLW1vZGUlMjBwcm9jZXNzZWRfbG9ncHJvYnMlMjAlNUMlMEElMjAlMjAlMjAlMjAtLXJldHVybi10b2tlbnMtYXMtdG9rZW4taWRzJTIwJTVDJTBBJTIwJTIwJTIwJTIwLS13ZWlnaHQtdHJhbnNmZXItY29uZmlnJTIwJyU3QiUyMmJhY2tlbmQlMjIlM0ElMjJuY2NsJTIyJTdEJyUwQSUwQSUyMyUyMFRlcm1pbmFsJTIwMiUwQUNVREFfVklTSUJMRV9ERVZJQ0VTJTNEMSUyMHB5dGhvbiUyMGV4YW1wbGVzJTJGc2NyaXB0cyUyRm9wZW5lbnYlMkZvcGVuY29kZS5weSUyMCU1QyUwQSUyMCUyMCUyMCUyMC0tbW9kZWwlMjBRd2VuJTJGUXdlbjMtNEItSW5zdHJ1Y3QtMjUwNyUyMC0tdmxsbS11cmwlMjBodHRwJTNBJTJGJTJGbG9jYWxob3N0JTNBODAwMA==",highlighted:`<span class="hljs-comment"># Terminal 1</span>
CUDA_VISIBLE_DEVICES=0 VLLM_SERVER_DEV_MODE=1 vllm serve Qwen/Qwen3-4B-Instruct-2507 \\
--host 0.0.0.0 --port 8000 \\
--enable-auto-tool-choice --tool-call-parser hermes \\
--logprobs-mode processed_logprobs \\
--return-tokens-as-token-ids \\
--weight-transfer-config <span class="hljs-string">&#x27;{&quot;backend&quot;:&quot;nccl&quot;}&#x27;</span>
<span class="hljs-comment"># Terminal 2</span>
CUDA_VISIBLE_DEVICES=1 python examples/scripts/openenv/opencode.py \\
--model Qwen/Qwen3-4B-Instruct-2507 --vllm-url http://localhost:8000`,lang:"bash",wrap:!1});var c=e(i,4);t(c,{title:"Full Recipe",local:"full-recipe",headingTag:"h2"});var M=e(c,4);T(M,{source:"https://github.com/huggingface/openenv/blob/main/docs/source/tutorials/opencode-agent-grpo.md"}),C(2),g(w,n),j()}export{x as component};

Xet Storage Details

Size:
7.38 kB
·
Xet hash:
8b4e65135ea6f243f865de3f8f6c1d3f097746163ad156c922b43d11fadf51ef

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.