Merge fine-tuned models with TIES

Why it matters Open model merges on the Hugging Face leaderboard are often TIES merges of several fine-tunes.

TIES merging combines several fine-tunes of one base model by trimming small changes and resolving sign conflicts.

Write ties_merge(base, finetuned, k, lam=1.0). base is (d,), finetuned is a list of (d,) arrays. Task vectors t_i = ft_i - base. Trim: in each t_i keep its top k entries by absolute value (stable ties by index), zero the rest. Elect: sign per coordinate = sign of the sum of trimmed vectors. Disjoint mean: average only the trimmed entries whose sign matches the elected sign (entries that are 0 do not count); coordinates with none get 0. Return base + lam * merged.

Signaturedef ties_merge(base, finetuned, k, lam=1.0)

Example

base [0, 0], finetuned [[1, 0], [-3, 0]], k 1: trimmed [1, 0] and [-3, 0], elected sign -1 on coordinate 0, only -3 agrees, result [-3, 0].

Understand the idea

TIES merging combines several fine-tuned copies of one base model into one model. Each fine-tune's change from the base is a task vector. Three steps: trim each task vector to its k largest changes, elect a sign per coordinate from the sum of trimmed vectors, then average only the entries that agree with that sign. This stops opposite updates from cancelling out. Worked example: base [0, 0, 0], task vectors [0.5, -0.1, 2] and [-0.8, 0.3, 1], k = 2. Trim: [0.5, 0, 2] and [-0.8, 0, 1]. Sums: [-0.3, 0, 3], so signs [-, 0, +]. Coordinate 0: only -0.8 agrees, giving -0.8. Coordinate 1: nothing kept, giving 0. Coordinate 2: both agree, (2 + 1) / 2 = 1.5. With lam 1 the result is [-0.8, 0, 1.5]. Rule: t_i = ft_i - base; keep the top k by absolute value (stable ties by index); sign = sign(sum of trimmed); merged = mean of nonzero trimmed entries whose sign matches, or 0 if none; return base + lam * merged. Used in: model merging tools that combine specialist LLM fine-tunes.

Shapes and data flow

task vectors T (n, d) -> trim top k per row -> Tr (n, d)
sign(sum over n) (d,) -> agree mask (n, d) -> mean -> (d,)
solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: exampleties_merge([0, 0], [[1, 0], [-3, 0]], 1)
  • Sample 2: three modelsties_merge([1, 1, 1, 1], [[2, 1, 0, 1], [1.5, 1, 3, 1], [0, 1, 2.5, 4]], 2)