Skip to content

restore image patch and attention axes in the ViT reference - #183

Open
ZenAlexa wants to merge 1 commit into
ScalingIntelligence:mainfrom
ZenAlexa:contrib/172-vit-image-tokens
Open

ZenAlexa wants to merge 1 commit into
ScalingIntelligence:mainfrom
ZenAlexa:contrib/172-vit-image-tokens

Conversation

@ZenAlexa

@ZenAlexa ZenAlexa commented Nov 14, 2026 •

Copy link
Copy Markdown

Why

Level 3's ViT reference sends [batch, tokens, dim] into a sequence-first Transformer encoder. Attention mixes images along the batch axis, and the classification token never attends to the image tokens. The shipped default model returns identical outputs when its images are multiplied by 10 or replaced with zeros, so the evaluator accepts an input-independent candidate.

The preceding patch construction also reshapes NCHW unfold results without arranging the spatial patches and channels. An explicit spatial-slice reference finds 80 of 96 elements in the wrong token positions for a small RGB image pair.

What changed

Use the existing F.unfold(...).transpose(1, 2) path to form complete spatial patch vectors, then configure the encoder with batch_first=True. These two changes restore the intended image-token boundary without adding dependencies or changing the shipped shapes, initialization arguments or input distribution.

Addresses the ViT case in #172. The other models in that issue remain separate. #108's level-3 review requests fidelity to the named model architecture, and its current patch doesn't touch this reference.

Verification

  • Four CPU behavior scenarios fail on main at 423217d9fda91e0c2d67e4a43bf62f96f6d104f1 and pass with the patch. They execute the complete reference against explicit spatial patch slices and a sequence-first encoder with identical weights, including image/projection gradients and batch independence.
  • The final local selection gives 21 passed, 1 GPU skip and 5 slow dataset cases deselected, including 17 existing local dataset cases.
  • The shipped default configuration uses two RGB 224×224 images, 16×16 patches, embedding dimension 512, six layers, eight heads and zero dropout. On H200, all five new scenarios pass and the original revision fails all five.
  • The actual eval_kernel_against_ref accepts an equivalent positive control in FP32, FP16 and BF16. A candidate that replaces every image with zeros changes from incorrect acceptance in all three precisions to rejection in all three.
  • In the complete FP32 model, multiplying images by 10 changes the output by approximately 0.296030, and image gradients are nonzero. The full FP64 output and image gradient match the independent sequence-first oracle exactly. The observed FP32 image-gradient difference is approximately 1.30e-6 and remains a measured floating-point path difference.
  • An additional FP64 Conv2d patch-projection oracle checks non-contiguous RGB inputs, complete outputs, input gradients and all small-model parameter gradients.
  • All 80 state_dict keys, 20,480,522 parameters and same-seed initial parameter values are unchanged, and strict state loading succeeds. Ruff, formatting and git diff --check pass.

Local execution used Python 3.10.21 and PyTorch 2.9.0. GPU execution used Python 3.10.20, PyTorch 2.9.0+cu128, CUDA 12.8 and one H200, with dependencies isolated in the run directory. AMD hardware and other GPU architectures remain unrun.

Reference version

This intentionally changes the reference outputs for level 3 problem 28. Existing solutions need correctness re-evaluation, and reference timings need regeneration for the new revision. The offline analysis currently matches timing records by filename, so old timing JSON must stay associated with its original reference revision. No speedup is claimed for this semantic correction. The Hugging Face dataset has its own publication step and is unchanged by this repository patch.

Copilot AI balanced review requested due to automatic review settings November 14, 2026 09:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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.

2 participants