Causal Mediation Analysis in Language Models
Description
Causal mediation analysis traces which internal components of a language model carry the effect of a change in the input to the output. The analyst makes a controlled change to the input, such as swapping 'nurse' for 'man' in a prompt, and measures how the model's next-word probabilities shift. To find the indirect effect through one component (a single neuron, or an attention head, which is the part of the model that chooses which earlier words to focus on), they feed the model the original input but give that component the value it took on the changed input. To find the direct effect, they feed the changed input but hold that component at its original value. Repeating this across many components shows where in the network a behaviour is carried.
Example Use Cases
Safety
A safety team finds that a language model gives unsafe medical advice when a request is phrased as an instruction rather than a question. They compare outputs for the two phrasings and use mediation analysis to find which attention heads carry most of the shift in the probability of the unsafe reply, so a fix can target those heads and the analysis can be rerun to check it worked.
Explainability
A research team studying why a language model sometimes gets subject-verb agreement wrong in sentences with an intervening clause changes the number of the subject noun and measures the shift in the verb's probability. Mediation analysis then shows which neurons carry the number information, and whether the same neurons are used across sentence structures.
Limitations
- Needs white-box access and knowledge of the architecture. The analyst must read and overwrite activations at chosen points inside the model, so it cannot be used on a model offered only through an API, and a poorly chosen intervention point can give misleading conclusions.
- Conclusions hold for the contrast tested. Swapping 'nurse' for 'man' and tracking one probability ratio tests a single contrast, and a different prompt set or outcome measure can point to different components. Effects also differ between models: larger GPT-2 models showed larger effects, and masked language models matched less well.
- Every component and every prompt needs its own forward passes, so testing all the neurons or heads of a large model on many prompts is expensive. Analysts often test a subset or groups of components, which can miss effects elsewhere in the network.
- Single-component effects do not simply add up. Vig et al. found that intervening on all neurons together produced an effect far larger than the sum of the single-neuron effects, so ranking components one at a time can understate or misplace where a behaviour is carried.
- The split into direct and indirect effects is not guaranteed to be exact. In a non-linear network the total effect need not equal their sum, and Vig et al. report only an approximate match for GPT-2, so the split should be checked rather than assumed.
Resources
Research Papers
Investigating Gender Bias in Language Models Using Causal Mediation Analysis
Common methods for interpreting neural models in natural language processing typically examine either their structure or their behavior, but not both. We propose a methodology grounded in the theory of causal mediation analysis for interpreting which parts of a model are causally implicated in its behavior. It enables us to analyze the mechanisms by which information flows from input to output through various model components, known as mediators. We apply this methodology to analyze gender bias in pre-trained Transformer language models. We study the role of individual neurons and attention heads in mediating gender bias across three datasets designed to gauge a model's sensitivity to gender bias. Our mediation analysis reveals that gender bias effects are (i) sparse, concentrated in a small part of the network; (ii) synergistic, amplified or repressed by different components; and (iii) decomposable into effects flowing directly from the input and indirectly through the mediators.