Skip to content

[Bug Report] Residual-decomposition APIs raise KeyError on SSM / hybrid bridges (Qwen3.5, Mamba-2, NemotronH, GraniteMoeHybrid) #1986

Closed

Description

@SBrouck

Describe the bug

On SSM and hybrid bridges, the ActivationCache residual-decomposition APIs raise KeyError on the first block without attention:

API Error
decompose_resid() KeyError: 'blocks.0.hook_attn_out'
decompose_resid(mode="mlp") KeyError: 'blocks.0.hook_mlp_out' (Mamba-2, NemotronH, GraniteMoeHybrid)
compute_head_results(), stack_head_results() KeyError: 'blocks.0.attn.hook_z'
get_full_resid_decomposition() KeyError: 'blocks.0.attn.hook_z'
logit_attrs(cache.decompose_resid(), ...) KeyError: 'blocks.0.hook_attn_out'

Reproduced on Qwen3.5, Mamba-2, NemotronH and GraniteMoeHybrid. accumulated_resid works.

These methods loop over range(n_layers) and assume every block has attn and mlp. Since #1578, gated-delta-net and Mamba blocks intentionally expose their mixer output (linear_attn.hook_out, mixer.hook_out) instead of hook_attn_out, and Mamba / NemotronH blocks have no MLP. The cache APIs were not updated for that.

The fix looks straightforward because the residual stream still decomposes exactly. On Qwen3.5, NemotronH and Mamba-2, resid_post - resid_pre equals the mixer output (attention or SSM) plus mlp_out when present, within ~1e-8.

Code example

import torch
from transformers import AutoTokenizer
from transformer_lens.model_bridge import TransformerBridge

name = "Qwen/Qwen3.5-0.8B"
bridge = TransformerBridge.boot_transformers(name, device="cpu", dtype=torch.float32)
ids = AutoTokenizer.from_pretrained(name)("The capital of Germany is", return_tensors="pt").input_ids
_, cache = bridge.run_with_cache(ids)

cache.accumulated_resid()      # OK
cache.decompose_resid()        # KeyError: 'blocks.0.hook_attn_out'
cache.stack_head_results()     # KeyError: 'blocks.0.attn.hook_z'

The same happens without Hub access on the tiny random fixtures in tests/integration/model_bridge/test_ssm_effective_attention_dispatch.py.

System Info

  • transformer_lens 4.3.0 from PyPI; the code involved is unchanged on dev (837fb72)
  • macOS (Apple Silicon), CPU, fp32
  • Python 3.12, transformers 5.13 and 5.22

Additional context

I have a fix with tests ready and will open a PR against dev. Blocks without attention contribute their mixer output in the attention slot, only attention layers contribute heads, and only blocks with an MLP contribute mlp_out or neurons. All decompositions still sum to the residual stream.

Checklist

  • I have checked that there is no similar issue in the repo (required)

Activity

  1. added
    enhancement
    complexity-moderate
    TransformerBridge
    on Nov 5, 2026
  2. jlarson4 commented on Nov 5, 2026

    @jlarson4
    Collaborator

    Thanks for documenting this and putting together the fix @SBrouck, I just submitted a review on the issue, let me know when you have a moment to review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

TransformerBridgecomplexity-moderateenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions