Fix import latency [2/N]: Implement native _is_package_available#5129
Merged
albertvillanova merged 1 commit intohuggingface:mainfrom Feb 20, 2026
Merged
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
2 tasks
qgallouedec
approved these changes
Feb 19, 2026
Member
qgallouedec
left a comment
There was a problem hiding this comment.
Very cool. 2.5 seconds multiplied by the number of trl imports is probably a considerable amount of time you've saved humanity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement native
_is_package_available.This is the 2nd PR to fix the TRL import latency I am working on:
This PR updates the way the
_is_package_availableutility function is imported and implemented in the codebase. The main change is that the function is now defined locally intrl/import_utils.pyinstead of being imported from thetransformerslibrary, and all internal imports have been updated accordingly.Context
Both
trl/import_utils.pyandtrl/_compat.pyimport_is_package_availablefromtransformersat the top level, which was introduced by:transformersutilities when possible #2064Both files are eagerly loaded when import trl runs, triggering a full transformers load (~3s).
Solution
Replace
transformers..utils.import_utils._is_package_availablewith local implementation of_is_package_available.After the fix (#5128 and #5129), the import latency passed from
~3sto<0.5s:Key changes:
Refactoring of package availability utility:
_is_package_availablefunction from being imported fromtransformers.utils.import_utilsto a local implementation intrl/import_utils.py, ensuring the codebase no longer depends on the external implementation.trl/_compat.pyto use the local_is_package_availablefrom.import_utilsinstead of the one fromtransformers.utils.import_utils.Enhancements to local implementation:
_is_package_availablefunction is copied from thetransformerslibrary and includes logic to check for a package's existence and optionally retrieve its version, handling edge cases such as distribution name differences and editable installs.