Skip to content

partially initialized module 'torchvision' has no attribute 'extension' #1793

Closed

Description

@z-x-x136

from unsloth import FastLanguageModel
import torch


AttributeError Traceback (most recent call last)
File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/utils/import_utils.py:1863, in _LazyModule._get_module(self, module_name)
1862 try:
-> 1863 return importlib.import_module("." + module_name, self.name)
1864 except Exception as e:

File ~/miniconda3/envs/unsloth_env/lib/python3.11/importlib/init.py:126, in import_module(name, package)
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)

File :1204, in _gcd_import(name, package, level)

File :1176, in find_and_load(name, import)

File :1147, in find_and_load_unlocked(name, import)

File :690, in _load_unlocked(spec)

File :940, in exec_module(self, module)

File :241, in _call_with_frames_removed(f, *args, **kwds)

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/modeling_utils.py:53
52 from .integrations.sdpa_attention import sdpa_attention_forward
---> 53 from .loss.loss_utils import LOSS_MAPPING
54 from .pytorch_utils import ( # noqa: F401
55 Conv1D,
56 apply_chunking_to_forward,
(...)
62 translate_to_torch_parallel_style,
63 )

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/loss/loss_utils.py:19
17 from torch.nn import BCEWithLogitsLoss, MSELoss
---> 19 from .loss_deformable_detr import DeformableDetrForObjectDetectionLoss, DeformableDetrForSegmentationLoss
20 from .loss_for_object_detection import ForObjectDetectionLoss, ForSegmentationLoss

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/loss/loss_deformable_detr.py:4
2 import torch.nn as nn
----> 4 from ..image_transforms import center_to_corners_format
5 from ..utils import is_scipy_available

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/image_transforms.py:22
20 import numpy as np
---> 22 from .image_utils import (
23 ChannelDimension,
24 ImageInput,
25 get_channel_dimension_axis,
26 get_image_size,
27 infer_channel_dimension_format,
28 )
29 from .utils import ExplicitEnum, TensorType, is_jax_tensor, is_tf_tensor, is_torch_tensor

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/image_utils.py:65
64 if is_torchvision_available():
---> 65 from torchvision import io as torchvision_io
66 from torchvision.transforms import InterpolationMode

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/torchvision/init.py:10
9 from .extension import _HAS_OPS # usort:skip
---> 10 from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils # usort:skip
12 try:

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/torchvision/_meta_registrations.py:25
22 return wrapper
---> 25 @register_meta("roi_align")
26 def meta_roi_align(input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio, aligned):
27 torch._check(rois.size(1) == 5, lambda: "rois must have shape as Tensor[K, 5]")

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/torchvision/_meta_registrations.py:18, in register_meta..wrapper(fn)
17 def wrapper(fn):
---> 18 if torchvision.extension._has_ops():
19 get_meta_lib().impl(getattr(getattr(torch.ops.torchvision, op_name), overload_name), fn)

AttributeError: partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import)

The above exception was the direct cause of the following exception:

RuntimeError Traceback (most recent call last)
File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/utils/import_utils.py:1863, in _LazyModule._get_module(self, module_name)
1862 try:
-> 1863 return importlib.import_module("." + module_name, self.name)
1864 except Exception as e:

File ~/miniconda3/envs/unsloth_env/lib/python3.11/importlib/init.py:126, in import_module(name, package)
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)

File :1204, in _gcd_import(name, package, level)

File :1176, in find_and_load(name, import)

File :1147, in find_and_load_unlocked(name, import)

File :690, in _load_unlocked(spec)

File :940, in exec_module(self, module)

File :241, in _call_with_frames_removed(f, *args, **kwds)

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/integrations/integration_utils.py:36
34 import packaging.version
---> 36 from .. import PreTrainedModel, TFPreTrainedModel
37 from .. import version as version

File :1229, in handle_fromlist(module, fromlist, import, recursive)

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/utils/import_utils.py:1851, in _LazyModule.getattr(self, name)
1850 elif name in self._class_to_module.keys():
-> 1851 module = self._get_module(self._class_to_module[name])
1852 value = getattr(module, name)

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/utils/import_utils.py:1865, in _LazyModule._get_module(self, module_name)
1864 except Exception as e:
-> 1865 raise RuntimeError(
1866 f"Failed to import {self.name}.{module_name} because of the following error (look up to see its"
1867 f" traceback):\n{e}"
1868 ) from e

RuntimeError: Failed to import transformers.modeling_utils because of the following error (look up to see its traceback):
partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import)

The above exception was the direct cause of the following exception:

RuntimeError Traceback (most recent call last)
Cell In[10], line 1
----> 1 from unsloth import FastLanguageModel
2 import torch

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/init.py:212
209 raise ImportError("Unsloth: Please install unsloth_zoo via pip install unsloth_zoo")
210 pass
--> 212 from .models import *
213 from .save import *
214 from .chat_templates import *

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/models/init.py:16
1 # Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
(...)
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
---> 16 from .granite import FastGraniteModel
17 from .loader import FastLanguageModel, FastVisionModel
18 from .llama import FastLlamaModel

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/models/granite.py:15
1 # Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
(...)
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
---> 15 from .llama import *
16 import os
17 from ._utils import version

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/models/llama.py:20
18 from functools import partial
19 from typing import Optional, Tuple, List, Union
---> 20 from ._utils import *
21 from ._utils import version
22 from torch.nn.functional import scaled_dot_product_attention

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/models/_utils.py:147
144 del transformers_training_args_logger
146 # No label_names provided for model class
--> 147 from transformers.trainer import logger as transformers_trainer_logger
148 transformers_trainer_logger.addFilter(HideLoggingMessage("No label_names"))
149 del transformers_trainer_logger

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/trainer.py:42
37 from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Type, Union
40 # Integrations must be imported before ML frameworks:
41 # isort: off
---> 42 from .integrations import (
43 get_reporting_integration_callbacks,
44 )
46 # isort: on
48 import huggingface_hub.utils as hf_hub_utils

File :1229, in handle_fromlist(module, fromlist, import, recursive)

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/utils/import_utils.py:1851, in _LazyModule.getattr(self, name)
1849 value = Placeholder
1850 elif name in self._class_to_module.keys():
-> 1851 module = self._get_module(self._class_to_module[name])
1852 value = getattr(module, name)
1853 elif name in self._modules:

File ~/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/transformers/utils/import_utils.py:1865, in _LazyModule._get_module(self, module_name)
1863 return importlib.import_module("." + module_name, self.name)
1864 except Exception as e:
-> 1865 raise RuntimeError(
1866 f"Failed to import {self.name}.{module_name} because of the following error (look up to see its"
1867 f" traceback):\n{e}"
1868 ) from e

RuntimeError: Failed to import transformers.integrations.integration_utils because of the following error (look up to see its traceback):
Failed to import transformers.modeling_utils because of the following error (look up to see its traceback):
partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import)

Activity

  1. z-x-x136 commented on Feb 22, 2025

    @z-x-x136
    Author

    torch 2.6.0
    torchaudio 2.4.1+cu121
    torchvision 0.21.0
    unsloth 2025.2.15
    unsloth_zoo 2025.2.7
    uri-template 1.3.0
    urllib3 2.3.0
    wcwidth 0.2.13
    webcolors 24.11.1
    webencodings 0.5.1
    websocket-client 1.8.0
    wheel 0.45.1
    widgetsnbextension 4.0.13
    xformers 0.0.28.post1
    xxhash 3.5.0
    yarl 1.18.3
    zipp 3.21.0

  2. z-x-x136 commented on Feb 22, 2025

    @z-x-x136
    Author

    cuda12.1 linux

  3. z-x-x136 commented on Feb 22, 2025

    @z-x-x136
    Author

    python=3.11

  4. horiacristescu commented on Mar 6, 2025

    @horiacristescu

    I tried dozens of times and I can't install unsloth in Azure or GCP in a VM with GPU.

  5. ItzAmirreza commented on Mar 10, 2025

    @ItzAmirreza

    Getting the same error on raw continues fine tuning:

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    File /opt/conda/lib/python3.10/site-packages/transformers/utils/import_utils.py:1863, in _LazyModule._get_module(self, module_name)
       1862 try:
    -> 1863     return importlib.import_module("." + module_name, self.__name__)
       1864 except Exception as e:
    
    File /opt/conda/lib/python3.10/importlib/__init__.py:126, in import_module(name, package)
        125         level += 1
    --> 126 return _bootstrap._gcd_import(name[level:], package, level)
    
    File <frozen importlib._bootstrap>:1050, in _gcd_import(name, package, level)
    
    File <frozen importlib._bootstrap>:1027, in _find_and_load(name, import_)
    
    File <frozen importlib._bootstrap>:1006, in _find_and_load_unlocked(name, import_)
    
    File <frozen importlib._bootstrap>:688, in _load_unlocked(spec)
    
    File <frozen importlib._bootstrap_external>:883, in exec_module(self, module)
    
    File <frozen importlib._bootstrap>:241, in _call_with_frames_removed(f, *args, **kwds)
    
    File /opt/conda/lib/python3.10/site-packages/transformers/modeling_utils.py:53
         52 from .integrations.sdpa_attention import sdpa_attention_forward
    ---> 53 from .loss.loss_utils import LOSS_MAPPING
         54 from .pytorch_utils import (  # noqa: F401
         55     Conv1D,
         56     apply_chunking_to_forward,
       (...)
         62     translate_to_torch_parallel_style,
         63 )
    
    File /opt/conda/lib/python3.10/site-packages/transformers/loss/loss_utils.py:19
         17 from torch.nn import BCEWithLogitsLoss, MSELoss
    ---> 19 from .loss_deformable_detr import DeformableDetrForObjectDetectionLoss, DeformableDetrForSegmentationLoss
         20 from .loss_for_object_detection import ForObjectDetectionLoss, ForSegmentationLoss
    
    File /opt/conda/lib/python3.10/site-packages/transformers/loss/loss_deformable_detr.py:4
          2 import torch.nn as nn
    ----> 4 from ..image_transforms import center_to_corners_format
          5 from ..utils import is_scipy_available
    
    File /opt/conda/lib/python3.10/site-packages/transformers/image_transforms.py:22
         20 import numpy as np
    ---> 22 from .image_utils import (
         23     ChannelDimension,
         24     ImageInput,
         25     get_channel_dimension_axis,
         26     get_image_size,
         27     infer_channel_dimension_format,
         28 )
         29 from .utils import ExplicitEnum, TensorType, is_jax_tensor, is_tf_tensor, is_torch_tensor
    
    File /opt/conda/lib/python3.10/site-packages/transformers/image_utils.py:65
         64 if is_torchvision_available():
    ---> 65     from torchvision import io as torchvision_io
         66     from torchvision.transforms import InterpolationMode
    
    File /opt/conda/lib/python3.10/site-packages/torchvision/__init__.py:6
          5 import torch
    ----> 6 from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils
          8 from .extension import _HAS_OPS
    
    File /opt/conda/lib/python3.10/site-packages/torchvision/_meta_registrations.py:26
         22     return wrapper
         25 @register_meta("roi_align")
    ---> 26 def meta_roi_align(input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio, aligned):
         27     torch._check(rois.size(1) == 5, lambda: "rois must have shape as Tensor[K, 5]")
    
    File /opt/conda/lib/python3.10/site-packages/torchvision/_meta_registrations.py:18, in register_meta.<locals>.wrapper(fn)
         17 def wrapper(fn):
    ---> 18     if torchvision.extension._has_ops():
         19         get_meta_lib().impl(getattr(getattr(torch.ops.torchvision, op_name), overload_name), fn)
    
    AttributeError: partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import)
    
    The above exception was the direct cause of the following exception:
    
    RuntimeError                              Traceback (most recent call last)
    File /opt/conda/lib/python3.10/site-packages/transformers/utils/import_utils.py:1863, in _LazyModule._get_module(self, module_name)
       1862 try:
    -> 1863     return importlib.import_module("." + module_name, self.__name__)
       1864 except Exception as e:
    
    File /opt/conda/lib/python3.10/importlib/__init__.py:126, in import_module(name, package)
        125         level += 1
    --> 126 return _bootstrap._gcd_import(name[level:], package, level)
    
    File <frozen importlib._bootstrap>:1050, in _gcd_import(name, package, level)
    
    File <frozen importlib._bootstrap>:1027, in _find_and_load(name, import_)
    
    File <frozen importlib._bootstrap>:1006, in _find_and_load_unlocked(name, import_)
    
    File <frozen importlib._bootstrap>:688, in _load_unlocked(spec)
    
    File <frozen importlib._bootstrap_external>:883, in exec_module(self, module)
    
    File <frozen importlib._bootstrap>:241, in _call_with_frames_removed(f, *args, **kwds)
    
    File /opt/conda/lib/python3.10/site-packages/transformers/integrations/integration_utils.py:36
         34 import packaging.version
    ---> 36 from .. import PreTrainedModel, TFPreTrainedModel
         37 from .. import __version__ as version
    
    File <frozen importlib._bootstrap>:1075, in _handle_fromlist(module, fromlist, import_, recursive)
    
    File /opt/conda/lib/python3.10/site-packages/transformers/utils/import_utils.py:1851, in _LazyModule.__getattr__(self, name)
       1850 elif name in self._class_to_module.keys():
    -> 1851     module = self._get_module(self._class_to_module[name])
       1852     value = getattr(module, name)
    
    File /opt/conda/lib/python3.10/site-packages/transformers/utils/import_utils.py:1865, in _LazyModule._get_module(self, module_name)
       1864 except Exception as e:
    -> 1865     raise RuntimeError(
       1866         f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
       1867         f" traceback):\n{e}"
       1868     ) from e
    
    RuntimeError: Failed to import transformers.modeling_utils because of the following error (look up to see its traceback):
    partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import)
    
    The above exception was the direct cause of the following exception:
    
    RuntimeError                              Traceback (most recent call last)
    Cell In[4], line 1
    ----> 1 from unsloth import FastLanguageModel
          2 import torch
          3 max_seq_length = 5048 # Choose any! We auto support RoPE Scaling internally!
    
    File /opt/conda/lib/python3.10/site-packages/unsloth/__init__.py:214
        211     raise ImportError("Unsloth: Please install unsloth_zoo via `pip install unsloth_zoo`")
        212 pass
    --> 214 from .models import *
        215 from .models import __version__
        216 from .save import *
    
    File /opt/conda/lib/python3.10/site-packages/unsloth/models/__init__.py:15
          1 # Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
          2 #
          3 # Licensed under the Apache License, Version 2.0 (the "License");
       (...)
         12 # See the License for the specific language governing permissions and
         13 # limitations under the License.
    ---> 15 from .llama   import FastLlamaModel
         16 from .loader  import FastLanguageModel, FastVisionModel, FastTextModel, FastModel
         17 from .mistral import FastMistralModel
    
    File /opt/conda/lib/python3.10/site-packages/unsloth/models/llama.py:20
         18 import functools
         19 from typing import Optional, Tuple, List, Union
    ---> 20 from ._utils import *
         21 from ._utils import patch_unsloth_smart_gradient_checkpointing
         22 from ._utils import __version__
    
    File /opt/conda/lib/python3.10/site-packages/unsloth/models/_utils.py:145
        142 del transformers_training_args_logger
        144 # No label_names provided for model class
    --> 145 from transformers.trainer import logger as transformers_trainer_logger
        146 transformers_trainer_logger.addFilter(HideLoggingMessage("No label_names"))
        147 del transformers_trainer_logger
    
    File /opt/conda/lib/python3.10/site-packages/transformers/trainer.py:42
         37 from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Type, Union
         40 # Integrations must be imported before ML frameworks:
         41 # isort: off
    ---> 42 from .integrations import (
         43     get_reporting_integration_callbacks,
         44 )
         46 # isort: on
         48 import huggingface_hub.utils as hf_hub_utils
    
    File <frozen importlib._bootstrap>:1075, in _handle_fromlist(module, fromlist, import_, recursive)
    
    File /opt/conda/lib/python3.10/site-packages/transformers/utils/import_utils.py:1851, in _LazyModule.__getattr__(self, name)
       1849     value = Placeholder
       1850 elif name in self._class_to_module.keys():
    -> 1851     module = self._get_module(self._class_to_module[name])
       1852     value = getattr(module, name)
       1853 elif name in self._modules:
    
    File /opt/conda/lib/python3.10/site-packages/transformers/utils/import_utils.py:1865, in _LazyModule._get_module(self, module_name)
       1863     return importlib.import_module("." + module_name, self.__name__)
       1864 except Exception as e:
    -> 1865     raise RuntimeError(
       1866         f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
       1867         f" traceback):\n{e}"
       1868     ) from e
    
    RuntimeError: Failed to import transformers.integrations.integration_utils because of the following error (look up to see its traceback):
    Failed to import transformers.modeling_utils because of the following error (look up to see its traceback):
    partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions