Files
transformers/docs/source/en/internal/import_utils.md
陈赣 06f1fd69a6
Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
first commit
2026-06-05 16:53:03 +08:00

3.9 KiB

Import Utilities

This page goes through the transformers utilities to enable lazy and fast object import. While we strive for minimal dependencies, some models have specific dependencies requirements that cannot be worked around. We don't want for all users of transformers to have to install those dependencies to use other models, we therefore mark those as soft dependencies rather than hard dependencies.

The transformers toolkit is not made to error-out on import of a model that has a specific dependency; instead, an object for which you are lacking a dependency will error-out when calling any method on it. As an example, if torchvision isn't installed, the fast image processors will not be available.

This object is still importable:

>>> from transformers import DetrImageProcessor
>>> print(DetrImageProcessor)
<class 'DetrImageProcessor'>

However, no method can be called on that object:

>>> DetrImageProcessor.from_pretrained()
ImportError:
DetrImageProcessor requires the Torchvision library but it was not found in your environment. Check out the instructions on the
installation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.
Please note that you may need to restart your runtime after installation.

Let's see how to specify specific object dependencies.

Specifying Object Dependencies

Filename-based

All objects under a given filename have an automatic dependency to the tool linked to the filename

PyTorch: All files starting with modeling_ have an automatic PyTorch dependency

Tokenizers: All files starting with tokenization_ and ending with _fast have an automatic tokenizers dependency

Vision: All files starting with image_processing_ have an automatic dependency to the vision dependency group; at the time of writing, this only contains the pillow dependency.

Vision + Torch + Torchvision: All files starting with image_processing_ and ending with _fast have an automatic dependency to vision, torch, and torchvision.

All of these automatic dependencies are added on top of the explicit dependencies that are detailed below.

Explicit Object Dependencies

We add a method called requires that is used to explicitly specify the dependencies of a given object. As an example, the Trainer class has two hard dependencies: torch and accelerate. Here is how we specify these required dependencies:

from .utils.import_utils import requires

@requires(backends=("torch", "accelerate"))
class Trainer:
    ...

Backends that can be added here are all the backends that are available in the import_utils.py module.

Additionally, specific versions can be specified in each backend. For example, this is how you would specify a requirement on torch>=2.6 on the Trainer class:

from .utils.import_utils import requires

@requires(backends=("torch>=2.6", "accelerate"))
class Trainer:
    ...

You can specify the following operators: ==, >, >=, <, <=, !=.

Methods

autodoc utils.import_utils.define_import_structure

autodoc utils.import_utils.requires

autodoc utils.import_utils.requires_backends