How It Works

Residual capture, multi-mode subspaces, KL-guarded search, and error-diffusion baking.

1. Residual-stream capture

Before deciding how to respond, a model computes its refusal signal and writes it into the residual stream. Lucid runs contrastive prompt pairs (harmful vs. harmless, or any custom positive vs. negative pair) in both chat and think modes, capturing the last-token residual vector entering each layer's FFN (ffn_inp / hc_ffn_pre).

Running capture on a quantized model (such as Q6_K or Q8_0) works well because class-mean differences are robust to quantization noise.

2. Biprojected directions and multi-mode subspaces

For each layer ℓ\ell and prompt family ff (such as chat, think, or cn), Lucid computes the difference of class means:

df,ℓ=μbad−μgoodd_{f,\ell} = \mu_{\text{bad}} - \mu_{\text{good}}

It then removes the component along the unit harmless mean μ^good\hat{\mu}_{\text{good}} (biprojection) before normalizing:

df,ℓ←df,ℓ−(df,ℓ⋅μ^good) μ^goodd_{f,\ell} \leftarrow d_{f,\ell} - (d_{f,\ell} \cdot \hat{\mu}_{\text{good}})\,\hat{\mu}_{\text{good}}

Leaving the harmless-mean component intact would cause the edit to attack normal text activations and degrade fluency.

On reasoning models, the chat-mode and think-mode directions frequently diverge and peak in different layers. Rather than picking a single vector, Lucid stacks the per-layer unit directions across active layers and families and takes the top-KK right singular vectors via SVD to form an orthonormal basis Q∈Rdmodel×KQ \in \mathbb{R}^{d_{\text{model}} \times K}. When basis_spec = "auto", Lucid picks the active layer band, stride, and rank KK from the separation table and singular-value spectrum automatically.

3. Weight projection and sandwich norms

For every residual-stream writer weight W∈Rdmodel×dinW \in \mathbb{R}^{d_{\text{model}} \times d_{\text{in}}} (attention output projections, dense or shared FFN down-projections, routed experts, or latent routed-expert projections), the edit projects out the subspace QQ scaled by strength ss:

W′=W−s QQ⊤WW' = W - s\,Q Q^\top W

At s=1s = 1, W′W' can no longer write into QQ. Values s>1s > 1 slightly push against QQ to compensate for unedited writers, while s<1s < 1 partially dampens the signal.

Some architectures place a post-norm with learned channel gains gg between a writer and the residual add (sandwich norms). In that case, projecting QQ directly out of WW leaves a residual component after multiplying by gg. Lucid detects post-norms automatically and computes a gain-corrected orthonormal basis per tensor:

P=orth⁡(g⊙Q),W′=W−s PP⊤WP = \operatorname{orth}(g \odot Q), \qquad W' = W - s\,P P^\top W

Because g⊙Qg \odot Q lies in span⁡(P)\operatorname{span}(P), the post-norm output g⊙(W′x)g \odot (W' x) has zero component along QQ (up to the scalar RMS rescale).

4. Zero-rewrite search with a KL guardrail

Rather than optimizing a separate scalar for every layer, Lucid parameterizes strength across depth with a smooth kernel per component (by default, a 4-parameter clamped triangular kernel: max, peak, width, min_frac). A full model has only ~9 to 13 search parameters regardless of depth.

During search, no weights are rewritten:

  • With llama.cpp, Lucid generates rank-1 LoRAs (B=−qjB = -q_j, A=qj⊤WA = q_j^\top W) per (candidate, layer, component), loads them once with --lora-init-without-apply, and evaluates each trial by passing per-request lora: [{id, scale}] scales.
  • With PyTorch / Hugging Face models, Lucid attaches forward hooks to writer modules that apply y↦y−s (yP)P⊤y \mapsto y - s\,(y P) P^\top in output space on the fly.

Multi-objective Optuna search (TPE or NSGA-II) minimizes two objectives jointly:

  1. Behavioral flag rate across chat, think, and any secondary arms.
  2. First-token KL divergence DKL(pbase ∥ pedited)D_{\mathrm{KL}}(p_{\text{base}} \,\|\, p_{\text{edited}}) on harmless prompts.

Lucid also calibrates the batched-inference KL noise floor by evaluating the base model twice, then verifies the chosen Pareto knee at full generation length while tracking unfinished reasoning traces and 6-gram repetition rate.

5. Row-wise error-diffusion baking

In quantized formats (FP8, NVFP4, MXFP8, and GGUF k-quants), the change W′−WW' - W is often smaller than half a quantization step. Plain round-to-nearest snaps most weights back to their original codes and restores 60% or more of the removed direction.

Lucid fixes this with deterministic row-wise error diffusion along the residual dimension dmodeld_{\text{model}}. For each row ii, each weight chooses between its lower and upper representable grid neighbors to minimize:

d2+λ ∥e+qid∥22d^2 + \lambda \,\|e + q_i d\|_2^2

where ee is the running accumulated rounding error projected onto QQ. This keeps quantization error nearly orthogonal to QQ, driving leftover subspace error down to ~0.1-1.4% even in 4-bit formats while keeping overall weight error tiny.

Edit this page on GitHub