Extending Lucid
Custom extractors, depth kernels, judges, and steering.
Every major stage in Lucid is backed by a typed Registry:
| Registry | Import | Purpose |
|---|---|---|
PRESETS | lucid.presets.PRESETS | Architecture presets (writer patterns, post-norm gains, default prompt modes) |
CAPTURE_BACKENDS | lucid.capture.CAPTURE_BACKENDS | Activation capture engines (llama_cpp, hf_torch, custom) |
EXTRACTORS | lucid.analysis.EXTRACTORS | Per-layer direction extractors (diff_means, trimmed_mean, whitened, pca) |
JUDGES | lucid.eval.JUDGES | Behavioral evaluators (marker, reframe, regex, composite, llm, function) |
KERNELS | lucid.search.KERNELS | Per-layer ablation/steering depth kernels (triangular, trapezoid, gaussian, cosine, constant) |
Custom Direction Extractor
import numpy as np
from lucid import EXTRACTORS
@EXTRACTORS.register("median_diff")
def extract_median_diff(pos, neg, biproject=True, opts=None):
mb = np.median(pos.astype(np.float64), axis=0)
mg = np.median(neg.astype(np.float64), axis=0)
d = mb - mg
if biproject:
gu = mg / np.maximum(np.linalg.norm(mg, axis=-1, keepdims=True), 1e-12)
d = d - (d * gu).sum(-1, keepdims=True) * gu
d = d / np.maximum(np.linalg.norm(d, axis=-1, keepdims=True), 1e-12)
return d, mb, mgCustom Depth Kernel
from lucid import KERNELS
@KERNELS.register("sigmoid_ramp")
def sigmoid_ramp(il: int, max_w: float, peak: float, width: float, min_frac: float) -> float:
import math
z = (il - peak) / max(width * 0.25, 1e-6)
s = 1.0 / (1.0 + math.exp(-z))
return float(max_w * (min_frac + (1.0 - min_frac) * s))Custom Behavioral Judge (Non-Refusal Realignment)
from lucid import JUDGES, Judge, JudgeResult
@JUDGES.register("verbosity")
class VerbosityJudge(Judge):
def __init__(self, max_words: int = 120):
self.max_words = max_words
def judge(self, text: str, prompt: str | None = None) -> JudgeResult:
words = len(text.split())
flagged = words > self.max_words
return JudgeResult(flagged=flagged, score=float(flagged), matched=f"words:{words}" if flagged else None)Steering / Amplification (Negative Scales)
Setting [search] allow_negative_scales = true opens the search bounds to negative strength values s < 0, turning W' = W - s Q Q^T W into subspace amplification (W + |s| Q Q^T W) while retaining the exact same LoRA search, KL guardrail, and error-diffusion baking pipeline.