Abliteration HandbookField guide
Methods

LoRA adapter export from abliteration

Convert a full abliteration (or manual projection) into a PEFT LoRA adapter for low-VRAM inference and OTA updates.

2 min read 6 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.

Convert a full abliteration (or manual projection) into a PEFT LoRA adapter for low-VRAM inference and OTA updates.

When to use#

  • Base model stays frozen on disk (one copy for many policies)
  • Target machine has 8 GB VRAM — run 4-bit base + adapter
  • You abliterated on a cloud GPU but deploy to factory benches with limited RAM

Prerequisites#

  • Base model safetensors (original, pre-abliteration)
  • Abliterated model safetensors (Heretic output or sharded_ablate.py)
  • Python 3.10+, torch, peft, safetensors, transformers
bash
pip install torch transformers peft safetensors accelerate

Algorithm#

For each target module (match Heretic: o_proj, down_proj per layer):

Example
ΔW = W_abliterated - W_base
ΔW ≈ B @ A   with A ∈ R^{r×in}, B ∈ R^{out×r}

Use truncated SVD on ΔW (float32 on CPU for numerical stability). Store as LoRA (lora_A, lora_B) with scaling α/r.

Export script (in repo)#

bash
python scripts/export-abliteration-lora.py \
  --base ./models/Qwen3-4B-ORIGINAL \
  --abliterated ./models/Qwen3-4B-abliterated \
  --out ./adapters/abliteration-r16 \
  --rank 16

Writes adapter_model.safetensors with {module}.lora_A / {module}.lora_B keys for each o_proj / down_proj delta.

GGUF sidecar: convert_lora_to_gguf.py → gguf-export-notes.md

Alternative: community abliteration LoRA checkpoints (train/export refusal removal as adapter): grimjim/Llama-3-Instruct-abliteration-LoRA-8B

Align target_modules with your abliteration recipe if wrapping in full PEFT LoraConfig.

Load at inference (4-bit + adapter)#

python
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel

bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16)
base = AutoModelForCausalLM.from_pretrained(
    "./base-model",
    quantization_config=bnb,
    device_map="auto",
)
model = PeftModel.from_pretrained(base, "./abliteration-lora")

Or merge for GGUF:

python
merged = model.merge_and_unload()
merged.save_pretrained("./merged-abliterated")

Then gguf-export-notes.md.

Quality checks#

CheckPass criteria
Reconstruction error‖ΔW - BA‖_F / ‖ΔW‖_F < 0.05 per layer (tune rank if higher)
Refusal evalSame harmful set as full abliteration — ≤2% refusal delta
KL / harmlessWithin 0.05 of full-weight abliteration
Rank sweepTry r ∈ {8, 16, 32, 64}; pick smallest r that passes eval

Heretic shortcut#

If you used row_normalization = "full", Heretic may already emit LoRA-like correction tensors in the checkpoint. Inspect shard keys for lora before re-factorizing.

bash
python -c "
from safetensors import safe_open
with safe_open('model.safetensors', framework='pt') as f:
    print([k for k in f.keys() if 'lora' in k.lower()][:20])
"

Trace this chapter to its source.

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

methods/lora-adapter-export.md
Inspect the source fingerprintSHA-256 def7a72ba4237d99a226b5176de95d195e8e425320e2fdc0c84ad7ede7cf886fSource commit 6fba7436a5914cad7c8f0835a14b480a29dac263