Import PyTorch / torch in build 2021.39010?

How should I import torch?
I’m on 2021.39010 on Windows.
I installed Python 3.9 to C:/Python39.
I installed torch version 1.10.2+cu113 and can import it outside of TouchDesigner.

Inside TouchDesigner, I have a DAT:

import sys

custompath = "C:/Python39/Lib/site-packages"
if sys.path[0] != custompath:
	sys.path.insert(0, custompath)

custompath = "C:/Python39/Lib"
if sys.path[1] != custompath:
	sys.path.insert(1, custompath)

#sys.path.remove('C:\\Program Files\\Derivative\\TouchDesigner.2021.39010\\bin\\lib\\site-packages')

for a in sys.path:
	print(a)

import torch

output:

C:/Python39/Lib/site-packages
C:/Python39/Lib
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin\python39.zip
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin\DLLs
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin\lib
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin\lib\site-packages
C:/Program Files/Derivative/TouchDesigner.2021.39010/Config/Cmd
C:/Python39/Lib/site-packages
C:/Python39/Lib/site-packages
Traceback (most recent call last):
  File "</project1/text1:op('/project1/text1').run()>", line 1
td.tdError: File "/project1/text1", line 16
    r = previousimport(*args, **kw)
  File "C:\Python39/Lib/site-packages\torch\__init__.py", line 16
    import ctypes
    r = previousimport(*args, **kw)
  File "C:\Python39/Lib\ctypes\__init__.py", line 8
    from _ctypes import Union, Structure, Array
    r = previousimport(*args, **kw)
ImportError: DLL load failed while importing _ctypes: The specified module could not be found.
Results of run operation resulted in exception.
python >>> 
1 Like

hey @DavidBraun , at first sight this looks like the same bug from this thread where importing ctypes does not work in experimental.
This should be fixed in 2021.39070 and later.

Hi
I tried to import torch with the official version.
I had same errors but I modified paths in a file in torch site package (I don’t remember which cause I don’t have my computer) I think it was init.py cause when I print the paths to dlls it tries to go in touchdesigner dlls folder. After modifications ( I hard coded paths in that file to try) torch imported more dlls but failed to import all but I think that should work. It failed for me cause I think I installed cuda 11.4 to use onnx runtime, but torch waits cuda 11.3…
You can try change the paths, maybe that should work…

When is this build scheduled to be available? Might solve a lot of issues…

(Or is it already for commercial/prof versions?)

Hey @Drake_Rubicon

It should be available soon. For any license type.

Best,
Michel

2022.20150 is now available with this fix.

Thanks. On 2022.20150 Windows I’m able to import ctypes but import torch results in a hang without anything printed to textport or console, or saved to Desktop or .dmp.

Hey @DavidBraun

Are you importing torch with CUDA / CuDNN support ?

Are you importing torch from a Conda environment or similar ?

Thanks,
Michel

I think it will be trying to import the CUDA support with 11.3. I have an ordinary non-conda installation at C:/Python39

Hey @DavidBraun

Can you please share your import script? If there is anything specific? Or are you just calling import torch ?

How are you adding C:/Python39 to the PATH ?

Thanks,
Michel

I’ve tried the script in my first post and also separately just import torch. Both freeze TouchDesigner in the same way.

My Windows system PATH includes both C:/Python39 and C:/Python39/scripts. My TouchDesigner preferences for Python 64-bit Module Path is “C:/Python39/Lib/site-packages”

@DavidBraun Can you add those to an onStart() Exec DAT and see if that helps?

import os
import sys
os.add_dll_directory('C:\\Python39\\DLLs')
os.add_dll_directory('C:\\Python39\\Library\\bin') # If you have this folder
sys.path = ['C:\\Python39\\Lib\\site-packages'] + sys.path # Optional if the first 2 lines here are not enough, disable your TD Module Path preferences though

If that works or help at all, or bring another issue please let me know.

Edit: FYI, as per os — Miscellaneous operating system interfaces — Python 3.12.1 documentation

Add a path to the DLL search path.

This search path is used when resolving dependencies for imported extension modules (the module itself is resolved through sys.path), and also by ctypes.

Remove the directory by calling close() on the returned object or using it in a with statement.

See the Microsoft documentation for more information about how DLLs are loaded.

Raises an auditing event os.add_dll_directory with argument path.

Availability: Windows.

New in version 3.8: Previous versions of CPython would resolve DLLs using the default behavior for the current process. This led to inconsistencies, such as only sometimes searching PATH or the current working directory, and OS functions such as AddDllDirectory having no effect.

In 3.8, the two primary ways DLLs are loaded now explicitly override the process-wide behavior to ensure consistency. See the porting notes for information on updating libraries.

For me C:\\Python39\\Library\\bin doesn’t exist, so I skipped that. The other two things didn’t fix it.

The CUDA DLLs are in C:\\Python39\\Lib\\site-packages\\torch\\lib so I also tried

sys.path = ['C:\\Python39\\Lib\\site-packages\\torch\\lib'] + sys.path
for p in sys.path:
    print(p)
import torch

but that also leads to a freeze.

But if you just open a console and call python import torch outside of the TouchDesigner context it all works ?


Long shot… but by any chance, can you do an install of Anaconda and try to import with anaconda ? My guide for anaconda is here: Anaconda - Managing Python Environments and 3rd-Party Libraries in TouchDesigner | Derivative

If you give it a try, make sure to use the following when installing Pytorch in a new conda env (the version of that conda env should match TD’s python version, just to be safe)

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

Thanks,
Michel

yes python import torch works outside TouchDesigner.

I tried miniconda with 3.9.5 and 2022.20150 but it froze when importing torch.
Then I tried miniconda with 3.7.2 and 2021.16410 and everything is working.

Also, in the tutorial you shared, there’s a typo with &gt; instead of “>”.

Ok, I’ll try to make an easy to reproduce case on my end for developers to debug.

Thanks for taking the time to check.

Some issues with the character in the code formatting tool we use sadly.

Hey @DavidBraun

Do you have the hang occurring on import torch exactly, or do you have lines related to torch coming after?

I am trying to reproduce and I am not encountering any issues at the moment.

When installing Pytorch using Conda, or Pip, what CUDA version did you go with ?

What is your GPU / Driver version ?

If I remember correctly, sm_86 only works with CUDA 11.1+
Edit: Your original post mention cu113 so ignore that previous line.

Best,
Michel

Edit:

Those worked properly as well:

python >>> print(torch.rand(2,3).cuda())
tensor([[0.3321, 0.9165, 0.4908],
        [0.3690, 0.8679, 0.6116]], device='cuda:0')
python >>> test = torch.FloatTensor([0., 1., 2.])
python >>> test
tensor([0., 1., 2.])

Yes I’m only trying import torch. My GPU is NVIDIA RTX 2080 with driver 511.79. Maybe I’ll try a clean re-install of Python and TouchDesigner at some point.

You can also send your project at support and I can give it a quick look to see if I spot anything odd, if you feel like it.

Technically… You could say that the Conda install is a clean install…

I’m having a similar issue with pytorch + cuda on python 3.11 and td 2023.12.

seems to work fine without gpu but if I try to use pytorch + cuda I get error that seems to be common.

OSError: [WinError 127] The specified procedure could not be found. Error loading "C:\Users/davis/anaconda3/envs/td-demo-ptorch/Lib/site-packages\torch\lib\shm.dll" or one of its dependencies.

Have tried several of the work arounds but they don’t work.