diff --git a/tests/unit/model_bridge/generalized_components/test_ssm_conv_hooks.py b/tests/unit/model_bridge/generalized_components/test_ssm_conv_hooks.py new file mode 100644 index 000000000..c00dfb3dc --- /dev/null +++ b/tests/unit/model_bridge/generalized_components/test_ssm_conv_hooks.py @@ -0,0 +1,372 @@ +"""Conv hooks of the Mamba-family mixers fire on transformers >= 5.15. + +transformers 5.15 rewrote these mixers to pass ``self.conv1d.weight`` to a +module-level ``causal_conv1d_fn`` instead of calling ``self.conv1d(...)``, so the +``DepthwiseConv1DBridge`` swapped in for the conv no longer ran. The mixer bridges +now route that call through the conv bridge (``route_causal_conv1d``). + +The first group of tests uses a stand-in mixer shaped like transformers >= 5.15, +so it checks the routing on any installed transformers. The second group wires +the real HF mixers: on transformers < 5.15 they call the conv module themselves, +on >= 5.15 the routing does; either way the hooks fire with the same shapes and +the output stays bit-exact with HF's (torch path; no causal-conv1d kernel in CI), +and calls with packed-sequence arguments behave exactly as HF's. +""" + +from __future__ import annotations + +import copy +import sys +import threading + +import pytest +import torch +import torch.nn.functional as F + +from tests.unit.model_bridge.supported_architectures.helpers import make_bridge_cfg +from transformer_lens.factories.architecture_adapter_factory import ( + ArchitectureAdapterFactory, +) +from transformer_lens.model_bridge.component_setup import setup_submodules +from transformer_lens.model_bridge.generalized_components.depthwise_conv1d import ( + DepthwiseConv1DBridge, + route_causal_conv1d, +) + + +def causal_conv1d_fn(hidden_states, weight, bias=None, activation=None, **kwargs): + """Copy of the torch path of transformers' ``causal_conv1d_fn``, which refuses packed sequences.""" + if kwargs.get("seq_idx") is not None or kwargs.get("cu_seqlens") is not None: + raise ValueError("can't keep packed sequences apart without the kernel") + seq_len = hidden_states.shape[-1] + out = F.conv1d( + hidden_states.to(weight.dtype), + weight=weight.unsqueeze(1), + bias=bias, + padding=weight.shape[-1] - 1, + groups=hidden_states.shape[1], + )[:, :, :seq_len] + if activation is not None: + out = F.silu(out) + return out.to(hidden_states.dtype) + + +class _KernelStyleMixer(torch.nn.Module): + """Calls the module-level conv function with its conv's weight, as transformers >= 5.15.""" + + def __init__(self, channels: int = 6, kernel: int = 4) -> None: + super().__init__() + self.conv1d = torch.nn.Conv1d( + channels, channels, kernel, groups=channels, padding=kernel - 1 + ) + self.other = torch.nn.Conv1d( + channels, channels, kernel, groups=channels, padding=kernel - 1 + ) + + def forward(self, x: torch.Tensor, use_other: bool = False, **kwargs) -> torch.Tensor: + conv = self.other if use_other else self.conv1d + return causal_conv1d_fn(x, conv.weight.squeeze(1), conv.bias, activation="silu", **kwargs) + + +def _bridged_kernel_style_mixer(): + torch.manual_seed(0) + mixer = _KernelStyleMixer() + reference = copy.deepcopy(mixer) + conv = DepthwiseConv1DBridge(name="conv1d") + conv.set_original_component(mixer.conv1d) + mixer.conv1d = conv + return mixer, reference, conv + + +def _record(store: dict, name: str): + def hook(tensor, hook): + store[name] = tensor.detach().clone() + return tensor + + return hook + + +def test_route_runs_the_conv_bridge_and_matches_the_function() -> None: + mixer, reference, conv = _bridged_kernel_style_mixer() + seen: dict = {} + conv.hook_in.add_hook(_record(seen, "in")) + conv.hook_out.add_hook(_record(seen, "out")) + x = torch.randn(2, 6, 5) + with torch.no_grad(), route_causal_conv1d(mixer, "conv1d"): + out = mixer(x) + torch.testing.assert_close(out, reference(x), rtol=0.0, atol=0.0) + assert seen["in"].shape == (2, 6, 5) + assert seen["out"].shape == (2, 6, 5 + 4 - 1) + + +def test_route_patch_on_conv_hook_out_reaches_the_output() -> None: + mixer, reference, conv = _bridged_kernel_style_mixer() + conv.hook_out.add_hook(lambda tensor, hook: torch.zeros_like(tensor)) + x = torch.randn(2, 6, 5) + with torch.no_grad(), route_causal_conv1d(mixer, "conv1d"): + out = mixer(x) + torch.testing.assert_close(out, torch.zeros_like(out), rtol=0.0, atol=0.0) + assert not torch.equal(out, reference(x)) + + +def test_route_leaves_other_convs_and_the_module_function_alone() -> None: + mixer, reference, conv = _bridged_kernel_style_mixer() + seen: dict = {} + conv.hook_in.add_hook(_record(seen, "in")) + x = torch.randn(2, 6, 5) + function_before = sys.modules[__name__].causal_conv1d_fn + with torch.no_grad(), route_causal_conv1d(mixer, "conv1d"): + out = mixer(x, use_other=True) + torch.testing.assert_close(out, reference(x, use_other=True), rtol=0.0, atol=0.0) + assert seen == {} + assert sys.modules[__name__].causal_conv1d_fn is function_before + + +def test_route_restores_the_module_function_after_an_error() -> None: + mixer, _, conv = _bridged_kernel_style_mixer() + + def fail(tensor, hook): + raise RuntimeError("hook failed") + + conv.hook_in.add_hook(fail) + function_before = sys.modules[__name__].causal_conv1d_fn + with pytest.raises(RuntimeError, match="hook failed"): + with torch.no_grad(), route_causal_conv1d(mixer, "conv1d"): + mixer(torch.randn(2, 6, 5)) + assert sys.modules[__name__].causal_conv1d_fn is function_before + + +def test_route_is_a_no_op_without_a_conv_bridge() -> None: + torch.manual_seed(0) + mixer = _KernelStyleMixer() + x = torch.randn(2, 6, 5) + function_before = sys.modules[__name__].causal_conv1d_fn + with torch.no_grad(): + expected = mixer(x) + with route_causal_conv1d(mixer, "conv1d"): + assert sys.modules[__name__].causal_conv1d_fn is function_before + torch.testing.assert_close(mixer(x), expected, rtol=0.0, atol=0.0) + + +@pytest.mark.parametrize("name", ["seq_idx", "cu_seqlens"]) +def test_route_leaves_packed_sequences_to_the_function(name) -> None: + mixer, _, conv = _bridged_kernel_style_mixer() + seen: dict = {} + conv.hook_in.add_hook(_record(seen, "in")) + packed = {name: torch.zeros(2, 5, dtype=torch.int32)} + with torch.no_grad(), route_causal_conv1d(mixer, "conv1d"): + with pytest.raises(ValueError, match="packed sequences"): + mixer(torch.randn(2, 6, 5), **packed) + assert seen == {} + + +def test_route_keeps_concurrent_forwards_apart() -> None: + """Thread B's route outlives thread A's: B's conv hook still fires and the function is put back.""" + mixer_a, reference, conv_a = _bridged_kernel_style_mixer() + mixer_b, _, conv_b = _bridged_kernel_style_mixer() + seen: dict = {} + conv_a.hook_in.add_hook(_record(seen, "a")) + conv_b.hook_in.add_hook(_record(seen, "b")) + x = torch.randn(2, 6, 5) + function_before = sys.modules[__name__].causal_conv1d_fn + a_entered, b_entered, a_left = threading.Event(), threading.Event(), threading.Event() + outputs: dict = {} + errors: list = [] + + def run_a() -> None: + try: + with torch.no_grad(), route_causal_conv1d(mixer_a, "conv1d"): + a_entered.set() + assert b_entered.wait(10) + outputs["a"] = mixer_a(x) + except BaseException as exc: # surfaced in the main thread below + errors.append(exc) + finally: + a_left.set() + + def run_b() -> None: + try: + assert a_entered.wait(10) + with torch.no_grad(), route_causal_conv1d(mixer_b, "conv1d"): + b_entered.set() + assert a_left.wait(10) + outputs["b"] = mixer_b(x) + except BaseException as exc: # surfaced in the main thread below + errors.append(exc) + finally: + b_entered.set() + + threads = [threading.Thread(target=run_a), threading.Thread(target=run_b)] + for thread in threads: + thread.start() + for thread in threads: + thread.join(30) + assert not errors, errors + assert set(seen) == {"a", "b"} + with torch.no_grad(): + expected = reference(x) + torch.testing.assert_close(outputs["a"], expected, rtol=0.0, atol=0.0) + torch.testing.assert_close(outputs["b"], expected, rtol=0.0, atol=0.0) + assert sys.modules[__name__].causal_conv1d_fn is function_before + + +# --- Real HF mixers, wired as the adapters wire them -------------------------------- + + +def _wire(arch: str, key: str, hf_module): + adapter = ArchitectureAdapterFactory.select_architecture_adapter( + make_bridge_cfg(arch, d_model=32, n_heads=4, d_head=8) + ) + bridge = copy.deepcopy(adapter.component_mapping["blocks"].submodules[key]) + bridge.set_original_component(hf_module) + setup_submodules(bridge, adapter, hf_module) + return bridge + + +def _mamba(): + from transformers import MambaConfig + from transformers.models.mamba.modeling_mamba import MambaMixer + + cfg = MambaConfig( + hidden_size=32, state_size=8, expand=2, conv_kernel=4, num_hidden_layers=2, vocab_size=64 + ) + return MambaMixer(cfg, layer_idx=0) + + +def _mamba2(): + from transformers import Mamba2Config + from transformers.models.mamba2.modeling_mamba2 import Mamba2Mixer + + cfg = Mamba2Config( + hidden_size=32, + num_heads=8, + head_dim=8, + expand=2, + n_groups=1, + state_size=8, + conv_kernel=4, + chunk_size=8, + num_hidden_layers=2, + vocab_size=64, + ) + return Mamba2Mixer(cfg, layer_idx=0) + + +def _nemotron_h(): + from transformers.models.nemotron_h.configuration_nemotron_h import NemotronHConfig + from transformers.models.nemotron_h.modeling_nemotron_h import NemotronHMamba2Mixer + + cfg = NemotronHConfig( + hidden_size=32, + mamba_num_heads=4, + mamba_head_dim=8, + ssm_state_size=8, + conv_kernel=4, + n_groups=1, + num_hidden_layers=2, + vocab_size=64, + intermediate_size=64, + expand=2, + hybrid_override_pattern="MM", + ) + return NemotronHMamba2Mixer(cfg, layer_idx=0) + + +def _lfm2(): + from transformers import Lfm2Config + from transformers.models.lfm2.modeling_lfm2 import Lfm2ShortConv + + cfg = Lfm2Config( + hidden_size=32, + intermediate_size=64, + num_hidden_layers=2, + num_attention_heads=4, + num_key_value_heads=2, + vocab_size=64, + conv_L_cache=3, + layer_types=["conv", "full_attention"], + ) + return Lfm2ShortConv(cfg, layer_idx=0) + + +HF_MIXERS = [ + pytest.param("MambaForCausalLM", "mixer", "conv1d", _mamba, id="mamba"), + pytest.param("Mamba2ForCausalLM", "mixer", "conv1d", _mamba2, id="mamba2"), + pytest.param("NemotronHForCausalLM", "mixer", "conv1d", _nemotron_h, id="nemotron_h"), + pytest.param("Lfm2ForCausalLM", "conv", "conv", _lfm2, id="lfm2"), +] + + +def _first(output): + return output[0] if isinstance(output, tuple) else output + + +@pytest.mark.parametrize("arch,key,conv_attr,build", HF_MIXERS) +def test_hf_mixer_conv_hooks_fire_and_output_is_bit_exact(arch, key, conv_attr, build) -> None: + torch.manual_seed(0) + hf_module = build().eval() + reference = copy.deepcopy(hf_module) + bridge = _wire(arch, key, hf_module) + conv = getattr(bridge, conv_attr) + module = sys.modules[type(hf_module).__module__] + function_before = getattr(module, "causal_conv1d_fn", None) + seen: dict = {} + conv.hook_in.add_hook(_record(seen, "in")) + conv.hook_out.add_hook(_record(seen, "out")) + + x = torch.randn(2, 7, 32) + with torch.no_grad(): + out = _first(bridge(x)) + expected = _first(reference(x)) + + torch.testing.assert_close(out, expected, rtol=0.0, atol=0.0) + channels = conv.original_component.in_channels + kernel = conv.original_component.kernel_size[0] + assert seen["in"].shape == (2, channels, 7) + assert seen["out"].shape == (2, channels, 7 + kernel - 1) + assert getattr(module, "causal_conv1d_fn", None) is function_before + + +@pytest.mark.parametrize("arch,key,conv_attr,build", HF_MIXERS) +def test_hf_mixer_patch_on_conv_hook_out_reaches_the_output(arch, key, conv_attr, build) -> None: + torch.manual_seed(0) + hf_module = build().eval() + reference = copy.deepcopy(hf_module) + bridge = _wire(arch, key, hf_module) + getattr(bridge, conv_attr).hook_out.add_hook(lambda tensor, hook: torch.zeros_like(tensor)) + + x = torch.randn(2, 7, 32) + with torch.no_grad(): + assert not torch.allclose(_first(bridge(x)), _first(reference(x))) + + +def _outcome(call): + """The call's first output, or the type and message of what it raised.""" + try: + return _first(call()) + except Exception as exc: + return type(exc), str(exc) + + +PACKED_SEQUENCES = { + "seq_idx": torch.zeros(2, 7, dtype=torch.int32), + "cu_seqlens": torch.tensor([0, 7, 14], dtype=torch.int32), +} + + +@pytest.mark.parametrize("name", list(PACKED_SEQUENCES)) +@pytest.mark.parametrize("arch,key,conv_attr,build", HF_MIXERS) +def test_hf_mixer_packed_sequences_behave_as_hf(arch, key, conv_attr, build, name) -> None: + torch.manual_seed(0) + hf_module = build().eval() + reference = copy.deepcopy(hf_module) + bridge = _wire(arch, key, hf_module) + x = torch.randn(2, 7, 32) + packed = {name: PACKED_SEQUENCES[name]} + with torch.no_grad(): + expected = _outcome(lambda: reference(x, **packed)) + got = _outcome(lambda: bridge(x, **packed)) + if isinstance(expected, torch.Tensor): + torch.testing.assert_close(got, expected, rtol=0.0, atol=0.0) + else: + assert got == expected diff --git a/transformer_lens/model_bridge/generalized_components/depthwise_conv1d.py b/transformer_lens/model_bridge/generalized_components/depthwise_conv1d.py index b8abd07e3..11cf81c4f 100644 --- a/transformer_lens/model_bridge/generalized_components/depthwise_conv1d.py +++ b/transformer_lens/model_bridge/generalized_components/depthwise_conv1d.py @@ -1,5 +1,8 @@ """Bridge for Mamba-style depthwise causal Conv1d (distinct from GPT-2's Conv1D linear).""" -from typing import Any, Dict, Optional, cast +import contextlib +import inspect +import threading +from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, cast import torch @@ -7,6 +10,10 @@ GeneralizedComponent, ) +_CAUSAL_CONV1D_FN = "causal_conv1d_fn" +# Packed-sequence arguments: HF's torch path refuses them and the kernel honours them. +_PACKED_SEQUENCE_KWARGS = ("seq_idx", "cu_seqlens") + class DepthwiseConv1DBridge(GeneralizedComponent): """Wraps an ``nn.Conv1d`` depthwise causal convolution with input/output hooks. @@ -19,6 +26,11 @@ class DepthwiseConv1DBridge(GeneralizedComponent): values index the causal padding rather than real tokens — slice positive positions from the left, or trim the last ``conv_kernel - 1`` entries first. + From transformers 5.15 the mixers no longer call ``self.conv1d(...)``: they + pass ``self.conv1d.weight`` to a module-level ``causal_conv1d_fn``. The mixer + bridges run that call through this bridge (see ``route_causal_conv1d``), so + the hooks fire as before. + Decode-step limitation: on stateful generation, HF's Mamba/Mamba-2 mixers bypass ``self.conv1d(...)`` and read ``self.conv1d.weight`` directly, so the forward hook never fires on decode steps — only on prefill. For per-step @@ -79,3 +91,147 @@ def __repr__(self) -> str: f"original_component={type(self.original_component).__name__})" ) return f"DepthwiseConv1DBridge(name={self.name}, original_component=None)" + + +def _is_view_of(tensor: Any, param: torch.Tensor) -> bool: + """Whether ``tensor`` is ``param`` or a view of it (HF passes ``weight.squeeze(1)``).""" + return tensor is param or getattr(tensor, "_base", None) is param + + +class _ConvRoute: + """The conv bridge one ``route_causal_conv1d`` context runs HF's calls through.""" + + def __init__( + self, namespace: Dict[str, Any], conv: DepthwiseConv1DBridge, conv_module: torch.nn.Conv1d + ) -> None: + self.namespace = namespace + self.conv = conv + self.conv_module = conv_module + + def matches(self, args: Tuple[Any, ...], kwargs: Dict[str, Any]) -> bool: + weight = args[1] if len(args) > 1 else kwargs.get("weight") + bias = args[2] if len(args) > 2 else kwargs.get("bias") + return ( + len(args) >= 2 + and isinstance(args[0], torch.Tensor) + and _is_view_of(weight, self.conv_module.weight) + and bias is self.conv_module.bias + and all(kwargs.get(name) is None for name in _PACKED_SEQUENCE_KWARGS) + ) + + def run(self, args: Tuple[Any, ...], kwargs: Dict[str, Any]) -> torch.Tensor: + hidden_states = args[0] + activation = args[3] if len(args) > 3 else kwargs.get("activation") + seq_len = hidden_states.shape[-1] + out = self.conv(hidden_states.to(self.conv_module.weight.dtype))[..., :seq_len] + if activation is not None: + from transformers.activations import ACT2FN + + out = ACT2FN[activation](out) + return out.to(hidden_states.dtype) + + +class _InstalledDispatch: + """The stand-in for one modeling file's ``causal_conv1d_fn`` while any route uses it.""" + + def __init__(self, namespace: Dict[str, Any], original: Callable[..., Any]) -> None: + self.original = original + self.users = 0 + + def dispatch(*args: Any, **kwargs: Any) -> Any: + # Only the calling thread's routes apply, so concurrent forwards stay apart. + for route in reversed(_thread_routes()): + if route.namespace is namespace and route.matches(args, kwargs): + return route.run(args, kwargs) + return original(*args, **kwargs) + + self.function = dispatch + + +_ROUTING_LOCK = threading.Lock() +_INSTALLED: Dict[int, _InstalledDispatch] = {} # keyed by id() of the modeling file's globals +_THREAD_STATE = threading.local() + + +def _thread_routes() -> List[_ConvRoute]: + routes = getattr(_THREAD_STATE, "routes", None) + if routes is None: + routes = [] + _THREAD_STATE.routes = routes + return routes + + +def _install_dispatch(namespace: Dict[str, Any]) -> Optional[_InstalledDispatch]: + with _ROUTING_LOCK: + installed = _INSTALLED.get(id(namespace)) + if installed is None: + original = namespace.get(_CAUSAL_CONV1D_FN) + if not callable(original): + return None + installed = _InstalledDispatch(namespace, original) + namespace[_CAUSAL_CONV1D_FN] = installed.function + _INSTALLED[id(namespace)] = installed + installed.users += 1 + return installed + + +def _release_dispatch(namespace: Dict[str, Any], installed: _InstalledDispatch) -> None: + with _ROUTING_LOCK: + installed.users -= 1 + if installed.users == 0: + del _INSTALLED[id(namespace)] + # Leave alone a function that someone else has put there since. + if namespace.get(_CAUSAL_CONV1D_FN) is installed.function: + namespace[_CAUSAL_CONV1D_FN] = installed.original + + +@contextlib.contextmanager +def route_causal_conv1d(hf_module: Any, conv_attr: str) -> Iterator[None]: + """Run HF's ``causal_conv1d_fn`` through the conv bridge while ``hf_module`` runs. + + From transformers 5.15 the Mamba-family mixers (Mamba, Mamba-2 and the hybrids + built on them, LFM2's short conv) no longer call their conv module: the forward + passes ``self..weight`` and ``.bias`` to a module-level + ``causal_conv1d_fn`` of the modeling file, so the ``DepthwiseConv1DBridge`` + swapped in for the conv never runs and its hooks never fire. Inside this + context, a call of that function on this thread with this conv's own weight and + bias runs the bridge instead, trims the output to the input length and applies + the activation, which is what the function's torch path computes. The bridge + then runs the torch convolution even where a causal-conv1d kernel is installed. + + Every other call goes to the original function: calls from other threads, calls + that carry ``seq_idx`` or ``cu_seqlens`` (packed sequences, which the torch path + refuses and the kernel keeps apart), and the decode-step ``causal_conv1d_update``. + The module-level function is replaced while any thread is inside such a context + and put back when the last one leaves. Nothing changes when the conv is not a + bridge, when the modeling file has no such function, or on transformers < 5.15, + whose mixers call the conv module directly (their ``causal_conv1d_fn`` is None + without the kernel package, and the kernel path calls it with keyword arguments). + """ + conv = getattr(hf_module, conv_attr, None) + forward = getattr(type(hf_module), "forward", None) + namespace = getattr(inspect.unwrap(forward), "__globals__", None) if forward else None + conv_module = getattr(conv, "original_component", None) + if ( + namespace is None + or not isinstance(conv, DepthwiseConv1DBridge) + or not isinstance(conv_module, torch.nn.Conv1d) + # The bridge reproduces the function only for a causal depthwise conv. + or conv_module.padding != (conv_module.kernel_size[0] - 1,) + or conv_module.groups != conv_module.in_channels + ): + yield + return + installed = _install_dispatch(namespace) + if installed is None: + yield + return + + route = _ConvRoute(namespace, conv, conv_module) + routes = _thread_routes() + routes.append(route) + try: + yield + finally: + routes.remove(route) + _release_dispatch(namespace, installed) diff --git a/transformer_lens/model_bridge/generalized_components/lfm2_gated_short_conv.py b/transformer_lens/model_bridge/generalized_components/lfm2_gated_short_conv.py index b5b10c48b..89984d915 100644 --- a/transformer_lens/model_bridge/generalized_components/lfm2_gated_short_conv.py +++ b/transformer_lens/model_bridge/generalized_components/lfm2_gated_short_conv.py @@ -1,8 +1,12 @@ """LiquidAI LFM2 gated short-convolution mixer bridge.""" +from typing import Any from transformer_lens.model_bridge.generalized_components.base import ( GeneralizedComponent, ) +from transformer_lens.model_bridge.generalized_components.depthwise_conv1d import ( + route_causal_conv1d, +) class Lfm2ShortConvBridge(GeneralizedComponent): @@ -27,3 +31,9 @@ class Lfm2ShortConvBridge(GeneralizedComponent): "hook_conv": "conv.hook_out", "hook_gated": "out.hook_in", } + + def forward(self, *args: Any, **kwargs: Any) -> Any: + # transformers >= 5.15 calls a module-level causal_conv1d_fn with conv's + # weight; route it through the conv bridge so its hooks fire. + with route_causal_conv1d(self.original_component, "conv"): + return super().forward(*args, **kwargs) diff --git a/transformer_lens/model_bridge/generalized_components/ssm2_mixer.py b/transformer_lens/model_bridge/generalized_components/ssm2_mixer.py index 3f93f46bd..8239052af 100644 --- a/transformer_lens/model_bridge/generalized_components/ssm2_mixer.py +++ b/transformer_lens/model_bridge/generalized_components/ssm2_mixer.py @@ -8,6 +8,9 @@ from transformer_lens.model_bridge.generalized_components.base import ( GeneralizedComponent, ) +from transformer_lens.model_bridge.generalized_components.depthwise_conv1d import ( + route_causal_conv1d, +) from transformer_lens.model_bridge.generalized_components.ssm_protocol import ( SSMStateHookMixin, ) @@ -77,7 +80,10 @@ def forward(self, *args: Any, **kwargs: Any) -> Any: if self.eager_scan and hidden_states is not None and kwargs.get("cache_params") is None: output: Any = self._eager_scan_forward(hidden_states, kwargs.get("attention_mask")) else: - output = self.original_component(*args, **kwargs) + # transformers >= 5.15 calls a module-level causal_conv1d_fn with + # conv1d's weight; route it through the conv1d bridge so its hooks fire. + with route_causal_conv1d(self.original_component, "conv1d"): + output = self.original_component(*args, **kwargs) if isinstance(output, tuple) and len(output) > 0: first = output[0] diff --git a/transformer_lens/model_bridge/generalized_components/ssm_mixer.py b/transformer_lens/model_bridge/generalized_components/ssm_mixer.py index 33a0191fb..c8cfc60b9 100644 --- a/transformer_lens/model_bridge/generalized_components/ssm_mixer.py +++ b/transformer_lens/model_bridge/generalized_components/ssm_mixer.py @@ -8,6 +8,9 @@ from transformer_lens.model_bridge.generalized_components.base import ( GeneralizedComponent, ) +from transformer_lens.model_bridge.generalized_components.depthwise_conv1d import ( + route_causal_conv1d, +) from transformer_lens.model_bridge.generalized_components.ssm_protocol import ( SSMStateHookMixin, ) @@ -80,7 +83,10 @@ def forward(self, *args: Any, **kwargs: Any) -> Any: if self.eager_scan and hidden_states is not None and kwargs.get("cache_params") is None: output: Any = self._eager_scan_forward(hidden_states, kwargs.get("attention_mask")) else: - output = self.original_component(*args, **kwargs) + # transformers >= 5.15 calls a module-level causal_conv1d_fn with + # conv1d's weight; route it through the conv1d bridge so its hooks fire. + with route_causal_conv1d(self.original_component, "conv1d"): + output = self.original_component(*args, **kwargs) # Hook the primary output tensor, preserving tuple structure if isinstance(output, tuple) and len(output) > 0: