Structured Pruning › Head Pruning
Language AI HandbookAll chapters Structured Pruning › Head PruningOne section · Sections in this chapter3 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Transformer attention layers contain multiple parallel attention heads. Each head applies a separate set of queries, keys, and values to the input, then projects the results back to the model dimension. The motivation for multiple heads is that different heads can attend to different aspects of the input: one head might track syntactic subject-verb dependencies while another tracks positional relationships, and another might focus on coreference.
The multi-head attention computation concatenates the outputs of all heads and applies a final linear projection. The full attention output for a layer is:
where each head computes:
where:
- is the number of heads
- is the head dimension
- are the query, key, and value projections for head
- is the output projection that assembles all heads
Because each head's contribution is additive after the output projection, you can set any head's output to zero and the remaining heads still produce a valid (though potentially degraded) result. This additive structure is precisely what makes head pruning possible: it is a well-defined operation with predictable effects.
Importance Scores for Heads
The most direct importance score for a head is its sensitivity to removal. You can estimate this by computing how much the loss increases when you zero out that head's output. Formally, for head in layer , define the importance score as:
where:
- is the loss with all heads present
- is the loss with head in layer zeroed out
- is the absolute value, giving a non-negative importance score
Computing this exactly requires a separate forward pass for each head, which is expensive. A first-order approximation uses gradient information instead. For a head parameterized by its output projection weights , the gradient-based importance score is:
where:
- is the gradient of the loss with respect to the head's output projection weights
- is element-wise multiplication
- is the Frobenius norm, giving the overall magnitude of the score
This gradient-weight product, sometimes called the Taylor expansion criterion, approximates the change in loss from zeroing out the head. Intuitively, a head with large weights and large gradients is one where the model is heavily relying on that head and adjusting it actively. Large values indicate that the weights are large and their gradients are large, meaning the head contributes substantially to the loss. Small values indicate the head could be removed with little effect. This approximation is derived from a first-order Taylor expansion of the loss around the current parameters, treating the removal of the head as a perturbation.
A simpler but surprisingly effective approach uses attention head activation norms. For each head, compute the mean magnitude of its output across a calibration dataset:
where is the output vector produced by head in layer for input , before it is passed through the output projection.
Activation norm-based importance has the advantage that it requires no gradient computation and can be collected during normal inference. The disadvantage is that it measures how active a head is, not how important it is to the final output. A very active head whose output is largely cancelled by the output projection could score high by this criterion yet contribute little to the model's predictions. In practice, gradient-based scores tend to be more accurate, but activation norms are a useful fast approximation.
Head Pruning Mechanics
Once importance scores are computed, pruning proceeds by selecting the heads to remove and modifying the weight matrices accordingly. An attention layer in a transformer has query, key, value, and output projection matrices. With heads each of dimension :
Each head uses columns through of these matrices. The output projection assembles the contributions from all heads. To remove head , you delete the corresponding columns from , , , and the corresponding rows from .
After removing heads, the model dimension of the attention projections shrinks from to . This directly reduces the memory footprint of the attention layer and the number of FLOPs in every forward pass. The change is structural and hardware-visible: the new, smaller weight matrices are stored in contiguous memory, and standard matrix multiplication routines operate on the smaller tensors with no overhead.
Pruning decisions can be made at the layer level (removing a fixed number of heads from each layer independently) or globally (ranking all heads across all layers together and removing the globally least important ones). Global pruning is generally preferable because it finds that some layers have more redundant heads than others. In BERT, for example, researchers consistently found that middle layers had more redundant heads than early or late layers. A layer-level strategy that forces uniform pruning across layers would retain unnecessary heads in middle layers while potentially over-pruning early or late layers.
Recovery Fine-Tuning After Head Pruning
Removing heads causes an immediate quality drop. The model was trained with all heads present, and the surviving heads were not optimized to compensate for the removed ones. A brief fine-tuning pass, sometimes called recovery fine-tuning, allows the remaining heads to adapt. In practice, this recovery phase needs far fewer steps than the original training because you are not learning from scratch but adjusting weights that already encode useful representations.
The amount of quality you can recover depends on how aggressively you pruned. Removing 10-20% of heads typically recovers fully within a small number of fine-tuning steps. Removing 50% or more often results in permanent quality degradation, though the model may still be useful for applications that tolerate some accuracy loss in exchange for speed. The recovery fine-tuning learning rate should be substantially lower than the original training learning rate to avoid disrupting the representations that the surviving heads have already learned.
One practical consideration is the order of operations. If you plan to prune both layers and heads, the recommended order is: compute layer importance and remove layers first, then compute head importance on the reduced model and remove heads, then fine-tune. This order matters because removing layers changes the distribution of attention patterns, which can shift head importance scores. Computing head importance on the original model and then removing both layers and heads simultaneously tends to produce worse results.