Skip to content

Stop the Hugging Face hub patch from importing unused transformers modules - #97

Merged
piotrekno1 merged 1 commit into
develfrom
fix/hf-hub-patch-lazy-imports
Oct 21, 2026
Merged

piotrekno1 merged 1 commit into
develfrom
fix/hf-hub-patch-lazy-imports

Conversation

@piotrekno1

@piotrekno1 piotrekno1 commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Enabling the huggingface hub imported unused transformers model modules at startup.

Problem

To patch every reference to hf_hub_download and snapshot_download, _patch_symbol_references called getattr(mod, symbol) on every module in sys.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-level huggingface_hub package is lazy too, but it resolves names from its submodules on each access, so from huggingface_hub import hf_hub_download still returns the tracked function.

Testing

  • New test with a lazy module stub. It fails on the old code (the stub's __getattr__ is called) and passes with the fix.
  • 900 passed, 28 skipped. Ruff is clean.
  • Ran examples/transformers_example.py (classify, generate, embed) against local ingest through a logging proxy, before and after the fix:
"failed to patch" lines Events sent Tracked download calls
Before 430 per run 14, all accepted 3 snapshot_download
After 0 14, all accepted 3 snapshot_download

The events are the same: 3 model loads, 8 inferences and 3 unloads.

  • After init, hf_hub_download and snapshot_download resolve to the tracked functions through huggingface_hub, huggingface_hub.file_download and transformers.utils.hub.
  • The log line that counts patched namespaces drops from 5 to 4 for hf_hub_download and from 3 to 2 for snapshot_download. The namespace no longer patched is the lazy huggingface_hub package, 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

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Coverage report

  • Total: 90% (12126/13536 lines)

Top uncovered files

  • wildedge/integrations/transformers.py: 63.2% (111 lines missed)
  • wildedge/integrations/mlx.py: 43.0% (98 lines missed)
  • wildedge/integrations/ultralytics.py: 67.8% (84 lines missed)
  • wildedge/integrations/pytorch.py: 64.5% (70 lines missed)
  • wildedge/platforms/macos.py: 57.1% (67 lines missed)

getattr on transformers' lazy modules imported unused model modules and
logged a debug line for each one that failed to import.
@piotrekno1
piotrekno1 force-pushed the fix/hf-hub-patch-lazy-imports branch from 9d8aceb to 8adbaca Compare October 21, 2026 17:30
@piotrekno1
piotrekno1 merged commit 90cb8cd into devel Oct 21, 2026
14 checks passed
@piotrekno1
piotrekno1 deleted the fix/hf-hub-patch-lazy-imports branch October 21, 2026 17:39
@piotrekno1 piotrekno1 mentioned this pull request Oct 21, 2026
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