AttributeError: module 'torch' has no attribute '_utils'

I installed

pip3 install torch torchvision torchaudio --index-url

also my nvcc --version is 12.1

and this is my system details.

However, when I run the code it shown

AttributeError: module ‘torch’ has no attribute ‘_utils’

So I tried to run conda install pytorch torchvision torchaudio cudatoolkit=11.1 as I asked chatGPT but it still show same issue.

The question is what should I import or install to fix this issue.

Please help. Thank you in advanced.

Could you describe when this error is raised?
I can directly access this module in 2.0.0:

import torch
# 2.0.0+cu118
# <module 'torch._utils' from ...

but also note that it’s an internal module.

Could you post the full stacktrace here?

Based on the stacktrace it seems the error is raised from here. However, I cannot reproduce the error by initializing the model via:

model = models.segmentation.deeplabv3_resnet50(pretrained=False, num_classes=19, output_stride=8)

so would need to get an executable code snippet to debug it further.

As of today, I am getting a similar error where I did not previously get any errors:

Torch version is 1.13.1, torchvision 0.14.1

from torchvision import models


AttributeError                            Traceback (most recent call last)
/var/folders/d8/265wdp1n0bn_r85dh3pp95fh0000gq/T/ipykernel_64242/ in <cell line: 2>()
      1 from torchvision import models
----> 2 models.resnet34(weights='DEFAULT')

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/ in wrapper(*args, **kwargs)
    140             kwargs.update(keyword_only_kwargs)
--> 142         return fn(*args, **kwargs)
    144     return wrapper

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/ in inner_wrapper(*args, **kwargs)
    226                 kwargs[weights_param] = default_weights_arg
--> 228             return builder(*args, **kwargs)
    230         return inner_wrapper

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/ in resnet34(weights, progress, **kwargs)
    695     weights = ResNet34_Weights.verify(weights)
--> 697     return _resnet(BasicBlock, [3, 4, 6, 3], weights, progress, **kwargs)

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/ in _resnet(block, layers, weights, progress, **kwargs)
    300     if weights is not None:
--> 301         model.load_state_dict(weights.get_state_dict(progress=progress))
    303     return model

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/ in get_state_dict(self, progress)
     65     def get_state_dict(self, progress: bool) -> Mapping[str, Any]:
---> 66         return load_state_dict_from_url(self.url, progress=progress)
     68     def __repr__(self) -> str:

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torch/ in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash, file_name)
    733     if _is_legacy_zip_format(cached_file):
    734         return _legacy_zip_load(cached_file, model_dir, map_location)
--> 735     return torch.load(cached_file, map_location=map_location)

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torch/ in load(f, map_location, pickle_module, weights_only, **pickle_load_args)
    787                     except RuntimeError as e:
    788                         raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None
--> 789                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
    790         if weights_only:
    791             try:

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torch/ in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
   1129     unpickler = UnpicklerWrapper(data_file, **pickle_load_args)
   1130     unpickler.persistent_load = persistent_load
-> 1131     result = unpickler.load()
   1133     torch._utils._validate_loaded_sparse_tensors()

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torch/ in persistent_load(saved_id)
   1099         if key not in loaded_storages:
-> 1100             nbytes = numel * torch._utils._element_size(dtype)
   1101             load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))

AttributeError: module 'torch' has no attribute '_utils'

Downgrading to torchvision 0.14.0 or upgrading torchvision to 0.15.1 both resolve this error