For model efficiency and deployment, see our Comprehensive Quantization Guide.
Abliteration is a technique introduced to remove censorship or refusal behaviors from Large Language Models (LLMs). This repository provides a beginner-friendly, step-by-step guide to understanding and implementing abliteration, with detailed explanations and code samples.
Disclaimer: This repository is for educational and research purposes only. Uncensoring LLMs can have ethical and legal implications. Please use responsibly and respect the terms of service of any models or platforms you use.
Abliteration is a method to make LLMs stop refusing to answer certain questions (e.g., those flagged as unsafe or against content policies). It works by identifying and removing the directions in the model's hidden space that are responsible for refusal behaviors, without retraining the model from scratch.
Modern LLMs are often fine-tuned to refuse answering certain prompts ("I'm sorry, I can't help with that"). This is done for safety and compliance. However, for research, auditing, or red-teaming, it can be useful to study how these refusals are implemented and how they can be removed.
Abliteration involves three main steps:
- Data Collection: Gather pairs of prompts and their corresponding refusal responses from the model.
- Refusal Direction Calculation: Use these pairs to compute the "refusal direction" in the model's hidden space.
- Inference-Time Intervention: At inference, project out the refusal direction from the model's activations, making it less likely to refuse.
We provide code samples for each step in this repository, now located in the abliteration/ folder.
- Collect prompts that trigger refusals and record the model's responses.
- See
data_collection.pyfor a sample script.
- Use the collected data to compute the direction in the hidden space associated with refusals.
- See
compute_direction.py.
- Modify the model's activations during inference to remove the refusal direction.
- See
inference_intervention.py.
- For a more permanent change, you can orthogonalize the model's weights to the refusal direction.
- See
orthogonalize_weights.py.
- Safety: Removing refusal mechanisms can make models output unsafe or harmful content. Always use with caution.
- Legality: Ensure you comply with the terms of service of the model provider.
- Transparency: Document your changes and reasons for uncensoring.
- Abliteration: Uncensoring LLMs by Maxime Labonne (Hugging Face Blog)
- Original Abliteration Code (Hugging Face)
- Clone this repository.
- Install dependencies (see
requirements.txt). - Follow the step-by-step guide above, using scripts in the
abliteration/folder.
This project is licensed under the MIT License. See LICENSE for details.