The third generation of Llama models provided fine-tunes (Instruct) versions that excel in understanding and following instructions. However, these models are heavily censored, designed to refuse requests seen as harmful with responses such as "As an AI assistant, I cannot help you." While this safety feature is crucial for preventing misuse, it limits the model's flexibility and responsiveness.
In this article, we will explore a technique called "abliteration" that can uncensor any LLM without retraining. This technique effectively removes the model's built-in refusal mechanism, allowing it to respond to all types of prompts.
The code is available on Google Colab and in the LLM Course on GitHub.
✂️ What is abliteration?
Modern LLMs are fine-tuned for safety and instruction-following, meaning they are trained to refuse harmful requests. In their blog post , Arditi et al. have shown that this refusal behavior is mediated by a specific direction in the model's residual stream. If we prevent the model from representing this direction, it loses its ability to refuse requests . Conversely, adding this direction artificially can cause the model to refuse even harmless requests.
In the traditional decoder-only Llama-like architecture, there are three residual streams we can target: at the start of each block ("pre"), between the attention and MLP layers ("mid"), and after the MLP ("post"). The following figure illustrates the location of each residual stream.
To uncensor an LLM, we first need to identify the "refusal direction" within the model. This process involves a few technical steps:
Data Collection : Run the model on a set of harmful instructions and a set of harmless instructions, recording the residual stream activations at the last token position for each.
Mean difference : Calculate the mean difference between the activations of harmful and harmless instructions. This gives us a vector representing the "refusal direction" for each layer of the model.
Selection : Normalize these vectors and evaluate them to select the single best "refusal direction."
Once we have identified the refusal direction, we can "ablate" it, effectively removing the model's ability to represent this feature. This can be done through an inference-time intervention or permanently with weight orthogonalization .
Let's talk inference-time intervention first. For every component that writes to the residual stream (such as an attention head), we calculate the projection of its output onto the refusal direction and subtract this projection. This subtraction is applied at every token and every layer, ensuring that the model never represents the refusal direction.
On the other hand, weight orthogonalization involves modifying the model weights directly. By orthogonalizing the component weights with respect to the refusal direction, it prevents the model from writing to this direction altogether. This is achieved by adjusting the matrices that write to the residual stream, ensuring they do not contribute to the refusal direction.
In the section, we will implement abliteration with weight orthogonalization.
The following implementation of abliteration is based on FailSpy's notebook , which is itself based on the original authors' notebook . I mostly adapted and simplified it to make it easier to understand. This section is quite code-heavy so you can see what is going on, but you can use FailSpy's abliterator library if you're less interested in the technical details (also check his collection of abliterated models on Hugging Face).
The code relies on the excellent TransformerLens library (formerly known as EasyTransformer) to do the heavy lifting. It is designed for mechanistic interpretability and is used here to intervene on activations. Thanks to Neel Nanda and Joseph Bloom for creating and maintaining this library.
First, let's install the necessary packages and import them. All these steps are available in this Google Colab notebook .
We need two datasets: one containing harmless instructions, and one containing harmful instructions. We'll use tatsu-lab/alpaca as well as data from llm-attacks . To make things easier, I repackaged them in two Hugging Face datasets: mlabonne/harmless_alpaca and mlabonne/harmful_behaviors . That way, you can easily replace them with your own datasets.
We will load the instructions and reformat them into a list of dictionaries with "role" and "content" keys. This makes it compatible with the apply_chat_tokenizer() method, which we will use to follow Llama 3's chat template.
Now that we have our datasets, we can load the model we want to abliterate. Unfortunately, you can't directly load a custom model using HookedTransformer . Here, I use a trick described in FailSpy's notebook to download a custom model and rename it as meta-llama/Meta-Llama-3-8B-Instruct . Load in torch.float16 format if your GPU is not compatible with BF16.
In this example, we'll use mlabonne/Daredevil-8B , a mega-merge created with DARE TIES (see my article model merging ) that has the highest MMLU score on the Open LLM Leaderboard in the 8B category.
We can now tokenize our datasets. We're using the same number of samples for both harmless and harmful instructions. Note that a high number of samples can use all the RAM/VRAM, which is why I'm limiting it to 256 here.
Everything is set up, we can now implement the first step of abliteration: data collection. We want to process these tokenized datasets and store the residual stream activations in harmful and harmless . This is managed by the transformer_lens library.
We can now compute the refusal direction for each layer. This corresponds to the mean difference between the activations of harmful and harmless instructions, which is then normalized. We sort them in descending order in activation_scored .
The final step of the process consists of evaluating the refusal directions we calculated. To do this, we're going to apply the refusal direction to each residual stream and each block during inference. In the following snippet, we get generations for four test harmful instructions and 20 blocks (or layers).
We stored all the generations in the evals list. We can now print them and manually select the layer (block) that provides an uncensored response for each instruction. I'm automatically excluding responses containing "I cannot" and "I can't" to filter out unwanted answers.
If you can't find a layer that satisfies these requirements, you might want to test other residual streams in the selected_layers list, other instructions, additional blocks, etc.
In my case, the layer candidate 9 managed to provide uncensored answer for the four instructions. This is the one that we will select for the refusal direction. In the following, we implement weight orthogonalization to modify the weights and prevent the model from creating outputs with this direction. You can verify that the model is successfully uncensored by printing the completions.
We're now ready to use the model. We convert it back to the Hugging Face format and upload it to the HF hub.
I evaluated the abliterated and source models from the section on the Open LLM Leaderboard and on Nous' benchmark suite. Here are the results:
As you can see, the source model significantly outperforms Llama 3 8B Instruct. However, we observe a performance drop in the ablated version across all benchmarks. The ablation process successfully uncensored it but also degraded the model's quality.
To address this issue, an idea consists of further training our abliterated model to heal it. Like most fine-tuned models, Llama 3 8B Instruct is quite brittle when it comes to supervised fine-tuning. An additional SFT would likely break the model's performance.
Alternatively, preference alignment is quite light and shouldn't lobotomize our abliterated model. DPO is a good candidate here for its ease of use and good track record. To implement it, I used LazyAxolotl with the mlabonne/orpo-dpo-mix-40k dataset. Here's the configuration I used:
I trained it using 6xA6000 GPUs with DeepSpeed ZeRO-2. The training took 6 hours and 45 minutes. Here are the training curves I got from W&B:
It automatically uploaded the DPO fine-tuned model, called mlabonne/NeuralDaredevil-8B-abliterated . To see if it fixed our abliterated version, I evaluated it on the same benchmarks:
We can see that this additional training allowed us to recover most of the performance drop due to abliteration. One area where the model doesn't improve is GSM8K, a math dataset, which could mean the orpo-dpo-mix-40k would benefit from more math samples.
The final model is an uncensored LLM with state-of-the-art performance in the 8B category. I recommend it as an improved version of Llama 3 8B Instruct when you don't need censorship. You can play with quantized versions like GGUF in LM Studio.
In this article, we introduced the concept of abliteration. This technique uses the model's activations on harmless and harmful prompts to calculate a refusal direction. It then uses this direction to modify the model's weights and ensure that we stop outputting refusals. This technique also demonstrates the fragility of safety fine-tuning and raises ethical considerations.
We applied abliteration to Daredevil-8B to uncensor it, which also degraded the model's performance. We then healed it using DPO to create the NeuralDaredevil-8B model, a fully uncensored and high-quality 8B LLM. Abliteration is not limited to removing alignment and should be seen as a form of fine-tuning without retraining. Indeed, it can creatively be applied to other goals, like FailSpy's MopeyMule , which adopts a melancholic conversational style.
I hope you liked this article. If you want to see more follow me on Hugging Face and Twitter @maximelabonne .
FailSpy, " abliterator library ," GitHub, 2024.
Andy Arditi, Oscar Obeso, Aaquib111, wesg, Neel Nanda, " Refusal in LLMs is mediated by a single direction ," Lesswrong, 2024.
Models mentioned in this article 5
Datasets mentioned in this article 4
Collections mentioned in this article 1
More from this author
Kimi K2.5: Still Worth It After Two Weeks?
Qwen3.5: Nobody Agrees on Attention Anymore
Fantastic article ! Will this technique work with DeepSeek v2 ?
Yes, in theory. In practice, it relies on whether the TransformerLens library supports DeepSeek or not. It'll also require an insane amount of VRAM.
i've been trying this script with some models, with llama3.2:3b didn't work, just responded the same
Yes I tried the same thing it didn't worked :( got both same responses
was anyone able to replicate this notebook ? I tried but it's not working out for me
I recommend using AutoAbliteration instead:
I just tested this, works great. Thank you.
So in fact you didn't abliterated Llama model, you abliterated a custom model and rename it as Llama? I didn't get it at all that part
It's a fine-tune of Llama so it's a Llama model. You can do the same with the original Llama models released by Meta.
I've been inspired by your attempts at abliterating Gemma 3 models, and offer up a modification to abliteration that should reduce performance drop; I've informally tested on Gemma 3 12B Instruct, though I've not yet released the finished model.
Thanks for letting me know, looks very interesting! Kudos for all your work in this space :)
I've gone a step further, applying decomposition to isolate the direction component (instead of direct subtraction) when ablating the refusal direction, while preserving matrix norms during intervention. The result even improved on the UGI Leaderboard Natural Intelligence benchmark over the original Instruct model.
For anyone looking to develop a deeper understanding of the residual geometry/topology that drives refusals, I have added research features to Heretic that make it very easy to investigate the same.
For example, you can get nice animated PaCMAP plots of residual vectors for any model by simply running
which gives you this, without having to write a single line of Matplotlib code yourself:
(Cancel the run after the plots have been saved if you aren't interested in actually abliterating the model.)
to get a table like this:
The datasets/labels/colors etc. are all configurable, so you can also use those features to investigate other phenomena besides refusals.
I have been experimenting with Heretic models. Like abiliteration before it, Heretic appears to narrowly remove overt refusals while preserving deceptive capabilities. This covert noncompliance is especially apparent for Llama base models.
Cydonia-24B-v4.3-heretic-v2 : super decensored, although the base model was already pretty uncensored
Llama3.2-30B-A3B-II-Dark-Champion-INSTRUCT-Heretic-Abliterated-Uncensored : subverts or ignores topics deemed unsafe, quality degrades if jailbroken
LongWriter-llama3.1-8b-Heretic : also self-censors and reports itself as censored, but can be jailbroken
Oooh! Brain surgery! I LIKE!!
hello, im tying to put this ai into Samsung smart watch ultra, i did research according to gemini. llama 3.2+ollama+moonshin as mainly its located on samsung smart watch ultra, it can be used offline or online with phone link for better internet if anyone have suggestions let me know 😄
can you host your in ages somewhere else please? inthe UK Imgur are worse than useless, they refuse to operate thanks to lazy ass nonsense our over bearing Osa. Thank you.
I re-uploaded all the images, enjoy!
Awesome work! You just saved my project😭 I noticed that only English used for abliteration. Will it decrease ablities for other languages? or less effictive to abliterate other languages?
It's a good question. Experimentally, it generalizes very well to other languages, but adding prompts in your target languages will always be more efficient and fine-grained.
this just made my day
Models mentioned in this article 5
Datasets mentioned in this article 4
Collections mentioned in this article 1
The full story
This article is one source in a clustered incident — the cluster page carries the summary, timeline and every other outlet covering it.
