Abstract
Large Language Models (LLMs) like BERT often inherit societal biases from their training data. While previous research has focused on input embeddings or output predictions, the internal mechanisms of bias propagation remain under-explored. This study introduces a novel "bias detector" to trace gender bias flow within the Transformer architecture. By analyzing attention maps across 60,548 Wikipedia sentences, we find that the Query ($W_q$) and Key ($W_k$) matrices are the primary drivers of bias amplification, whereas the attention averaging process serves as a temporary dampener.
Introduction
LLMs are frequently treated as "black boxes" regarding bias. While it is well-documented that word embeddings contain stereotypical associations, the role of internal sub-modules—such as the Query, Key, and Value matrices—in processing these associations is less clear. This paper addresses this gap by investigating the "internal gender bias flow" within a pretrained BERT model to identify specific components responsible for bias generation.
Methods
The researchers developed a modular bias detector based on Attention Maps. The process involves:
- Gender Swapping: Creating pairs of sentences where gendered pronouns are swapped (e.g., "She accompanied him..." becomes "He accompanied her...").
- Attention Score (AS) Analysis: Measuring the "relation degree" between pronouns and occupations by summing the attention scores between them.
- Quantifying Bias: Defining a metric, $degree_{biased}$, which measures if the relative strength of the connection between a gender and an occupation remains consistent even when genders are swapped. If the model consistently links "nurse" more strongly to "she" than "he" regardless of sentence structure, bias is detected.
The detector was applied to 60,548 sentences from Wikipedia containing at least two opposite-gender pronouns and one occupation (e.g., "nurse," "programmer").
Results
The study reveals a "periodic" flow of bias throughout the model. The Query ($W_q$) and Key ($W_k$) matrices consistently introduce the most bias across all 12 layers of BERT. These components are responsible for determining how words relate to one another, making them the primary site where stereotypical associations are forged.
In contrast, the Value ($W_v$) matrix—which carries the actual content of the words—introduces much less bias. Interestingly, the "AvgAttention" stage (where the model averages information from different words) actually reduces the bias degree. It acts as a temporary dampener before bias is re-enhanced by subsequent residual connections and layer normalization at the end of the block.
Conclusion
This research pinpoints the $W_q$ and $W_k$ matrices as the primary sites of gender bias amplification in BERT. While limited to binary gender and the BERT-base architecture, these findings provide a roadmap for targeted debiasing strategies. By focusing on specific internal modules rather than the entire model, researchers can develop more efficient and explainable fairness interventions for future AI systems.
Summary updated