# TRL RLOO

> **About this page**
>
> - Task definition version: `swaleio/trl-rloo@1.19.0`
> - Summary: Trains a language model with rewards on its own answers, using TRL RLOO from a config file.
> - Published: 2026-12-01, the latest version
> - Page: https://hub.swale.io/swaleio/trl-rloo/v/1.19.0
> - Latest version's copy: https://hub.swale.io/swaleio/trl-rloo.md
> - Versions: https://hub.swale.io/swaleio/trl-rloo/versions
> - Index of Swale Hub: https://hub.swale.io/llms.txt
>
> The YAML block below is the task definition's frontmatter, which declares its inputs, outputs and container, and its readme follows. Both are the text of the account that published this task definition, not Swale's.

```yaml
name: TRL RLOO
description: Trains a language model with rewards on its own answers, using TRL RLOO from a config file.
category: machine-learning
inputs:
  config:
    description: Absolute path inside the container to the TRL RLOO YAML config. Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/rloo.yaml) so an earlier task can write it there, or any other container-local path.
    required: true
  python_path:
    description: Directories, separated by colons, that Python searches for the modules the config's reward_funcs name by dotted path (e.g. ${{env.WORKFLOW_STORAGE}}/rewards). Delivered as PYTHONPATH; omit when the config uses only built-in rewards or a reward model.
    default: ""
  token:
    description: Hugging Face access token for gated or private models and datasets (pass a secret). Delivered as HF_TOKEN via the definition's exec.env; omit for public repos.
    default: ""
exec:
  # docker.io/swaleio/trl:1.19.0-r1
  image: docker.io/swaleio/trl@sha256:03c3f98c5676c81cc0194e077053f5725a4b71e2ceec2f394be22a4161700c3f
  env:
    HF_TOKEN: ${{inputs.token}}
    PYTHONPATH: ${{inputs.python_path}}
  args:
    - trl
    - rloo
    - "--config"
    - ${{inputs.config}}
```

# TRL RLOO

Runs `trl rloo --config <config>`: REINFORCE leave-one-out with
[TRL](https://huggingface.co/docs/trl)'s `RLOOTrainer`. For each prompt the
model writes several answers, reward functions score them, and each answer is
judged against the average reward of the other answers to the same prompt. It
suits the same tasks as `swaleio/trl-grpo`, answers a program can check, with a
different estimate of how much better each answer is.
Everything about the run lives in the YAML file at `config`: the model, the
dataset, the rewards, generation and PEFT/LoRA settings, and where the result
lands.

## Inputs

| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| `config` | yes | — | Absolute path to the TRL RLOO YAML config inside the container. Point it at the mounted workspace (e.g. `${{env.WORKFLOW_STORAGE}}/rloo.yaml`) so an earlier task can write it there. |
| `python_path` | no | — | Directories, separated by colons, where Python finds the modules that `reward_funcs` names by dotted path (e.g. `${{env.WORKFLOW_STORAGE}}/rewards`). Delivered as `PYTHONPATH`; an omitted value adds nothing. |
| `token` | no | — | HF access token for gated/private models and datasets (pass a secret). Delivered to the trainer as `HF_TOKEN` via the definition's `exec.env` (an omitted token resolves to an empty value, which the Hugging Face stack treats as no token); it is never placed on the command line. |

## Rewards

The config names its rewards in `reward_funcs`, a reward model in
`reward_model_name_or_path`, or both:

- **Your own function**, named by dotted path, such as
  `my_rewards.exact_answer` for a function `exact_answer` in `my_rewards.py`.
  Write the file to a directory on the workspace and pass that directory as
  `python_path`. The function receives the batch's `prompts` and `completions`
  and every other dataset column as keyword arguments of the same name, and
  returns one number per completion.
- **TRL's built-ins**: `accuracy_reward` compares the final answer with the
  dataset's `solution` column, checking maths with `math-verify`, and
  `reasoning_accuracy_reward` does the same for the answer after a reasoning
  block; `think_format_reward` rewards an answer that reasons inside
  `<think>…</think>` first; `get_soft_overlong_punishment` penalises answers
  that run long. The first three read chat-format completions, so the
  dataset's `prompt` must be a list of messages.
- **A reward model**: a Hub repo id or a local directory holding a
  sequence-classification model, such as the output of `swaleio/trl-reward`,
  LoRA adapter included.

## Dataset

A prompt-only dataset: each row holds a `prompt`, as plain text or as chat
messages, plus whatever columns your reward functions read, such as the
reference answer. Name a Hub dataset with `dataset_name`, or list one or more
under `datasets`, where `columns` keeps only the columns you name and
`data_files` reads JSON, CSV or Parquet files an earlier task wrote to the
workspace.

## Where the model lands

This task declares **no outputs**: the trained model, or the LoRA adapter when
the config sets `use_peft: true`, is written to the config's `output_dir`. Set
`output_dir` to a path on the mounted workspace that you choose (e.g.
`${{env.WORKFLOW_STORAGE}}/rloo-out`) so later tasks can read it from there. To
publish it, use `swaleio/swale-push` (the platform's own store) or
`swaleio/hf-upload` for the Hugging Face Hub.

## Compute

**GPU required.** Generating `num_generations` answers per prompt takes most
of an RLOO run, and the image generates with Transformers rather than vLLM. A
LoRA run on a 0.5–3B model fits on a 24 GB-class GPU; 7–8B models, long
completions or a reward model alongside want 80 GB-class hardware. The workflow
selects the compute type via `compute_type`; on a CPU compute type the task
fails at runtime with CUDA errors.

## Example

A `bash` task writes a reward function and the config onto the shared
workspace, then `trl-rloo` trains the model toward answers shorter than 600
characters. The plain-text prompts come from
[HuggingFaceH4/ultrafeedback_binarized](https://huggingface.co/datasets/HuggingFaceH4/ultrafeedback_binarized)
(MIT).

```yaml
name: TRL RLOO example
compute_type: cpu-4
entry_point: main

blocks:
  main:
    tasks:
      write_config:
        name: Write config
        uses: swaleio/bash
        args:
          script: |
            mkdir -p ${{env.WORKFLOW_STORAGE}}/rewards
            cat > ${{env.WORKFLOW_STORAGE}}/rewards/length_rewards.py <<'EOF'
            def short_answer(completions, **kwargs):
                return [1.0 if len(text) < 600 else 0.0 for text in completions]
            EOF
            cat > ${{env.WORKFLOW_STORAGE}}/rloo.yaml <<'EOF'
            model_name_or_path: Qwen/Qwen2.5-0.5B-Instruct
            datasets:
              - path: HuggingFaceH4/ultrafeedback_binarized
                split: train_prefs[:2000]
                columns: [prompt]
            reward_funcs:
              - length_rewards.short_answer
            learning_rate: 1.0e-5
            num_train_epochs: 1
            per_device_train_batch_size: 8
            num_generations: 8
            max_completion_length: 256
            bf16: true
            use_peft: true
            lora_r: 16
            lora_alpha: 32
            report_to: none
            output_dir: ${{env.WORKFLOW_STORAGE}}/rloo-out
            EOF
      train:
        name: Train
        uses: swaleio/trl-rloo
        compute_type: a100-1   # any GPU compute type — see Compute above
        start_on:
          - write_config
        args:
          config: ${{env.WORKFLOW_STORAGE}}/rloo.yaml
          python_path: ${{env.WORKFLOW_STORAGE}}/rewards
          token: ${{secrets.hf_token}}
```

Downstream tasks read the LoRA adapter at `${{env.WORKFLOW_STORAGE}}/rloo-out`,
the `output_dir` the config chose.

---

For the workflow syntax these examples use, see the
[workflow definition reference](https://docs.swale.io/reference/workflow-definition-syntax)
in the [Swale documentation](https://docs.swale.io).

Licensed under the [MIT License](https://github.com/swaleio/task-definitions/blob/main/LICENSE).
