Skip to content

Visual Encoders (ViT, DeiT) Support Rollout - #1546

Merged
jlarson4 merged 77 commits into
TransformerLensOrg:devfrom
david-wei-01001:vit
Jul 28, 2026
Merged

Visual Encoders (ViT, DeiT) Support Rollout#1546
jlarson4 merged 77 commits into
TransformerLensOrg:devfrom
david-wei-01001:vit

Conversation

@david-wei-01001

@david-wei-01001 david-wei-01001 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Description

Extended model_bridge of TransformerLens to support visual encoders (ViT, Deit)
Thoroughly tested:
unit test in tests/unit/model_bridge/supported_architectures/test_vit_adapter.py
intergration test in tests/integration/model_bridge/test_vit_adapter.py

can be tested as:

cd TransformerLens 
uv sync

uv run pytest tests/unit/model_bridge/supported_architectures/test_vit_adapter.py -v
uv run pytest tests/integration/model_bridge/test_vit_adapter.py -v 

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Screenshots

Please attach before and after screenshots of the change if applicable.

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

Implement ViT/DeiT architecture adapter for model bridging.
Implement VisionClassifierHeadBridge to handle CLS token slicing for classification.
Updated documentation for pixel_values parameter to clarify its use with vision models.
Added vision model architectures and classification heads.
Updated the VisionClassifierHeadBridge to directly use an already-pooled CLS token instead of slicing from the sequence output. Adjusted the forward method to reflect this change and improved error handling for the original component.
This file contains unit tests for the ViTArchitectureAdapter, covering component mapping, configuration flags, weight conversions, and model preparation methods.
Reintroduce a patch_layers function to inject a dummy 'mlp' attribute into ViTLayer blocks for MLPBridge compatibility.
Removed the patch_layers function and its call, which injected a dummy 'mlp' attribute into ViTLayer blocks. Updated comments for clarity regarding the MLPBridge container.
Inject a dummy 'mlp' attribute into ViTLayer blocks to satisfy hasattr check for TransformerLens.
@david-wei-01001

david-wei-01001 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

All checks pass now, ready to merge

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for getting this all up to date with the latest state of the repo, I greatly appreciate this. The adapter looks great, just some small nits on testing & other small configuration things that can sometimes slip by the wayside. Once these are wrapped up I will get this merged and ready for the next release!

Comment thread transformer_lens/model_bridge/supported_architectures/vit.py Outdated
Comment thread tests/unit/model_bridge/supported_architectures/test_vit_adapter.py Outdated
Comment thread transformer_lens/model_bridge/supported_architectures/vit.py Outdated
Comment thread tests/integration/model_bridge/test_vit_adapter.py
Comment thread tests/integration/model_bridge/test_vit_adapter.py Outdated
Comment thread transformer_lens/model_bridge/supported_architectures/vit.py
Comment thread transformer_lens/model_bridge/bridge.py Outdated
Added comment to clarify the lack of tokenizer support for vision models.
Removed unused head_dim assignment from hf_config.
Updated error message for clarity and added handling for last_hidden_state in output.
Update test to verify that the forward method returns a tensor instead of a raw HF output object. Adjust assertions to match the expected behavior after changes in bridge.py.
Removed deprecated tests for prepare_loading() in TestViTPrepareLoading.
@david-wei-01001

Copy link
Copy Markdown
Contributor Author

All issues resolved, all formats checked. All tests passed, ready to merge.

@jlarson4

Copy link
Copy Markdown
Collaborator

Great work @david-wei-01001!

@jlarson4
jlarson4 merged commit 713e2ce into TransformerLensOrg:dev Jul 28, 2026
25 checks passed
@david-wei-01001

Copy link
Copy Markdown
Contributor Author

Thank you very much!

@david-wei-01001
david-wei-01001 deleted the vit branch July 28, 2026 15:18
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