interlens.interp.steering¶
interlens.interp.steering
¶
SteeringSpec
dataclass
¶
A residual-stream intervention applied during generation via forward hooks on decoder layers.
mode='add' adds coef * direction to the residual at layers; mode='ablate' projects the
direction component out of the residual (directional ablation). The same mechanism covers both because
ablation is just the projection-removal variant of an additive hook.
A summary (mode, layers, coef, direction norm) is recorded into Message.metadata['steering'] by the
participant so a steered/ablated turn is reproducible.
difference_of_means
classmethod
¶
difference_of_means(
pos_acts,
neg_acts,
layers,
coef: float = 1.0,
mode: Mode = "add",
) -> "SteeringSpec"
Build a spec whose direction is the unit difference-of-means of two activation populations,
normalize(mean(pos) − mean(neg)) — the classic contrastive/concept-direction recipe, pointing TOWARD
the positive class. pos_acts/neg_acts are [n, d_model] (a stack of per-example residuals) or a
pre-pooled [d_model] vector; anything torch.as_tensor accepts works. Steer toward the positive
concept with coef>0 and away from it (suppress) with coef<0. layers is an int or an
iterable of decoder-layer indices. This lives here (not re-hand-rolled per experiment) per the
contribution convention.
Source code in src/interlens/interp/steering.py
register
¶
Register the steering hooks on model and return the handles (caller removes them after generate).