Heretic
Heretic Architecture: Residual Directions, Weight Kernels, and Optuna
Trace Heretic from prompt residuals through directional ablation, parameter search, evaluation, and research visualizations.

What you will learn
- Explain the KL/refusal optimization objective
- Run a pinned model experiment
- Design broader safety evaluation
Before you start
- Basic Git and command-line usage
- Comfort reading a project README
You can explain the project, run its documented first step, and decide what to verify before adopting it.
Key takeaways
- Prompt residuals and weight surgery are distinct stages.
- Float direction indices and flexible kernels enlarge the search space.
- The research visualization path is separate from model transformation.
A layered mental model
The README exposes four useful layers: prompt groups produce first-token residuals; layer-wise differences define directions; supported attention out-projection and MLP down-projection matrices are orthogonalized; Optuna searches parameters against refusals and KL divergence.
The special `per layer` direction index selects each layer's direction, while a float index interpolates between neighboring directions. This turns a discrete set of measured vectors into a larger search space, but also makes experiment provenance essential.
The ablation kernel
`max_weight`, `max_weight_position`, `min_weight`, and `min_weight_distance` describe a flexible weight shape over layers. Attention and MLP components receive separate parameters because the README reports that MLP interventions can damage capabilities more.
The optimization objective is a co-minimization: fewer refusals on harmful prompts and low KL divergence on harmless prompts. Neither term is a complete model-quality objective; add capability, calibration, bias, and misuse evaluations around it.
Research branch
Optional research features compute residual geometry, use PaCMAP to project layers, align consecutive projections, and emit PNG/GIF artifacts. Keep this CPU-heavy analysis off the critical transform path and preserve the source arrays or hashes used to create plots.
Decision guide
| Criterion | Option A | Option B |
|---|---|---|
| Best when | You need predictable behavior and easy auditing | You need adaptive optimization and have reliable telemetry |
| Main risk | May leave performance on the table | Can become difficult to explain or debug |
Implementation steps
- 1
Map residual collection and direction construction.
- 2
Trace matrix orthogonalization by component and layer.
- 3
Inspect Optuna parameters and objective metrics.
- 4
Separate plots, evaluation, and export from the transform core.
Copy-ready example
harmful/harmless prompts
-> first-token residuals
-> layer directions
-> attention/MLP matrix orthogonalization
-> Optuna TPE (refusals + KL)
-> checkpoint + evaluationFrequently asked questions
Does Heretic edit every transformer layer equally?
No. It supports layer-specific directions and flexible, separately optimized weight kernels.
Is KL divergence a safety metric?
No. It measures deviation from the original model on a chosen prompt set; pair it with broader behavioral and human review.
Sources
- Heretic README (captured 2026-08-31)Source checked 2026-08-31
- Heretic repositorySource checked 2026-08-31