Abliteration HandbookField guide
Methods

Safetensor abliteration pipeline

How alignment refusal directions are removed from Hugging Face checkpoint files (*.safetensors) — the correct surgery surface.

2 min read 5 examples Source-linked

In this chapter
On this page Interactive companion
Personal reading progress

0/8 sections reviewed

View original Markdown
Interactive companion

Explore the chapter map

Use the visual controls alongside the complete source chapter.

How alignment refusal directions are removed from Hugging Face checkpoint files (*.safetensors) — the correct surgery surface.


Target tensors#

ModuleTensorArchitectures
MLP blockmodel.layers.{L}.mlp.down_proj.weightLlama, Mistral, Qwen dense
Attentionmodel.layers.{L}.self_attn.o_proj.weightSame
MoE expert...experts.{E}.down_proj.weightQwen3-MoE, Phi-3.5-MoE, Mixtral
Granite hybridshared_mlp.output_linear, expert output_linearIBM Granite MoE

Heretic discovers layer kernel automatically. llm-abliteration uses YAML per destination layer.


Math (applied in-place on weights)#

Projected direction (Heretic default, orthogonalize_direction = true):

Example
g = normalize(mean(h_harmless))
r = normalize(mean(h_harmful) - mean(h_harmless))
r_proj = normalize(r - (r·g)g)
W' = W - λ · outer(r_proj, r_proj @ W)   # on down_proj / o_proj rows

Norm-preserving (row_normalization = "full"): approximate row magnitude restoration via rank-r LoRA correction bundled in output shards.

→ ../techniques/projected-norm-preserving-abliteration.md


Pipeline A — Heretic (automatic)#

bash
pip install heretic-llm==1.4.0 bitsandbytes accelerate safetensors
cp sources/heretic-tools/config.production.toml config.toml
heretic ./models/Qwen3-4B-Instruct-2507

On completion, choose save path — writes standard HF layout:

Example
out/
├── config.json
├── model.safetensors          # or sharded model-00001-of-00004.safetensors
├── tokenizer.json
├── generation_config.json
└── (optional lora_* keys if row_normalization=full)

Backup rule: copy source to *-ORIGINAL before run. Never overwrite original shards.


Pipeline B — llm-abliteration (manual)#

bash
python measure.py -m ./base -o directions.pt --quant 4bit --projected
python analyze.py directions.pt -c
python sharded_ablate.py my-model.yml --projected --normpreserve

sharded_ablate.py reads full-precision safetensors one shard at a time, writes new safetensors to output dir.

Critical: 4-bit quantized checkpoints are for measurement only. Ablation mutates FP16/BF16 weights.


Pipeline C — Hook prototype (no safetensor edit)#

TransformerLens / HF hooks subtract (h·r)r at inference — produces direction.pt only.

Use to validate prompts before permanent edit:

→ residual-hook-ablation.md · ../instructions/quickstart.md


Verification before GGUF export#

bash
# Spot-check shard keys unchanged count
python -c "
from safetensors import safe_open
for p in ['ORIGINAL/model.safetensors','ABLITERATED/model.safetensors']:
    with safe_open(p, framework='pt') as f:
        print(p, len(f.keys()))
"

# Heretic built-in
heretic --evaluate-model ./ABLITERATED
CheckPass
Shard count / keysSame keys as base (values differ on target modules)
Refusal rate harmful setDown vs original
KL harmless setHeretic target ~0.01–0.5 depending on deploy
Factory JSONL../data/eval/hardware-factory-prompts.jsonl

Common mistakes#

MistakeConsequence
Abliterate GGUF directlyNot supported — convert to HF first
Abliterate 4-bit stored weightsUse Heretic/llm-abliteration on load-quant or FP base
Skip orthogonalize_directionHigher KL / capability loss
Overwrite ORIGINAL folderNo rollback
Merge wrong adapter before GGUFGarbage logits — verify PEFT target modules

Next step#

→ gguf-export-notes.md · ../docs/toolchain-safetensors-gguf-lora.md

Trace this chapter to its source.

The complete Markdown remains the editable source. This page adds navigation, typography, and a visual companion.

methods/safetensor-abliteration-pipeline.md
Inspect the source fingerprintSHA-256 3cf6866bd4ed15b6034ead4f421b1f1a638f50ac6c1639ff75dfce6326f867bdSource commit 6fba7436a5914cad7c8f0835a14b480a29dac263