Skip to content

Conversation

wcrzlh
Copy link
Contributor

@wcrzlh wcrzlh commented Oct 9, 2025

What does this PR do?

Fixes # (issue)
✅ supplement condition of taking model as processor on hf transformers 4.54 base

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@xxx

@wcrzlh wcrzlh requested a review from vigo999 as a code owner October 9, 2025 03:49
Copy link
Contributor

Summary of Changes

Hello @wcrzlh, 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, authored by wcrzlh, addresses a fix in the mindone/transformers library related to how Processor classes handle the loading of their subcomponents (like image processors and tokenizers). The primary intent is to improve the condition for taking a model as a processor by refining the dynamic module loading mechanism. Key changes include the introduction of a new static method get_possibly_dynamic_module to centralize and standardize the process of finding and instantiating these subcomponents, prioritizing mindone.transformers implementations while providing a fallback to upstream transformers modules. Additionally, a specific warning has been added for image processors when the use_fast parameter is not explicitly set, indicating an upcoming change in default behavior. The changes aim to make the component loading more robust and explicit.

Highlights

  • Intent: This PR fixes and refines the logic for how Processor classes load their subcomponents (like image processors and tokenizers) from pretrained models. The goal is to ensure that mindone.transformers specific implementations are prioritized, with a graceful fallback to upstream transformers implementations, and to provide better handling for use_fast parameter in image processors.
  • Key Changes: The main changes involve refactoring the component loading mechanism within _get_arguments_from_pretrained by introducing a new static helper method get_possibly_dynamic_module. This new method centralizes the logic for dynamically importing module classes, first attempting to load from mindone.transformers and then falling back to transformers_module with a warning. Additionally, a specific warning has been added for image processors when the use_fast parameter is unset, anticipating a change in default behavior in a future version.
  • Detailed Changes: 1. Refactored _get_arguments_from_pretrained: The logic for determining the attribute_class (e.g., image processor, tokenizer) has been simplified. Instead of repetitive if 'ImageProcess' in class_name checks and sys.path manipulations, it now consistently calls the new cls.get_possibly_dynamic_module() method.
  1. Added image_processor specific warning: A logger.warning_once is now issued when attribute_name is image_processor and use_fast is None, informing users about the upcoming default change to use_fast=True in v4.52.
  2. Introduced get_possibly_dynamic_module static method: This new method handles the dynamic loading of module classes:
    • It first attempts to import the module from mindone.transformers by manipulating sys.path to include the parent directory.
    • If the module is not found in mindone.transformers (caught by AttributeError), it logs a warning about falling back to 🤗 Transformers and potential PyTorch backend mismatches.
    • It then attempts to retrieve the module from transformers_module.
    • It gracefully handles module_name is None by returning None.
  • Reviewer Notes: Reviewers should focus on the correctness of the module loading fallback logic in get_possibly_dynamic_module, ensuring that mindone.transformers components are correctly prioritized and that the fallback to transformers_module is robust. Also, verify the clarity and necessity of the new warning message regarding use_fast for image processors.
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

I've reviewed the changes and the refactoring in processing_utils.py is a significant improvement in terms of code clarity and robustness. The new get_possibly_dynamic_module method is much better than the previous approach of string-matching module names. I have one suggestion to fix a potential bug in the sys.path manipulation logic.

Comment on lines 1366 to +1371
sub_path = os.path.abspath(os.path.dirname(__file__))
sub_path = str(Path(sub_path).parent)
sys.path.insert(0, sub_path)
mindone_transformers_module = importlib.import_module("mindone.transformers")
if not hasattr(mindone_transformers_module, module_name):
raise ValueError(
f"Expect to have `{module_name}` registered in `mindone.transformers`, but failed to load it!"
)
return getattr(mindone_transformers_module, module_name)
else:
if sub_path not in sys.path:
sys.path.insert(0, sub_path)
mindone_tsf_path = importlib.import_module("mindone.transformers")
return getattr(mindone_tsf_path, module_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

There appears to be a bug in the sys.path manipulation and a confusing variable name here.

  1. Path issue: The code adds the mindone directory to sys.path, but to import mindone.transformers, the parent directory of mindone (the project root) should be added. The current implementation will likely fail with an ImportError in environments where the project root is not already in sys.path.

  2. Naming issue: The variable mindone_tsf_path is misleading as it holds a module object, not a path.

I suggest refactoring this block to correctly set the path and use a clearer variable name.

Suggested change
sub_path = os.path.abspath(os.path.dirname(__file__))
sub_path = str(Path(sub_path).parent)
sys.path.insert(0, sub_path)
mindone_transformers_module = importlib.import_module("mindone.transformers")
if not hasattr(mindone_transformers_module, module_name):
raise ValueError(
f"Expect to have `{module_name}` registered in `mindone.transformers`, but failed to load it!"
)
return getattr(mindone_transformers_module, module_name)
else:
if sub_path not in sys.path:
sys.path.insert(0, sub_path)
mindone_tsf_path = importlib.import_module("mindone.transformers")
return getattr(mindone_tsf_path, module_name)
project_root = str(Path(__file__).resolve().parent.parent.parent)
if project_root not in sys.path:
sys.path.insert(0, project_root)
mindone_transformers_module = importlib.import_module("mindone.transformers")
return getattr(mindone_transformers_module, module_name)

@wcrzlh wcrzlh changed the title fix(transformers): supplement condition of taking model as processor fix(transformers_4.54_base): supplement condition of taking model as processor Oct 9, 2025
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.

1 participant