Skip to content

Add bidirectional image-token attention to MultimodalLM - #726

Merged
jason718 merged 1 commit into
visionfrom
jasonr/vlm-bidirectional-image-attn
Jun 13, 2026
Merged

jason718 merged 1 commit into
visionfrom
jasonr/vlm-bidirectional-image-attn

Conversation

@jason718

Copy link
Copy Markdown
Contributor

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.

@allenai allenai deleted a comment from chatgpt-codex-connector Bot Jun 13, 2026
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
jason718 force-pushed the jasonr/vlm-bidirectional-image-attn branch from effa2e2 to c20a163 Compare June 13, 2026 05:28
@jason718
jason718 merged commit 99d4b9a into vision Jun 13, 2026
1 check passed
@jason718
jason718 deleted the jasonr/vlm-bidirectional-image-attn branch June 13, 2026 05:30
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.
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