Skip to content

Fix 2-D AViT axial attention: second pass attended across heads instead of along h - #112

Open
skagankose wants to merge 1 commit into
PolymathicAI:masterfrom
skagankose:fix-avit-2d-axial-attention
Open

skagankose wants to merge 1 commit into
PolymathicAI:masterfrom
skagankose:fix-avit-2d-axial-attention

Conversation

@skagankose

Copy link
Copy Markdown

Fixes #111.

Problem

In the 2-D path of AxialAttentionBlock, forward rearranges q, k and v in place on each pass, so the second in_permutation receives the layout produced by the first one (b h he w c). The old second permutation mapped it to b h w he c, which puts the heads on the attention axis. F.scaled_dot_product_attention therefore attended across the heads of each token instead of along h, and tokens mixed only along w. The 3-D branch chains its permutations correctly and is unchanged.

Change

-                ("b h he w c -> b h w he c", "b h w he c -> b h w (he c)"),
+                ("b h he w c -> b w he h c", "b w he h c -> b h w (he c)"),

The second pass now attends along h, matching the pattern used in the 3-D branch. No parameter names or shapes change, so existing checkpoints still load. Checkpoints trained with the old code (including the released 2-D AViT weights) learned without mixing along h, though, so they would need retraining to benefit.

Test

tests/models/test_transformer.py::test_axial_attention_mixes_along_every_axis perturbs one token and checks that the change reaches exactly the tokens on its axis-aligned lines, in 2-D and 3-D. The perturbation is a random vector, because the block's pre-LayerNorm would remove a constant shift.

  • On master, the 2-D case fails: only the 4 tokens sharing the h index change, out of the 11 expected on an 8 × 4 grid. The 3-D case passes.
  • With this change, both cases pass, along with the rest of tests/models/test_transformer.py. ruff check and ruff format --check (v0.6.4, as in pre-commit) pass.

Effect on 2-D datasets

The patch embedding and decoder are non-overlapping strided convolutions, so before this fix the only coupling across patch rows along the first spatial axis came from GroupNorm statistics. We trained AViT from scratch on rayleigh_benard (512 × 128). Its predictions showed seams at the 16-pixel strip boundaries along x: one step ahead the jump across them was 2.4–3.2× the ordinary pixel-to-pixel jump, rising to 7–13× after 30 rollout steps, and the one-step error was 1.35–1.87× higher at strip edges than at their centres. Retrained with this fix (same size, recipe and data), the jump fell to 1.3–1.4× at step 1 and 1.2–1.4× at step 30, and the edge-to-centre error ratio fell to 1.06–1.24, about the same as along y. The fixed model was also the most accurate of the models we compared on this dataset (CNextU-Net, TFNO, AViT).

🤖 Generated with Claude Code

…heads

In the 2-D path the second in_permutation received the layout left by the
first pass (b h he w c) and mapped it to b h w he c, so attention ran over
the heads of each token instead of along h. Tokens therefore mixed only
along w. The new permutation puts h on the attention axis, as the 3-D
branch already does. Parameters are unchanged.

Adds a test that a change to one token reaches every token on its
axis-aligned lines, in 2-D and 3-D. It fails on the old 2-D permutation.

Fixes PolymathicAI#111

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
skagankose added a commit to skagankose/sciml-project that referenced this pull request Dec 1, 2026
…#111)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

AViT (2-D): the second axial-attention pass attends over the heads instead of the first spatial axis

1 participant