# TRL reward model

> **About this page**
>
> - Task definition: `swaleio/trl-reward`
> - Summary: Trains a reward model on preference pairs with TRL from a config file.
> - Category: Machine learning & AI
> - Latest version: 1.19.0, published 2026-12-01
> - Page: https://hub.swale.io/swaleio/trl-reward
> - Versions: https://hub.swale.io/swaleio/trl-reward/versions, where each version's page has a Markdown copy at its address with `.md` added
> - 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 reward model
description: Trains a reward model on preference pairs with TRL from a config file.
category: machine-learning
inputs:
  config:
    description: Absolute path inside the container to the TRL reward YAML config. Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/reward.yaml) so an earlier task can write it there, or any other container-local path.
    required: true
  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}}
  args:
    - trl
    - reward
    - "--config"
    - ${{inputs.config}}
```

# TRL reward model

Runs `trl reward --config <config>`: trains a reward model with
[TRL](https://huggingface.co/docs/trl)'s `RewardTrainer`. The result scores a
prompt and answer with a single number, higher for answers people prefer, and
can drive `swaleio/trl-grpo` or `swaleio/trl-rloo` through their
`reward_model_name_or_path`, or rank candidate answers. Everything about the run
lives in the YAML file at `config`: the base model, the dataset, PEFT/LoRA
settings, precision, batch sizes, and where the result lands.

## Inputs

| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| `config` | yes | — | Absolute path to the TRL reward YAML config inside the container. Point it at the mounted workspace (e.g. `${{env.WORKFLOW_STORAGE}}/reward.yaml`) so an earlier task can write it there. |
| `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. |

## Dataset

A preference dataset: each row holds a `chosen` and a `rejected` answer to the
same prompt, as plain text or as chat messages, with an optional `prompt`
column. 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.

The model gets a sequence-classification head with one output. With LoRA, set
`lora_task_type: SEQ_CLS` so the new head is trained and saved with the
adapter.

## Where the model lands

This task declares **no outputs**: the reward model, or its LoRA adapter, 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}}/reward-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.** A reward model holds one model and scores both answers of
each pair, so a LoRA run on a 0.5–8B model fits on a 24 GB-class GPU, and a
full fine-tune of a 7–8B model wants 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 the config onto the shared workspace, then `trl-reward`
consumes it. `columns` keeps the `chosen` and `rejected` conversations of
[HuggingFaceH4/ultrafeedback_binarized](https://huggingface.co/datasets/HuggingFaceH4/ultrafeedback_binarized)
(MIT).

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

blocks:
  main:
    tasks:
      write_config:
        name: Write config
        uses: swaleio/bash
        args:
          script: |
            cat > ${{env.WORKFLOW_STORAGE}}/reward.yaml <<'EOF'
            model_name_or_path: Qwen/Qwen2.5-0.5B-Instruct
            datasets:
              - path: HuggingFaceH4/ultrafeedback_binarized
                split: train_prefs
                columns: [chosen, rejected]
            learning_rate: 1.0e-4
            num_train_epochs: 1
            per_device_train_batch_size: 8
            gradient_accumulation_steps: 2
            max_length: 1024
            bf16: true
            use_peft: true
            lora_r: 16
            lora_alpha: 32
            lora_task_type: SEQ_CLS
            report_to: none
            output_dir: ${{env.WORKFLOW_STORAGE}}/reward-out
            EOF
      train:
        name: Train
        uses: swaleio/trl-reward
        compute_type: a100-1   # any GPU compute type — see Compute above
        start_on:
          - write_config
        args:
          config: ${{env.WORKFLOW_STORAGE}}/reward.yaml
          token: ${{secrets.hf_token}}
```

Downstream tasks read the reward model's adapter at
`${{env.WORKFLOW_STORAGE}}/reward-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).
