TRL RLOO
swaleio/[email protected]
TRL RLOO
Runs trl rloo --config <config>: REINFORCE leave-one-out with
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_answerfor a functionexact_answerinmy_rewards.py. Write the file to a directory on the workspace and pass that directory aspython_path. The function receives the batch'spromptsandcompletionsand every other dataset column as keyword arguments of the same name, and returns one number per completion. - TRL's built-ins:
accuracy_rewardcompares the final answer with the dataset'ssolutioncolumn, checking maths withmath-verify, andreasoning_accuracy_rewarddoes the same for the answer after a reasoning block;think_format_rewardrewards an answer that reasons inside<think>…</think>first;get_soft_overlong_punishmentpenalises answers that run long. The first three read chat-format completions, so the dataset'spromptmust 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
(MIT).
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 in the Swale documentation.
Licensed under the MIT License.