How training works
This page explains the ideas behind training with Looper: how reinforcement learning (RL) works, the two kinds of GPU jobs Looper runs for it, and the difference between synchronous and asynchronous RL. It ends with supervised fine-tuning (SFT), which is simpler. Other pages use these terms without explaining them again.
How RL works
In RL, the model learns from its own attempts. Instead of showing it correct answers, you give it prompts, it writes answers, and your program scores them. The model then learns to write more answers like the ones that scored well.
For example, say you want a model to get better at arithmetic:
- You give the model the prompt "What is 12 times 7?"
- The model writes eight different answers. Some say 84, and some say other numbers. They differ because the model picks each word with some randomness, which the temperature setting controls.
- Your program scores each answer: 1 if it says 84, and 0 if not. This score is the reward.
- The model is updated so that answers like the ones that scored 1 become more likely, and answers like the ones that scored 0 become less likely.
Then you repeat with more prompts. Over many rounds, the model writes high-scoring answers more often.
The answers to one prompt form a group. The model learns by comparing answers within a group: answers that scored above the group's average are reinforced, and answers below it are discouraged. If every answer in a group gets the same score, there is nothing to compare, and the group teaches the model nothing.
The trainer and the sampler
Each round of RL is split between Looper and your own code. Looper runs the model as two jobs, each on its own GPUs, and your code goes between them:
- The sampler writes answers with a copy of the model. In the example above, this is the part where the model is asked "What is 12 times 7?" and writes eight answers. This is called sampling.
sampleruns on the sampler, and creating a sampling client starts one. - Your own code grades the answers. In the example, it gives each answer a reward of 1 if it says 84, and 0 if not. Then it sends the answers and their rewards to the trainer.
- The trainer holds the model's weights and updates them. In the example, this is the part where the model learns to favor the answers that said 84.
forward_backwardandoptim_steprun on the trainer, and creating a training client starts one.
The sampler has its own copy of the weights, so it doesn't see the trainer's updates on its own. After the trainer updates the weights, publish_weights copies them to the sampler, so its next answers come from the improved model.
publish_weights
Trainer ------------------> Sampler
^ |
| answers and rewards | answers
| v
+------- Your program: scores the answers
So in Looper, one round of RL looks like this:
- Publish: copy the trainer's weights to the sampler.
- Sample: the sampler writes several answers to each prompt.
- Score: your program gives each answer a reward.
- Train: send the answers and rewards to the trainer with
forward_backward(..., Grpo()), then calloptim_step.
Train on rewards walks through one round in code.
Versions
Each time a run publishes its weights, the sampler gets a new version, numbered by the training step it came from. When these docs say "sample a run's own weights," they mean writing answers with the latest version the run has published.
Every answer records the version that wrote it, in sequence.version, so you can always tell how old the weights behind an answer were.
A sampler doesn't have to follow a training run. It can also write answers from a base model, as it was before any training, or from a checkpoint your project saved. Sampling covers all three.
Synchronous and asynchronous RL
You can run RL rounds in two ways.
Synchronous: finish each round before starting the next, and wait for each publish before sampling. Every answer comes from the newest weights. The downside is that the GPUs take turns sitting idle: the sampler waits while the trainer trains, and the trainer waits while the sampler writes.
Asynchronous: keep both jobs busy. The sampler writes the next batch of answers while the trainer trains on the last one, and the run publishes new weights as they're ready. Training finishes sooner, but some answers come from weights a step or two old.
You don't have to build either one yourself. The cookbook has a loop for each: train_rl runs synchronous RL, and train_rl_async runs asynchronous RL. With train_rl_async, you set one thing, max_policy_lag: how many steps old an answer may be. The loop handles the rest: it keeps the sampler and trainer busy, publishes new weights after each step, and drops answers that have fallen too far behind. If you want full control, you can write your own loop with the SDK's calls instead.
Slightly old answers are normal in async RL, and the loss accounts for them. Each answer records its logprobs: how likely each of its tokens was under the weights that wrote it. Grpo and Dapo compare those with the current weights, and their clipping keeps an old answer from pushing the model too far.
Answers that are too old do more harm than good, which is why train_rl_async drops answers older than max_policy_lag. Sampling while a step trains shows how to choose it.
Supervised fine-tuning
SFT is simpler than RL. You already have examples of what the model should write, such as questions with good answers, and the model learns to reproduce them. There's nothing to generate or score, so SFT only needs the trainer. Each step:
- Your program sends a batch of examples with
forward_backward(..., CrossEntropy()). optim_stepupdates the weights.
You can start a sampler to try out the model as it trains, but training doesn't depend on it. Training covers the calls, and the cookbook's Supervised learning page has a ready-made loop.
Next steps
- What you build: what Looper, the SDK and the cookbook each do, and what you write yourself.
- Quickstart: sample a base model, fine-tune it and take one RL step.
- Training: every training call in detail.
- Sampling: create samplers and read their answers.
- Reinforcement learning: the cookbook's ready-made RL loops, synchronous and asynchronous.