TRL KTO
Runs trl kto --config <config>: Kahneman-Tversky optimization with
TRL's KTOTrainer. KTO learns from single
answers labelled desirable or undesirable, so it fits feedback that never
compared two answers side by side, such as a thumbs up or down. Everything about
the run lives in the YAML file at config: the 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 KTO YAML config inside the container. Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/kto.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
An unpaired preference dataset: each row holds a prompt, a completion and a
boolean label, true for a desirable answer. A paired preference dataset,
with chosen and rejected columns, also works: TRL splits each pair into one
desirable and one undesirable row. 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}}/kto-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. Like DPO, KTO scores answers against a frozen reference
model; with LoRA the reference is the base model with the adapter switched off,
so a 0.5–8B model fits on a 24 GB-class GPU, while a full fine-tune keeps a
second copy and wants 80 GB-class hardware from about 3B parameters. 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-kto
consumes it. The pairs of
HuggingFaceH4/ultrafeedback_binarized
(MIT) are split into labelled rows.
name: TRL KTO 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}}/kto.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: 5.0e-6
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
report_to: none
output_dir: ${{env.WORKFLOW_STORAGE}}/kto-out
EOF
train:
name: Train
uses: swaleio/trl-kto
compute_type: a100-1 # any GPU compute type — see Compute above
start_on:
- write_config
args:
config: ${{env.WORKFLOW_STORAGE}}/kto.yaml
token: ${{secrets.hf_token}}
Downstream tasks read the LoRA adapter at ${{env.WORKFLOW_STORAGE}}/kto-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.