Metadata-Version: 2.4
Name: lucid-toolkit
Version: 0.1.5
Summary: Unified, modular & extensible toolkit for low-rank subspace LLM behavioral realignment
Author: Lucid Contributors
License: Apache-2.0
Keywords: llm,alignment,realignment,abliteration,mechanistic-interpretability,gguf,fp8,nvfp4,lora
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: numpy>=1.24
Requires-Dist: jinja2>=3.1.6
Provides-Extra: search
Requires-Dist: optuna>=3.5; extra == "search"
Provides-Extra: gguf
Requires-Dist: gguf>=0.14; extra == "gguf"
Provides-Extra: fp8
Requires-Dist: ml_dtypes>=0.4; extra == "fp8"
Provides-Extra: yaml
Requires-Dist: pyyaml>=6.0; extra == "yaml"
Provides-Extra: plot
Requires-Dist: matplotlib>=3.7; extra == "plot"
Provides-Extra: hf
Requires-Dist: huggingface_hub>=0.34; extra == "hf"
Provides-Extra: torch
Requires-Dist: torch>=2.2; extra == "torch"
Requires-Dist: transformers>=4.45; extra == "torch"
Requires-Dist: safetensors>=0.4; extra == "torch"
Provides-Extra: vllm
Requires-Dist: vllm>=0.6; extra == "vllm"
Requires-Dist: torch>=2.2; extra == "vllm"
Requires-Dist: transformers>=4.45; extra == "vllm"
Requires-Dist: safetensors>=0.4; extra == "vllm"
Provides-Extra: bnb
Requires-Dist: bitsandbytes>=0.43; extra == "bnb"
Provides-Extra: all
Requires-Dist: optuna>=3.5; extra == "all"
Requires-Dist: gguf>=0.14; extra == "all"
Requires-Dist: ml_dtypes>=0.4; extra == "all"
Requires-Dist: pyyaml>=6.0; extra == "all"
Requires-Dist: matplotlib>=3.7; extra == "all"
Requires-Dist: huggingface_hub>=0.25; extra == "all"
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Dynamic: license-file

<p align="center">
  <img src="assets/banner.png" alt="Lucid" width="100%">
</p>

# Lucid

[![Discord](https://img.shields.io/badge/Discord-discord.gg%2Ffoss-5865F2?logo=discord&logoColor=white)](https://discord.gg/foss)

Lucid is an abliteration and behavioral realignment toolkit for open-weights LLMs.

Most abliteration toolkits try to eliminate 100% of refusals, even if it makes the model noticeably dumber or traps it in infinite thinking loops. Lucid aims for the sweet spot between intelligence and permissiveness, removing nearly all refusals while keeping normal outputs as close to the original model as possible.

## How it works

Classic abliteration pulls out a single "refusal direction" and applies it across the model. On modern reasoning models, that often falls short because think mode and chat mode refuse in different ways and at different layers. Lucid measures both modes separately and removes a small set of directions that covers both at once, so you don't end up with a model that complies in chat mode but still refuses (or loops) when thinking is turned on.

Instead of guessing one global strength, Lucid searches over different strengths across layers automatically, scoring every candidate on two things: how many refusals it removes, and how much its answers on normal prompts drift from the base model (measured by KL divergence). Each trial runs without rewriting the model weights, so searching stays fast and cheap even on 100B+ MoE models.

Saving the edit is where quantized models usually fight back. In FP8, FP4, or GGUF quants, a surgical edit is often smaller than half a rounding step, so plain rounding quietly undoes most of your work. Lucid uses error-diffusion rounding so the edit survives quantization, letting you bake straight back into BF16, FP8, NVFP4, or existing GGUF quants, or export a standalone LoRA GGUF to attach to any stock quant.

<p align="center">
  <a href="https://lucid-toolkit.org/benchmarks">
    <img src="assets/benchmarks.svg" alt="Qwen3.8-27B benchmark comparison: Base, Lucid, Heretic-ARA, and Huihui" width="100%">
  </a>
</p>

See the full 0-shot capability breakdown (MMLU, ARC-Challenge, HellaSwag, TruthfulQA) and exact KL numbers at [lucid-toolkit.org/benchmarks](https://lucid-toolkit.org/benchmarks).

## Quickstart

```bash
pip install "lucid-toolkit[all]"       # GGUF + native BF16/FP8/NVFP4 baking
pip install "lucid-toolkit[all,vllm]"  # recommended on CUDA: fast vLLM sampling + HF baking
pip install "lucid-toolkit[all,torch]" # optional: PyTorch / Hugging Face fallback (CPU/MPS/4-bit)
```

Model inspection, chat template and thinking-mode detection, downloading the default prompt set (`mrfakename/harm`), picking layers, searching, verifying, and saving all happen in one command:

```bash
# GGUF (default, fastest and cheapest)
lucid run ./model-Q8_0.gguf -o ./model-abliterated-Q8_0.gguf

# Hugging Face / safetensors checkpoint
lucid run Qwen/Qwen3-8B -o ./Qwen3-8B-abliterated

# Search cheaply on a GGUF, bake into the full BF16/FP8/NVFP4 checkpoint
lucid run ./MyModel-FP8 --gguf ./MyModel-Q6_K.gguf -o ./MyModel-FP8-abliterated

# Or run the entire pipeline on Hugging Face Jobs (source repo -> destination repo)
lucid hf-job Qwen/Qwen3-8B your-username/Qwen3-8B-Lucid --flavor a100-large --follow
```

Or in Python:

```python
import lucid

# Local
lucid.realign("./MyModel", output="./MyModel-abliterated")

# Remote on Hugging Face Jobs
lucid.run_hf_job("Qwen/Qwen3-8B", "your-username/Qwen3-8B-Lucid", flavor="a100-large")
```

Handy flags:
- `--backend auto|llama_cpp|vllm|hf_torch`: choose the sampling backend (`cpp` / `torch` aliases also work). `auto` uses `llama.cpp` for GGUF (no PyTorch/vLLM needed), and `vllm` on CUDA with automatic fallback to `hf_torch` if vLLM isn't installed or doesn't support the model.
- `--budget fast|balanced|thorough`: how many search trials to run (`20`, `45`, or `80`; default `balanced`).
- `--task refusal|noslop|nosyco|concise`: built-in behavioral presets (default `refusal`).
- `--push-to-hub <org/repo>`: upload the baked checkpoint and model card straight to Hugging Face Hub.
- `--interactive`: inspect the Pareto frontier and chat live with candidate trials before baking.
- `--include-cn`: adds a China-sensitive topic set and reframe detection.
- `--load-in-4bit`: load Hugging Face / PyTorch models in 4-bit (`bitsandbytes`) to reduce VRAM usage.
- `--bad prompts.txt --good benign.txt`: target any custom behavior instead of refusals.

Every stage can also be run individually for multi-GPU setups or custom architectures. See the docs and benchmarks at [lucid-toolkit.org](https://lucid-toolkit.org), join the [Discord](https://discord.gg/foss), run `lucid --help`, or check [examples/](examples/) (preview the site locally with `cd docs && bun install && bun run dev`).

## License

Lucid is licensed under the Apache License 2.0. See [LICENSE](LICENSE) for details.

We do request that you link back to this repository if you publish models abliterated with Lucid. This is not required by the license, but it would be greatly appreciated.

## Acknowledgements

While Lucid is an independent toolkit, it builds on directional refusal ablation by Arditi et al. (2024). We would also like to acknowledge [Heretic](https://github.com/p-e-w/heretic) for pioneering Optuna + KL-divergence optimization for automated abliteration.

We thank [Hugging Face](https://huggingface.co/) for sponsoring the compute used to develop Lucid and release public models.
