# Merge two fine-tunes

> **About this page**
>
> - Kind: Example
> - Summary: Merges two fine-tunes of one base model with mergekit's TIES method, scores the merge and both parents on the same benchmark, and uploads the merge.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/merge-models
> - 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/merge-models`](https://github.com/swaleio/examples/tree/main/workflows/merge-models) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/merge-models/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/merge-models/workflow.yaml).

Combines two fine-tunes of the same base model into one, without training. A
TIES merge keeps the largest changes each fine-tune made to the base weights,
settles their disagreements by sign, and adds them back to the base. The
merge and both parents are then scored on the same benchmark, side by side, so
you can see what the merge kept. The defaults merge Qwen's chat model with
NuMind's extraction model, both fine-tuned from Qwen2.5-0.5B.

Both fine-tunes must come from `base` itself. A model built by long further
pretraining has moved too far from it: Qwen2.5-Coder-0.5B-Instruct, merged
with Qwen2.5-0.5B-Instruct this way, answered math questions with strings of
zeros.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Write the merge config | [`swaleio/bash`](https://hub.swale.io/swaleio/bash) | `cpu-4` |
| Merge | [`swaleio/mergekit`](https://hub.swale.io/swaleio/mergekit) | `cpu-4` |
| Evaluate the merge, and each parent | [`swaleio/lm-eval`](https://hub.swale.io/swaleio/lm-eval) | `a100-1` each |
| Compare the three scores | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Upload the merge, 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 parents are scored while the merge runs. The
merge itself runs on the CPU.

Merge only models whose licenses allow it, and give the merge a license that
respects both parents.

## Inputs

| Input | Default | Description |
|-------|---------|-------------|
| `base` | `Qwen/Qwen2.5-0.5B` | Base model of both fine-tunes. |
| `model_a` | `Qwen/Qwen2.5-0.5B-Instruct` | First fine-tune; the merge takes its tokenizer and chat template. |
| `model_b` | `numind/NuExtract-1.5-tiny` | Second fine-tune: NuMind's model for extracting structured data from text. |
| `eval_task` | `gsm8k` | lm-evaluation-harness task to score them on. |
| `hub_repo` | empty | Hugging Face model repo to upload the merge to. |

The merge, the parents' evaluations and the upload read the secret `hf_token`.

## Output

The merged model in `merged/` on the workspace, with a model card recording the
merge config, the scores in `eval/`, and the comparison 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](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: Merge two fine-tunes
description: Merges two fine-tunes of one base model with mergekit's TIES method, scores the merge and both parents on the same benchmark, and uploads the merge.
compute_type: cpu-4

inputs:
  base:
    description: Hugging Face repo id of the base model both fine-tunes come from.
    default: Qwen/Qwen2.5-0.5B
  model_a:
    description: Hugging Face repo id of the first fine-tune.
    default: Qwen/Qwen2.5-0.5B-Instruct
  model_b:
    description: Hugging Face repo id of the second fine-tune.
    default: numind/NuExtract-1.5-tiny
  eval_task:
    description: The lm-evaluation-harness task the merge and both parents are scored on.
    default: gsm8k
  hub_repo:
    description: Hugging Face model repo to upload the merge to, as namespace/name. Leave empty to keep it on the workspace only.
    default: ""

entry_point: main

blocks:
  main:
    tasks:
      write_config:
        name: Write merge config
        uses: swaleio/bash
        args:
          script: |
            cat > "$WORKFLOW_STORAGE/merge.yaml" <<EOF
            merge_method: ties
            base_model: ${{inputs.base}}
            models:
              - model: ${{inputs.model_a}}
                parameters:
                  density: 0.5
                  weight: 0.5
              - model: ${{inputs.model_b}}
                parameters:
                  density: 0.5
                  weight: 0.5
            parameters:
              normalize: true
            tokenizer_source: ${{inputs.model_a}}
            dtype: bfloat16
            EOF

      merge:
        name: Merge
        uses: swaleio/mergekit
        start_on:
          - write_config
        args:
          config: ${{env.WORKFLOW_STORAGE}}/merge.yaml
          output_dir: ${{env.WORKFLOW_STORAGE}}/merged
          token: ${{secrets.hf_token}}

      evaluate_merge:
        name: Evaluate the merge
        uses: swaleio/lm-eval
        compute_type: a100-1
        start_on:
          - merge
        args:
          model_dir: ${{env.WORKFLOW_STORAGE}}/merged
          tasks: ${{inputs.eval_task}}
          output_path: ${{env.WORKFLOW_STORAGE}}/eval/merge

      evaluate_a:
        name: Evaluate fine-tune A
        uses: swaleio/lm-eval
        compute_type: a100-1
        args:
          model_dir: ${{inputs.model_a}}
          tasks: ${{inputs.eval_task}}
          output_path: ${{env.WORKFLOW_STORAGE}}/eval/a
          token: ${{secrets.hf_token}}

      evaluate_b:
        name: Evaluate fine-tune B
        uses: swaleio/lm-eval
        compute_type: a100-1
        args:
          model_dir: ${{inputs.model_b}}
          tasks: ${{inputs.eval_task}}
          output_path: ${{env.WORKFLOW_STORAGE}}/eval/b
          token: ${{secrets.hf_token}}

      compare:
        name: Compare
        uses: swaleio/duckdb
        start_on:
          - evaluate_merge
          - evaluate_a
          - evaluate_b
        args:
          sql: |
            CREATE TABLE scores AS
              SELECT CASE WHEN filename LIKE '%/eval/merge/%' THEN 'merge'
                          WHEN filename LIKE '%/eval/a/%' THEN 'A: ${{inputs.model_a}}'
                          ELSE 'B: ${{inputs.model_b}}' END AS model,
                     unnest(map_entries(results."${{inputs.eval_task}}"::MAP(VARCHAR, JSON)), recursive := true)
              FROM read_json('${{env.WORKFLOW_STORAGE}}/eval/*/*/results_*.json',
                             filename = true, columns = {'results': 'JSON'});
            PIVOT (SELECT model, key AS metric, round(value::DOUBLE, 4) AS score
                   FROM scores WHERE key NOT LIKE '%stderr%' AND key <> 'sample_len'
                     AND try_cast(value AS DOUBLE) IS NOT NULL)
              ON model USING first(score) GROUP BY metric ORDER BY metric;

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

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

## Licenses of the defaults

- Models: [Qwen/Qwen2.5-0.5B](https://huggingface.co/Qwen/Qwen2.5-0.5B) and [Qwen/Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct), Apache-2.0; [numind/NuExtract-1.5-tiny](https://huggingface.co/numind/NuExtract-1.5-tiny), MIT. The merge carries both: Apache-2.0, keeping NuExtract's MIT notice.
- Evaluation data: [openai/gsm8k](https://huggingface.co/datasets/openai/gsm8k), MIT.
- The merge runs [mergekit](https://github.com/arcee-ai/mergekit), LGPL-3.0, inside `swaleio/mergekit`.
