Cannot import torch after a succesful custom pytorch build

Hello, I’m experiencing the following trouble:

Python 3.11.1 (main, Jan  6 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/path/pytorch/torch/__init__.py", line 932, in <module>
    from ._tensor import Tensor
  File "/home/user/path/pytorch/torch/_tensor.py", line 12, in <module>
    import torch.utils.hooks as hooks
  File "/home/user/path/pytorch/torch/utils/__init__.py", line 7, in <module>
    from .backend_registration import rename_privateuse1_backend
  File "/home/user/path/pytorch/torch/utils/backend_registration.py", line 1, in <module>
    from torch._C import _rename_privateuse1_backend
ImportError: cannot import name '_rename_privateuse1_backend' from 'torch._C' (/home/user/path/pytorch/torch/_C.cpython-311-x86_64-linux-gnu.so)
>>> 

I used this building setup when compiling pytorch:
BUILD_TEST=0 CXX="/home/user/path/gcc_custom/bin/g++" CC="/home/user/path/gcc_custom/bin/gcc" CMAKE_CXX_FLAGS="-march=znver3 -fPIE" python setup.py develop --install-dir ~/.local/lib/python3.11/site-packages/

I had the pytorch switched to nightly branch (git 0a73b08b9f2)

I used gcc 11.1.0 and cuda 12.0 toolkit to link up to. The built was successful without any issues (at least as I could see).

I tried to inspect the**_C.cpython-311-x86_64-linux-gnu.so** and I was surprised that basically all of the static functions from torch/csrc/Module.cpp are missing (or perhaps I’m looking at wrong place, I’m not sure).

Has anyone solved this kind of problem. Or does anybody already know what I’m doing wrong from the description of the issue?

Thanks a lot

Are you running your REPL in the source directory?

 File "/home/user/path/pytorch/torch/utils/backend_registration.py", line 1, in <module>
    from torch._C import _rename_privateuse1_backend
ImportError: cannot import name '_rename_privateuse1_backend' from 'torch._C' (/home/user/path/pytorch/torch/_C.cpython-311-x86_64-linux-gnu.so)

The error points to source files located in your /home while you were trying to install it into a site-packages folder located in your /home
If so, switch the wdir and check if this would fix the error.

1 Like

Thank you a lot! I was staring at it for too long. You were right about wdir; and probably I had also have LD_LIBRARY_PATH crippled too. Thanks once again

1 Like

In my case, it appears to be that LD_LIBRARY_PATH was wrongly set, two versions of pythons were installed. Removing the unnecessary one will do the work.