Skip to content

Mistral: use fork-capable attention bridge (enable hook_attn_in / set_use_attn_in)#2

Closed
almogtavor wants to merge 1 commit into
mainfrom
feat/bridge-attn-in-hook
Closed

Mistral: use fork-capable attention bridge (enable hook_attn_in / set_use_attn_in)#2
almogtavor wants to merge 1 commit into
mainfrom
feat/bridge-attn-in-hook

Conversation

@almogtavor

@almogtavor almogtavor commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Problem

Mistral models load through the plain AttentionBridge, which delegates q/k/v to the underlying HF attention module and exposes no per-receiver fork point. As a result, on a bridged Mistral:

  • set_use_attn_in(True) raises, and
  • blocks.{i}.hook_attn_in (and the hook_q_input/hook_k_input/hook_v_input forks) never exist.

That makes per-head attention-input interventions impossible on Mistral — activation/path patching that edits a receiver's input (e.g. blocking a single S -> M edge) fails with a missing-hook error. Mistral is architecturally identical to Qwen2 here (separate q/k/v/o_proj, RoPE, GQA, RMSNorm, gated MLP), and Qwen2/Qwen3 already use the fork-capable PositionEmbeddingsAttentionBridge.

Fix

  • Point the Mistral adapter's attention at PositionEmbeddingsAttentionBridge (same bridge as Qwen2/Qwen3).
  • Pass config=self.cfg to Mistral's BlockBridge so the block wires hook_mlp_in too.

Test

tests/unit/model_bridge/test_mistral_attn_in_fork.py boots hf-internal-testing/tiny-random-MistralForCausalLM and checks:

  • the attention bridge is PositionEmbeddingsAttentionBridge;
  • set_use_attn_in(True) works and blocks.0.hook_attn_in fires at [batch, pos, n_heads, d_model];
  • zeroing a single head's forked input actually changes the logits (the fork genuinely intervenes, not just a dangling hook).

Verified locally: inference is unchanged, and per-head input ablation now propagates to the output.

Mistral has separate q/k/v/o projections with RoPE and GQA, structurally
identical to Qwen2, but its adapter used the plain AttentionBridge, which
delegates q/k/v to HF and exposes no per-receiver fork point. As a result
set_use_attn_in() raised and blocks.{i}.hook_attn_in did not exist, so
per-head input interventions (activation/path patching on attention inputs)
were impossible on Mistral.

Switch the Mistral adapter to PositionEmbeddingsAttentionBridge (the same
fork-capable bridge Qwen2/Qwen3 use) and pass config to its BlockBridge so
hook_mlp_in is wired too. Add a test booting the tiny random Mistral that
checks the attention bridge is fork-capable, hook_attn_in fires at
[batch, pos, n_heads, d_model], and zeroing a single head's forked input
actually changes the logits.
@almogtavor almogtavor closed this Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant