# Multi-turn on-policy distillation

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

<!-- From examples/turnopd/README.md in the engine repo; configs and scripts it names are linked on GitHub. -->
This recipe pairs a Qwen3.5-2B student with a Qwen3.5-9B teacher on the local `multihop-tools`
environment. It shows teacher log-ratio rewards ([on-policy distillation](/engine/guides/rl-training/#on-policy-distillation)), turn-level supervision
metrics, and adaptive rollout-depth budgeting. The step budget is tiny because the point is
to show the wiring. It makes no claim of reward improvement and does not reproduce a paper.

The teacher must score the exact student token IDs, using the same tokenizer.
Native Surogate serving can score prompt tokens too (`prompt_logprobs` on
`/v1/chat/completions/tokens`, see [docs/inference/api.md](/engine/inference/api/));
this recipe uses a vLLM teacher instead. The included
[teacher_proxy.py](https://github.com/invergent-ai/surogate/blob/main/examples/turnopd/teacher_proxy.py) translates calls to the orchestrator's token endpoint
into vLLM `/v1/completions` prompt-scoring requests. It picks out the logprob of each actual input
token and keeps the first-token placeholder, so supervision stays aligned.

Run the external teacher in a separate vLLM environment (GPU 2), then the CPU proxy:

```bash
CUDA_VISIBLE_DEVICES=2 vllm serve Qwen/Qwen3.5-9B \
  --port 8010 --max-model-len 8192 --max-logprobs 1
# Another terminal, no GPU used by the proxy:
python examples/turnopd/teacher_proxy.py --upstream http://127.0.0.1:8010/v1 --port 8008
```

With Surogate's environment active, run from the repository root:

```bash
surogate grpo --train examples/turnopd/train.yaml --infer examples/turnopd/infer.yaml \
  --orch examples/turnopd/orch.yaml --infer-gpus 0 --trainer-gpus 1
python examples/turnopd/analyze.py outputs/turnopd/turn_stats.jsonl
```

The proxy exposes only health checks, model discovery and token scoring, on localhost.
It is an example adapter for a local teacher and is not meant as a general inference server. Set
`TEACHER_API_KEY` if the upstream requires authentication. An error, or a missing or
misaligned prompt score, stops the request rather than inventing training targets.
For other student/teacher pairs, check tokenizer and vocabulary compatibility first.

[orch.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/turnopd/orch.yaml) uses `use_token_client: false` because this chat template can rewrite
earlier thinking blocks, which breaks token-prefix extension. `rollout_depth`
adapts the turn cap from success-conditioned coverage, with periodic full-depth
probes. Turn depth and per-turn generation limits are set independently. If no trajectories
succeed, the coverage controller has little to go on, so start with an easier
environment or a stronger student.

[train.yaml](https://github.com/invergent-ai/surogate/blob/main/examples/turnopd/train.yaml) sets `teacher_tau: 1.0` and `turn_diagnostics: true`.
The diagnostics route the loss through Python, which is slower, so disable them for
normal throughput. To compare against fixed-depth rollouts, set `rollout_depth.enabled: false`
instead of keeping a second, almost identical experiment directory.

To see how well the student handles the task before training, start only the student and probe it:

```bash
CUDA_VISIBLE_DEVICES=0 surogate grpo-infer examples/turnopd/infer.yaml
python examples/turnopd/probe_env.py --port 8007 --model Qwen/Qwen3.5-2B -n 20
```

Stop that standalone student before you use the split runner, which starts its own.
The probe reports malformed tools, repeated lookups, turn-cap hits and task success.

Next: the [GRPO example](/engine/examples/recipes/grpo/) covers the runners and config alignment this recipe
builds on, and [On-policy distillation](/engine/guides/rl-training/#on-policy-distillation) lists
the teacher settings.
