|
| 1 | +<!--Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +# Import Utilities |
| 18 | + |
| 19 | +This page goes through the transformers utilities to enable lazy and fast object import. |
| 20 | +While we strive for minimal dependencies, some models have specific dependencies requirements that cannot be |
| 21 | +worked around. We don't want for all users of `transformers` to have to install those dependencies to use other models, |
| 22 | +we therefore mark those as soft dependencies rather than hard dependencies. |
| 23 | + |
| 24 | +The transformers toolkit is not made to error-out on import of a model that has a specific dependency; instead, an |
| 25 | +object for which you are lacking a dependency will error-out when calling any method on it. As an example, if |
| 26 | +`torchvision` isn't installed, the fast image processors will not be available. |
| 27 | + |
| 28 | +This object is still importable: |
| 29 | + |
| 30 | +```python |
| 31 | +>>> from transformers import DetrImageProcessorFast |
| 32 | +>>> print(DetrImageProcessorFast) |
| 33 | +<class 'DetrImageProcessorFast'> |
| 34 | +``` |
| 35 | + |
| 36 | +However, no method can be called on that object: |
| 37 | + |
| 38 | +```python |
| 39 | +>>> DetrImageProcessorFast.from_pretrained() |
| 40 | +ImportError: |
| 41 | +DetrImageProcessorFast requires the Torchvision library but it was not found in your environment. Checkout the instructions on the |
| 42 | +installation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment. |
| 43 | +Please note that you may need to restart your runtime after installation. |
| 44 | +``` |
| 45 | + |
| 46 | +Let's see how to specify specific object dependencies. |
| 47 | + |
| 48 | +## Specifying Object Dependencies |
| 49 | + |
| 50 | +### Filename-based |
| 51 | + |
| 52 | +All objects under a given filename have an automatic dependency to the tool linked to the filename |
| 53 | + |
| 54 | +**TensorFlow**: All files starting with `modeling_tf_` have an automatic TensorFlow dependency. |
| 55 | + |
| 56 | +**Flax**: All files starting with `modeling_flax_` have an automatic Flax dependency |
| 57 | + |
| 58 | +**PyTorch**: All files starting with `modeling_` and not valid with the above (TensorFlow and Flax) have an automatic |
| 59 | +PyTorch dependency |
| 60 | + |
| 61 | +**Tokenizers**: All files starting with `tokenization_` and ending with `_fast` have an automatic `tokenizers` dependency |
| 62 | + |
| 63 | +**Vision**: All files starting with `image_processing_` have an automatic dependency to the `vision` dependency group; |
| 64 | +at the time of writing, this only contains the `pillow` dependency. |
| 65 | + |
| 66 | +**Vision + Torch + Torchvision**: All files starting with `image_processing_` and ending with `_fast` have an automatic |
| 67 | +dependency to `vision`, `torch`, and `torchvision`. |
| 68 | + |
| 69 | +All of these automatic dependencies are added on top of the explicit dependencies that are detailed below. |
| 70 | + |
| 71 | +### Explicit Object Dependencies |
| 72 | + |
| 73 | +We add a method called `requires` that is used to explicitly specify the dependencies of a given object. As an |
| 74 | +example, the `Trainer` class has two hard dependencies: `torch` and `accelerate`. Here is how we specify these |
| 75 | +required dependencies: |
| 76 | + |
| 77 | +```python |
| 78 | +from .utils.import_utils import requires |
| 79 | + |
| 80 | +@requires(backends=("torch", "accelerate")) |
| 81 | +class Trainer: |
| 82 | + ... |
| 83 | +``` |
| 84 | + |
| 85 | +Backends that can be added here are all the backends that are available in the `import_utils.py` module. |
| 86 | + |
| 87 | +## Methods |
| 88 | + |
| 89 | +[[autodoc]] import_utils.define_import_structure |
| 90 | + |
| 91 | +[[autodoc]] import_utils.requires |
0 commit comments