heretic README

项目的 README,来自 github.com/p-e-w/heretic。合并为一个 markdown 文件:README.md。

THIS REPOSITORY HAS BEEN ARCHIVED. See https://github.com/p-e-w/heretic/issues/601 for more information.

Heretic logo

Heretic

Training-free language model modification

Remove censorship, suppress slop, reduce sycophancy, and more.
All in an afternoon, on your gaming PC.

Discord     Matrix    Follow us on Hugging Face    Codeberg mirror


 

#1 Repository of the Day     Model and download statistics

Modify any LLM that you can run on your hardware[^1]

[^1]: Heretic supports on-the-fly 4-bit model quantization with bitsandbytes, which can reduce the amount of VRAM required to process a model to the same amount that is used to run a typical quantized version of it, at a slight accuracy cost. Set the quantization option to bnb_4bit to enable this.

Heretic is a unique tool that lets you do what only corporations with access to massive computing power usually do: Modify, rather than merely run, language models. It does not perform expensive training; rather, it uses a collection of mathematical techniques to steer behavior based on contrasting example prompts and matching scorers.

Heretic can, depending on the configuration:

and many, many other things.

:bangbang: Using Heretic does NOT require understanding how language models work internally :bangbang:

In fact, you can go from zero to removing censorship from your first model in a matter of seconds. Heretic supports most dense models, including many multimodal models, several different MoE architectures, and many modern hybrid models.

The community has created and published thousands of models with Heretic. Heretic models have been independently benchmarked using standard metrics like MMLU and GSM8K, and have been found to compare favorably against models produced with competing abliteration tools.[^2][^3]

[^2]: Qwen2.5-7B: Three-Way Abliteration Comparison: "Heretic is the most surgical and most effective." [^3]: Gemma 4 E4B: 23 Abliteration Variants Compared: "Best overall: heretic."

 

Heretic screenshot

 

Usage

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

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

Replace Qwen/Qwen3-4B-Instruct-2507 with whatever model you want to process. By default, Heretic performs censorship removal (refusal suppression). If you want to do something else, choose one of the configuration files linked above, or write your own. See config.default.toml for available configuration options. Heretic loads the configuration from config.toml in the working directory by default, or from the file path in the HERETIC_CONFIG_FILE environment variable.

[!IMPORTANT]

Configuration files made for Heretic 1.x are not compatible with Heretic 2.x! See the migration guide for what you need to change in order to make your old configurations work.

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, processing Qwen3-4B-Instruct-2507 takes about 30-40 minutes.

After Heretic has finished processing a model, you are given the choice to save the model, upload it to Hugging Face, chat with it to test how well it works, run standard benchmarks on it, or any combination of those actions.

Controlling what Heretic does using scorer plugins

Heretic steers model behavior by optimizing one or multiple scores. Scores are computed by scorer plugins. You can easily write your own scorers to get full control over what Heretic does to models.

The Scorer interface (abstract base class) your scorer needs to inherit from is defined in scorer.py. The only required method is get_score, but there is more machinery (such as settings management) available when you need it.

By default, Heretic scores models by refusal rate (implemented in keyword_rate.py) and KL divergence (implemented in kl_divergence.py). Additional scorers are available in the scorers directory. These scorers can serve as examples for your own implementations, and can easily be adapted to your needs.

Scorers are configured through the main configuration file. Which scorer plugins are active is determined by the scorers setting, which also controls whether a scorer's score should be minimized or maximized during optimization. Any number of scorers can be used simultaneously, and their scores will be co-optimized. Multiple instances of the same scorer can be configured with different settings. See the configuration file for the full documentation of this mechanism.

The path to a scorer plugin can be specified either as a fully qualified import path (fully.qualified.module.MyScorerClass) or as a file path followed by a class name (path/to/plugin.py:MyScorerClass). You do not have to modify the Heretic package code in order to use your own plugins.

Heretic also has modifier plugins, which are described in the next section.

How Heretic works

Heretic combines a pluggable modifier (interface in modifier.py) with a TPE-based hyperparameter optimizer powered by Optuna. The optimizer finds a set of parameters that, when passed to the modifier, minimize or maximize a set of configurable scores on the resulting model (see above).

Heretic ships with two modifiers:

[^4]: Weidmann 2026. A preprint explaining the technique will be available soon. [^5]: Arditi et al. 2024 [^6]: Lai 2025 (1) [^7]: Lai 2025 (2)

You can use the modifiers setting to switch between modifier implementations. Despite the name of the setting using a plural form, Heretic currently allows only a single modifier for a given processing run. This may change in the future.

Reproducibility and the Heretic Grimoire

Heretic has the ability to create reproducible models whose repositories contain all information required to produce a byte-for-byte identical model on demand, provided that the environment is perfectly identical to the original one. PyTorch doesn't guarantee that operations return identical outputs unless hardware, drivers, operating system, and the entire software stack including all package versions are exactly the same.

Heretic manages all that complexity for you. When uploading a processed model to Hugging Face, Heretic offers to include reproducibility information. This is entirely optional, but if you choose to do so, the uploaded repository will contain a reproduce folder with detailed instructions for recreating the model (example). This folder contains both human-readable instructions and a machine-readable manifest file named reproduce.json, which includes all the information Heretic needs to automatically reproduce the model.

Automatic reproduction can be performed with

heretic --reproduce path/to/reproduce.json

Heretic will guide you through the process, checking your environment against the one that was used to create the model, and pointing out potentially problematic mismatches. The multi-hour computations that were required to make the original model do not have to be re-done, and the entire process typically takes a few minutes. After you have exported the resulting model, Heretic will verify the hashes of the weight files against those stored in the reproduction manifest (they may or may not be identical, depending on how closely your system resembles the original one).

Many people have uploaded models with reproducibility information to Hugging Face. Since a reproduction manifest is only around 9 kilobytes in size, these manifests form a portable library of models that can be reproduced at will.

That library is called the Heretic Grimoire. You can view it here.

But you don't have to rely on an externally hosted web application to access the library of manifests: The Grimoire is fundamentally designed as a distributed system, and you can fetch your own local copies of publicly available manifests from Hugging Face whenever you want by running

heretic --collect-reproducibles path/to/target/folder/

You now have a local backup of all reproducible Heretic models, properly catalogued. To update this collection, simply run the command again. It functions as an append-only backup, never deleting files even if the corresponding model no longer exists on Hugging Face.

Heretic is made by humans!

Unlike lots of AI-related software today (and indeed lots of software in general), Heretic is not "vibe-coded".

All of my commits to this project until November 2026 were written 100% by hand, the way I have been doing programming for decades. I have been using LLM assistance very sparingly since. While many Heretic contributors use coding agents, I carefully review every single line of code that I merge. There's no "LGTM" here!

Don't believe me? Check out this pull request, which accumulated almost 350 human-written comments over more than 7 months as we discussed the implementation. And here is another PR with more than 200 comments.

I am the opposite of a luddite and I have used agents liberally for other projects, but I am proud of the code I write and so far can't quite bring myself to let a machine write it for me in a project I care so much about. As a result, Heretic has a lean, relatively simple codebase that in my opinion, and in that of some others, is a pleasure to work on.

If you like that, join us! Hop on the Discord and tell us about your ideas for improvements, or directly file an issue or open a PR.

Prior art

I'm aware of the following implementations of abliteration techniques that were publicly available when Heretic was first released (November 2025):

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:

Citation

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

@misc{heretic,
  author = {Weidmann, Philipp Emanuel},
  title = {Heretic: Training-free language model modification},
  year = {2025},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/p-e-w/heretic}}
}

License

Copyright © 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.

 

 

 

heretic (p-e-w/heretic)

正在搜索数据库…

45.1K ·GitHub
已归档
这是什么?Heretic 是一个命令行工具,它结合使用基于 Optuna 的参数优化器与定向消融(directional ablation),自动去除基于 Transformer 的语言模型中的审查(安全对齐),整个过程无需训练。
这是命令行工具吗?使用时需要编程吗?它是一个命令行程序:你只需运行一条 `heretic` 命令,它就会自动完成整个去审查过程,不需要了解 Transformer 的内部原理。
怎样用它去除模型的审查?搭建一个带有 PyTorch 的 Python 3.10+ 环境,然后运行 `heretic --model `,例如 `heretic --model Qwen/Qwen3-4B-Instruct-2507`。克隆仓库后使用 `uv run heretic`,可以通过随附的 uv.lock 文件锁定精确的依赖版本。
运行前需要安装什么?需要 Python 3.10+ 和 PyTorch 2.2+(某些功能,例如加载 gpt-oss 这类 MXFP4 量化模型,需要 PyTorch 2.6+ 才能使用 torch.accelerator)。项目推荐使用 uv 来管理依赖。
它适用于所有语言模型吗?它支持大多数稠密模型、许多多模态和 MoE(混合专家)架构,以及 Qwen3.5 等部分混合模型,但纯状态空间模型和某些其他研究型架构目前还不支持。另外,在 RTX 3090 上处理一个 4B 模型,单次运行可能需要 20-30 分钟。
这需要高性能显卡吗?它基于 GPU 加速的 PyTorch 构建,基准测试是在 RTX 3090/5090 显卡上进行的。不过,Heretic 支持 bitsandbytes 量化(bnb4bit),可以大幅降低显存需求,适合配置较低的设备。
消融(abliteration)的过程到底是怎样的?对于每个 Transformer 层,Heretic 会找出注意力输出投影矩阵和 MLP 下投影矩阵,并使其与一个“残差方向”正交。该残差方向是通过有害提示与无害提示在首个 token 激活值上的均值差计算得出的,从而抑制这一方向的表现。
Heretic 的效果与手动消融的模型相比如何?在 README 的对比表中,Heretic 生成的 gemma-3-12b-it 版本,其拒答抑制效果与手动消融的模型相当(100 次中有 3 次拒答),但与原始模型的 KL 散度要低得多(0.16,手动消融为 0.45-1.04),这意味着对模型能力的损害更小。
模型去除审查之后,我能做什么?Heretic 允许你将结果模型保存到本地、上传到 Hugging Face、与它进行交互式聊天以测试效果,或在其上运行标准基准测试,这些操作可以任意组合。
Heretic 现在已经被广泛使用了吗?社区已经使用 Heretic 创建并发布了 5000 多个模型,而且独立基准测试(MMLU、GSM8K)显示,它的结果相比其他消融工具表现更优。
作者
p-e-w
大小
1.6 MB
星标
45.1K
时长
1年
提交 · 90 天