fix(examples): add backwards-compatible fallback for transformers>=5.0 in ViT/DeiT models#540
fix(examples): add backwards-compatible fallback for transformers>=5.0 in ViT/DeiT models#540Aamod007 wants to merge 2 commits into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Aamod007 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Code Review
This pull request introduces a fallback mechanism in vit.py to define local implementations of ViT components (ViTSelfAttention, ViTSelfOutput, ViTIntermediate, and ViTOutput) in case they cannot be imported from the transformers library, and updates deit.py to use these local imports. Feedback was provided pointing out a critical bug in the fallback ViTOutput implementation, which is missing the residual connection addition (hidden_states + input_tensor) and would lead to incorrect model outputs.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
f17670e to
ac6273f
Compare
|
/assign @jaypume |
…0 in ViT/DeiT models Signed-off-by: Aamod007 <aamodkumar2006@gmail.com>
Signed-off-by: Aamod007 <aamodkumar2006@gmail.com>
ac6273f to
a49061f
Compare
|
/assign @jaypume |
What type of PR is this?
/kind bug
What this PR does / why we need it:
This PR fixes compatibility with
transformers >= 5.0in thevit.pyanddeit.pyexamples.In
transformers5.x, several ViT classes were removed or refactored (e.g.ViTSelfAttention,ViTSelfOutput,ViTIntermediate,ViTOutput). This previously caused anImportErrorpreventing the models from running.This PR implements a robust, backwards-compatible fallback mechanism: it attempts to import the original components, and upon encountering an
ImportError, it defines the necessary internalViTblock classes directly using standardnn.Module. This ensures that Ianvs runs flawlessly on both legacy and moderntransformersenvironments without breaking changes.Which issue(s) this PR fixes:
Fixes #509
Special notes for your reviewer:
N/A