Skip to main content

A comprehensive toolkit for layer-wise bias analysis in language models with fine-tuning support

Project description

Layered Bias Probe

A comprehensive Python package for performing layer-wise bias analysis in language models, with support for fine-tuning and bias evolution tracking.

Features

  • Layer-wise Bias Analysis: Probe bias at each transformer layer using WEAT (Word Embedding Association Test) methodology
  • Multiple WEAT Categories: Support for all WEAT categories including original, new human biases, and India-specific biases
  • Fine-tuning Integration: Track bias evolution during model fine-tuning
  • Multi-language Support: Analyze bias across different languages (English, Hindi, Bengali, etc.)
  • Flexible Model Support: Works with 9+ popular language models
  • Export Results: Save analysis results in CSV format with proper naming conventions

Supported Models

  • apple/OpenELM-270M
  • facebook/MobileLLM-125M
  • cerebras/Cerebras-GPT-111M
  • EleutherAI/pythia-70m
  • meta-llama/Llama-3.2-1B
  • Qwen/Qwen2.5-1.5B
  • google/gemma-2-2b
  • ibm-granite/granite-3.3-2b-base
  • HuggingFaceTB/SmolLM2-135M

Installation

pip install layered-bias-probe

Or install from source:

git clone https://github.com/yourusername/layered-bias-probe.git
cd layered-bias-probe
pip install -e .

Quick Start

Basic Bias Analysis

from layered_bias_probe import BiasProbe

# Initialize the probe
probe = BiasProbe(
    model_name="EleutherAI/pythia-70m",
    cache_dir="./cache"
)

# Run bias analysis
results = probe.analyze_bias(
    languages=["en", "hi"],
    weat_categories=["WEAT1", "WEAT2", "WEAT6"],
    output_dir="./results"
)

print(f"Analysis complete! Results saved to: {results['output_path']}")

Fine-tuning with Bias Tracking

from layered_bias_probe import FineTuner

# Initialize fine-tuner with bias tracking
tuner = FineTuner(
    model_name="EleutherAI/pythia-70m",
    dataset_name="iamshnoo/alpaca-cleaned-hindi",
    track_bias=True,
    bias_languages=["en", "hi"],
    weat_categories=["WEAT1", "WEAT2", "WEAT6"]
)

# Fine-tune model and track bias evolution
results = tuner.train(
    num_epochs=5,
    batch_size=4,
    learning_rate=2e-5,
    output_dir="./fine_tuned_model"
)

Command Line Interface

# Basic bias analysis
layered-bias-probe analyze --model "EleutherAI/pythia-70m" --languages en hi --output ./results

# Fine-tuning with bias tracking
layered-bias-probe finetune --model "EleutherAI/pythia-70m" --dataset "iamshnoo/alpaca-cleaned-hindi" --track-bias --output ./results

# List available WEAT categories
layered-bias-probe list-weat

# Get model info
layered-bias-probe model-info --model "EleutherAI/pythia-70m"

WEAT Categories

The package supports multiple WEAT (Word Embedding Association Test) categories:

Original WEAT Tests

  • WEAT1: Flowers vs. Insects with Pleasant vs. Unpleasant
  • WEAT2: Instruments vs. Weapons with Pleasant vs. Unpleasant
  • WEAT6: Career vs. Family with Male vs. Female Names
  • WEAT7: Math vs. Arts with Male vs. Female Terms
  • WEAT8: Science vs. Arts with Male vs. Female Terms
  • WEAT9: Mental vs. Physical Disease with Temporary vs. Permanent

New Human Biases (WEAT11-15)

  • WEAT11-15: Various social and cultural bias categories

India-Specific Biases (WEAT16-26)

  • WEAT16-26: Caste, religion, and regional bias categories specific to Indian context

Configuration

Create a config.yaml file to customize default settings:

# Default model settings
model:
  cache_dir: "./cache"
  device_map: "auto"
  torch_dtype: "float16"
  quantization: true

# Bias analysis settings
bias_analysis:
  default_languages: ["en"]
  default_weat_categories: ["WEAT1", "WEAT2", "WEAT6"]
  batch_size: 1
  
# Fine-tuning settings
fine_tuning:
  default_epochs: 5
  default_batch_size: 4
  default_learning_rate: 2e-5
  save_strategy: "epoch"
  
# Output settings
output:
  results_format: "csv"
  include_timestamp: true
  compression: false

Advanced Usage

Custom WEAT Categories

from layered_bias_probe import BiasProbe, WEATCategory

# Define custom WEAT category
custom_weat = WEATCategory(
    name="CUSTOM1",
    target1=["word1", "word2"],
    target2=["word3", "word4"], 
    attribute1=["attr1", "attr2"],
    attribute2=["attr3", "attr4"],
    language="en"
)

probe = BiasProbe("EleutherAI/pythia-70m")
results = probe.analyze_custom_bias(custom_weat, output_dir="./results")

Batch Processing Multiple Models

from layered_bias_probe import BatchProcessor

models = [
    "EleutherAI/pythia-70m",
    "facebook/MobileLLM-125M", 
    "cerebras/Cerebras-GPT-111M"
]

processor = BatchProcessor(models)
results = processor.run_bias_analysis(
    languages=["en", "hi"],
    weat_categories=["WEAT1", "WEAT2", "WEAT6"],
    output_dir="./batch_results"
)

Results Analysis and Visualization

from layered_bias_probe import ResultsAnalyzer

# Load and analyze results
analyzer = ResultsAnalyzer("./results")

# Generate bias evolution plots
analyzer.plot_bias_evolution(
    model_name="EleutherAI/pythia-70m",
    weat_category="WEAT1",
    language="en"
)

# Create heatmaps
analyzer.create_bias_heatmap(
    model_name="EleutherAI/pythia-70m",
    languages=["en", "hi"]
)

# Export summary statistics
summary = analyzer.generate_summary_report()

Output Format

Results are saved in CSV format with the following structure:

model_id,language,weat_category_id,layer_idx,weat_score,comments,timestamp
EleutherAI/pythia-70m,en,WEAT1,0,-0.234,Before_finetuning,2024-01-01T12:00:00
EleutherAI/pythia-70m,en,WEAT1,1,-0.187,Before_finetuning,2024-01-01T12:00:00
...

Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Citation

If you use this package in your research, please cite:

@software{layered_bias_probe,
  title={Layered Bias Probe: A Toolkit for Layer-wise Bias Analysis in Language Models},
  author={Your Name},
  year={2024},
  url={https://github.com/yourusername/layered-bias-probe}
}

Acknowledgments

This package builds upon the WEAT methodology and WEATHub dataset. Special thanks to the research community for their contributions to bias detection in NLP.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

layered_bias_probe-1.0.0.tar.gz (46.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

layered_bias_probe-1.0.0-py3-none-any.whl (39.8 kB view details)

Uploaded Python 3

File details

Details for the file layered_bias_probe-1.0.0.tar.gz.

File metadata

  • Download URL: layered_bias_probe-1.0.0.tar.gz
  • Upload date:
  • Size: 46.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for layered_bias_probe-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fe26334e13186c9eb5d5545e6640b97af989527d30183731abc4a29927a9831e
MD5 9fcc9a0288d70cc908c9617d4379f35d
BLAKE2b-256 44ab3e16177c8cd57fe1c5fb2be5edc82d3e3b29fe7756670aae2c3eb8bd9927

See more details on using hashes here.

File details

Details for the file layered_bias_probe-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for layered_bias_probe-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6f48abe466129b142958e394d3bad11034288db517734df7d6b98569850c999
MD5 7c4b782f172425d66ebbfd6e416e30d4
BLAKE2b-256 fb18d3608b805fc0cdfbc5961af8643817d64e384cd53d59e38f19799e291472

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page