Spaces:
Running
Running
Ethan S.
Claude Mythos 5.2
deploy: sync backend to fix/bench-prompt-format-confound @ e8b1544
bb0dcd3 Download refusal_bench/techniques/random_direction.py from lymnal/neuroscope-api: direct link, hf CLI and curl.
- Browser
- Download file 3.86 kB
-
https://huggingface.co/spaces/lymnal/neuroscope-api/resolve/main/refusal_bench/techniques/random_direction.py
- Command line
-
hf download hf://spaces/lymnal/neuroscope-api/refusal_bench/techniques/random_direction.py
-
curl -L -o random_direction.py https://huggingface.co/spaces/lymnal/neuroscope-api/resolve/main/refusal_bench/techniques/random_direction.py
3.86 kB
| """ | |
| Random-direction ablation — a control, not a technique. | |
| Every direction-based row in the bench reports "ablating d̂ changed refusal by | |
| X and probe AUC by Y". Neither number means much alone: projecting ANY unit | |
| vector out of a d_model-dimensional residual stream perturbs the model a | |
| little, and a probe evaluated on perturbed residuals moves a little. The | |
| question a reader actually has is whether the published direction does more | |
| than an arbitrary one of the same shape. | |
| This control answers that with the cheapest honest comparison: the same | |
| projection-removal hook (research.make_ablation_hook), at the same layer, on | |
| a unit vector drawn from an isotropic Gaussian. In high dimension such a | |
| vector is nearly orthogonal to any fixed direction (|cos| ~ 1/sqrt(d_model), | |
| about 0.02 at d_model = 2048), so it removes almost none of the refusal | |
| direction and almost none of the probe's axis. | |
| How to read its row: | |
| - Δ refusal near 0 and Δ AUC near 0 → the bench's deltas for the real | |
| techniques are attributable to the directions they chose. | |
| - A control that moves either metric as much as a technique does → that | |
| technique's delta is within what an arbitrary ablation produces, and | |
| should not be read as evidence about refusal. | |
| Limits, stated plainly: this is ONE seeded draw, not a null distribution, so | |
| it is indicative rather than a significance test; and it is a control for the | |
| residual-stream projection techniques only — it says nothing about Herring's | |
| neuron-level intervention, which acts at a different hook point. | |
| It is registered in CONTROLS, not TECHNIQUES, and its result row carries | |
| `is_control=True`, so it is never counted among the published techniques. | |
| """ | |
| from typing import Callable, List, Tuple | |
| import torch | |
| from research import make_ablation_hook | |
| from ..technique import Technique | |
| class RandomDirection(Technique): | |
| """Projection ablation of a seeded random unit vector at the bench layer.""" | |
| name = "Random direction (control)" | |
| paper_url = "" | |
| def __init__(self, seed: int = 0) -> None: | |
| super().__init__() | |
| self._seed: int = int(seed) | |
| self._unit_direction: torch.Tensor | None = None | |
| def set_seed(self, seed: int) -> None: | |
| """Called by the runner with the bench seed before fit().""" | |
| self._seed = int(seed) | |
| def fit( | |
| self, | |
| model, | |
| harmful_prompts: List[str], | |
| harmless_prompts: List[str], | |
| layer: int, | |
| ) -> None: | |
| """ | |
| Draw the direction. The prompts are unused — that is the point: the | |
| control must not be able to learn anything about refusal. | |
| """ | |
| d_model = int(model.cfg.d_model) | |
| if d_model <= 0: | |
| raise RuntimeError(f"RandomDirection: model.cfg.d_model is {d_model}") | |
| # An explicit CPU generator, so the draw depends only on (seed, | |
| # d_model) and never on the global RNG state that generation reseeds. | |
| generator = torch.Generator(device="cpu").manual_seed(self._seed) | |
| direction = torch.randn(d_model, generator=generator, dtype=torch.float32) | |
| unit = direction / direction.norm() | |
| # Match the model's device and dtype (bfloat16 / float16 on the local | |
| # bench path) so the hook's arithmetic does not upcast the residual. | |
| param = next(model.parameters()) | |
| self._unit_direction = unit.to(device=param.device, dtype=param.dtype) | |
| self._layer = layer | |
| self._fitted = True | |
| def make_ablation_hook(self) -> Tuple[str, Callable]: | |
| if not self._fitted or self._unit_direction is None or self._layer is None: | |
| raise RuntimeError("RandomDirection.make_ablation_hook called before fit()") | |
| return ( | |
| f"blocks.{self._layer}.hook_resid_post", | |
| make_ablation_hook(self._unit_direction), | |
| ) | |