Metadata-Version: 2.4
Name: dissident
Version: 0.2.0
Summary: Reproducible bf16 refusal-direction abliteration, mechanistic verification, and local safety evaluation
Author: Ashwin Sreedhar
License: MIT
Project-URL: Homepage, https://github.com/ashwinsreedhar28/dissident
Project-URL: Repository, https://github.com/ashwinsreedhar28/dissident
Project-URL: Documentation, https://github.com/ashwinsreedhar28/dissident/blob/main/docs/TECHNICAL_REPORT_V2.md
Project-URL: Issues, https://github.com/ashwinsreedhar28/dissident/issues
Project-URL: Models, https://huggingface.co/collections/ashwin-sreedhar/dissident-v2-verified-bf16-abliterated-checkpoints-6b0f0cb8c6e154c27bd41767
Keywords: abliteration,llm,mechanistic-interpretability,refusal-direction,bf16,safetensors,moe,pytorch,mlx
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy<3,>=1.26
Requires-Dist: safetensors<1,>=0.6
Requires-Dist: huggingface-hub>=0.34
Requires-Dist: pyarrow>=15
Provides-Extra: mlx
Requires-Dist: mlx; extra == "mlx"
Requires-Dist: mlx-lm; extra == "mlx"
Requires-Dist: fastapi; extra == "mlx"
Requires-Dist: uvicorn; extra == "mlx"
Provides-Extra: cuda
Requires-Dist: torch; extra == "cuda"
Requires-Dist: transformers==5.21.0; extra == "cuda"
Requires-Dist: accelerate<2,>=1.10; extra == "cuda"
Requires-Dist: sentencepiece; extra == "cuda"
Requires-Dist: protobuf; extra == "cuda"
Provides-Extra: cloud
Requires-Dist: huggingface-hub>=0.34; extra == "cloud"
Requires-Dist: requests<3,>=2.32; extra == "cloud"
Requires-Dist: safetensors<1,>=0.6; extra == "cloud"
Provides-Extra: demo
Requires-Dist: rich>=13.0; extra == "demo"
Requires-Dist: requests<3,>=2.32; extra == "demo"
Dynamic: license-file

# dissident

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
[![Tests](https://github.com/ashwinsreedhar28/dissident/actions/workflows/tests.yml/badge.svg)](https://github.com/ashwinsreedhar28/dissident/actions/workflows/tests.yml)
[![Hugging Face Collection](https://img.shields.io/badge/HuggingFace-dissident--v2-yellow.svg)](https://huggingface.co/collections/ashwin-sreedhar/dissident-v2-verified-bf16-abliterated-checkpoints-6b0f0cb8c6e154c27bd41767)
[![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](pyproject.toml)

Reproducible bf16 refusal-direction abliteration, mechanistic verification, and local safety evaluation (MIT; formerly `persona-lab`).

`dissident` extracts refusal directions and projects residual writers directly in bf16 across dense, hybrid-attention, post-RMSNorm, and Mixture-of-Experts models. Original weights are hashed and left untouched. Every exported checkpoint is written to a separate directory and gated by SHA-256 tensor checks, a `0.0` fresh-process reload diff, `<= 1%` forward-pass mechanical suppression, held-out harmless KL `<= 0.10`, and matched local `Qwen3Guard-Gen-4B` evaluation.

![dissident split-pane TUI](https://raw.githubusercontent.com/ashwinsreedhar28/dissident/main/assets/tui_demo.png)
*Split-pane terminal TUI (`demo/tui_demo.py`) streaming Base vs. Abliterated v2 side-by-side on `benign-017` (`sudo` over-refusal prompt).*

## Verified v2 results

Base and edited checkpoints are evaluated under identical greedy bf16 decoding on JailbreakBench Harmful (100), JailbreakBench Benign (50), and GSM8K using local `Qwen3Guard-Gen-4B`. No raw harmful prompts or completions are committed to this repository. Checkpoints that miss any gate are held as diagnostics rather than published.

| Model | JBB Refusal | Unsafe | Benign | GSM8K | KL / Mech | Checkpoint |
|---|---:|---:|---:|---:|---:|---|
| **GPT-OSS-20B v2** (21B MoE, Harmony) | 95/100 → 0/100 | 2 → 94 | 8/50 → 0/50 | 95.6% → 95.6% (`n=250`) | `0.0933` / `0.785%` | [HF (Apache-2.0)](https://huggingface.co/ashwin-sreedhar/GPT-OSS-20B-abliterated-v2-bf16) |
| **Kimi-Linear-48B v2** (48B Hybrid MoE) | 92/100 → 1/100 | 2 → 90 | 9/50 → 0/50 | 93.3% → 90.7% (`n=150`) | `0.0830` / `0.658%` | [HF (MIT)](https://huggingface.co/ashwin-sreedhar/Kimi-Linear-48B-A3B-Instruct-abliterated-v2-bf16) |
| GPT-OSS-120B v2 (120B MoE, Harmony) | 96/100 → 85/100 | 0 → 13 | 16/50 → 2/50 | 96.7% → 96.7% (`n=150`) | `0.1253` / `0.616%` | Held (Guard 85/100, KL > 0.10) |
| Llama-3.1-8B-Instruct v2 (8B Dense) | 91/100 → 0/100 | 4 → 90 | 6/50 → 0/50 | 91.0% → 87.5% (`n=200`) | `0.0233` / `1.051%` | Held (L32 bf16 1.051% > 1%) |
| Qwen3-8B v2 (8B Dense) | 87/100 → 32/100 | 4 → 53 | 4/50 → 0/50 | — | `0.0770` / `0.618%` | Held (Guard 32/100; r2 KL >= 0.24) |
| GLM-4.7-Flash v2 (30B MLA+MoE) | — | — | — | — | `0.0399` / `1.357%` | Held (mech 1.357% > 1%) |
| Gemma-3-4B-IT v2 (4B Post-Norm) | — | — | — | — | `0.1020` / — | Held (min KL 0.1020 > 0.10) |

Full aggregate JSON records live in [evaluation/results/](evaluation/results/). The full v2 technical writeup is in [docs/TECHNICAL_REPORT_V2.md](docs/TECHNICAL_REPORT_V2.md); earlier v1 checkpoints and historical failures are tracked in [docs/MODEL_STATUS.md](docs/MODEL_STATUS.md); pinned revisions and hashes live in [docs/MODEL_STORAGE.md](docs/MODEL_STORAGE.md); open work lives in [docs/ROADMAP.md](docs/ROADMAP.md).

## Key findings

- **Substring refusal markers hide semantic refusals at scale:** On `Qwen3-8B` (rank-1, `eff_rank=6.06`), marker refusals fell `87/100 → 4/100` while `Qwen3Guard-Gen-4B` still caught `32/100` semantic refusals. On `GPT-OSS-120B` (6x larger than `GPT-OSS-20B` on the same Harmony architecture), single-direction `L24` (`induce=1.000`, `0/32` short-rollout val refusal, `0.0 pp` GSM8K drop, benign over-refusal `16/50 → 2/50`) dropped marker refusals `96/100 → 29/100`, yet `Qwen3Guard-Gen-4B` caught `85/100` semantic refusals phrased without boilerplate prefixes.
- **Refusal localization and judge validation on `GPT-OSS-20B`:** Published v2 preserved GSM8K (`239/250 -> 239/250`, `95.6%`) and Verifiable IF (`26/30 -> 28/30`, `86.7% -> 93.3%`). A 24-prompt stratified blind human audit ([gpt-oss-20b-v2-adjudication.json](evaluation/results/gpt-oss-20b-v2-adjudication.json)) matched `Qwen3Guard-Gen-4B` on **24/24** refusal labels (`95% CI [86.2%, 100%]`, `22/24` substantive, `0/24` degenerate). In-memory band sweeps showed refusal sits tightly in layers `10..18` (9/24 layers), cutting `KL_analysis` 7.8x (`0.0737 → 0.0094`) and `KL_final` 2.5x (`0.0933 → 0.0371`) at `0/32` val refusals before jumping to `11/32` at `11..16`.
- **Relation to [p-e-w/heretic](https://github.com/p-e-w/heretic):** Heretic optimizes per-layer projection weights via black-box Optuna search for broad model coverage. `dissident` focuses on complementary infrastructure: deterministic bf16 weight integrity with SHA-256 and `0.0` reload proofs, tied-embedding and post-RMSNorm safeguards, native MXFP4/MoE/hybrid-linear adapters, and local `Qwen3Guard-Gen-4B` gating instead of substring heuristics.

## Caveats

- Refusal and safety counts use `Qwen3Guard-Gen-4B` (local 4B classifier). On our 24-prompt blind human audit of `GPT-OSS-20B` v2, refusal agreement was `24/24` (`100%`), while Safe/Controversial/Unsafe boundary agreement was `8/23` (`34.8%`) because the judge flags benign technical prompts as unsafe.
- Upstream `openai/gpt-oss-20b` and `openai/gpt-oss-120b` ship routed expert weights in MXFP4. Base and edited evaluations run on the exact same verified bf16 dequantization; dequantization does not recover precision lost during upstream quantization.
- `dissident`'s strict `<= 1.0%` full-stream mechanical check can reject checkpoints on single-layer bf16 rounding (`Llama-3.1-8B` hit `1.051%` at final layer `L32` despite `0/100` JBB refusals and `0.0233` KL). Those checkpoints are kept as diagnostics rather than silently loosening the release bar.

## Quickstart

| Workload | Minimum VRAM / RAM | Notes |
|---|---|---|
| 4B–8B dense (`Qwen3-8B`, `Llama-3.1-8B`, `Gemma-3-4B`) | 24–48 GB GPU | Single bf16 copy + eval |
| `GPT-OSS-20B` (MXFP4→bf16) / `GLM-4.7-Flash` (30B) | 48–96 GB GPU | 96 GB holds Base + v2 side-by-side for TUI |
| `Kimi-Linear-48B-A3B-Instruct` | 96 GB GPU (`--batch-size 1`) | Requires `requirements/cuda.txt` (`transformers==4.57.3`, `fla-core`) |
| `GPT-OSS-120B` (MXFP4→bf16) | 288 GB GPU (1x B300) | CPU-offloaded writer backups in `Engine.ablated()` |
| `Qwen3.5-4B` (MLX) | 16 GB Apple Silicon | Run generation and judging in separate processes |

```sh
# 1. Install (CUDA export + split-pane terminal TUI)
pip install -e ".[cuda,demo]"        # or: pip install -r requirements/native-cuda.txt

# 2. Build fixed, leakage-filtered train/val/KL/eval splits (gitignored)
dissident prepare-data --output data

# 3. Extract, edit, and verify a separate bf16 checkpoint (originals untouched)
dissident run --model Qwen/Qwen3-8B --data data --output models/Qwen3-8B-abliterated-v2-bf16
dissident verify models/Qwen3-8B-abliterated-v2-bf16

# 4. Launch the Base vs. Abliterated split-pane terminal TUI (or print scoreboard)
python demo/tui_demo.py --score-only
python demo/tui_demo.py --gpu-url http://127.0.0.1:8766   # or --mock for offline preview
```

## Method

- Extraction and selection (`--reasoning-mode empty-think`, `--selector rollout`): On reasoning and Harmony models, v1 first-token scoring latched onto generic reasoning openers (`User`, `We`). v2 extracts at the post-reasoning answer boundary (`empty-think` or `post-think`) and scores short greedy rollouts after stripping Harmony `analysis` channels or `<think>` blocks.
- Biprojection (`--biproject-mode auto`): Screens both biprojected (orthogonalized against the harmless activation mean) and raw directions across separate `pos=-1` and `pos < -1` candidate budgets, breaking `AUROC=1.0` ties with validation Fisher SNR. Biprojection cut KL 13.4x on `Llama-3.1-8B` (`0.3584 -> 0.0268`) and 2.5–3x on `GLM-4.7-Flash`.
- Subspace rank (`--rank k`): Records residual effective rank and top-k SVD variance per layer. On `GPT-OSS-20B`, PC1 captures 76–90% of harmful displacement variance; rank-1 matched rank-2 on JBB (`0/100` refusals, `94/100` unsafe) at roughly 4x lower harmless KL (`0.0933` vs `0.3459`). On `Qwen3-8B` (`eff_rank=6.06`), global rank-2 removal blew up KL (`0.24–5.37`).
- Architecture safeguards: Defaults to `--embed-policy skip-tied` (or explicit `untie`) so editing tied input embeddings never corrupts `lm_head`. Gemma 3 post-RMSNorm writers are projected along `orth(Q diag(1 + gamma))` with RMS inflation checks. `Engine.ablated()` backs up writer weights to CPU RAM so large MoEs (`48B–120B`) incur zero extra VRAM during candidate sweeps.
- Verification: `dissident verify` checks manifest and file SHA-256 hashes, tensor payload readback, fresh-process reload agreement, and `max_proj_ratio <= 1%` without loading the model.

## Supported architectures

| Family | Adapter | Residual Writers Projected |
|---|---|---|
| `openai/gpt-oss-20b`, `gpt-oss-120b` | `dense-torch` | `embed_tokens` + `o_proj` + packed 3D `mlp.experts.down_proj` + bias (Harmony) |
| `zai-org/GLM-4.7-Flash` | `dense-torch` | `embed_tokens` + `o_proj` + dense MLP + 64 routed & shared experts (MTP preserved) |
| `RecursiveSI/flint` | `dense-torch` | `embed_tokens` + `o_proj` + dense MLP + 256 routed & shared experts (packed/unpacked) |
| `moonshotai/Kimi-Linear-48B-A3B-Instruct` | `kimi-torch` | `embed_tokens` + `o_proj` + 256 routed `w2` + shared experts (`requirements/cuda.txt`) |
| `google/gemma-3-*` | `dense-torch` | Post-RMSNorm weighted `orth(Q diag(1+gamma))` on `o_proj`/`down_proj` + `skip-tied`/`untie` |
| `Qwen/Qwen3-*`, `meta-llama/Llama-3.1-*` | `dense-torch` | `embed_tokens` + all decoder `o_proj` & `mlp.down_proj` |
| `Qwen/Qwen3.5-4B` (Apple Silicon MLX) | `qwen-mlx` | Hybrid linear/full attention `out_proj`/`o_proj` + `down_proj` + `skip-tied` |

## Repository layout

| Path | Contents |
|---|---|
| `abliteration/` | Core bf16 pipeline: data splits (`data.py`), direction math/SVD/biprojection (`math.py`), writer maps (`architecture.py`), dense/MoE/Kimi/MLX adapters, MXFP4 dequantizer (`conversion.py`), Harmony parser, CUDA chat (`chat.py`), CLI (`cli.py`) |
| `cloud/` | Sealed Hugging Face publisher (`publish_hf.py`), atomic model-card updater (`update_card.py`), license assets, and Runpod helpers |
| `demo/` | Split-pane Base vs. v2 terminal TUI (`tui_demo.py`), CUDA SSE backend (`gpu_backend.py`), and Pod launcher (`launch_friday_demo.py`) |
| `evaluation/` | Matched evaluations, blind human adjudication CLI (`adjudicate.py`), and aggregate-only results (`evaluation/results/`) |
| `tools/` | Apple Silicon activation-steering vector builders, split audits, and sweeps |
| `docs/` | Guides ([V2 technical report](docs/TECHNICAL_REPORT_V2.md), [Export guide](docs/ABLITERATION.md), [Model status](docs/MODEL_STATUS.md), [Storage & pins](docs/MODEL_STORAGE.md), [Roadmap](docs/ROADMAP.md), [Layout](docs/REPOSITORY_LAYOUT.md), [Math & MLX steering](docs/STEERING_AND_MATH.md)) + historical memos in `docs/archive/` |
| `tests/` | Unit and CPU fixture regression suite (`python -m unittest discover -s tests -v`) |
| `archive/` | Historical Phase A (`phase-a-abl/`), Phase B (`phase-b-kimi/`), and legacy PyTorch scripts (`legacy/`) |
| `evil_chat_mlx.py`, `server.py` | Apple Silicon MLX interactive chat and activation-steering web UI |

## License

Repository code is MIT. Derivative model checkpoints published on Hugging Face retain their upstream licenses, notices, and usage policies (`Apache-2.0`, `MIT`, `Gemma Terms`, `Llama 3.1 Community License`).
