Skip to main content

Merge two fine-tunes

Source on GitHub: the workflows/merge-models folder, with its README and its workflow file.

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.

StepTask definitionCompute
Write the merge configswaleio/bashcpu-4
Mergeswaleio/mergekitcpu-4
Evaluate the merge, and each parentswaleio/lm-evala100-1 each
Compare the three scoresswaleio/duckdbcpu-4
Upload the merge, 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 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​

InputDefaultDescription
baseQwen/Qwen2.5-0.5BBase model of both fine-tunes.
model_aQwen/Qwen2.5-0.5B-InstructFirst fine-tune; the merge takes its tokenizer and chat template.
model_bnumind/NuExtract-1.5-tinySecond fine-tune: NuMind's model for extracting structured data from text.
eval_taskgsm8klm-evaluation-harness task to score them on.
hub_repoemptyHugging 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 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: 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​