Skip to content

feature: added import complexity checker#45013

Merged
tarekziade merged 5 commits intomainfrom
tarekziade-import-verif
Mar 31, 2026
Merged

feature: added import complexity checker#45013
tarekziade merged 5 commits intomainfrom
tarekziade-import-verif

Conversation

@tarekziade
Copy link
Copy Markdown
Collaborator

@tarekziade tarekziade commented Mar 26, 2026

What does this PR do?

  • Adds a new checker in make chek-repo that will import transformers and count the number of imported module.
  • Lazy import of torch when doing import transformers

The change will reduce the import time from ~1.5s to ~0.7s (+3000 modules to 619 modules)
The checker will error out when it goes over a threshold - for instance if our code imports a lib like torch not lazily
This will prevent imports slowdowns regression.

Can be used directly to display the import counts and import tree:

✗ python utils/check_import_complexity.py
Import complexity regression: `import transformers` triggered 1890 module imports (maximum allowed: 1500).

Run the following command to display the full import tree and identify the cause:

    python utils/check_import_complexity.py --display

✗ python utils/check_import_complexity.py --display
...
    │   │   │   │   ├── tqdm.contrib
    │   │   │   │   ├── tqdm.contrib.concurrent
    │   │   │   │   ├── huggingface_hub._buckets
    │   │   │   │   ├── huggingface_hub._commit_api
    │   │   │   │   │   └── huggingface_hub.lfs
    │   │   │   │   ├── huggingface_hub._dataset_viewer
    │   │   │   │   ├── huggingface_hub._eval_results
    │   │   │   │   ├── huggingface_hub._inference_endpoints
    │   │   │   │   ├── huggingface_hub._jobs_api
    │   │   │   │   │   └── huggingface_hub._space_api
    │   │   │   │   ├── huggingface_hub._upload_large_folder
    │   │   │   │   ├── huggingface_hub.community
    │   │   │   │   ├── huggingface_hub.repocard_data
    │   │   │   │   ├── huggingface_hub.utils._deprecation
    │   │   │   │   ├── huggingface_hub.utils._verification
    │   │   │   │   └── huggingface_hub.utils.endpoint_helpers
    │   │   │   ├── huggingface_hub.repocard
    │   │   │   └── huggingface_hub._snapshot_download
    │   │   ├── transformers.utils.kernel_config
    │   │   └── transformers.utils.peft_utils
    │   ├── transformers.utils.versions
    │   ├── tokenizers
    │   └── accelerate
    ├── sentencepiece
    ├── mistral_common
    ├── torchvision
    ├── torchaudio
    ├── librosa
    ├── timm
    └── scipy

Total modules imported: 1890

@tarekziade tarekziade requested a review from LysandreJik March 26, 2026 07:47
@tarekziade tarekziade self-assigned this Mar 26, 2026
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

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.

@ydshieh
Copy link
Copy Markdown
Collaborator

ydshieh commented Mar 26, 2026

Not super clear too me, this depends on the environment, no? I mean, say in our .[dev] environment which has almost everything, import transformers will import a lot of modules, then this check will fail (?)

@tarekziade
Copy link
Copy Markdown
Collaborator Author

Not super clear too me, this depends on the environment, no? I mean, say in our .[dev] environment which has almost everything, import transformers will import a lot of modules, then this check will fail (?)

The test will do an import transformers on a fresh Python interpreter and will detect what modules get pulled in when you do this.

when you install .[dev] you install a lot of extra packages used for developing. In theory those should not be pulled under a vanilla import transformers unless dev installs specific versions of packages that are also in production. But in practice you are absolutely right! we should run this checker in a prod-like environment if we want to make sure we exercise what people out there do.

@tarekziade tarekziade marked this pull request as draft March 27, 2026 09:48
Copy link
Copy Markdown
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it, and aligned with the direction. Thanks Tarek!

Comment on lines +15 to +24
"""Check that `import transformers` does not pull in too many modules.

Traces the full import tree triggered by `import transformers` using a custom
``importlib.abc.MetaPathFinder`` and counts every module that gets loaded.
If the count exceeds ``MAX_IMPORT_COUNT`` the check fails, signalling a
potential regression in import speed.

Usage:
python utils/check_import_complexity.py # CI check mode
python utils/check_import_complexity.py --display # show the full import tree
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

Comment thread utils/check_import_complexity.py Outdated
Comment on lines -50 to -51
if is_torch_available():
from torch import Tensor
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quite surprised, there are fewer offending torch import statements than I would have thought!

@tarekziade tarekziade force-pushed the tarekziade-import-verif branch from 0940ea4 to b7fd8f6 Compare March 31, 2026 06:20
@tarekziade tarekziade marked this pull request as ready for review March 31, 2026 06:21
@tarekziade tarekziade enabled auto-merge March 31, 2026 06:21
@github-actions
Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45013&sha=b7fd8f

@tarekziade tarekziade disabled auto-merge March 31, 2026 06:38
@tarekziade tarekziade added this pull request to the merge queue Mar 31, 2026
Merged via the queue into main with commit 6b28829 Mar 31, 2026
31 checks passed
@tarekziade tarekziade deleted the tarekziade-import-verif branch March 31, 2026 07:01
sirzechs66 pushed a commit to sirzechs66/transformers that referenced this pull request Mar 31, 2026
* added import complexity checker

* lazy torch

* lower the threashold

* simplified checks

* raise threshold
SangbumChoi pushed a commit to SangbumChoi/transformers that referenced this pull request Apr 4, 2026
* added import complexity checker

* lazy torch

* lower the threashold

* simplified checks

* raise threshold
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.

4 participants