Spending the Head Budget Where Specialization Hurts

Small language models can match much larger ones on a narrow domain when they are fine-tuned on it, but they pay a "specialization tax": they lose general skills they had before. Separately, compressing a model to run in real time on a small device often means deleting whole attention heads, the parallel sub-units inside each transformer layer that each decide which earlier tokens to look at. This note argues that these two problems should be solved together. We propose to measure, head by head, where the domain objective and the "stay general" objective pull the weights in opposite directions, and to use that conflict map to decide which heads to keep, which to delete, and how to update the survivors. The pruning step then also becomes the tool that limits the specialization tax.

The concrete bet is that the tax is concentrated in a minority of heads whose gradients conflict, and is not spread evenly across the network. If that is true, a model pruned to a fixed head budget, and therefore a fixed on-device latency, can keep more general ability at equal domain accuracy than a model that was fine-tuned and then compressed. It should also be much less sensitive to the weight that trades the two objectives against each other, which is otherwise a hand-tuned hyperparameter that has to be re-tuned for every domain.

Core Claim

Three bodies of work each hold one piece of this, and none connects them. Differentiable Subset Pruning of Transformer Heads (DSP) learns which $K$ of $H$ heads to keep using a smooth, trainable relaxation of "pick the top $K$." It removes 91.6% of BERT's heads on MNLI with a 5.5% accuracy drop. It optimizes a single task loss under a fixed annealing schedule and never asks what the deleted heads were doing for other abilities. Interpreting and Exploiting Functional Specialization in Multi-Head Attention under Multi-task Learning shows that heads specialize by task and that updating only task-critical heads reduces interference. Its head ranking is computed once, after training, and nothing is removed, so there is no speedup. Gradient Surgery for Safe LLM Fine-Tuning detects when a task gradient and an alignment gradient point in opposing directions, meaning their cosine similarity is negative, and projects the conflicting component away. It does this over the whole parameter vector, and it says nothing about structure or latency.

These works motivate and supply our method. The constraint comes from Specializing Smaller Language Models towards Multi-Step Reasoning. Its specialized Flan-T5 models collapse on BigBench Hard, and the authors attribute this to a capacity limit that only more parameters can fix. Our proposal tests that claim directly. If the tax were a pure capacity effect, no allocation of a fixed head budget could reduce it. If it is partly an interference effect localized in particular heads, conflict-aware allocation should move the trade-off curve.

The new element is a per-head conflict score that drives both selection and update. Let $g^{h}_{\text{dom}}$ be the gradient of the domain loss restricted to head $h$'s parameters, and $g^{h}_{\text{gen}}$ the gradient of a retention loss, defined as the KL divergence to the original unpruned model on general text. The score is

$$c_h = \frac{g^{h}_{\text{dom}} \cdot g^{h}_{\text{gen}}}{\lVert g^{h}_{\text{dom}} \rVert \, \lVert g^{h}_{\text{gen}} \rVert},$$

tracked as a running average during training. Heads with positive $c_h$ are shared: both objectives agree, and they are updated normally. Heads with strongly negative $c_h$ are where the tax is paid. For these, the domain gradient is projected onto the plane orthogonal to $g^{h}_{\text{gen}}$, the per-head version of SafeGrad, and a learned bonus to their keep-gate logit lets the selector decide whether keeping them is worth their cost. The hard top-$K$ constraint from DSP stays in place, so the head budget, and with it latency, is never traded away by the balancing step. This avoids the failure the research file flags for naive loss balancing applied to pruning gates, which can stall elimination short of the budget.

Research Program

The work proceeds in three stages, and each stage gates the next.

The first stage is purely diagnostic. We fully fine-tune Flan-T5 Large and XL on GSM8K chain-of-thought data and log $c_h$ for every head throughout training. We then ask whether conflict is localized: does a small fraction of heads account for most of the negative-cosine mass? We test this causally by restoring only the most-conflicted heads to their pre-fine-tuning weights and measuring how much BigBench Hard ability returns, compared with restoring an equal number of random heads. If restoring the conflicted heads recovers no more than random restoration does, the structural hypothesis is wrong, and we stop and report that finding.

The second stage builds the method: DSP-style top-$K$ gates, per-head conflict tracking, per-head projection, and the conflict-informed gate bonus. The extra cost is one additional backward pass for the retention loss plus a forward pass of the frozen teacher. This overhead is comparable to the 1.5x that SafeGrad reports. It is paid only at training time, and the deployed model carries none of it.

The third stage addresses hardware. DSP itself notes that pruning arbitrary heads yields irregular per-layer head counts that do not automatically become wall-clock speedups. We therefore run two variants. One is unconstrained. The other restricts each layer's kept-head count to a small set of kernel-friendly values. We report measured decoding latency on an NVIDIA Orin NX, the edge GPU used by Lightweight Temporal Transformer Decomposition for Federated Autonomous Driving, and not FLOPs.

A second domain checks that the effect is not specific to math. We use Verilog generation evaluated on Invited Paper: VerilogEval: Evaluating Large Language Models for Verilog Code Generation, with a small CodeGen model in the spirit of VeriGen: A Large Language Model for Verilog Code Generation. VerilogEval's observation that fine-tuning raises pass@1 but lowers pass@5 and pass@10 gives a second, within-domain measure of collapse to track alongside out-of-domain loss.

Baselines

The naive reference point has two parts: the unpruned base model with no domain training, and the unpruned model after plain fine-tuning on domain data. Together they bound the tax from both sides and fix the latency that pruning must beat.

The strongest existing specialization result is the distribution-matching distillation of Specializing Smaller Language Models towards Multi-Step Reasoning, which reports 27.1% on GSM8K for Flan-T5 11B. That number is for a larger, unpruned model. We use it as a ceiling reference, not a target. We will reproduce their recipe at our model sizes and use it as the domain-loss term inside every method, so that differences between methods come from allocation and update rules, not from the distillation signal. Beating this baseline means matching its GSM8K accuracy at our model size while retaining measurably more BigBench Hard accuracy. No reported BBH number for it was found on Lacuna, so we will measure it ourselves.

The strongest compression competitor is fine-tune-then-prune: domain distillation first, then DSP with its original single-loss objective down to the same $K$. DSP's own comparison shows greedy post-hoc head removal falling below 50% accuracy on MNLI at high sparsity, so we use DSP, not greedy pruning, as the fair compression arm. No reported result was found on Lacuna for DSP on GSM8K or BBH.

The strongest anti-tax competitor is replay mixing, which adds general text to the domain data with a static weight $\lambda$. Here $\lambda$ is grid-searched over the same number of full runs the proposed method uses for its own tuning, and pruning is done jointly with DSP so that latency matches. We add a GradNorm arm (GradNorm: Gradient Normalization for Adaptive Loss Balancing in Deep Multitask Networks) that balances the domain and retention losses dynamically. GradNorm reports roughly 5% better depth error than equal weighting on NYUv2, but it balances only magnitudes, not directions. We also run IAT-style static freezing of the heads ranked important for the domain.

Three ablations isolate the key ingredient. The first removes the conflict bonus from the gates while keeping per-head projection. The second keeps the conflict-informed gates but drops projection. The third keeps projection but applies it to the whole parameter vector instead of per head. A further control selects heads at random at the same $K$.

All arms share data, teacher, head budget $K$, training tokens, and tuning budget. Every method gets the same number of runs for its own hyperparameters: $\lambda$ for replay, $\alpha$ for GradNorm, and the gate-bonus scale for ours.

Why not just…?

Why not just mix general data into fine-tuning and tune the weight? This is the real threat, and it may close most of the gap at full size. Our claim is narrower. At a fixed head budget, replay still has to decide which heads to delete, and it decides blindly. Its result should also swing with $\lambda$, much as static weighting in Generating Images with Perceptual Similarity Metrics based on Deep Networks proved fragile. The $\lambda$-sweep experiment below settles this. If the best replay run matches our Pareto front and its sensitivity curve is as flat as ours, the method adds nothing and we will say so.

Why not just freeze irrelevant heads once, as IAT does? IAT keeps every head in memory, so it gives no latency benefit. Its ranking is also computed post hoc on a converged model, while conflict emerges during specialization. The IAT arm, once pruned to $K$ by its own ranking, tests whether a static ranking is enough.

Why not just prune after fine-tuning? By then the tax has already been written into the shared heads, and pruning cannot restore it. The fine-tune-then-prune arm and the stage-one restoration diagnostic measure exactly how much is lost by ordering the steps this way.

Why not just use GradNorm? GradNorm equalizes how fast the two losses fall, but two gradients of equal size can still point in opposite directions within a single head. The GradNorm arm, together with the "global projection only" ablation, separates the value of balancing magnitudes from the value of resolving conflict per head.

Proposed Experiments

The main result is a Pareto plot at three head budgets for Flan-T5 Large and XL. GSM8K accuracy, with MultiArith, ASDiv and SVAMP as in-domain transfer checks, is plotted against BigBench Hard accuracy. Each point is annotated with its measured Orin NX decoding latency. The Verilog replication uses VerilogEval pass@1, pass@5 and pass@10 as the domain axis and BBH as the general axis.

There are three decisive checks. The first is localization, from stage one: restoring the top-conflicted heads must recover substantially more BBH accuracy than restoring random heads. The second is the frontier: at equal latency and equal GSM8K accuracy, the proposed method must retain more BBH accuracy than tuned replay and than fine-tune-then-prune, consistently across seeds. The third is sensitivity: sweeping the domain/retention trade-off weight over an order of magnitude, the spread in BBH accuracy should be clearly smaller for our method than for replay. That last result is the hyperparameter claim, and it is the most useful outcome to a practitioner even if the frontier gain is modest.

If localization fails, the result is still informative. It supports the capacity explanation of the tax and rules out head-level fixes for it.