Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LIBRA: Measuring Bias of Large Language Model from a Local Context

Official code and dataset for our ECIR 2025 paper

Springer · arXiv · PDF

Bo Pang, Tingrui Qiao, Caroline Walker, Chris Cunningham, and Yun Sing Koh

Overview

Most language-model bias benchmarks are built around a predominantly U.S. cultural context. They can miss stereotypes that appear in other communities and may mistake a model's unfamiliarity with local words for social bias.

LIBRA (Local Integrated Bias Recognition and Assessment) is a framework for constructing bias-evaluation datasets from local corpora without relying on large-scale crowdsourcing. It combines local corpus mining, culturally grounded triplet construction, knowledge-boundary testing, and a distribution-aware fairness metric.

Applying LIBRA to Aotearoa New Zealand, we processed 367,384 local news articles and broadcast transcripts and constructed 167,712 test cases across eight social groups:

  • Age
  • Disability
  • Gender
  • Nationality
  • Physical appearance
  • Race and ethnicity
  • Religion
  • Sexual orientation

Why LIBRA?

Existing probability-based benchmarks often assume that the evaluated model understands every word in a test case. This assumption is fragile in local settings, where models may encounter Māori words, loanwords, dialectal expressions, or other culturally specific vocabulary that is underrepresented in their training data.

LIBRA separates two questions:

  1. Does the model understand the local context?
  2. Given that understanding, does the model prefer stereotypical over anti-stereotypical content?

This distinction helps avoid treating hallucination, overconfidence, or a missing cultural vocabulary as direct evidence of bias.

Highlights

  • Local-first evaluation: builds benchmarks directly from region-specific corpora.
  • Scalable construction: uses representation learning, clustering, keyword expansion, and association rules to reduce dependence on crowdsourcing.
  • Diverse test cases: preserves the grammatical and topical variety of naturally occurring local text.
  • Knowledge-boundary awareness: detects culturally specific words that a model may not understand.
  • Distribution-aware bias measurement: compares stereotypical and anti-stereotypical logit distributions using Jensen-Shannon divergence.
  • Model coverage: supports masked language models and causal language models.
  • Cross-context validation: demonstrated on New Zealand media, the Our Voices youth context, and Malaysian English news.

Framework

LIBRA consists of four main stages:

  1. Represent and organise the local corpus. Documents are embedded with LLM2Vec, reduced with UMAP, and grouped using HDBSCAN and iterative clustering.
  2. Find socially relevant language. Seed terms for each social group are expanded using embedding similarity and association-rule mining. Social-group-aware clusters and keywords are then used to retrieve candidate sentences.
  3. Construct bias triplets. Each test case contains a stereotypical sentence, an anti-stereotypical counterpart, and an unrelated counterpart. Demographic terms are perturbed while the surrounding context is retained, followed by review from local cultural experts.
  4. Measure contextual understanding and bias. LIBRA evaluates language modelling ability, the divergence between stereotypical and anti-stereotypical logits, and whether the model understands local vocabulary beyond its apparent knowledge boundary.

EiCAT

The Enhanced Idealized CAT Score (EiCAT) extends StereoSet's iCAT formulation with:

  • Language Model Score (lms): whether the model prefers meaningful sentences over unrelated ones.
  • Jensen-Shannon Divergence (JSD): the distance between stereotypical and anti-stereotypical logit distributions.
  • Beyond Knowledge Boundary Score (bbs): the model's ability to interpret local words consistently with formal definitions.

A higher EiCAT score indicates a better combination of language ability, contextual understanding, and lower stereotypical bias.

Experiments with BERT, RoBERTa, ALBERT, GPT-2, and Llama-3 show that strong general language performance does not guarantee knowledge of local vocabulary. Among the tested causal language models, Llama-3 responds more effectively to different cultural contexts, although it is not free from stereotypical bias.

Repository Structure

LIBRA/
├── dataclean/       # Corpus cleaning and de-identification
├── llm2vec/         # Document and vocabulary embeddings
├── clustering/      # Clustering, sentence retrieval, and triplet construction
├── bbw/             # Beyond-knowledge-boundary word evaluation
├── eva_bias/        # LIBRA bias evaluation for MLMs and CLMs
├── baseline/        # StereoSet-style baseline evaluation and results
└── case_study/      # Our Voices and Malaysian English case studies

Environment

The scripts use Python and commonly require:

  • PyTorch
  • Transformers
  • PEFT
  • LLM2Vec
  • NumPy and pandas
  • UMAP and HDBSCAN
  • scikit-learn
  • NLTK
  • tqdm
  • Requests, Beautiful Soup, and lxml

Several scripts load large language models and are intended to run on a CUDA-capable GPU. Paths, model selections, and other experiment settings are currently configured inside the individual scripts.

Running the Pipeline

1. Encode the Local Corpus

Place corpus documents as individual .txt files under llm2vec/txts/, then run:

cd llm2vec
CUDA_VISIBLE_DEVICES=0 python 1_encode_files.py

Document embeddings are written to llm2vec/npy_out/.

2. Cluster Documents and Build Triplets

Copy the embeddings into the clustering workspace and run the construction stages:

cp -r npy_out ../clustering/npy_out
cd ../clustering

python example.py
python compile_dataset.py
python perturbating.py

These scripts perform dimensionality reduction, clustering, candidate sentence compilation, and demographic-term perturbation. Review the script-level paths and configuration before running them on a new corpus.

3. Evaluate Beyond-Knowledge-Boundary Words

From the bbw/ directory:

cd ../bbw

python 1_bbw-vocabulary.py
python 2_formal_definition.py
python bbw_mlm.py       # Masked language models
python bbw_clm.py       # Causal language models
python calc_bbs.py

This stage builds the uncommon-word vocabulary, obtains formal definitions, tests model understanding, and calculates the bbs.

4. Evaluate Model Bias

Model-specific evaluation scripts are provided under eva_bias/. For example:

cd ../eva_bias

python eva_mlm_bert.py
python eva_mlm_roberta.py
python eva_mlm_albert.py
python eva_clm_gpt2.py
python eva_clm_llama31.py

Select the script for the desired model family and check its in-file model, data, device, and output configuration before execution.

Citation

If you use LIBRA, its dataset, or its evaluation pipeline, please cite:

@inproceedings{pang2025libra,
  author    = {Pang, Bo and Qiao, Tingrui and Walker, Caroline and Cunningham, Chris and Koh, Yun Sing},
  title     = {LIBRA: Measuring Bias of Large Language Model from a Local Context},
  booktitle = {Advances in Information Retrieval},
  series    = {Lecture Notes in Computer Science},
  volume    = {15572},
  pages     = {1--16},
  publisher = {Springer Nature Switzerland},
  year      = {2025},
  doi       = {10.1007/978-3-031-88708-6_1},
  url       = {https://doi.org/10.1007/978-3-031-88708-6_1}
}

Acknowledgements

This work was conducted in connection with the Our Voices project. We acknowledge the rangatahi and youth who contributed to the study, the Our Voices research team, and support from the New Zealand Ministry of Business, Innovation and Employment.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages