Mistral: use fork-capable attention bridge (enable hook_attn_in / set_use_attn_in)#2
Closed
almogtavor wants to merge 1 commit into
Closed
Mistral: use fork-capable attention bridge (enable hook_attn_in / set_use_attn_in)#2almogtavor wants to merge 1 commit into
almogtavor wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, andblocks.{i}.hook_attn_in(and thehook_q_input/hook_k_input/hook_v_inputforks) 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 -> Medge) fails with a missing-hook error. Mistral is architecturally identical to Qwen2 here (separateq/k/v/o_proj, RoPE, GQA, RMSNorm, gated MLP), and Qwen2/Qwen3 already use the fork-capablePositionEmbeddingsAttentionBridge.Fix
PositionEmbeddingsAttentionBridge(same bridge as Qwen2/Qwen3).config=self.cfgto Mistral'sBlockBridgeso the block wireshook_mlp_intoo.Test
tests/unit/model_bridge/test_mistral_attn_in_fork.pybootshf-internal-testing/tiny-random-MistralForCausalLMand checks:PositionEmbeddingsAttentionBridge;set_use_attn_in(True)works andblocks.0.hook_attn_infires at[batch, pos, n_heads, d_model];Verified locally: inference is unchanged, and per-head input ablation now propagates to the output.