pytorch - 💡(How to fix) Fix CUDA driver error: invalid argument in fft backward when output used in subtraction [3 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177561Fetched 2026-04-08 00:47:28
View on GitHub
Comments
3
Participants
4
Timeline
149
Reactions
0
Assignees
Timeline (top)
mentioned ×66subscribed ×66labeled ×10commented ×3

Error Message

RuntimeError: CUDA driver error: invalid argument, but succeeds for device='cpu': res = data_f - x # addition, data_f + x, does not produce an error

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 16 On-line CPU(s) list: 0-15 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) Ultra 9 285H CPU family: 6 Model: 197 Thread(s) per core: 1 Core(s) per socket: 16 Socket(s): 1 Stepping: 2 CPU(s) scaling MHz: 37% CPU max MHz: 5400.0000 CPU min MHz: 400.0000 BogoMIPS: 7372.80 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni lam wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr ibt flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 480 KiB (12 instances) L1i cache: 768 KiB (12 instances) L2 cache: 28 MiB (9 instances) L3 cache: 24 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-15 Vulnerability Gather data sampling: Not affected Vulnerability Ghostwrite: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS Not affected; BHI BHI_DIS_S Vulnerability Srbds: Not affected Vulnerability Tsa: Not affected Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

Code Example

import torch
device='cuda'

x = torch.randn(4, 2, 16, 16, device=device, requires_grad=True)
data = torch.randn(4, 2, 16, 16, device=device)
data_f = torch.fft.fft(data, dim=0)

x = torch.fft.fft(x, dim=0)

res = data_f - x # addition, data_f + x, does not produce an error
loss = (res.real ** 2 + res.imag ** 2).sum()
loss.backward()
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

In the following code, backward after fft and subtraction operation fails for device='cuda' with RuntimeError: CUDA driver error: invalid argument, but succeeds for device='cpu':

import torch
device='cuda'

x = torch.randn(4, 2, 16, 16, device=device, requires_grad=True)
data = torch.randn(4, 2, 16, 16, device=device)
data_f = torch.fft.fft(data, dim=0)

x = torch.fft.fft(x, dim=0)

res = data_f - x # addition, data_f + x, does not produce an error
loss = (res.real ** 2 + res.imag ** 2).sum()
loss.backward()

Addition, res = data_f + x, succeeds for both 'cuda' and 'cpu'.

Versions

PyTorch version: 2.10.0+cu130 Is debug build: False CUDA used to build PyTorch: 13.0 ROCM used to build PyTorch: N/A

OS: Ubuntu 25.10 (x86_64) GCC version: (Ubuntu 15.2.0-4ubuntu4) 15.2.0 Clang version: Could not collect CMake version: Could not collect Libc version: glibc-2.42

Python version: 3.13.7 (main, Mar 3 2026, 12:19:54) [GCC 15.2.0] (64-bit runtime) Python platform: Linux-6.17.0-19-generic-x86_64-with-glibc2.42 Is CUDA available: True CUDA runtime version: 12.4.131 CUDA_MODULE_LOADING set to: GPU models and configuration: GPU 0: NVIDIA GeForce RTX 5090 Laptop GPU Nvidia driver version: 580.126.09 cuDNN version: Could not collect Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 16 On-line CPU(s) list: 0-15 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) Ultra 9 285H CPU family: 6 Model: 197 Thread(s) per core: 1 Core(s) per socket: 16 Socket(s): 1 Stepping: 2 CPU(s) scaling MHz: 37% CPU max MHz: 5400.0000 CPU min MHz: 400.0000 BogoMIPS: 7372.80 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni lam wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr ibt flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 480 KiB (12 instances) L1i cache: 768 KiB (12 instances) L2 cache: 28 MiB (9 instances) L3 cache: 24 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-15 Vulnerability Gather data sampling: Not affected Vulnerability Ghostwrite: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS Not affected; BHI BHI_DIS_S Vulnerability Srbds: Not affected Vulnerability Tsa: Not affected Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

Versions of relevant libraries: [pip3] numpy==2.3.5 [pip3] nvidia-cublas==13.1.0.3 [pip3] nvidia-cuda-cupti==13.0.85 [pip3] nvidia-cuda-nvrtc==13.0.88 [pip3] nvidia-cuda-runtime==13.0.96 [pip3] nvidia-cudnn-cu13==9.15.1.9 [pip3] nvidia-cufft==12.0.0.61 [pip3] nvidia-curand==10.4.0.35 [pip3] nvidia-cusolver==12.0.4.66 [pip3] nvidia-cusparse==12.6.3.3 [pip3] nvidia-cusparselt-cu13==0.8.0 [pip3] nvidia-nccl-cu13==2.28.9 [pip3] nvidia-nvjitlink==13.0.88 [pip3] nvidia-nvtx==13.0.85 [pip3] torch==2.10.0+cu130 [pip3] torchvision==0.25.0+cu130 [pip3] triton==3.6.0 [conda] Could not collect

cc @ezyang @gchanan @kadeng @msaroufim @peterjc123 @mszhanyi @skyline75489 @nbcsm @iremyux @Blackhex @albanD @gqchen @nikitaved @soulitzer @Varal7 @bobrenjc93 @ptrblck @eqy @jerryzh168 @tinglvv @nWEIdia @mruberry

extent analysis

Fix Plan

The issue arises from the subtraction operation between two complex tensors on a CUDA device. To fix this, we can try the following steps:

  • Ensure that both tensors have the same dtype and device.
  • Use the torch.sub function or the - operator with caution, as it may not work correctly for complex tensors on CUDA devices.
  • As an alternative, try using the torch.complex module to handle complex tensor operations.

Here's an example code snippet that demonstrates the fix:

import torch

device = 'cuda'
x = torch.randn(4, 2, 16, 16, device=device, requires_grad=True)
data = torch.randn(4, 2, 16, 16, device=device)

data_f = torch.fft.fft(data, dim=0)
x_f = torch.fft.fft(x, dim=0)

# Use torch.complex to handle complex tensor operations
res = torch.complex(data_f.real - x_f.real, data_f.imag - x_f.imag)

loss = (res.real ** 2 + res.imag ** 2).sum()
loss.backward()

Alternatively, you can also try using the torch.fft.ifft function to convert the complex tensors back to real tensors before performing the subtraction operation:

import torch

device = 'cuda'
x = torch.randn(4, 2, 16, 16, device=device, requires_grad=True)
data = torch.randn(4, 2, 16, 16, device=device)

data_f = torch.fft.fft(data, dim=0)
x_f = torch.fft.fft(x, dim=0)

# Convert complex tensors back to real tensors
data_real = torch.fft.ifft(data_f, dim=0)
x_real = torch.fft.ifft(x_f, dim=0)

res = data_real - x_real

loss = (res ** 2).sum()
loss.backward()

Verification

To verify that the fix worked, you can check the following:

  • The loss.backward() call no longer raises a RuntimeError.
  • The gradients of the input tensor x are correctly computed and updated.

Extra Tips

  • When working with complex tensors on CUDA devices, it's essential to use the torch.complex module to handle complex tensor operations correctly.
  • Be cautious when using the - operator or torch.sub function with complex tensors, as it may not work correctly on CUDA devices.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING