Fine-tune a translation model
Source on GitHub: the workflows/translation folder, with its README and its workflow file.
Fine-tunes a translation model on sentence pairs, then translates every held-out sentence and scores the translations with BLEU and chrF. The default fine-tunes a German-to-English Marian model on WMT24++ pairs, a small, recent and carefully translated set.
| Step | Task definition | Compute |
|---|---|---|
Fetch this repository's scripts at examples_ref | swaleio/git-clone | cpu-4 |
| Fetch the model | swaleio/hf-download | cpu-4 |
| Prepare the splits, leaving out pairs flagged as bad | swaleio/duckdb | cpu-4 |
Tokenize sources and targets (preprocess.py) | swaleio/python-3-13 | cpu-4 |
Train (train.py) | swaleio/transformers | a100-1 |
Translate the eval sentences, with beam search (generate.py) | swaleio/transformers | a100-1 |
Score BLEU and chrF (score.py) | swaleio/python-3-13 | cpu-4 |
Upload the 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.
The scripts are the summarization workflow's.
What stays one script: the training loop, which holds the model, the optimizer and the batches together on one GPU.
Inputs
| Input | Default | Description |
|---|---|---|
model | Helsinki-NLP/opus-mt-de-en | Translation model to fine-tune. |
pairs | the English–German file of google/wmt24pp | JSON Lines or Parquet file of pairs, as a DuckDB path. |
source_column, target_column | target, source | Columns holding the sentence to translate and its reference: WMT24++ translates English into German, so its German target is the source here. |
eval_rows | 160 | Pairs held out for evaluation; the rest train. |
prefix | empty | Text put before every source, for models that expect one. |
epochs | 3 | Training epochs. |
examples_ref | main | Branch, tag or full commit SHA of this repository whose scripts run. |
hub_repo | empty | Hugging Face model repo to upload the model to. |
The download and the upload read the secret hf_token. The prepare step reads
WMT24++'s is_bad_source, document_id and segment_id columns; edit it for
other data.
Output
The fine-tuned model in model/ on the workspace, each eval sentence's
reference and translation in predictions.parquet, and BLEU and chrF in
metrics.json.
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: Fine-tune a translation model
description: Fine-tunes a translation model on sentence pairs, with tokenizing on a CPU, training and generation on a GPU, and BLEU and chrF computed on a CPU.
compute_type: cpu-4
inputs:
model:
description: Hugging Face repo id of the sequence-to-sequence model to fine-tune.
default: Helsinki-NLP/opus-mt-de-en
pairs:
description: JSON Lines or Parquet file of sentence pairs, as a DuckDB path.
default: hf://datasets/google/wmt24pp/en-de_DE.jsonl
source_column:
description: Column holding the sentence to translate.
default: target
target_column:
description: Column holding its reference translation.
default: source
eval_rows:
description: Pairs held out for evaluation; the rest train.
default: "160"
prefix:
description: Text put before every source, as the model expects.
default: ""
epochs:
description: Training epochs.
default: "3"
examples_ref:
description: Branch, tag or full commit SHA of swaleio/examples whose scripts run.
default: main
hub_repo:
description: Hugging Face model repo to upload the fine-tuned model to, as namespace/name. Leave empty to keep it on the workspace only.
default: ""
entry_point: main
blocks:
main:
tasks:
fetch_scripts:
name: Fetch the example scripts
uses: swaleio/git-clone
args:
repository_url: https://github.com/swaleio/examples.git
revision: ${{inputs.examples_ref}}
dest: ${{env.WORKFLOW_STORAGE}}/examples
fetch_model:
name: Fetch the model
uses: swaleio/hf-download
args:
repo: ${{inputs.model}}
dest: ${{env.WORKFLOW_STORAGE}}/base-model
token: ${{secrets.hf_token}}
prepare:
name: Prepare the splits
uses: swaleio/duckdb
args:
sql: |
-- WMT24++ translates English into German; this workflow trains German to English,
-- so the German target is the source here. Pairs flagged as bad sources are left out.
CREATE TABLE pairs AS
SELECT "${{inputs.source_column}}" AS source, "${{inputs.target_column}}" AS target,
row_number() OVER (ORDER BY hash(document_id, segment_id)) AS n
FROM '${{inputs.pairs}}' WHERE NOT is_bad_source;
COPY (SELECT source, target FROM pairs WHERE n > ${{inputs.eval_rows}})
TO '${{env.WORKFLOW_STORAGE}}/train.parquet' (FORMAT parquet);
COPY (SELECT source, target FROM pairs WHERE n <= ${{inputs.eval_rows}})
TO '${{env.WORKFLOW_STORAGE}}/eval.parquet' (FORMAT parquet);
SELECT (SELECT count(*) FROM '${{env.WORKFLOW_STORAGE}}/train.parquet') AS train_pairs,
(SELECT count(*) FROM '${{env.WORKFLOW_STORAGE}}/eval.parquet') AS eval_pairs;
tokenize:
name: Tokenize
uses: swaleio/python-3-13
start_on:
- fetch_scripts
- fetch_model
- prepare
args:
script: |
import os, subprocess, sys
storage = os.environ["WORKFLOW_STORAGE"]
examples = f"{storage}/examples"
subprocess.run([sys.executable, "-m", "pip", "install", "--quiet", "--target", "/tmp/deps",
"-r", f"{examples}/requirements/cpu-tokenize.txt"], check=True)
subprocess.run([sys.executable, f"{examples}/workflows/summarization/preprocess.py",
"--model", f"{storage}/base-model", "--prefix", "${{inputs.prefix}}",
"--source-max-length", "256", "--target-max-length", "256",
"--train", f"{storage}/train.parquet", "--eval", f"{storage}/eval.parquet",
"--output", f"{storage}/tokenized"],
env={**os.environ, "PYTHONPATH": "/tmp/deps"}, check=True)
train:
name: Train
uses: swaleio/transformers
compute_type: a100-1
start_on:
- tokenize
args:
script: |
import os, subprocess, sys
storage = os.environ["WORKFLOW_STORAGE"]
subprocess.run([sys.executable, f"{storage}/examples/workflows/summarization/train.py",
"--model", f"{storage}/base-model", "--data", f"{storage}/tokenized",
"--epochs", "${{inputs.epochs}}", "--learning-rate", "2e-5",
"--output", f"{storage}/model"], check=True)
generate:
name: Generate for the eval split
uses: swaleio/transformers
compute_type: a100-1
start_on:
- train
args:
script: |
import os, subprocess, sys
storage = os.environ["WORKFLOW_STORAGE"]
subprocess.run([sys.executable, f"{storage}/examples/workflows/summarization/generate.py",
"--model", f"{storage}/model", "--eval", f"{storage}/eval.parquet",
"--prefix", "${{inputs.prefix}}", "--source-max-length", "256",
"--max-new-tokens", "256", "--output", f"{storage}/predictions.parquet"], check=True)
score:
name: Score
uses: swaleio/python-3-13
start_on:
- generate
args:
script: |
import os, subprocess, sys
storage = os.environ["WORKFLOW_STORAGE"]
examples = f"{storage}/examples"
subprocess.run([sys.executable, "-m", "pip", "install", "--quiet", "--target", "/tmp/deps",
"-r", f"{examples}/requirements/cpu-score-bleu.txt"], check=True)
subprocess.run([sys.executable, f"{examples}/workflows/summarization/score.py",
"--predictions", f"{storage}/predictions.parquet", "--metric", "bleu",
"--output", f"{storage}/metrics.json"],
env={**os.environ, "PYTHONPATH": "/tmp/deps"}, check=True)
publish:
name: Upload when asked
start_on:
- score
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}}/model
token: ${{secrets.hf_token}}
Licenses
- Model: Helsinki-NLP/opus-mt-de-en, Apache-2.0.
- Dataset: google/wmt24pp, Apache-2.0.
- Packages the steps install: Transformers, Tokenizers, Datasets, SentencePiece and the Hugging Face Hub client to tokenize, and sacrebleu and PyArrow to score, with their dependencies, all under permissive licenses (Apache-2.0, BSD, MIT and PSF).