Weird behaviour of insufficient resources error in torch.sparse.mm

I encountered an insufficient resources error with the sparse matrix multiplication torch.sparse.mm
RuntimeError: CUDA error: insufficient resources when calling cusparseSpGEMM_compute( handle, opA, opB, &alpha, matA, matB, &beta, matC, computeType, CUSPARSE_SPGEMM_DEFAULT, spgemmDesc, &bufferSize2, dBuffer2)
which is behaving unexpected for me:

When multiplying two certain matrices A1 and B I get the error, while the error does not occur when multiplying A2 and B. This is although A2 was constructed to contain all non-zeros of A1 and a few additional ones.
One example where I observed this error pattern is:

import torch

a1 = torch.sparse_coo_tensor(torch.tensor([[10203, 220497],[2, 2]]), torch.ones([2]), size=[220500,3]).to('cuda:0')
a2 = torch.sparse_coo_tensor(torch.stack([torch.tensor(range(220500)),torch.ones([220500])*2]), torch.ones([220500]), size=[220500,3]).to('cuda:0')
b = torch.sparse_coo_tensor(torch.tensor([[2, 2],[2, 8707]]), torch.ones([2]), size=[3,220500]).to('cuda:0')

# no error in multiplication:
torch.sparse.mm(a2,b) 

# insufficient resources error in multiplication: 
torch.sparse.mm(a1,b) 

As the operation including A2 should use at least the same memory as the operation with A1 (due to A2’s construction), this is very weird to me. Is there some strange behaviour of the memory allocation of this operation?
I am using pytorch 2.0.1 with CUDA 11.8.

Does anyone have an idea why I get this error and/or how I could circumvent it?
Thanks!

Could you check if the code works using the latest stable release with CUDA 12?
If it’s still failing, could you post which GPU are are using?

Unfortunately, I still got the error when installing python 2.3.0 with CUDA 12.1 on a notebook using a NVIDIA GeForce RTX 3080 Ti Laptop GPU.

Below you find also the output of python -m torch.utils.collect_env:

PyTorch version: 2.3.0
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.12.3 | packaged by Anaconda, Inc. | (main, Apr 19 2024, 16:50:38) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.146.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3080 Ti Laptop GPU
Nvidia driver version: 551.76
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

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):                             20
On-line CPU(s) list:                0-19
Vendor ID:                          GenuineIntel
Model name:                         12th Gen Intel(R) Core(TM) i9-12900H
CPU family:                         6
Model:                              154
Thread(s) per core:                 2
Core(s) per socket:                 10
Socket(s):                          1
Stepping:                           3
BogoMIPS:                           5836.79
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni umip waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize flush_l1d arch_capabilities
Virtualization:                     VT-x
Hypervisor vendor:                  Microsoft
Virtualization type:                full
L1d cache:                          480 KiB (10 instances)
L1i cache:                          320 KiB (10 instances)
L2 cache:                           12.5 MiB (10 instances)
L3 cache:                           24 MiB (1 instance)
Vulnerability Gather data sampling: 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 Retbleed:             Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] torch==2.3.0
[pip3] torchaudio==2.3.0
[pip3] torchvision==0.18.0
[conda] blas                      1.0                         mkl
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46344
[conda] mkl-service               2.4.0           py312h5eee18b_1
[conda] mkl_fft                   1.3.8           py312h5eee18b_0
[conda] mkl_random                1.2.4           py312hdb19cb5_0
[conda] numpy                     1.26.4          py312hc5e2394_0
[conda] numpy-base                1.26.4          py312h0da6c21_0
[conda] pytorch                   2.3.0           py3.12_cuda12.1_cudnn8.9.2_0    pytorch
[conda] pytorch-cuda              12.1                 ha16c6d3_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.3.0               py312_cu121    pytorch
[conda] torchvision               0.18.0              py312_cu121    pytorch

Thanks for the additional check!

I can also reproduce it with a nightly release on a 3090, but am unsure which “resources” are insufficient and need to check it with the cuSPARSE team.
CC @eqy

Hi :slight_smile:

Are there any news regarding this problem? Or any idea how I could circumvent it?

I had the same problem, I saw this can handle it, however it requireds cuda > 12.0