Concepts / Mixture-of-Experts Models

Mixture-of-Experts Models

Model merging builds a new model from several existing ones, and it can add value through better quality or a smaller memory footprint.

  • Programming

Merging Instead of Finetuning

Suppose you have two trained models, and each one is good at a different part of the same job. The natural question is whether you could combine them into a single model that is better than either one. This article covers that idea, which is called model merging. The hub this material belongs to is named Mixture-of-Experts Models, and the source material for it is organized around merging.

What do you think happens?

Model A answers the first 60% of a set of questions well. Model B answers the last 60% well. If you merge them into one model, could it answer more questions correctly than either model could alone?

  • No, merging can never beat the models it came from

  • Yes, the merged model could answer roughly 80% of the questions

Reveal answer
Answer:

Yes. The merged model could answer around 80% of the questions, which neither model could do alone.

Each source model covers a different slice of the questions. Combining them lets the merged model draw on both strengths, so its coverage can exceed what either source achieves by itself.

Definition

Model merging builds a new model from several existing ones. The value it adds can take the form of better quality, a smaller memory footprint, or both.

Merging is easiest to understand by comparing it with finetuning. Finetuning changes one model so that it becomes better at a particular job, and it does this by updating that model's weights with new data. Model merging takes a different route. It starts with several existing models and produces a new model by combining them. That new model is the custom model you end up using. The flexibility of this route is the main reason practitioners reach for it.

AspectFinetuningModel merging
Starting pointOne modelSeveral existing models
What changesThat model's weights, using new dataA new model is produced by combining the constituent models
Typical goalMake one model better at a particular jobProduce one model worth more than the group it came from
Training requiredGradient training on new dataCombining existing models, without further gradient training

Finetuning updates one model, while merging combines several existing models into a new one.

The purpose of merging is to produce one model that is worth more than the models it came from. The first kind of added value is better quality. The second is a smaller footprint: two models that each do a different task can sometimes be merged into one model that does both tasks with fewer parameters. This is especially attractive for adapter-based models, where two adapters trained on the same base model can be folded into a single adapter. Merging also supports multi-task finetuning. Instead of training one model to handle several tasks through more involved routes, you can build those capabilities by combining separately produced models.

Weight-Level Merging: Linear Combination and SLERP

Every merging technique answers one question: how should the parameters of the constituent models be combined? Summing is the answer that works directly on weight values. Each weight in the merged model is computed from the matching weights of the constituent models. Two summing methods are widely used, linear combination and spherical linear interpolation. Both assume the components being merged line up with each other, so the first practical step is to check their scales. If one model's parameters are far larger than the other's, you can rescale the models before summing so their values fall into a similar range.

Linear combination is a weighted average. For a single pair of matching weights, one from model A and one from model B, the merged weight is a blend controlled by a mixing value alpha. The expression below captures that blend.

Formula, step by step
  1. 1
    w_A is the weight taken from model A at a given position, and w_B is the matching weight from model B.
  2. 2
    alpha is the blending value. Choosing alpha between 0 and 1 weights the two models against each other.
  3. 3
    The merged weight w_merged is the weighted average of the two matching weights, computed once for every position in the weights.

Linear combination works best when the models share a base. Its meaning is clearest in that situation, because the two models then differ only in what their finetuning added. The next worked example uses small numbers to trace a single pair of weights through the blend. The numbers are illustrative and are not taken from a real model.

Worked example

Blending one pair of weights with linear combination

Model A has a weight of 0.8 at some position, and model B has a weight of 0.4 at the same position. Merge them with alpha equal to 0.5. What is the merged weight?

  1. 1
    Identify the matching weights

    Both weights sit at the same position, so they are the corresponding pair to blend. w_A is 0.8 and w_B is 0.4.

  2. 2
    Apply the blend

    Compute alpha times w_A plus (1 minus alpha) times w_B. With alpha set to 0.5, this is 0.5 times 0.8 plus 0.5 times 0.4.

  3. 3
    Compute the result

    0.5 times 0.8 is 0.4, and 0.5 times 0.4 is 0.2. Adding them gives 0.6.

Result

The merged weight is 0.6, which sits midway between the two source weights because alpha is 0.5.

Model A weight0.8Scale checkRescale if ranges differLinear combinationWeighted average with alphaMerged weight setOne value per positionModel B weight0.4SLERPShortest path on a spherematching weightsmatching weightsoption oneoption twoblended valueinterpolated value
How do the weight values of two source models get traced step by step into one merged weight set, and how does the SLERP path differ from a straight-line average?

Spherical linear interpolation, or SLERP, is the second summing method. Instead of a straight-line blend, SLERP follows the shortest path between two vectors on a sphere. Its result is therefore not simply the midpoint of the two values, and the path it traces differs from a straight-line average. SLERP merges exactly two models at a time. If you need to combine more than two models, you must do it in steps rather than in one SLERP operation.

Three Ways to Combine Parameters

Summing is only one of three broad answers to the merging question. Layer stacking takes whole layers from different models and places them one after another. Concatenation joins parameters together so that the merged component holds all of them. These are not mutually exclusive, and a single merge may sum some layers while stacking others. The three approaches differ most in how much they change the size and shape of the result.

Blend matchingweightsEach merged weight comesfrom matching weightsSame architectureParameter count matches theinputsTake whole layersLayers from differentmodelsPlace one afteranotherCan create a newarchitectureJoin all parametersMerged component holdsevery sourceParameter count growsEquals the sum of sourcecountschanges shape?changes shape?changes shape?
When two models are combined, which parameters end up added together, which end up stacked as consecutive layers, and which end up placed side by side, and how does the resulting parameter shape differ for each?
ApproachHow it combines parametersEffect on size and shapeTypical need afterward
SummingBlends corresponding weights, using linear combination or SLERPKeeps the same architecture and parameter count as the inputsWorks without further finetuning in the source material's description
Layer stackingTakes whole layers from different models and places them one after anotherCan produce a new architecture with a new number of parametersUsually needs further finetuning before it performs well
ConcatenationJoins parameters so the merged component contains all of themGrows the parameter count to the sum of the sourcesNot specified in the source material

The three approaches differ in whether they blend, rearrange, or join parameters, and that choice determines the resulting size.

Summing keeps the architecture because every merged weight is a blend of corresponding weights. Layer stacking can change the architecture, since it rearranges whole layers. Concatenation is the opposite stance from summing. Rather than blending parameters, it joins them, so the parameter count of the merged component is the sum of the parameter counts of all its constituent components.

Layer stacking is also called passthrough or frankenmerging. It builds a merged model by taking different layers from one or more models and placing them on top of one another. For example, you might take the first layer from model 1 and the second layer from model 2. A well-known early example is Goliath-120B, which was built from two finetuned Llama 2-70B models, called Xwin and Euryale. The merge took 72 of the 80 layers from each model and combined them. The trade-off is that stacked models usually need further finetuning before they perform well, unlike models produced by summing.

Task Vectors and Task Arithmetic

Linear combination takes on its clearest meaning when the models share a base. Suppose you finetune a base model for one specific task. If you subtract the base model's weights from the finetuned model's weights, the difference is a vector that captures what that finetuning added. This difference is called a task vector, and it is also known as a delta parameter. When you finetune with LoRA, you can construct the task vector directly from the LoRA weights.

Finetuned weightsBase plus finetuningchangesTask vectorFinetuned minus baseBase plus task vectorAdds the capabilityBase weightsStarting pointBase minus taskvectorRemoves the capabilitysubtractsubtracted fromaddsubtract
What does subtracting the base model's weights from a finetuned model's weights produce, and how do adding or subtracting these vectors change the resulting model's behavior?

Once you have task vectors, you can do arithmetic on them, and this is called task arithmetic. Adding two task vectors combines the capabilities they represent. Subtracting a task vector reduces a capability. Subtraction has a practical use: it can remove behaviors you do not want, such as invasive capabilities like facial recognition or biases picked up during pre-training.

Worked example

Task arithmetic on a single weight

A base model has a weight of 0.5 at some position. Finetuning for task X changes that weight to 0.8. Finetuning for task Y changes the same weight to 0.7. What is the task vector for task X, and what happens if you add it to the base?

  1. 1
    Compute the task vector for X

    Subtract the base weight from the finetuned weight: 0.8 minus 0.5 gives 0.3. The task vector entry for X at this position is 0.3.

  2. 2
    Add the task vector to the base

    Adding 0.3 to the base weight of 0.5 gives 0.8, which reproduces the finetuned weight for task X.

  3. 3
    Subtract the task vector from the base

    Subtracting 0.3 from the base weight of 0.5 gives 0.2, which moves the weight away from the behavior that task X added.

Result

The task vector for X is 0.3 at this position. Adding it restores the capability, and subtracting it removes it. These numbers are illustrative only.

Finetuning adjusts many parameters, but most of those adjustments are small and do little for the task. An adjustment that contributes nothing to performance is called redundant. Yadav and colleagues showed that a large portion of a task vector can be reset to zero with minimal loss in performance. Resetting a parameter means returning it to its base-model value, which sets its task vector entry to zero.

Redundant values are harmless when a model is used alone, but they can cause trouble after merging. Merging techniques such as TIES and DARE therefore prune redundant parameters from task vectors before combining them. Both papers reported that this step can noticeably improve the quality of the merged model. The benefit grows with the number of models being merged, because each additional model gives redundant parameters another chance to interfere with the other tasks.

Common Merging Mistakes

  • Common mistake

    Assuming merging is just finetuning with a different name

    Why it fails

    Finetuning updates one model's weights using new data. Merging starts from several existing models and produces a new model by combining them.

    Do this instead

    Describe merging as a combination of existing models, and reserve finetuning for updating a single model with new training data.

  • Common mistake

    Expecting a stacked model to work well immediately

    Why it fails

    Stacked models usually need further finetuning before they perform well, unlike models produced by summing.

    Do this instead

    Plan for finetuning after layer stacking, and do not assume the stacked result is ready to use.

  • Common mistake

    Using SLERP to merge three or more models at once

    Why it fails

    SLERP merges exactly two models at a time.

    Do this instead

    Merge in steps, combining two models first and then merging the result with the next model.

  • Common mistake

    Summing models whose parameter scales are very different

    Why it fails

    Both summing methods assume the components line up. A model with much larger values dominates the blend.

    Do this instead

    Check parameter scales and rescale the models so their values fall into a similar range before summing.

  • Common mistake

    Thinking subtracting a task vector always removes a capability cleanly

    Why it fails

    A task vector contains many small adjustments, and redundant values can interfere with other tasks after merging.

    Do this instead

    Consider pruning redundant parameters, as TIES and DARE do, before combining or subtracting task vectors.

Applying the Merging Vocabulary

MEDIUM

A team has two finetuned models that share the same base model. One was finetuned for summarizing legal documents and the other for translating them. They want one model that does both, and they want to keep the parameter count the same as the inputs. Which merging approach fits best, and what would you compute first to find each model's contribution?

Hints
  • Consider which approach keeps the architecture and parameter count the same as the inputs.
  • Think about what the difference between each finetuned model and the shared base model represents.
  • Summing with linear combination works directly on matching weights when the models share a base.
EASY

Explain in one or two sentences why subtracting a task vector from a base model can remove a capability, using the terms task vector and task arithmetic.

Hints
  • A task vector is the finetuned weights minus the base weights.
  • Task arithmetic adds or subtracts these vectors.

Key Takeaways

  • Model merging builds a new model from several existing ones and can add value through better quality or a smaller memory footprint. Unlike finetuning, it combines existing models rather than updating one model with new data.
  • Summing blends corresponding weights through linear combination, a weighted average, or through SLERP, which follows the shortest path between two vectors on a sphere and merges exactly two models at a time. Check parameter scales before summing.
  • Layer stacking rearranges whole layers and can change architecture and parameter count, but usually needs further finetuning. Concatenation joins parameters so the merged component's count equals the sum of its sources.
  • A task vector is the finetuned model's weights minus the base model's weights. Task arithmetic adds task vectors to combine capabilities and subtracts them to reduce or remove capabilities, after pruning redundant parameters such as TIES and DARE do.

Related concepts

More concepts starting with M