Repository navigation
Stop the Hugging Face hub patch from importing unused transformers modules - #97
Merged
Merged
Conversation
Coverage report
Top uncovered files
|
getattr on transformers' lazy modules imported unused model modules and logged a debug line for each one that failed to import.
piotrekno1
force-pushed
the
fix/hf-hub-patch-lazy-imports
branch
from
October 21, 2026 17:30
9d8aceb to
8adbaca
Compare
Merged
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.
Enabling the
huggingfacehub imported unused transformers model modules at startup.Problem
To patch every reference to
hf_hub_downloadandsnapshot_download,_patch_symbol_referencescalledgetattr(mod, symbol)on every module insys.modules. transformers uses lazy modules that import code on attribute lookup. So the scan imported about 140 model modules the app never uses (Aria, Kosmos, ViTMatte and others), and took about 0.15 s. It also logged one "failed to patch" debug line for each import that failed on a missing package such as torchvision, about 430 per start.Fix
The scan reads each module's
__dict__instead. A lazy module that has not loaded the name yet cannot hold a reference to the original function, so skipping it misses nothing. The top-levelhuggingface_hubpackage is lazy too, but it resolves names from its submodules on each access, sofrom huggingface_hub import hf_hub_downloadstill returns the tracked function.Testing
__getattr__is called) and passes with the fix.examples/transformers_example.py(classify,generate,embed) against local ingest through a logging proxy, before and after the fix:snapshot_downloadsnapshot_downloadThe events are the same: 3 model loads, 8 inferences and 3 unloads.
hf_hub_downloadandsnapshot_downloadresolve to the tracked functions throughhuggingface_hub,huggingface_hub.file_downloadandtransformers.utils.hub.hf_hub_downloadand from 3 to 2 forsnapshot_download. The namespace no longer patched is the lazyhuggingface_hubpackage, which looks the name up again on each access.Rebased on devel after #96. The CHANGELOG entry is under Unreleased, above 0.3.0.
🤖 Generated with Claude Code