Repository navigation
Fix 2-D AViT axial attention: second pass attended across heads instead of along h - #112
Open
skagankose wants to merge 1 commit into
Open
skagankose wants to merge 1 commit into
skagankose wants to merge 1 commit into
Conversation
…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
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.
Fixes #111.
Problem
In the 2-D path of
AxialAttentionBlock,forwardrearrangesq,kandvin place on each pass, so the secondin_permutationreceives the layout produced by the first one (b h he w c). The old second permutation mapped it tob h w he c, which puts the heads on the attention axis.F.scaled_dot_product_attentiontherefore attended across the heads of each token instead of alongh, and tokens mixed only alongw. The 3-D branch chains its permutations correctly and is unchanged.Change
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 alongh, though, so they would need retraining to benefit.Test
tests/models/test_transformer.py::test_axial_attention_mixes_along_every_axisperturbs 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.master, the 2-D case fails: only the 4 tokens sharing thehindex change, out of the 11 expected on an 8 × 4 grid. The 3-D case passes.tests/models/test_transformer.py.ruff checkandruff 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