pytorch, AttributeError: module 'torch' has no attribute '_utils_internal'

Viewed 1611

I need to test some fastai models in an environment without GPU, specifically in a windows server. I trained some fastai models in Google colab, and now need to test them in real time, connected to an industrial process. Nevertheless I'm limited with hardware.

In Google colab, I worked with this configuration.

from fastai.vision import *
from fastai.metrics import error_rate
from PIL import Image as PImage
import numpy as np
import cv2
import os
import pandas as pd

The first line is the onlyone that has the trouble is the first one, and I get this error:

  ~\.conda\envs\fastai_1\lib\site-packages\fastai\vision\__init__.py", line 3, in <module>
    from .learner import *

  ~\.conda\envs\fastai_1\lib\site-packages\fastai\vision\learner.py", line 6, in <module>
    from . import models

  ~\.conda\envs\fastai_1\lib\site-packages\fastai\vision\models\__init__.py", line 2, in <module>
    from torchvision.models import ResNet,resnet18,resnet34,resnet50,resnet101,resnet152

  ~\.conda\envs\fastai_1\lib\site-packages\torchvision\__init__.py", line 4, in <module>
    from .extension import _HAS_OPS

  ~\.conda\envs\fastai_1\lib\site-packages\torchvision\extension.py", line 51, in <module>
    _register_extensions()

  ~\.conda\envs\fastai_1\lib\site-packages\torchvision\extension.py", line 47, in _register_extensions
    torch.ops.load_library(ext_specs.origin)

  ~\.conda\envs\fastai_1\lib\site-packages\torch\_ops.py", line 99, in load_library
    path = torch._utils_internal.resolve_library_path(path)

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

I had to installed Pytorch with conda as was suggested by pytorch start locally https://pytorch.org/get-started/locally/, but for fastai I had to installed using pip. Conda doesn't work in my limited hardware in the server

# for Pytorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
# for fastai
python -m pip install fastai==1.0.61

My torch version is 1.9.0 My python version is 3.7.7

I checked several similar threads, with different attribute that is not found, but, all are related to that specific error, not '_utils_internal'

I appreciate any help

1 Answers

I experienced the same problem (Pytorch 1.11.0, Torchvision 0.12.0, CUDA 11.3, Python 3.10) and came around with a somewhat "hacky" solution.

First I checked the file _ops.py in the given location, it contained indeed the statements and function:

import torch._utils_internal
...
path = torch._utils_internal.resolve_library_path(path)

Also _utils_internal.py was available and contained the statements and function:

import os
...
def resolve_library_path(path: str) -> str:
    return os.path.realpath(path)

As I didn't really understand, what the issue was, I just decided to copy the returned expression into the _ops.py file and add the os import. So this now contains:

import os
...
path = os.path.realpath(path)

And it works!

Related