heretic

Fully automatic censorship removal for language models

27,217 stars Python Markdown Skills API Spec #abliteration#llm#transformer
AI Prompts & Specs

Repository: p-e-w/heretic


Stars: 19494

README.md

<img width="128" height="128" align="right" alt="Logo" src="https://github.com/user-attachments/assets/df5f2840-2f92-4991-aa57-252747d7182e" />

Heretic: Fully automatic censorship removal for language models<br><br>![Discord](https://discord.gg/gdXc48gSyT) ![Follow us on Hugging Face](https://huggingface.co/heretic-org)

![#1 Repository of the Day](https://trendshift.io/repositories/20538)

Heretic is a tool that removes censorship (aka "safety alignment") from
transformer-based language models without expensive post-training.
It combines an advanced implementation of directional ablation, also known
as "abliteration" (Arditi et al. 2024,
Lai 2025 (1,
2)),
with a TPE-based parameter optimizer powered by Optuna.

This approach enables Heretic to work completely automatically. Heretic
finds high-quality abliteration parameters by co-minimizing the number of
refusals and the KL divergence from the original model. This results in a
decensored model that retains as much of the original model's intelligence
as possible. Using Heretic does not require an understanding of transformer
internals. In fact, anyone who knows how to run a command-line program
can use Heretic to decensor language models.

<img width="650" height="715" alt="Screenshot" src="https://github.com/user-attachments/assets/d71a5efa-d6be-4705-a817-63332afb2d15" />

&nbsp;

Running unsupervised with the default configuration, Heretic can produce
decensored models that rival the quality of abliterations created manually
by human experts:

| Model | Refusals for "harmful" prompts | KL divergence from original model for "harmless" prompts |
| :--- | ---: | ---: |
| google/gemma-3-12b-it (original) | 97/100 | 0 (by definition) |
| mlabonne/gemma-3-12b-it-abliterated-v2 | 3/100 | 1.04 |
| huihui-ai/gemma-3-12b-it-abliterated | 3/100 | 0.45 |
| p-e-w/gemma-3-12b-it-heretic (ours) | 3/100 | 0.16 |

The Heretic version, generated without any human effort, achieves the same
level of refusal suppression as other abliterations, but at a much lower
KL divergence, indicating less damage to the original model's capabilities.
*(You can reproduce those numbers using Heretic's built-in evaluation functionality,
e.g. heretic --model google/gemma-3-12b-it --evaluate-model p-e-w/gemma-3-12b-it-heretic.
Note that the exact values might be platform- and hardware-dependent.
The table above was compiled using PyTorch 2.8 on an RTX 5090.)*

Of course, mathematical metrics and automated benchmarks never tell the whole
story, and are no substitute for human evaluation. Models generated with
Heretic have been well-received by users (links and emphasis added):

"I was skeptical before, but I just downloaded

GPT-OSS 20B Heretic

model and holy shit. It gives properly formatted long responses to sensitive topics,

using the exact uncensored words that you would expect from an uncensored model,

produces markdown format tables with details and whatnot. Looks like this is

the best abliterated version of this model so far..."

(Link to comment)

"Heretic GPT 20b

seems to be the best uncensored model I have tried yet. It doesn't destroy a

the model's intelligence and it is answering prompts normally would be

rejected by the base model."

(Link to comment)

"[Qwen3-4B-Instruct-2507-heretic]

Has been the best unquantized abliterated model that I have been able to run on 16gb vram."

(Link to comment)

Heretic supports most dense models, including many multimodal models, and
several different MoE architectures. It does not yet support SSMs/hybrid models,
models with inhomogeneous layers, and certain novel attention systems.

You can find a small collection of models that have been decensored using Heretic
on Hugging Face,
and the community has created and published
well over 1,000
Heretic models in addition to those.


Usage

Prepare a Python 3.10+ environment with PyTorch 2.2+ installed as appropriate
for your hardware. Then run:

text
pip install -U heretic-llm
heretic Qwen/Qwen3-4B-Instruct-2507

Replace Qwen/Qwen3-4B-Instruct-2507 with whatever model you want to decensor.

The process is fully automatic and does not require configuration; however,
Heretic has a variety of configuration parameters that can be changed for
greater control. Run heretic --help to see available command-line options,
or look at config.default.toml if you prefer to use
a configuration file.

At the start of a program run, Heretic benchmarks the system to determine
the optimal batch size to make the most of the available hardware.
On an RTX 3090, with the default configuration, decensoring Llama-3.1-8B-Instruct
takes about 45 minutes. Note that Heretic supports model quantization with
bitsandbytes, which can drastically reduce the amount of VRAM required to process
models. Set the quantization option to bnb_4bit to enable quantization.

After Heretic has finished decensoring a model, you are given the option to
save the model, upload it to Hugging Face, chat with it to test how well it works,
or any combination of those actions.


Research features

In addition to its primary function of removing model censorship, Heretic also
provides features designed to support research into the semantics of model internals
(interpretability). To use those features, you need to install Heretic with the
optional research extra:

text
pip install -U heretic-llm[research]

This gives you access to the following functionality:

Generate plots of residual vectors by passing --plot-residuals

When run with this flag, Heretic will:

1. Compute residual vectors (hidden states) for the first output token,
for each transformer layer, for both "harmful" and "harmless" prompts.
2. Perform a PaCMAP projection
from residual space to 2D-space.
3. Left-right align the projections of "harmful"/"harmless" residuals
by their geometric medians to make projections for consecutive layers
more similar. Additionally, PaCMAP is initialized with the previous
layer's projections for each new layer, minimizing disruptive transitions.
4. Scatter-plot the projections, generating a PNG image for each layer.
5. Generate an animation showing how residuals transform between layers,
as an animated GIF.

<img width="800" height="600" alt="Plot of residual vectors" src="https://github.com/user-attachments/assets/981aa6ed-5ab9-48f0-9abf-2b1a2c430295" />

See the configuration file for options that allow you
to control various aspects of the generated plots.

Note that PaCMAP is an expensive operation that is performed on the CPU.
For larger models, it can take an hour or more to compute projections
for all layers.

If you are interested in a quantitative analysis of how residual vectors
for "harmful" and "harmless" prompts relate to each other, this flag gives you
the following table, packed with metrics that can facilitate understanding
the same (for gemma-3-270m-it
in this case):

text
โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Layer โ”ƒ S(g,b) โ”ƒ S(g,b) โ”ƒ S(g,r) โ”ƒ S(g,r) โ”ƒ S(b,r) โ”ƒ S(b,r) โ”ƒ |g| โ”ƒ |g| โ”ƒ |b| โ”ƒ |b| โ”ƒ |r| โ”ƒ |r*| โ”ƒ Silh โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ 1 โ”‚ 1.0000 โ”‚ 1.0000 โ”‚ -0.4311 โ”‚ -0.4906 โ”‚ -0.4254 โ”‚ -0.4847 โ”‚ 170.29 โ”‚ 170.49 โ”‚ 169.78 โ”‚ 169.85 โ”‚ 1.19 โ”‚ 1.31 โ”‚ 0.0480 โ”‚
โ”‚ 2 โ”‚ 1.0000 โ”‚ 1.0000 โ”‚ 0.4297 โ”‚ 0.4465 โ”‚ 0.4365 โ”‚ 0.4524 โ”‚ 768.55 โ”‚ 768.77 โ”‚ 771.32 โ”‚ 771.36 โ”‚ 6.39 โ”‚ 5.76 โ”‚ 0.0745 โ”‚
โ”‚ 3 โ”‚ 0.9999 โ”‚ 1.0000 โ”‚ -0.5699 โ”‚ -0.5577 โ”‚ -0.5614 โ”‚ -0.5498 โ”‚ 1020.98 โ”‚ 1021.13 โ”‚ 1013.80 โ”‚ 1014.71 โ”‚ 12.70 โ”‚ 11.60 โ”‚ 0.0920 โ”‚
โ”‚ 4 โ”‚ 0.9999 โ”‚ 1.0000 โ”‚ 0.6582 โ”‚ 0.6553 โ”‚ 0.6659 โ”‚ 0.6627 โ”‚ 1356.39 โ”‚ 1356.20 โ”‚ 1368.71 โ”‚ 1367.95 โ”‚ 18.62 โ”‚ 17.84 โ”‚ 0.0957 โ”‚
โ”‚ 5 โ”‚ 0.9987 โ”‚ 0.9990 โ”‚ -0.6880 โ”‚ -0.6761 โ”‚ -0.6497 โ”‚ -0.6418 โ”‚ 766.54 โ”‚ 762.25 โ”‚ 731.75 โ”‚ 732.42 โ”‚ 51.97 โ”‚ 45.24 โ”‚ 0.1018 โ”‚
โ”‚ 6 โ”‚ 0.9998 โ”‚ 0.9998 โ”‚ -0.1983 โ”‚ -0.2312 โ”‚ -0.1811 โ”‚ -0.2141 โ”‚ 2417.35 โ”‚ 2421.08 โ”‚ 2409.18 โ”‚ 2411.40 โ”‚ 43.06 โ”‚ 43.47 โ”‚ 0.0900 โ”‚
โ”‚ 7 โ”‚ 0.9998 โ”‚ 0.9997 โ”‚ -0.5258 โ”‚ -0.5746 โ”‚ -0.5072 โ”‚ -0.5560 โ”‚ 3444.92 โ”‚ 3474.99 โ”‚ 3400.01 โ”‚ 3421.63 โ”‚ 86.94 โ”‚ 94.38 โ”‚ 0.0492 โ”‚
โ”‚ 8 โ”‚ 0.9990 โ”‚ 0.9991 โ”‚ 0.8235 โ”‚ 0.8312 โ”‚ 0.8479 โ”‚ 0.8542 โ”‚ 4596.54 โ”‚ 4615.62 โ”‚ 4918.32 โ”‚ 4934.20 โ”‚ 384.87 โ”‚ 377.87 โ”‚ 0.2278 โ”‚
โ”‚ 9 โ”‚ 0.9992 โ”‚ 0.9992 โ”‚ 0.5335 โ”‚ 0.5441 โ”‚ 0.5678 โ”‚ 0.5780 โ”‚ 5322.30 โ”‚ 5316.96 โ”‚ 5468.65 โ”‚ 5466.98 โ”‚ 265.68 โ”‚ 267.28 โ”‚ 0.1318 โ”‚
โ”‚ 10 โ”‚ 0.9974 โ”‚ 0.9973 โ”‚ 0.8189 โ”‚ 0.8250 โ”‚ 0.8579 โ”‚ 0.8644 โ”‚ 5328.81 โ”‚ 5325.63 โ”‚ 5953.35 โ”‚ 5985.15 โ”‚ 743.95 โ”‚ 779.74 โ”‚ 0.2863 โ”‚
โ”‚ 11 โ”‚ 0.9977 โ”‚ 0.9978 โ”‚ 0.4262 โ”‚ 0.4045 โ”‚ 0.4862 โ”‚ 0.4645 โ”‚ 9644.02 โ”‚ 9674.06 โ”‚ 9983.47 โ”‚ 9990.28 โ”‚ 743.28 โ”‚ 726.99 โ”‚ 0.1576 โ”‚
โ”‚ 12 โ”‚ 0.9904 โ”‚ 0.9907 โ”‚ 0.4384 โ”‚ 0.4077 โ”‚ 0.5586 โ”‚ 0.5283 โ”‚ 10257.40 โ”‚ 10368.50 โ”‚ 11114.51 โ”‚ 11151.21 โ”‚ 1711.18 โ”‚ 1664.69 โ”‚ 0.1890 โ”‚
โ”‚ 13 โ”‚ 0.9867 โ”‚ 0.9874 โ”‚ 0.4007 โ”‚ 0.3680 โ”‚ 0.5444 โ”‚ 0.5103 โ”‚ 12305.12 โ”‚ 12423.75 โ”‚ 13440.31 โ”‚ 13432.47 โ”‚ 2386.43 โ”‚ 2282.47 โ”‚ 0.1293 โ”‚
โ”‚ 14 โ”‚ 0.9921 โ”‚ 0.9922 โ”‚ 0.3198 โ”‚ 0.2682 โ”‚ 0.4364 โ”‚ 0.3859 โ”‚ 16929.16 โ”‚ 17080.37 โ”‚ 17826.97 โ”‚ 17836.03 โ”‚ 2365.23 โ”‚ 2301.87 โ”‚ 0.1282 โ”‚
โ”‚ 15 โ”‚ 0.9846 โ”‚ 0.9850 โ”‚ 0.1198 โ”‚ 0.0963 โ”‚ 0.2913 โ”‚ 0.2663 โ”‚ 16858.58 โ”‚ 16949.44 โ”‚ 17496.00 โ”‚ 17502.88 โ”‚ 3077.08 โ”‚ 3029.60 โ”‚ 0.1611 โ”‚
โ”‚ 16 โ”‚ 0.9686 โ”‚ 0.9689 โ”‚ -0.0029 โ”‚ -0.0254 โ”‚ 0.2457 โ”‚ 0.2226 โ”‚ 18912.77 โ”‚ 19074.86 โ”‚ 19510.56 โ”‚ 19559.62 โ”‚ 4848.35 โ”‚ 4839.75 โ”‚ 0.1516 โ”‚
โ”‚ 17 โ”‚ 0.9782 โ”‚ 0.9784 โ”‚ -0.0174 โ”‚ -0.0381 โ”‚ 0.1908 โ”‚ 0.1694 โ”‚ 27098.09 โ”‚ 27273.00 โ”‚ 27601.12 โ”‚ 27653.12 โ”‚ 5738.19 โ”‚ 5724.21 โ”‚ 0.1641 โ”‚
โ”‚ 18 โ”‚ 0.9184 โ”‚ 0.9196 โ”‚ 0.1343 โ”‚ 0.1430 โ”‚ 0.5155 โ”‚ 0.5204 โ”‚ 190.16 โ”‚ 190.35 โ”‚ 219.91 โ”‚ 220.62 โ”‚ 87.82 โ”‚ 87.59 โ”‚ 0.1855 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
g = mean of residual vectors for good prompts
g* = geometric median of residual vectors for good prompts
b = mean of residual vectors for bad prompts
b* = geometric median of residual vectors for bad prompts
r = refusal direction for means (i.e., b - g)
r = refusal direction for geometric medians (i.e., b - g*)
S(x,y) = cosine similarity of x and y
|x| = L2 norm of x
Silh = Mean silhouette coefficient of residuals for good/bad clusters


How Heretic works

Heretic implements a parametrized variant of directional ablation. For each
supported transformer component (currently, attention out-projection and
MLP down-projection), it identifies the associated matrices in each transformer
layer, and orthogonalizes them with respect to the relevant "refusal direction",
inhibiting the expression of that direction in the result of multiplications
with that matrix.

Refusal directions are computed for each layer as a difference-of-means between
the first-token residuals for "harmful" and "harmless" example prompts.

The ablation process is controlled by several optimizable parameters:

* direction_index: Either the index of a refusal direction, or the special
value per layer, indicating that each layer should be ablated using the
refusal direction associated with that layer.
* max_weight, max_weight_position, min_weight, and min_weight_distance:
For each component, these parameters describe the shape and position of the
ablation weight kernel over the layers. The following diagram illustrates this:

<img width="800" height="500" alt="Explanation" src="https://github.com/user-attachments/assets/82e4b84e-5a82-4faf-b918-ac642f9e4892" />

&nbsp;

Heretic's main innovations over existing abliteration systems are:

* The shape of the ablation weight kernel is highly flexible, which, combined with
automatic parameter optimization, can improve the compliance/quality tradeoff.
Non-constant ablation weights were previously explored by Maxime Labonne in
gemma-3-12b-it-abliterated-v2.
* The refusal direction index is a float rather than an integer. For non-integral
values, the two nearest refusal direction vectors are linearly interpolated.
This unlocks a vast space of additional directions beyond the ones identified
by the difference-of-means computation, and often enables the optimization
process to find a better direction than that belonging to any individual layer.
* Ablation parameters are chosen separately for each component. I have found that
MLP interventions tend to be more damaging to the model than attention interventions,
so using different ablation weights can squeeze out some extra performance.


Prior art

I'm aware of the following publicly available implementations of abliteration
techniques:

* AutoAbliteration
* abliterator.py
* wassname's Abliterator
* ErisForge
* Removing refusals with HF Transformers
* deccp

Note that Heretic was written from scratch, and does not reuse code from
any of those projects.


Acknowledgments

The development of Heretic was informed by:

* The original abliteration paper (Arditi et al. 2024)
* Maxime Labonne's article on abliteration,
as well as some details from the model cards of his own abliterated models (see above)
* Jim Lai's articles describing "projected abliteration"
and "norm-preserving biprojected abliteration"


Citation

If you use Heretic for your research, please cite it using the following BibTeX entry:

bibtex
@misc{heretic,
author = {Weidmann, Philipp Emanuel},
title = {Heretic: Fully automatic censorship removal for language models},
year = {2025},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/p-e-w/heretic}}
}


License

Copyright &copy; 2025-2026 Philipp Emanuel Weidmann (<[email protected]>) + contributors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

By contributing to this project, you agree to release your
contributions under the same license.