Looper Documentation
Looper Cookbook

Supervised learning

Supervised fine-tuning (SFT) trains a model on examples of what it should write. You give it conversations with good assistant replies, and the model learns to write replies like them. For example, if your conversations ask about animals and the replies always list the animal's habitat, diet and a fun fact, the trained model learns to answer in that shape.

The cookbook's SFT loop, looper_cookbook.train_sft, runs this training for you. You provide the conversations and create the training run. The cookbook turns the conversations into training data, and runs the optimizer steps.

This page covers:

Words used on this page:

Word Meaning
Conversation A list of chat messages, such as a user's question and the assistant's reply. Your data is a file of these.
Datum One training example. It holds a reply's prompt as context, and the reply itself as the tokens the model learns to write. See Prepare data.
Batch The datums trained in one optimizer step.
Step One optimizer step, which updates the model's weights once, from one batch.
Pass One time through every conversation in your data. Also called an epoch.

Conversations and the dataset

Your data is a file of conversations, each a list of chat messages with a role and a content. To train on it, the cookbook needs to do two things:

  1. Turn each conversation into datums. The model's message formatter does this. It writes each conversation in the model's own chat format, and makes one datum for each assistant reply.
  2. Split the datums into batches, one batch for each optimizer step. A dataset does this. ChatDataset is the cookbook's dataset for conversations.

The code below builds a dataset from the example file that comes with the cookbook, which has 16 conversations about animals. It then prints how many datums each of the first four steps trains, and the text of the first datum's reply:

from importlib import resources

from transformers import AutoTokenizer

from looper_cookbook.datasets.conversations import ChatDataset, read_conversations
from looper_cookbook.message_formatters.models import message_formatter

tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3.5-9B")
formatter = message_formatter("Qwen/Qwen3.5-9B", tokenizer, enable_thinking=False)
path = resources.files("looper_cookbook") / "example_data" / "conversations.jsonl"
dataset = ChatDataset(read_conversations(path), formatter, batch_size=8, max_seq_len=2048, seed=0)
print(len(dataset), "batches a pass")
for step in range(4):
    print(step, len(dataset.batch(step)), "datums")
print("the first trains", formatter.targets(dataset.batch(0)[0]))
2 batches a pass
0 10 datums
1 8 datums
2 10 datums
3 8 datums
the first trains ['Habitat: the dry plains of inland Australia.\nDiet: grasses and shrubs.\nFun fact: it uses its tail as a fifth leg when it walks slowly.<|im_end|>']

The 16 conversations, at 8 per batch, make 2 batches per pass. Two of the conversations have a second assistant reply, so the whole pass is 18 datums: 10 in one batch and 8 in the other. Steps 2 and 3 are the second pass.

A dataset of your own

If your data isn't a file of conversations, you can write your own dataset. The loop only needs two methods:

That's the SupervisedDataset contract, in looper_cookbook.datasets.base. The example below is a complete dataset of your own, which gives every step the same datums. It's useful for checking that a run can learn one batch by heart:

from collections.abc import Sequence

from looper_cookbook.datasets.base import SupervisedDataset
from looper_sdk import Datum


class OneBatch:
    def __init__(self, datums: Sequence[Datum]) -> None:
        self.datums = datums

    def __len__(self) -> int:
        return 1

    def batch(self, index: int) -> Sequence[Datum]:
        return self.datums


overfit: SupervisedDataset = OneBatch(dataset.batch(0))
print(len(overfit), "batch a pass;", len(overfit.batch(7)), "datums at step 7")
1 batch a pass; 10 datums at step 7

To split items into batches the way ChatDataset does, in a different seeded order for each pass, use batch_positions, from the same module. It tells you which items a step holds.

The loop

Once you have a training client and a dataset, train runs the training. It takes the training client, the dataset and the loop's config:

from looper_cookbook import train_sft

config = train_sft.Config(learning_rate=2e-4)
results = await train_sft.train(training_client, dataset, config, steps=4 * len(dataset))
print(results[-1].loss_sum / results[-1].weighted_targets)

train(training_client, dataset, config, *, steps, on_step=None) trains the run from its current step up to step steps, one step per batch, and returns each step's result. Here, 4 * len(dataset) steps is four passes over the data. The last line prints the final step's loss per target token.

This assumes you've already created training_client, as Create a training client shows.

The config

Field Default What it sets
learning_rate No default, so you must set it The learning rate of the run's first step.
schedule "linear" How the learning rate changes over the steps. "linear" and "cosine" lower it over the steps, so it would reach 0 at the step after the last, and "constant" keeps it the same. The run's optimizer doesn't keep a schedule itself, so the loop sets each step's rate.
steps_ahead 1 How many steps the loop sends ahead of the one it's waiting on, so the run never waits for your machine. See Submit ahead.
max_call_tokens 1,000,000 The most tokens one call to Looper carries. A bigger batch is split over several calls, whose gradients are added up into the one step.

What the loop leaves to you

looper_cookbook/train_sft.py is short enough to read. If your training needs to work differently, you can copy it into your project and change it.

Next steps