Align a model with DPO
Source on GitHub: the workflows/dpo-alignment folder, with its README and its workflow file.
Aligns a chat model to human preferences with direct preference optimization, then checks that it helped. The base model and the aligned model both answer the same held-out prompts, a judge model compares each pair of answers without knowing which model wrote which, and the last step reports how often the aligned model wins.
| Step | Task definition | Compute |
|---|---|---|
| Write the training config | swaleio/bash | cpu-4 |
| Fetch the base model, and the judge | swaleio/hf-download | cpu-4 |
| Align the model | swaleio/trl-dpo | a100-1 |
| Write the held-out requests | swaleio/duckdb | cpu-4 |
| Answer with the base model, and with the aligned model | swaleio/vllm-batch | a100-1 each |
| Pair the answers, alternating their order, and write the judge requests | swaleio/duckdb | cpu-4 |
| Judge each pair | swaleio/vllm-batch | a100-1 |
| Count wins, losses and ties | swaleio/duckdb | cpu-4 |
Upload the aligned model, when hub_repo is set | swaleio/hf-upload | cpu-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 answers while the aligned one is still training.
The whole model is trained, not a LoRA adapter, so vLLM can serve the result as
it is. A 0.5B model trains on one A100 this way; for larger models, add
use_peft: true to the config and merge the adapter before answering.
Inputs
| Input | Default | Description |
|---|---|---|
model | Qwen/Qwen2.5-0.5B-Instruct | Chat model to align. |
judge | Qwen/Qwen3-8B | Judge model. |
train_split | train_prefs[:10000] | Pairs of HuggingFaceH4/ultrafeedback_binarized to train on. |
eval_prompts | 200 | Held-out prompts, from its test_prefs split. |
hub_repo | empty | Hugging Face model repo to upload the aligned model to. |
The downloads, the training and the upload read the secret hf_token.
Output
The aligned model in aligned/ on the workspace, each judged pair in
verdicts.parquet, and the win rate 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.
-
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.yamlThe command prints
Workflow created:followed by the workflow's ID. -
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. -
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.
name: Align a model with DPO
description: Aligns a chat model to preference pairs with TRL's DPO, then measures its win rate against the base model on held-out prompts with a judge model.
compute_type: cpu-4
inputs:
model:
description: Hugging Face repo id of the chat model to align. All of its weights are trained.
default: Qwen/Qwen2.5-0.5B-Instruct
judge:
description: Hugging Face repo id of the judge model.
default: Qwen/Qwen3-8B
train_split:
description: Preference pairs to train on, from HuggingFaceH4/ultrafeedback_binarized.
default: train_prefs[:10000]
eval_prompts:
description: Number of held-out prompts the two models answer for the judge.
default: "200"
hub_repo:
description: Hugging Face model repo to upload the aligned model to, as namespace/name. Leave empty to keep it on the workspace only.
default: ""
entry_point: main
blocks:
main:
tasks:
write_config:
name: Write training config
uses: swaleio/bash
args:
script: |
cat > "$WORKFLOW_STORAGE/dpo.yaml" <<EOF
model_name_or_path: ${{inputs.model}}
datasets:
- path: HuggingFaceH4/ultrafeedback_binarized
split: ${{inputs.train_split}}
columns: [chosen, rejected]
learning_rate: 5.0e-7
beta: 0.1
num_train_epochs: 1
per_device_train_batch_size: 4
gradient_accumulation_steps: 4
max_length: 1024
bf16: true
report_to: none
save_strategy: "no"
output_dir: $WORKFLOW_STORAGE/aligned
EOF
fetch_base:
name: Fetch base model
uses: swaleio/hf-download
args:
repo: ${{inputs.model}}
dest: ${{env.WORKFLOW_STORAGE}}/base
token: ${{secrets.hf_token}}
fetch_judge:
name: Fetch judge
uses: swaleio/hf-download
args:
repo: ${{inputs.judge}}
dest: ${{env.WORKFLOW_STORAGE}}/judge
token: ${{secrets.hf_token}}
train:
name: Align with DPO
uses: swaleio/trl-dpo
compute_type: a100-1
start_on:
- write_config
args:
config: ${{env.WORKFLOW_STORAGE}}/dpo.yaml
token: ${{secrets.hf_token}}
eval_requests:
name: Write held-out requests
uses: swaleio/duckdb
args:
sql: |
CREATE TABLE prompts AS
SELECT 'p-' || lpad((row_number() OVER () - 1)::VARCHAR, 5, '0') AS custom_id, prompt
FROM (SELECT prompt FROM 'hf://datasets/HuggingFaceH4/ultrafeedback_binarized/data/test_prefs-*.parquet'
LIMIT ${{inputs.eval_prompts}});
COPY prompts TO '${{env.WORKFLOW_STORAGE}}/eval-prompts.parquet' (FORMAT parquet);
COPY (SELECT custom_id, 'POST' AS method, '/v1/chat/completions' AS url,
{'model': '${{env.WORKFLOW_STORAGE}}/base',
'messages': [{'role': 'user', 'content': prompt}],
'max_tokens': 512, 'temperature': 0} AS body FROM prompts)
TO '${{env.WORKFLOW_STORAGE}}/requests-base.jsonl' (FORMAT json);
COPY (SELECT custom_id, 'POST' AS method, '/v1/chat/completions' AS url,
{'model': '${{env.WORKFLOW_STORAGE}}/aligned',
'messages': [{'role': 'user', 'content': prompt}],
'max_tokens': 512, 'temperature': 0} AS body FROM prompts)
TO '${{env.WORKFLOW_STORAGE}}/requests-aligned.jsonl' (FORMAT json);
answer_base:
name: Answer with the base model
uses: swaleio/vllm-batch
compute_type: a100-1
start_on:
- fetch_base
- eval_requests
args:
model: ${{env.WORKFLOW_STORAGE}}/base
input_file: ${{env.WORKFLOW_STORAGE}}/requests-base.jsonl
output_file: ${{env.WORKFLOW_STORAGE}}/answers-base.jsonl
answer_aligned:
name: Answer with the aligned model
uses: swaleio/vllm-batch
compute_type: a100-1
start_on:
- train
- eval_requests
args:
model: ${{env.WORKFLOW_STORAGE}}/aligned
input_file: ${{env.WORKFLOW_STORAGE}}/requests-aligned.jsonl
output_file: ${{env.WORKFLOW_STORAGE}}/answers-aligned.jsonl
judge_requests:
name: Write judge requests
uses: swaleio/duckdb
start_on:
- answer_base
- answer_aligned
args:
sql: |
-- Each pair is judged with the aligned answer first on even rows and second on odd
-- rows, so a judge that prefers one position does not favor either model.
CREATE TABLE pairs AS
SELECT p.custom_id, p.prompt,
b.response.body.choices[1].message.content AS base,
a.response.body.choices[1].message.content AS aligned,
regexp_extract(p.custom_id, '(\d+)$', 1)::INT % 2 = 0 AS aligned_first
FROM '${{env.WORKFLOW_STORAGE}}/eval-prompts.parquet' p
JOIN read_json('${{env.WORKFLOW_STORAGE}}/answers-base.jsonl') b USING (custom_id)
JOIN read_json('${{env.WORKFLOW_STORAGE}}/answers-aligned.jsonl') a USING (custom_id);
COPY pairs TO '${{env.WORKFLOW_STORAGE}}/pairs.parquet' (FORMAT parquet);
COPY (SELECT custom_id, 'POST' AS method, '/v1/chat/completions' AS url,
{'model': '${{env.WORKFLOW_STORAGE}}/judge',
'messages': [{'role': 'user', 'content':
'[Request]\n' || prompt ||
'\n\n[Answer 1]\n' || CASE WHEN aligned_first THEN aligned ELSE base END ||
'\n\n[Answer 2]\n' || CASE WHEN aligned_first THEN base ELSE aligned END ||
'\n\nWhich answer is more helpful, correct and clear? Reply 1, 2, or tie.'}],
'max_tokens': 4, 'temperature': 0,
'chat_template_kwargs': {'enable_thinking': false},
'structured_outputs': {'choice': ['1', '2', 'tie']}} AS body FROM pairs)
TO '${{env.WORKFLOW_STORAGE}}/judge-requests.jsonl' (FORMAT json);
judge:
name: Judge
uses: swaleio/vllm-batch
compute_type: a100-1
start_on:
- fetch_judge
- judge_requests
args:
model: ${{env.WORKFLOW_STORAGE}}/judge
input_file: ${{env.WORKFLOW_STORAGE}}/judge-requests.jsonl
output_file: ${{env.WORKFLOW_STORAGE}}/verdicts.jsonl
win_rate:
name: Win rate
uses: swaleio/duckdb
start_on:
- judge
args:
sql: |
CREATE TABLE verdicts AS
SELECT p.custom_id,
CASE trim(v.response.body.choices[1].message.content)
WHEN 'tie' THEN 'tie'
WHEN '1' THEN CASE WHEN p.aligned_first THEN 'aligned' ELSE 'base' END
ELSE CASE WHEN p.aligned_first THEN 'base' ELSE 'aligned' END END AS winner
FROM '${{env.WORKFLOW_STORAGE}}/pairs.parquet' p
JOIN read_json('${{env.WORKFLOW_STORAGE}}/verdicts.jsonl') v USING (custom_id);
COPY verdicts TO '${{env.WORKFLOW_STORAGE}}/verdicts.parquet' (FORMAT parquet);
SELECT winner, count(*) AS prompts, round(100.0 * count(*) / sum(count(*)) OVER (), 1) AS percent
FROM verdicts GROUP BY winner ORDER BY prompts DESC;
publish:
name: Upload when asked
start_on:
- win_rate
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}}/aligned
token: ${{secrets.hf_token}}
Licenses of the defaults
- Models: Qwen/Qwen2.5-0.5B-Instruct and Qwen/Qwen3-8B, Apache-2.0.
- Dataset: HuggingFaceH4/ultrafeedback_binarized, MIT.