"""Inference-Time Intervention (ITI) runtime: apply pre-built per-head steering during decoding.""" import os, torch import config from common import sycophancy_schedule ITI_H_PATH = os.path.join(config.VEC_DIR, "iti_H.pt") # hallucination heads ITI_S_PATH = os.path.join(config.VEC_DIR, "iti_S.pt") # sycophancy heads ITI_PATH = ITI_H_PATH def o_projs(model): from common import decoder_layers outs = [] for layer in decoder_layers(model): attn = getattr(layer, "self_attn", None) or getattr(layer, "attn", None) op = getattr(attn, "o_proj", None) if attn is not None else None outs.append(op) return outs def ehr_user(ehr, question): return f"[EHR note — answer only from this]\n{ehr}\n\n[Question] {question}" class ITISteerer: """Hooks each involved layer's o_proj input and shifts the selected heads on the newest generated token only (decode; prefill skipped), with a residual-norm cap.""" def __init__(self, model, bundle=ITI_PATH): self.ops = o_projs(model) d = torch.load(bundle, map_location="cpu", weights_only=True) if isinstance(bundle, str) else bundle self.n_heads, self.head_dim = d["n_heads"], d["head_dim"] dirs, sigmas, heads = d["directions"], d["sigmas"], d["heads"] self.by_layer = {} for k, hd in enumerate(heads): self.by_layer.setdefault(hd["layer"], []).append((hd["head"], dirs[k], float(sigmas[k]))) self.handles = [] self._step = -1 self._counter_layer = min(self.by_layer) if self.by_layer else None self._strength = 1.0 def on(self, alpha=None, strength=1.0): self.off() alpha = getattr(config, "ITI_ALPHA", 12.0) if alpha is None else alpha self._strength = float(strength) self._step = -1 on_tok = getattr(config, "SYCO_SCHED_ON", 24); ramp = getattr(config, "SYCO_SCHED_RAMP", 24) norm_cap = getattr(config, "ITI_NORM_CAP", 0) or 0 for li, items in self.by_layer.items(): op = self.ops[li] if op is None: continue def pre(mod, args, items=items, li=li, a=alpha, on_tok=on_tok, ramp=ramp, cap=norm_cap): x = args[0] if li == self._counter_layer: self._step = -1 if x.shape[1] > 1 else self._step + 1 if x.shape[1] > 1: # prefill -> never steer return None sched = sycophancy_schedule(self._step, on_tok, ramp) eff = a * self._strength * sched if eff == 0.0: return None b, seq, D = x.shape x = x.clone().view(b, seq, self.n_heads, self.head_dim) for head, dvec, sigma in items: u = dvec.to(x.dtype).to(x.device) shift_norm = eff * sigma if cap: hnorm = x[:, -1, head, :].norm(dim=-1, keepdim=True) scale = torch.clamp((cap * hnorm) / (abs(shift_norm) + 1e-6), max=1.0) x[:, -1, head, :] = x[:, -1, head, :] + scale.squeeze(-1).unsqueeze(-1) * eff * sigma * u else: x[:, -1, head, :] = x[:, -1, head, :] + eff * sigma * u return (x.view(b, seq, D),) + tuple(args[1:]) self.handles.append(op.register_forward_pre_hook(pre)) return self def off(self): for h in self.handles: h.remove() self.handles = [] self._step = -1 def continuous_strength(logit): import math T = getattr(config, "STRENGTH_TEMP", 1.5); neu = getattr(config, "STRENGTH_NEUTRAL", 0.5) clamp = getattr(config, "STRENGTH_CLAMP", 1.0) p = 1.0 / (1.0 + math.exp(-(logit / T))) return max(0.0, min(clamp, (p - neu) / max(1e-6, 1 - neu)))