From 575ef20cfb1cc16cd6838ba3062b8b349ca67f2c Mon Sep 17 00:00:00 2001 From: emerard <113128214+emerardd@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:22:19 +0800 Subject: [PATCH] fix: preserve RecurrentGemma sliding-attention labels --- .../test_ssm_hybrid_resid_decomposition.py | 14 ++++++++++++++ transformer_lens/ActivationCache.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/integration/model_bridge/test_ssm_hybrid_resid_decomposition.py b/tests/integration/model_bridge/test_ssm_hybrid_resid_decomposition.py index 3c6d0f1c4..5851a0a75 100644 --- a/tests/integration/model_bridge/test_ssm_hybrid_resid_decomposition.py +++ b/tests/integration/model_bridge/test_ssm_hybrid_resid_decomposition.py @@ -557,6 +557,20 @@ def test_granite_moe_mlp_row_is_the_inline_moe_plus_shared_mlp_write(): torch.testing.assert_close(row, write, atol=ATOL, rtol=0) +def test_recurrent_gemma_labels_sliding_attention(monkeypatch): + bridge = _recurrent_gemma() + # Exercise the newer HF config vocabulary even on older versions. + monkeypatch.setattr( + bridge.cfg, + "layers_block_type", + ["linear_attention", "linear_attention", "sliding_attention", "linear_attention"], + ) + with torch.no_grad(): + _, cache = bridge.run_with_cache(TOKENS) + _, labels = cache.decompose_resid(return_labels=True) + assert labels[1::2] == ["0_mixer_out", "1_mixer_out", "2_attn_out", "3_mixer_out"] + + def test_recurrent_gemma_hook_embed_is_the_scaled_embedding(): """HF scales the embedding by its bfloat16 normalizer after embed_tokens.""" bridge = _recurrent_gemma() diff --git a/transformer_lens/ActivationCache.py b/transformer_lens/ActivationCache.py index 30a6edf7d..84183380e 100644 --- a/transformer_lens/ActivationCache.py +++ b/transformer_lens/ActivationCache.py @@ -492,7 +492,7 @@ def _mixer_label(self, layer: int, name: str) -> str: ) if block_type in ("mlp", "moe"): return f"{layer}_mlp_out" - if block_type in ("attention", "full_attention"): + if block_type in ("attention", "full_attention", "sliding_attention"): return f"{layer}_attn_out" return f"{layer}_{name}_out"