Comfy-Org / Comfy-Org/ComfyUI

Python 3.12: Torch CUDA extensions fail to import unless torch DLL directory is registered

Open
#12,875 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
133k
Forks
15.7k
Avg merge
1d 7h
Merged PRs (30d)
158

Description

## Python 3.12: Torch CUDA extensions fail to import unless torch DLL directory is registered

### Description

When using **Python 3.12 + PyTorch 2.10 (CUDA 13)**, compiled CUDA extensions fail to load with:

```
ImportError: DLL load failed while importing : The specified module could not be found.
```

Example extension:

```
custom_rasterizer_kernel.cp312-win_amd64.pyd
```

The extension compiles successfully but fails to import.

---

### Environment

```
Windows 11
Python 3.12
Torch 2.10.0+cu130
CUDA 13
ComfyUI: 0.16.4
comfy-aimdo: 0.2.9
comfy-kitchen: 0.2.7
ComfyUI frontend: 1.41.11
```

Torch runtime libraries exist correctly in:

```
Python312/Lib/site-packages/torch/lib
```

Example files:

```
c10.dll
c10_cuda.dll
torch_cpu.dll
torch_cuda.dll
torch_python.dll
```

---

### Cause

Python ≥3.8 tightened DLL loading behavior and does **not search PATH automatically** for extension dependencies.

CUDA extensions that depend on Torch runtime libraries therefore fail to load unless the Torch DLL directory is explicitly registered.

---

### Workaround

Registering the Torch DLL directory before importing the extension fixes the issue:

```python
import os
os.add_dll_directory(r".../site-packages/torch/lib")

import torch
import custom_rasterizer_kernel
```

This allows the extension to load successfully.

---

### Suggested Fix

ComfyUI could register the Torch DLL directory before importing torch:

```python
import os, torch
os.add_dll_directory(os.path.join(os.path.dirname(torch.__file__), "lib"))
```

This would ensure that **all Torch CUDA extensions load correctly**, including those from custom nodes.

---

### Why this matters

New nodes using CUDA extensions (e.g. rasterizers, Triton kernels, 3D nodes) are increasingly common and may hit this issue with:

```
Python 3.12
Torch 2.10+
CUDA 13
```

Registering the Torch DLL directory once during startup would prevent these import errors.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.