Skip to main content

Train math reasoning with RLOO

Source on GitHub: the workflows/rloo-math folder, with its README and its workflow file.

Trains a model to solve math word problems with REINFORCE leave-one-out. For each question the model writes several answers; TRL's accuracy_reward gives 1 to the answers whose final result matches the reference, checked with math-verify, and the model learns to favor them. The base model and the trained model are both scored on GSM8K's test split.

StepTask definitionCompute
Turn GSM8K questions into chat prompts and reference solutionsswaleio/duckdbcpu-4
Write the training configswaleio/bashcpu-4
Trainswaleio/trl-rlooa100-1
Evaluate the base model, and the trained modelswaleio/lm-evala100-1 each
Compare the two scoresswaleio/duckdbcpu-4
Upload the trained model, when hub_repo is setswaleio/hf-uploadcpu-4

The Compute column gives each step's compute type, the compute its tasks are allocated.

Every step is its own task. The base model is scored while the other model trains.

Inputs​

InputDefaultDescription
modelQwen/Qwen2.5-0.5B-InstructChat model to train; all of its weights are trained.
train_questions2000GSM8K training questions to train on.
hub_repoemptyHugging Face model repo to upload the trained model to.

Training, the base model's evaluation and the upload read the secret hf_token.

Output​

The trained model in trained/ on the workspace, the scores in eval-base/ and eval-trained/, and the comparison in the last task's log.

Run it​

You need the CLI, logged in to your account, and a project to add the workflow to; Getting started sets them up.

  1. Save the file under The workflow file as workflow.yaml, and add it to a project:

    swale workflow create --project-name my-project --file workflow.yaml

    The command prints Workflow created: followed by the workflow's ID.

  2. The workflow reads the secret hf_token. Create it as a project secret, or as an account secret that allows the project, as Use secrets in workflows shows.

  3. Start a run with the workflow's ID:

    swale workflow run start --project-name my-project --workflow-id <workflow-id>

    Each input you leave out takes its default from Inputs, and --input <name>=<value> gives it another value.

Create and manage workflows covers the other ways to add a workflow, Start and manage workflow runs how to follow a run, and View and stream task logs how to read its tasks' logs.

The workflow file​

Workflow definition syntax describes each key in it.

workflow.yaml
name: Train math reasoning with RLOO
description: Trains a model on math word problems with TRL's RLOO, rewarding answers that match the reference, and compares its GSM8K score before and after.
compute_type: cpu-4

inputs:
model:
description: Hugging Face repo id of the chat model to train. All of its weights are trained.
default: Qwen/Qwen2.5-0.5B-Instruct
train_questions:
description: Number of GSM8K training questions to train on.
default: "2000"
hub_repo:
description: Hugging Face model repo to upload the trained model to, as namespace/name. Leave empty to keep it on the workspace only.
default: ""

entry_point: main

blocks:
main:
tasks:
prepare:
name: Prepare prompts and solutions
uses: swaleio/duckdb
args:
sql: |
-- RLOO reads a prompt column of chat messages; accuracy_reward compares each
-- answer with the solution column, the number after #### in GSM8K's answers.
COPY (
SELECT [{'role': 'user', 'content': question || '\nPut the final answer in \boxed{}.'}] AS prompt,
trim(split_part(answer, '####', 2)) AS solution
FROM 'hf://datasets/openai/gsm8k/main/train-*.parquet'
LIMIT ${{inputs.train_questions}}
) TO '${{env.WORKFLOW_STORAGE}}/rloo-train.parquet' (FORMAT parquet);
SELECT count(*) AS questions FROM '${{env.WORKFLOW_STORAGE}}/rloo-train.parquet';

write_config:
name: Write training config
uses: swaleio/bash
args:
script: |
cat > "$WORKFLOW_STORAGE/rloo.yaml" <<EOF
model_name_or_path: ${{inputs.model}}
datasets:
- path: parquet
data_files: $WORKFLOW_STORAGE/rloo-train.parquet
split: train
reward_funcs:
- accuracy_reward
learning_rate: 1.0e-6
num_train_epochs: 1
per_device_train_batch_size: 16
num_generations: 8
max_completion_length: 512
bf16: true
report_to: none
save_strategy: "no"
output_dir: $WORKFLOW_STORAGE/trained
EOF

train:
name: Train with RLOO
uses: swaleio/trl-rloo
compute_type: a100-1
start_on:
- prepare
- write_config
args:
config: ${{env.WORKFLOW_STORAGE}}/rloo.yaml
token: ${{secrets.hf_token}}

evaluate_base:
name: Evaluate the base model
uses: swaleio/lm-eval
compute_type: a100-1
args:
model_dir: ${{inputs.model}}
tasks: gsm8k
output_path: ${{env.WORKFLOW_STORAGE}}/eval-base
token: ${{secrets.hf_token}}

evaluate_trained:
name: Evaluate the trained model
uses: swaleio/lm-eval
compute_type: a100-1
start_on:
- train
args:
model_dir: ${{env.WORKFLOW_STORAGE}}/trained
tasks: gsm8k
output_path: ${{env.WORKFLOW_STORAGE}}/eval-trained

compare:
name: Compare
uses: swaleio/duckdb
start_on:
- evaluate_base
- evaluate_trained
args:
sql: |
SELECT CASE WHEN filename LIKE '%/eval-base/%' THEN 'base' ELSE 'trained' END AS model,
round((results.gsm8k->>'$."exact_match,strict-match"')::DOUBLE, 4) AS strict_match,
round((results.gsm8k->>'$."exact_match,flexible-extract"')::DOUBLE, 4) AS flexible_extract
FROM read_json(['${{env.WORKFLOW_STORAGE}}/eval-base/*/results_*.json',
'${{env.WORKFLOW_STORAGE}}/eval-trained/*/results_*.json'],
filename = true, columns = {'results': 'STRUCT(gsm8k JSON)'})
ORDER BY model;

publish:
name: Upload when asked
start_on:
- compare
if:
condition: ${{inputs.hub_repo}}
true: upload

upload:
tasks:
upload_model:
name: Upload to the Hugging Face Hub
uses: swaleio/hf-upload
args:
repo: ${{inputs.hub_repo}}
source: ${{env.WORKFLOW_STORAGE}}/trained
token: ${{secrets.hf_token}}

Licenses of the defaults​