Skip to content

fix(examples): add backwards-compatible fallback for transformers>=5.0 in ViT/DeiT models#540

Open
Aamod007 wants to merge 2 commits into
kubeedge:mainfrom
Aamod007:fix-transformers-5-compat
Open

fix(examples): add backwards-compatible fallback for transformers>=5.0 in ViT/DeiT models#540
Aamod007 wants to merge 2 commits into
kubeedge:mainfrom
Aamod007:fix-transformers-5-compat

Conversation

@Aamod007

Copy link
Copy Markdown

What type of PR is this?
/kind bug

What this PR does / why we need it:
This PR fixes compatibility with transformers >= 5.0 in the vit.py and deit.py examples.

In transformers 5.x, several ViT classes were removed or refactored (e.g. ViTSelfAttention, ViTSelfOutput, ViTIntermediate, ViTOutput). This previously caused an ImportError preventing 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 internal ViT block classes directly using standard nn.Module. This ensures that Ianvs runs flawlessly on both legacy and modern transformers environments without breaking changes.

Which issue(s) this PR fixes:
Fixes #509

Special notes for your reviewer:
N/A

@kubeedge-bot kubeedge-bot added the kind/bug Categorizes issue or PR as related to a bug. label Jun 13, 2026
@kubeedge-bot kubeedge-bot requested review from Poorunga and hsj576 June 13, 2026 14:30
@kubeedge-bot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Aamod007
To complete the pull request process, please assign jaypume after the PR has been reviewed.
You can assign the PR to them by writing /assign @jaypume in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubeedge-bot kubeedge-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 13, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@kubeedge-bot kubeedge-bot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jun 13, 2026
@Aamod007 Aamod007 force-pushed the fix-transformers-5-compat branch from f17670e to ac6273f Compare June 13, 2026 14:35
@kubeedge-bot kubeedge-bot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jun 13, 2026
@Aamod007

Copy link
Copy Markdown
Author

/assign @jaypume

Aamod007 and others added 2 commits June 15, 2026 10:47
…0 in ViT/DeiT models

Signed-off-by: Aamod007 <aamodkumar2006@gmail.com>
Signed-off-by: Aamod007 <aamodkumar2006@gmail.com>
@Aamod007 Aamod007 force-pushed the fix-transformers-5-compat branch from ac6273f to a49061f Compare June 15, 2026 05:17
@Aamod007

Copy link
Copy Markdown
Author

/assign @jaypume

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: vit.py and deit.py fail with ImportError on transformers >= 5.0 due to removed classes

3 participants