Skip to content

Conversation

@xwy-bit
Copy link

@xwy-bit xwy-bit commented Dec 12, 2025

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Summary This PR addresses a compatibility issue with transformers version 4.50+, where PreTrainedModel no longer inherits from GenerationMixin by default. As a result, the InternLM2 model loses its text generation capability, causing an AttributeError.

Changes

  • Modified get_model_tokenizer_internvl to perform a runtime check on the loaded model.
  • Dynamically injects GenerationMixin into the model's __bases__ if the generate method is missing.
  • Ensures backward compatibility with older transformers versions.

Before vs. After

Before

image

After
image

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @xwy-bit, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical bug impacting InternLM2 models when integrated with transformers versions 4.50 and newer. The issue stemmed from a change in the transformers library where the PreTrainedModel class no longer automatically includes the GenerationMixin, leading to an AttributeError when attempting text generation. The solution involves a dynamic runtime check within the model loading process to identify if the generate method is missing and subsequently injects the GenerationMixin into the model's inheritance chain, effectively restoring the model's generation capabilities and ensuring seamless operation across different transformers versions.

Highlights

  • Compatibility Fix: Addresses a compatibility issue with transformers versions 4.50+ where PreTrainedModel no longer inherits GenerationMixin by default, causing AttributeError for InternLM2 models.
  • Dynamic Method Injection: Introduces a runtime check in get_model_tokenizer_internvl to detect if the generate method is missing from the model's language component.
  • Restored Functionality: Dynamically injects GenerationMixin into the model's base classes if the generate method is absent, thereby restoring text generation capability for InternVL-2.5 models.
  • Backward Compatibility: Ensures backward compatibility with older transformers versions while fixing the issue for newer ones.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

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

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 fix for a compatibility issue with transformers>=4.50 by dynamically injecting GenerationMixin when the generate method is missing. The approach of monkey-patching __bases__ is a direct way to address this kind of library incompatibility. My review includes a suggestion to improve code style by correcting indentation and using a logger instead of print, which aligns with the logging practices in other parts of the codebase.

Comment on lines +142 to +151
if model is not None:
# fix missing generate method for InternVL-2.5 models when using transformers >= 4.50
llm_part = getattr(model, 'language_model', model)
if not hasattr(llm_part, 'generate'):
print("Detected missing 'generate' method (transformers >= 4.50). Injecting GenerationMixin...")

cls = llm_part.__class__
if GenerationMixin not in cls.__bases__:
cls.__bases__ = cls.__bases__ + (GenerationMixin,)

Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This block has some indentation issues and uses print for logging. For consistency with the rest of the codebase and to allow users to control log levels, it's better to use a proper logger.

I've corrected the indentation and replaced print with logger.info.

To make this work, you'll also need to add the following at the top of the file:

from swift.utils import get_logger

logger = get_logger()
Suggested change
if model is not None:
# fix missing generate method for InternVL-2.5 models when using transformers >= 4.50
llm_part = getattr(model, 'language_model', model)
if not hasattr(llm_part, 'generate'):
print("Detected missing 'generate' method (transformers >= 4.50). Injecting GenerationMixin...")
cls = llm_part.__class__
if GenerationMixin not in cls.__bases__:
cls.__bases__ = cls.__bases__ + (GenerationMixin,)
if model is not None:
# fix missing generate method for InternVL-2.5 models when using transformers >= 4.50
llm_part = getattr(model, 'language_model', model)
if not hasattr(llm_part, 'generate'):
logger.info("Detected missing 'generate' method (transformers >= 4.50). Injecting GenerationMixin...")
cls = llm_part.__class__
if GenerationMixin not in cls.__bases__:
cls.__bases__ = cls.__bases__ + (GenerationMixin,)

@Jintao-Huang
Copy link
Collaborator

try use transformers==4.49

@xwy-bit
Copy link
Author

xwy-bit commented Dec 12, 2025

You're right. I hope this PR remains compatible with both versions prior to 4.49 and above, since 4.50 introduced the breaking change, and transformers is already at 4.57.3 (5.0.0rc).

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