Description
🐛 Describe the bug
When initializing a torch.jit.ScriptModule that uses torch.cuda.Stream() within a @torch.jit.script_method-decorated method on a CPU-only system (no CUDA available), an internal assertion failure occurs at torch/csrc/jit/ir/ir.h:1503. This prevents model instantiation even when no GPU is present.
To Reproduce
import torch
class Model(torch.jit.ScriptModule):
def __init__(self):
super().__init__()
@torch.jit.script_method # Required to trigger the error
def forward(self, x):
stream = torch.cuda.Stream() # CUDA operation on CPU-only system
with torch.cuda.stream(stream):
y = x - torch.mean(x)
z = torch.relu(x) + torch.sigmoid(x)
return y + z
# Instantiation fails here
m = Model()
Error Message
Traceback (most recent call last):
File "test.py", line 15, in <module>
m = Model()
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\jit\_script.py", line 323, in init_then_script
] = torch.jit._recursive.create_script_module(
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\jit\_recursive.py", line 556, in create_script_module
return create_script_module_impl(nn_module, concrete_type, stubs_fn)
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\jit\_recursive.py", line 629, in create_script_module_impl
create_methods_and_properties_from_stubs(
File "D:\Programs\Python\virtualenvs\torch_code-afvE469o\lib\site-packages\torch\jit\_recursive.py", line 465, in create_methods_and_properties_from_stubs
concrete_type._create_methods_and_properties(
RuntimeError: type INTERNAL ASSERT FAILED at "C:\\actions-runner\\_work\\pytorch\\pytorch\\pytorch\\torch/csrc/jit/ir/ir.h":1503, please report a bug to PyTorch.
Versions
Collecting environment information...
PyTorch version: 2.7.0+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A
OS: Microsoft Windows 11
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 4.0.2
Libc version: N/A
Python version: 3.10.10 (tags/v3.10.10:aad5f6a, Feb 7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.26100-SP0
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
CPU:
Name: AMD Ryzen 5 7500F 6-Core Processor
Manufacturer: AuthenticAMD
Family: 107
Architecture: 9
ProcessorType: 3
DeviceID: CPU0
CurrentClockSpeed: 3701
MaxClockSpeed: 3701
L2CacheSize: 6144
L2CacheSpeed: None
Revision: 24834
Versions of relevant libraries:
[pip3] mypy_extensions==1.1.0
[pip3] numpy==2.2.4
[pip3] nvidia-cuda-runtime-cu12==12.9.37
[pip3] >
[pip3] onnx-coreml==1.3
[pip3] onnx-tf==1.10.0
[pip3] >
[pip3] >
[pip3] >
[pip3] optree==0.15.0
[pip3] pytorch-lightning==2.5.1
[pip3] torch==2.7.0
[pip3] torch-directml==0.2.5.dev240914
[pip3] torch-geometric==2.6.1
[pip3] torch_scatter==2.1.2
[pip3] torch_tensorrt==2.7.0
[pip3] torchani==2.2.4
[pip3] torchao==0.11.0
[pip3] torchaudio==2.7.0
[pip3] torchdata==0.11.0
[pip3] torchfx==0.1.0
[pip3] torchmetrics==1.7.1
[pip3] torchrec==0.1.0
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.18.0
[pip3] torchvision==0.19.1
[pip3] torchviz==0.0.3
[pip3] torchx-nightly==2025.5.28
[pip3] vector-quantize-pytorch==1.22.16
[conda] Could not collect