Skip to content

Commit 90cb8cd

Browse files
authored
Merge pull request #97 from wild-edge/fix/hf-hub-patch-lazy-imports
Stop the Hugging Face hub patch from importing unused transformers modules
2 parents 397a9bd + 8adbaca commit 90cb8cd

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ under Unreleased and move into a version section at release time.
55

66
## Unreleased
77

8+
### Fixed
9+
10+
- Enabling the `huggingface` hub no longer imports unused transformers model
11+
modules. To find references to `hf_hub_download` and `snapshot_download`,
12+
the hub tracker looked up the name on every loaded module, and transformers'
13+
lazy modules import code on lookup. With transformers loaded this imported
14+
about 140 extra modules, took about 0.15 s and logged about 430 "failed to
15+
patch" debug lines. The tracker now reads each module's namespace instead.
16+
817
## 0.3.0 - 2026-10-21
918

1019
### Added

‎tests/test_hubs.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import os
6+
import sys
67
import types
78
from unittest.mock import patch
89

@@ -188,3 +189,35 @@ def test_hf_tracker_drain_returns_and_clears_buffer(monkeypatch):
188189
assert result[0]["repo_id"] == "test/model"
189190
# Buffer should be cleared
190191
assert tracker.drain() == []
192+
193+
194+
class _LazyModule(types.ModuleType):
195+
"""Imports on attribute access, like transformers' lazy modules."""
196+
197+
def __init__(self, name: str) -> None:
198+
super().__init__(name)
199+
self.lookups: list[str] = []
200+
201+
def __getattr__(self, name: str):
202+
self.lookups.append(name)
203+
raise ModuleNotFoundError("No module named 'torchvision'")
204+
205+
206+
def test_patch_symbol_references_skips_lazy_attribute_lookup(monkeypatch):
207+
def original():
208+
pass
209+
210+
def replacement():
211+
pass
212+
213+
eager = types.ModuleType("eager_mod")
214+
eager.hf_hub_download = original
215+
lazy = _LazyModule("lazy_mod")
216+
monkeypatch.setitem(sys.modules, "eager_mod", eager)
217+
monkeypatch.setitem(sys.modules, "lazy_mod", lazy)
218+
219+
count = hf_mod._patch_symbol_references("hf_hub_download", original, replacement)
220+
221+
assert count >= 1
222+
assert eager.hf_hub_download is replacement
223+
assert lazy.lookups == []

‎wildedge/hubs/huggingface.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ def _patch_symbol_references(symbol: str, original: object, replacement: object)
7272
patched_count = 0
7373
for mod in list(sys.modules.values()):
7474
try:
75-
if getattr(mod, symbol, None) is original:
75+
# Read the module dict, not getattr: lazy modules (transformers)
76+
# import submodules on attribute access. A name a lazy module has
77+
# not loaded yet cannot hold a reference to the original. The lazy
78+
# huggingface_hub package resolves names from its submodules on
79+
# each access, so patching those covers `from huggingface_hub import`.
80+
namespace = vars(mod)
81+
except TypeError:
82+
continue
83+
try:
84+
if namespace.get(symbol) is original:
7685
setattr(mod, symbol, replacement)
7786
patched_count += 1
7887
except Exception as exc:

0 commit comments

Comments
 (0)