Skip to main content

Fine-tune, evaluate, quantize and publish

Source on GitHub: the workflows/fine-tune-evaluate-quantize folder, with its README and its workflow file.

The whole path from a base model to a quantized file that runs on a laptop: supervised fine-tuning with TRL, a benchmark score with lm-evaluation-harness, conversion to GGUF, 4-bit quantization with llama.cpp, and an upload to the Hugging Face Hub. Evaluation and conversion run side by side once training is done.

StepTask definitionCompute
Write the training configswaleio/bashcpu-4
Fine-tune the whole modelswaleio/trl-sfta100-1
Make the tokenizer config readable by the converterswaleio/python-3-13cpu-4
Evaluateswaleio/lm-evala100-1
Convert to a 16-bit GGUF fileswaleio/llamacpp-convertcpu-4
Quantizeswaleio/llamacpp-quantizecpu-4
Upload the quantized file, 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 tokenizer step exists because Transformers 5, which trains the model, saves extra_special_tokens in a form the converter's Transformers 4 cannot read; the step removes that key, whose tokens are also in tokenizer.json.

Inputs​

InputDefaultDescription
modelQwen/Qwen2.5-0.5B-InstructModel to fine-tune. All of its weights are trained.
datasetHuggingFaceH4/ultrafeedback_binarizedHugging Face dataset with a messages column.
splittrain_sft[:5000]Split, or slice of one, to train on.
eval_tasksgsm8klm-evaluation-harness tasks, separated by commas.
presetQ4_K_Mllama.cpp quantization preset.
hub_repoemptyHugging Face model repo to upload the quantized file to.

Training and the upload read the secret hf_token.

Output​

On the workspace: the fine-tuned model in sft/, the scores in eval/, and release/model-<preset>.gguf.

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: Fine-tune evaluate quantize and publish
description: Fine-tunes a small chat model with TRL, evaluates it with lm-evaluation-harness, converts it to GGUF, quantizes it, and uploads it to the Hugging Face Hub.
compute_type: cpu-4

inputs:
model:
description: Hugging Face repo id of the model to fine-tune. The whole model is trained, so keep it small or raise the compute type.
default: Qwen/Qwen2.5-0.5B-Instruct
dataset:
description: Hugging Face dataset with a messages column of chat turns.
default: HuggingFaceH4/ultrafeedback_binarized
split:
description: Split of the dataset to train on; a slice such as train_sft[:5000] trains on part of it.
default: train_sft[:5000]
eval_tasks:
description: lm-evaluation-harness tasks to score the fine-tuned model on, separated by commas.
default: gsm8k
preset:
description: llama.cpp quantization preset.
default: Q4_K_M
hub_repo:
description: Hugging Face model repo to upload the quantized GGUF file 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: |
mkdir -p "$WORKFLOW_STORAGE/gguf" "$WORKFLOW_STORAGE/release"
cat > "$WORKFLOW_STORAGE/sft.yaml" <<EOF
model_name_or_path: ${{inputs.model}}
datasets:
- path: ${{inputs.dataset}}
split: ${{inputs.split}}
columns: [messages]
learning_rate: 1.0e-5
num_train_epochs: 1
per_device_train_batch_size: 8
gradient_accumulation_steps: 2
max_length: 1024
bf16: true
report_to: none
save_strategy: "no"
output_dir: $WORKFLOW_STORAGE/sft
EOF

train:
name: Fine-tune
uses: swaleio/trl-sft
compute_type: a100-1
start_on:
- write_config
args:
config: ${{env.WORKFLOW_STORAGE}}/sft.yaml
token: ${{secrets.hf_token}}

tokenizer_config:
name: Make the tokenizer config readable by older Transformers
uses: swaleio/python-3-13
start_on:
- train
args:
script: |
# Transformers 5 saves extra_special_tokens as a list. The GGUF converter
# runs Transformers 4, which expects a mapping, so drop the key: the same
# tokens are listed in tokenizer.json.
import json, os
path = os.path.join(os.environ["WORKFLOW_STORAGE"], "sft", "tokenizer_config.json")
with open(path) as f:
config = json.load(f)
if isinstance(config.get("extra_special_tokens"), list):
del config["extra_special_tokens"]
with open(path, "w") as f:
json.dump(config, f, indent=2, ensure_ascii=False)
print("removed the list-form extra_special_tokens")

evaluate:
name: Evaluate
uses: swaleio/lm-eval
compute_type: a100-1
start_on:
- tokenizer_config
args:
model_dir: ${{env.WORKFLOW_STORAGE}}/sft
tasks: ${{inputs.eval_tasks}}
output_path: ${{env.WORKFLOW_STORAGE}}/eval

convert:
name: Convert to GGUF
uses: swaleio/llamacpp-convert
start_on:
- tokenizer_config
args:
model_dir: ${{env.WORKFLOW_STORAGE}}/sft
outfile: ${{env.WORKFLOW_STORAGE}}/gguf/model-bf16.gguf

quantize:
name: Quantize
uses: swaleio/llamacpp-quantize
start_on:
- convert
args:
input_gguf: ${{env.WORKFLOW_STORAGE}}/gguf/model-bf16.gguf
output_gguf: ${{env.WORKFLOW_STORAGE}}/release/model-${{inputs.preset}}.gguf
preset: ${{inputs.preset}}

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

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

Licenses of the defaults​