Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility for Reload Transformers imports cache for development workflow #35508 #35858

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

sambhavnoobcoder
Copy link

Problem Statement

When developing or modifying Transformers models, cached imports can prevent code changes from being picked up without manually restarting Python. This makes the development workflow more cumbersome, especially when making iterative changes to model implementations.

Fixes #35508

Root Cause Analysis

The issue stems from Python's module caching behavior in sys.modules. Additionally, Transformers uses a custom _LazyModule system for imports that maintains its own object cache. Both these caches need to be cleared to fully reload modified code.

Implementation

Added a clear_import_cache() utility function to transformers.utils.import_utils that:

  1. Identifies all Transformers modules in sys.modules
  2. Clears internal caches of _LazyModule instances
  3. Removes modules from sys.modules
  4. Forces reload of the main Transformers module

The implementation is minimally invasive and works with Transformers' existing module system.

Testing

Added a test case that verifies:

  1. Initial module imports work correctly
  2. Cache clearing removes modules from sys.modules
  3. Modules can be reimported after clearing
  4. The lazy loading system continues to function properly

The test ensures the cache clearing is thorough while maintaining backward compatibility.

Screenshots

Screenshot 2025-01-23 at 10 30 14 PM

cc : @ArthurZucker @SunMarc @Rocketknight1 and anyone interested in development workflow improvements.

@sambhavnoobcoder sambhavnoobcoder changed the title Add clear_import_cache() utility for development workflow #35508 Add utility for Reload Transformers imports cache for development workflow #35508 Jan 23, 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.

Reload Transformers imports
1 participant