Extending Lucid

Custom extractors, depth kernels, judges, and steering.

Every major stage in Lucid is backed by a typed Registry:

RegistryImportPurpose
PRESETSlucid.presets.PRESETSArchitecture presets (writer patterns, post-norm gains, default prompt modes)
CAPTURE_BACKENDSlucid.capture.CAPTURE_BACKENDSActivation capture engines (llama_cpp, hf_torch, custom)
EXTRACTORSlucid.analysis.EXTRACTORSPer-layer direction extractors (diff_means, trimmed_mean, whitened, pca)
JUDGESlucid.eval.JUDGESBehavioral evaluators (marker, reframe, regex, composite, llm, function)
KERNELSlucid.search.KERNELSPer-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, mg

Custom 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.

Edit this page on GitHub