Mitigating Gender Bias in Code Large Language Models via Model Editing

Zhanyue Qin, Haochuan Wang, Zecheng Wang, Deyuan Liu, Cunhang Fan, Zhao Lv, Zhiying Tu, Dianhui Chu, Dianbo SuiarXiv 2024 · DOI

Abstract

Code Large Language Models (LLMs) often inherit and amplify gender stereotypes found in their training data, such as associating specific professions with a particular gender. We introduce MG-Editing (Multi-Granularity model Editing), a surgical approach that identifies and modifies specific parameters—down to the individual neuron—to align model outputs with real-world gender distributions without destroying the model's general programming capabilities.

Introduction

As LLMs become central to software development, their "unscreened" training on massive codebases has a side effect: they learn social biases. For instance, a model might assign a significantly higher probability to a nurse being female than male, reflecting skewed training data rather than objective reality.

Existing debiasing methods, such as data cleaning or post-processing, are often computationally expensive or risk "model collapse," where the model loses its functional code-generation abilities. This research seeks a way to "edit" the model's bias precisely and efficiently by targeting the internal representations of the model itself.

Methods: The MG-Editing Framework

The proposed MG-Editing framework operates in two distinct phases: locating the biased parameters and then surgically editing them across various levels of granularity.

Overview of the MG-Editing framework, illustrating the locating and editing phases across different granularities.

1. The Locating Phase

Before bias can be corrected, it must be precisely located within the model's architecture. The authors investigate five levels of granularity: full parameters, layers, modules (e.g., Attention or MLP), rows of weight matrices, and individual neurons. Techniques such as the Logit Lens—which projects internal hidden states into the final vocabulary space—and gradient-based cosine similarity are employed to pinpoint the specific parameters driving biased predictions.

2. The Editing Phase

Once the biased components are identified, targeted fine-tuning is performed. A critical component of this phase is the inclusion of a recovery loss ($L_{recover}$) in the objective function. This ensures that while the model learns to be fairer, it retains its ability to solve complex coding problems, preventing the loss of general knowledge or "catastrophic forgetting."

Datasets and Evaluation

The study utilizes two primary datasets to evaluate performance and fairness:

  • CodeGenBias: A new benchmark containing code templates involving 320 different professions and various modifiers (e.g., "find_best_nurses") to trigger and measure gendered pronoun prediction.
  • HumanEval: A standard industry benchmark for code generation used to ensure that editing for bias does not degrade the model's functional performance (locality).

Key Insights

Factual Fairness vs. Absolute Fairness

The authors propose the FB-Score (Factual Bias Score), arguing that "absolute fairness" (a 50/50 split) can be unrealistic. Instead, factual fairness measures how much a model's prediction deviates from actual real-world labor statistics. If 90% of nurses are female in reality, a model predicting 90% is considered "factually fair."

The "Correction" Layer

Research indicates that bias is not uniformly distributed. In models like CodeGen-2B-mono, gender preferences often emerge or undergo a "correction" in the final layers of the network (typically around the 25th layer of a 32-layer model).

Surgical Precision

Editing at the row and neuron level provides the optimal balance. While full-parameter editing reduces bias significantly, it often leads to catastrophic performance degradation on general coding tasks. Surgical edits maintain the model's utility while effectively mitigating bias.

Key Results

  • Mitigation Success: For CodeGen-2B-mono, MG-Editing at the row level improved the FB-Score from 0.6900 to 0.3334.
  • Preserving Performance: For Stable-Code-3B, neuron-level editing maintained the original Pass@10 score on HumanEval (0.4122) while significantly reducing bias. In contrast, standard editing methods like ROME sometimes caused models to over-correct into extreme counterfactual bias.

Conclusion and Future Work

MG-Editing demonstrates that it is possible to mitigate social biases in LLMs with surgical precision. While this work focuses on binary gender categories due to data constraints, future research should extend this multi-granularity approach to other social biases, such as race, age, and religion, and optimize the process for even larger frontier models.

Summary updated

BibTeX
@misc{qin2024mitigating,
  author = {Zhanyue Qin and Haochuan Wang and Zecheng Wang and Deyuan Liu and Cunhang Fan and Zhao Lv and Zhiying Tu and Dianhui Chu and Dianbo Sui},
  title = {{Mitigating Gender Bias in Code Large Language Models via Model Editing}},
  year = {2024},
  eprint = {2410.07820},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.07820},
}
Figure