# GRPO: split GPUs, one shared GPU, or separate processes

Source: https://docs.surogate.ai/engine/examples/recipes/grpo/

<!-- From examples/grpo/README.md in the engine repo; configs and scripts it names are linked on GitHub. -->
The same [train](https://github.com/invergent-ai/surogate/blob/main/examples/grpo/train.yaml), [infer](https://github.com/invergent-ai/surogate/blob/main/examples/grpo/infer.yaml), and [orch](https://github.com/invergent-ai/surogate/blob/main/examples/grpo/orch.yaml) configs run
Qwen3-0.6B on the local `markdown-table-qa` reward environment. Run them from the repository
root with the RL dependencies installed. The environment loads from
`environments/markdown-table-qa`, so you don't need to install it separately.

```bash
# Two GPUs: separate rollout server and trainer.
surogate grpo --train examples/grpo/train.yaml --infer examples/grpo/infer.yaml \
  --orch examples/grpo/orch.yaml --infer-gpus 0 --trainer-gpus 1
```

On one GPU, use the same files with a fresh `outputs/grpo` directory:

```bash
CUDA_VISIBLE_DEVICES=0 surogate grpo-colocate \
  --train examples/grpo/train.yaml --infer examples/grpo/infer.yaml --orch examples/grpo/orch.yaml
```

Colocation shares one BF16 base model and alternates between generation and training. It requires
unquantized BF16 safetensors, LoRA, one GPU, and a model the shared
runner supports (Nemotron is not supported). It does not support checkpoint resume, QLoRA, CPU
weight offload, or QeRL noise. See [colocation limits](/engine/guides/rl-colocate/).

For rollouts with real function tools, use [tools-orch.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/grpo/tools-orch.yaml) with the
same training and inference files and a fresh output directory.

Split mode reads `enable_auto_tool_choice` from `infer.yaml`. A tool
environment does not work without it:

```bash
surogate grpo --train examples/grpo/train.yaml --infer examples/grpo/infer.yaml \
  --orch examples/grpo/tools-orch.yaml --infer-gpus 0 --trainer-gpus 1
```

Colocate hardcodes the equivalent in its shared server and ignores the flag, but
the same orchestrator config applies:

```bash
CUDA_VISIBLE_DEVICES=7 surogate grpo-colocate \
  --train examples/grpo/train.yaml --infer examples/grpo/infer.yaml \
  --orch examples/grpo/tools-orch.yaml
```

The local [tool environment](https://github.com/invergent-ai/surogate/blob/main/examples/grpo/tool_env.py) exposes `add(a, b)`, executes the model's
structured tool call, returns a tool message, and rewards the final answer.
Schemas come from Verifiers' `ToolEnv`, and custom environments can expose their own
Python functions the same way. Tool results are context tokens and carry no GRPO loss.
Every model family that native colocate supports can use tools. The shared training
server picks the checkpoint's tool protocol, or adapts templates that have no tool support.
Base models still need training before they call tools reliably. See the
[protocols and limits](/engine/guides/rl-colocate/#agentic-tool-rollouts).

To run independent processes, use three terminals:

```bash
CUDA_VISIBLE_DEVICES=0 surogate grpo-infer examples/grpo/infer.yaml
surogate grpo-orch examples/grpo/orch.yaml
CUDA_VISIBLE_DEVICES=1 surogate grpo-train examples/grpo/train.yaml
```

The processes must share the output filesystem and see the same broadcast paths.
You can't currently tell `grpo-train` where the orchestrator polls, so this
layout works only with an orchestrator `output_dir` of `run_default`, which is
what `orch.yaml` uses. With `tools-orch.yaml` (`run_tools`) the trainer
publishes somewhere the orchestrator does not read and the run fails once
`checkpoint_wait_timeout` expires. Use split mode for tool environments.
For remote servers, update `client.base_url` and the bind address in the inference
config. Multi-node SFT uses Ray, while this RL layout uses separate service processes.

The configs show grouped rewards, token-in/token-out rollouts, a temperature
schedule, online difficulty filtering, periodic evaluation, local metrics, and
orchestrator and trainer checkpoints. Evaluation runs before training and every ten
steps. Read metrics from `outputs/grpo/metrics.jsonl` (trainer) and
`/tmp/grpo_metrics.jsonl` (orchestrator).

When you customize the configs, keep these values aligned:

- The model name in all three configs, and `max_steps` in the trainer and orchestrator.
- `sequence_len` in the trainer and orchestrator, with a large enough `max_model_len` on the server.
- `orch.output_dir` must be `train.output_dir/run_<name>`.
- `rollouts_per_example` must divide `batch_size`. Inference needs room for the prompt
  plus the completion, and adapters must fit within `max_lora_rank`.

`use_token_client: true` keeps the tokens generated so far and appends the
environment's response through a template bridge. Surogate's bridge keeps
real function names and handles Qwen's reasoning rules. Truncated
turns or incompatible custom templates can still fall back to message rendering,
and the client logs when that happens. The [multi-turn OPD example](/engine/examples/recipes/turnopd/) uses
message rendering explicitly. Very short completion limits can truncate every answer and leave zero
reward. A small model may also need easier tasks or more steps to learn.

For split-mode resume, set trainer `resume_from_checkpoint: true` and orchestrator
`ckpt.resume_step` to the matching saved step (or `-1` for the latest), and keep
the same output paths. The optional `ckpt.spool_inflight: true` also saves in-flight
rollouts. Do not use the resume variant with colocation.

The active GRPO loss accepts `adv_tau`, `teacher_tau`, `kl_tau`, `ipo_mask_low`,
`ipo_mask_high`, and `ratio_clip`. The teacher term is demonstrated in
[TurnOPD](/engine/examples/recipes/turnopd/); [RULER](/engine/examples/recipes/ruler/) adds an external judge.
Native serving cannot apply QeRL base-weight noise, so both native runners reject
`noise_scheduler.enabled`. The replay/OPD reference-loss knobs do not yet
add up to a complete replay-training CLI workflow, so there are no runnable recipes for them.

## Token budgets, filtering and async collection

To collect each batch by a budget of generated tokens, copy `orch.yaml`, remove `batch_size`
and add:

```yaml
token_batch_size: 8192
max_inflight_rollouts: 16
```

Keep `rollouts_per_example`, because collection still works in complete groups. To get
metrics on degenerate rollouts, add a filter (set `enforce: true` to mask them):

```yaml
filters:
  - type: repetition
    enforce: false
    window: 32
    prob_threshold: 0.99
```

To train on several reward tasks, append named entries to `env` and set
`buffer.env_ratios` to one positive weight per environment. Different environment
arguments can make easier or harder variants of the same task. For split or
separate-process runs, bound policy staleness by tuning `max_async_level` in both the trainer and
orchestrator, and `max_off_policy_steps` in the orchestrator. Colocation
sets synchronous collection on its own.

Separate processes can send batches over ZMQ. Set `transport_type: zmq`
in the trainer and add the following to the orchestrator (adapter broadcasts still
need a shared filesystem):

```yaml
rollout_transport:
  type: zmq
  host: localhost
  port: 5555
  hwm: 10
```

Use filesystem transport with colocation. See the [RL guide](/engine/guides/rl-training/)
for custom environments and advantage functions.

Next: [RL environments](/engine/guides/rl-environments/) shows how to write your own task and reward, and
the [GRPO kits](https://github.com/invergent-ai/surogate/blob/main/examples/kits/README.md) are tested single-GPU recipes to start from.
