Skip to content

fix: MaskedCausalVisionTransformer build on all supported timm versions - #2043

Merged
gabrielfruet merged 2 commits into
lightly-ai:masterfrom
lorinczszabolcs:fix-masked-causal-vit-attention-kwargs
Aug 21, 2026
Merged

fix: MaskedCausalVisionTransformer build on all supported timm versions#2043
gabrielfruet merged 2 commits into
lightly-ai:masterfrom
lorinczszabolcs:fix-masked-causal-vit-attention-kwargs

Conversation

@lorinczszabolcs

@lorinczszabolcs lorinczszabolcs commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Closes #2042.

Two commits:

  1. Forward only attention kwargs in MaskedCausalBlock. The block forwarded all of its block-level kwargs to the timm Attention layer. Attention rejects arguments like mlp_ratio, so MaskedCausalVisionTransformer failed to build on every supported timm version. This is a regression from MaskedCausalBlock.__init__() got an unexpected keyword argument 'proj_bias' #1841 (the **kwargs forwarding that fixed MaskedCausalBlock.__init__() got an unexpected keyword argument 'proj_bias' #1829). The fix selects the kwargs from Attention.__init__'s signature, so it keeps MaskedCausalBlock.__init__() got an unexpected keyword argument 'proj_bias' #1841's version-robustness without a hardcoded argument list. Adds a regression test for the block and the model, this backbone had none before.

  2. Set global_pool="avg" in the AIM examples and benchmark. The examples build the backbone with class_token=False but no global_pool, which trips timm's class_token or global_pool != "token" assertion at init, before the block bug is even reached. AIM's self-supervised path uses forward_features, so "avg" satisfies the assertion without changing behavior. Regenerated the notebooks.

Verified on timm 0.9.9, 1.0.14, and 1.0.28. make format-check passes and the notebooks are up to date.

MaskedCausalBlock forwarded its full block-level kwargs to
MaskedCausalAttention, including arguments the timm Attention
constructor does not accept (e.g. mlp_ratio). This made
MaskedCausalVisionTransformer, and therefore the AIM model, fail to
construct on every supported timm version (0.9.9-1.0.28) with
"Attention.__init__() got an unexpected keyword argument 'mlp_ratio'".

Forward only the arguments that Attention.__init__ defines, selected via
its signature so the fix stays correct as timm evolves. Add a regression
test covering the block and the vision transformer.
timm's VisionTransformer asserts `class_token or global_pool != 'token'`.
The AIM examples and benchmark build MaskedCausalVisionTransformer with
class_token=False but did not set global_pool, so construction failed on
current timm. AIM's self-supervised path uses forward_features and is not
affected by global_pool, so "avg" satisfies the assertion without changing
behaviour. Regenerate the AIM example notebooks accordingly.
@lorinczszabolcs
lorinczszabolcs force-pushed the fix-masked-causal-vit-attention-kwargs branch from 6ca463a to 9ee3d4e Compare August 21, 2026 18:29
@lorinczszabolcs

Copy link
Copy Markdown
Contributor Author

Addressed the failing tests.

Comment thread lightly/models/modules/masked_causal_vision_transformer.py
@gabrielfruet
gabrielfruet enabled auto-merge August 21, 2026 19:52
@gabrielfruet
gabrielfruet added this pull request to the merge queue Aug 21, 2026
Merged via the queue into lightly-ai:master with commit 8e995ec Aug 21, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants