# Fine-tune, evaluate, quantize and publish

> **About this page**
>
> - Kind: Example
> - Summary: 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.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/fine-tune-evaluate-quantize
> - CLI reference for its commands: [`swale workflow create`](https://docs.swale.io/reference/cli-reference.md#workflow-create), [`swale workflow run start`](https://docs.swale.io/reference/cli-reference.md#workflow-run-start)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

Source on GitHub: the [`workflows/fine-tune-evaluate-quantize`](https://github.com/swaleio/examples/tree/main/workflows/fine-tune-evaluate-quantize) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/fine-tune-evaluate-quantize/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/fine-tune-evaluate-quantize/workflow.yaml).

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.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Write the training config | [`swaleio/bash`](https://hub.swale.io/swaleio/bash) | `cpu-4` |
| Fine-tune the whole model | [`swaleio/trl-sft`](https://hub.swale.io/swaleio/trl-sft) | `a100-1` |
| Make the tokenizer config readable by the converter | [`swaleio/python-3-13`](https://hub.swale.io/swaleio/python-3-13) | `cpu-4` |
| Evaluate | [`swaleio/lm-eval`](https://hub.swale.io/swaleio/lm-eval) | `a100-1` |
| Convert to a 16-bit GGUF file | [`swaleio/llamacpp-convert`](https://hub.swale.io/swaleio/llamacpp-convert) | `cpu-4` |
| Quantize | [`swaleio/llamacpp-quantize`](https://hub.swale.io/swaleio/llamacpp-quantize) | `cpu-4` |
| Upload the quantized file, when `hub_repo` is set | [`swaleio/hf-upload`](https://hub.swale.io/swaleio/hf-upload) | `cpu-4` |

The Compute column gives each step's [compute type](https://docs.swale.io/explanation/core-concepts.md#compute-types), 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

| Input | Default | Description |
|-------|---------|-------------|
| `model` | `Qwen/Qwen2.5-0.5B-Instruct` | Model to fine-tune. All of its weights are trained. |
| `dataset` | `HuggingFaceH4/ultrafeedback_binarized` | Hugging Face dataset with a `messages` column. |
| `split` | `train_sft[:5000]` | Split, or slice of one, to train on. |
| `eval_tasks` | `gsm8k` | lm-evaluation-harness tasks, separated by commas. |
| `preset` | `Q4_K_M` | llama.cpp quantization preset. |
| `hub_repo` | empty | Hugging 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](https://docs.swale.io/getting-started.md) sets them up.

1. Save the file under [The workflow file](#the-workflow-file) as `workflow.yaml`, and add it to a project:

   ```bash
   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](https://docs.swale.io/how-to-guides/use-secrets-in-workflows.md) shows.

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

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

   Each input you leave out takes its default from [Inputs](#inputs), and `--input <name>=<value>` gives it another value.

[Create and manage workflows](https://docs.swale.io/how-to-guides/create-and-manage-workflows.md) covers the other ways to add a workflow, [Start and manage workflow runs](https://docs.swale.io/how-to-guides/start-and-manage-workflow-runs.md) how to follow a run, and [View and stream task logs](https://docs.swale.io/how-to-guides/view-and-stream-task-logs.md) how to read its tasks' logs.

## The workflow file

[Workflow definition syntax](https://docs.swale.io/reference/workflow-definition-syntax.md) describes each key in it.

```yaml title="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

- Model: [Qwen/Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct), Apache-2.0. The fine-tuned model keeps it.
- Training data: [HuggingFaceH4/ultrafeedback_binarized](https://huggingface.co/datasets/HuggingFaceH4/ultrafeedback_binarized), MIT.
- Evaluation data, which lm-evaluation-harness downloads: [openai/gsm8k](https://huggingface.co/datasets/openai/gsm8k), MIT. Other tasks bring their own datasets and licenses.
