Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,52 @@ def test_native_inference_observers_do_not_treat_nonfinite_values_as_edits(inval
bridge.hook_scale.remove_hooks()
bridge.hook_normalized.remove_hooks()
torch.testing.assert_close(actual, expected, rtol=0, atol=0, equal_nan=True)


def test_native_path_unhooked_returns_hf_output(monkeypatch):
"""With nothing hooked, the native path skips the intermediates and returns HF's output."""
import transformer_lens.model_bridge.generalized_components.normalization as norm

monkeypatch.setattr(norm, "_run_intermediate_hook", lambda *a: pytest.fail("computed"))
bridge = _make_bridge(native=True, rms=True)
x = torch.randn(2, 5, 16)
assert torch.equal(bridge(x), bridge.original_component(x))


@pytest.mark.parametrize("pre", [False, True])
def test_native_path_global_hook_is_not_bypassed_by_unhooked_fast_path(pre):
"""A global module hook editing hook_scale must change the output, as in AttentionBridge."""
bridge = _make_bridge(native=True)
x = torch.randn(2, 5, 16)
baseline = bridge(x)
point = bridge.hook_scale

def edit_before(module, args):
if module is point:
return (args[0] * 2.0,)

def edit_after(module, args, output):
if module is point:
return output * 2.0

module_hooks = torch.nn.modules.module
handle = (
module_hooks.register_module_forward_pre_hook(edit_before)
if pre
else module_hooks.register_module_forward_hook(edit_after)
)
try:
with pytest.warns(UserWarning, match="reconstructed from the hooked values"):
patched = bridge(x)
finally:
handle.remove()
assert not torch.allclose(baseline, patched)


def test_native_path_raw_torch_hook_on_hook_scale_fires():
"""A hook registered on hook_scale with plain torch still runs."""
bridge = _make_bridge(native=True)
seen = []
bridge.hook_scale.register_forward_hook(lambda module, args, out: seen.append(out))
bridge(torch.randn(2, 5, 16))
assert len(seen) == 1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, ContextManager, Dict, Optional, cast

import torch
from torch.nn.modules import module as module_hooks

from transformer_lens.hook_points import HookPoint
from transformer_lens.model_bridge.generalized_components.base import (
Expand Down Expand Up @@ -202,6 +203,16 @@ def _hf_autograd_forward_with_hooks(self, x: torch.Tensor) -> torch.Tensor:
if self.hook_scale.bwd_hooks or self.hook_normalized.bwd_hooks:
warnings.warn(NATIVE_PATH_BWD_FALLBACK_WARNING)
return self._python_norm_forward(x)
# Global module hooks keep the intermediates in view, as in AttentionBridge._fire_post_hoc_hook.
if not (
module_hooks._global_forward_hooks
or module_hooks._global_forward_pre_hooks
or any(
p._forward_hooks or p._forward_pre_hooks
for p in (self.hook_scale, self.hook_normalized)
)
):
return self.original_component(x).to(x.dtype)
has_fwd_hooks = bool(self.hook_scale.fwd_hooks or self.hook_normalized.fwd_hooks)
# No hooks: skip building a graph for observation-only intermediates. With hooks,
# keep grad so an edited value stays connected to the input.
Expand Down
Loading