Repository navigation
Add bidirectional image-token attention to MultimodalLM - #726
Merged
Merged
Conversation
HF Molmo2 attends bidirectionally among image tokens (token_type_ids-driven) while text stays causal; our LM previously ran fully causal over the spliced image tokens. - Thread an `or_mask` (boolean allow-mask, True where image[q] & image[k]) through Transformer.forward -> block kwargs -> Attention -> the dense SDPA backend, which ORs it onto the causal/sliding base (mirrors HF's or_mask_function). Capability-gated via AttentionBackend.SUPPORTS_OR_MASK; flash/TE/Fused backends and context parallelism raise rather than silently ignore it. - MultimodalLM.forward gains a `token_type_ids` argument (HF-aligned) and builds the (B, 1, S, S) mask from it. - Add test_molmo2_image_bidirectional_attention_parity (196 image tokens): with token_type_ids our last-token logits match HF; the test also asserts that pure-causal is measurably worse, so it cannot silently regress to a causal-vs-causal comparison (the blind spot of the single-image-token full-pipeline parity test). Full vision suite: 89 passed, 2 skipped (O-7B YaRN). The new GPU parity test passes against real Molmo2-4B weights.
jason718
force-pushed
the
jasonr/vlm-bidirectional-image-attn
branch
from
June 13, 2026 05:28
effa2e2 to
c20a163
Compare
jason718
added a commit
that referenced
this pull request
Jun 13, 2026
## Summary Adds **bidirectional image-token attention** to `MultimodalLM`, matching HF Molmo2. HF Molmo2 attends **bidirectionally among image tokens** (driven by `token_type_ids`) while text stays causal — image tokens see *all* image tokens in the prefix, not just earlier ones. Our `MultimodalLM` previously ran the LM **fully causal** over the spliced image tokens, so this was a real parity gap. Targets the `vision` integration branch; stacked on the Molmo2 loader (#715). ## Why this wasn't caught before The existing full-pipeline logit-parity test used **exactly one** `<im_patch>` token, where causal and bidirectional attention are identical — a structural blind spot. The generation test used 196 image tokens but never passed `token_type_ids` to HF, so HF ran causal too (causal-vs-causal). So the gap was entirely untested. ## What's in this PR ### Fix - `MultimodalLM.forward` gains an HF-aligned `token_type_ids` argument and builds a `(B, 1, S, S)` boolean **`or_mask`** (`True` where `image[q] & image[k]`). - `or_mask` is threaded through `Transformer.forward` → block kwargs → `Attention.forward`/`sdpa` → `TorchAttentionBackend`, which **OR's it onto the causal/sliding base** — exactly mirroring HF's `or_mask_function`, and correct even for sliding-window layers. - Capability-gated by `AttentionBackend.SUPPORTS_OR_MASK` (only the dense `torch` SDPA backend supports it). Flash/TE/Fused backends and context parallelism **raise** rather than silently fall back to causal. - `token_type_ids=None` → unchanged causal behavior (fully backward compatible). ### Test (pins the gap) `test_molmo2_image_bidirectional_attention_parity` (196 image tokens, real Molmo2-4B): - **(1)** with `token_type_ids`, our last-token logits match HF (bidirectional); - **(2)** pure-causal is **measurably worse** — guards against the test silently regressing to causal-vs-causal. ## Verification - **Full vision suite: 89 passed, 2 skipped** (the 2 skips are the intentional Molmo2-O-7B per-layer-YaRN cases). The new GPU parity test passes against real Molmo2-4B weights. - End-to-end smoke confirms the mask flows through the whole stack (`token_type_ids` changes the output) and the causal path (`or_mask=None`) is unchanged. - `ruff` / `mypy` / `isort` / `black` clean. ## Scope / follow-ups (intentionally out of scope here) - The parity check compares **last-token** logits (< 5.0 bf16 threshold) and asserts causal-is-worse to prove the gap; it does not yet assert a per-image-token logit bound. - The `(B, 1, S, S)` mask is O(S²) memory — fine for these models, but a `flex_attention` path would be the scalable follow-up for long sequences and for enabling this on flash backends.
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.
Summary
Adds bidirectional image-token attention to
MultimodalLM, matching HF Molmo2.HF Molmo2 attends bidirectionally among image tokens (driven by
token_type_ids) while text stays causal — image tokens see all image tokens in the prefix, not just earlier ones. OurMultimodalLMpreviously ran the LM fully causal over the spliced image tokens, so this was a real parity gap.Targets the
visionintegration branch; stacked on the Molmo2 loader (#715).Why this wasn't caught before
The existing full-pipeline logit-parity test used exactly one
<im_patch>token, where causal and bidirectional attention are identical — a structural blind spot. The generation test used 196 image tokens but never passedtoken_type_idsto HF, so HF ran causal too (causal-vs-causal). So the gap was entirely untested.What's in this PR
Fix
MultimodalLM.forwardgains an HF-alignedtoken_type_idsargument and builds a(B, 1, S, S)booleanor_mask(Truewhereimage[q] & image[k]).or_maskis threaded throughTransformer.forward→ block kwargs →Attention.forward/sdpa→TorchAttentionBackend, which OR's it onto the causal/sliding base — exactly mirroring HF'sor_mask_function, and correct even for sliding-window layers.AttentionBackend.SUPPORTS_OR_MASK(only the densetorchSDPA backend supports it). Flash/TE/Fused backends and context parallelism raise rather than silently fall back to causal.token_type_ids=None→ unchanged causal behavior (fully backward compatible).Test (pins the gap)
test_molmo2_image_bidirectional_attention_parity(196 image tokens, real Molmo2-4B):token_type_ids, our last-token logits match HF (bidirectional);Verification
token_type_idschanges the output) and the causal path (or_mask=None) is unchanged.ruff/mypy/isort/blackclean.Scope / follow-ups (intentionally out of scope here)
(B, 1, S, S)mask is O(S²) memory — fine for these models, but aflex_attentionpath would be the scalable follow-up for long sequences and for enabling this on flash backends.