pytorch - ✅(Solved) Fix sspaddmm rejects dense mat1 with misleading error message ("got 0D tensor") [1 pull requests, 1 comments, 2 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#177951Fetched 2026-04-08 01:02:52
View on GitHub
Comments
1
Participants
2
Timeline
42
Reactions
0
Author
Timeline (top)
mentioned ×18subscribed ×18labeled ×4commented ×1

According to the current torch.sspaddmm documentation, input and mat1 are expected to be sparse, while mat2 is dense. However, when mat1 is provided as a dense 2D tensor, the operator does not raise a clear layout/type error. Instead, it raises the misleading message:

ERROR type: RuntimeError
ERROR msg : sspaddmm: Argument #2: matrices expected, got 0D tensor

This appears to be an error-reporting issue: the provided mat1_dense is clearly a 2D tensor, not a 0D tensor.

Error Message

Case 1 (mat1 is dense 2D) fails with:

Root Cause

According to the current torch.sspaddmm documentation, input and mat1 are expected to be sparse, while mat2 is dense. However, when mat1 is provided as a dense 2D tensor, the operator does not raise a clear layout/type error. Instead, it raises the misleading message:

ERROR type: RuntimeError
ERROR msg : sspaddmm: Argument #2: matrices expected, got 0D tensor

This appears to be an error-reporting issue: the provided mat1_dense is clearly a 2D tensor, not a 0D tensor.

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 48 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 128 On-line CPU(s) list: 0-127 Vendor ID: AuthenticAMD Model name: AMD Ryzen Threadripper PRO 5995WX 64-Cores CPU family: 25 Model: 8 Thread(s) per core: 2 Core(s) per socket: 64 Socket(s): 1 Stepping: 2 Frequency boost: enabled CPU max MHz: 2700.0000 CPU min MHz: 1800.0000 BogoMIPS: 5389.77 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca Virtualization: AMD-V L1d cache: 2 MiB (64 instances) L1i cache: 2 MiB (64 instances) L2 cache: 32 MiB (64 instances) L3 cache: 256 MiB (8 instances) NUMA node(s): 1 NUMA node0 CPU(s): 0-127 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: 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; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

PR fix notes

PR #179037: sspaddmm: validate mat1/mat2 layout and clarify errors

Description (problem / solution / changelog)

fixes #177951

Adds explicit layout checks in CPU sspaddmm so wrong mat1/mat2 types fail with clear messages, and fixes the dimension check label for mat2.

Changed files

  • aten/src/ATen/native/sparse/SparseTensorMath.cpp (modified, +5/-1)
  • test/test_sparse.py (modified, +19/-0)

Code Example

ERROR type: RuntimeError
ERROR msg : sspaddmm: Argument #2: matrices expected, got 0D tensor

---

import torch

def make_sparse_coo(m, n, nnz=4, dtype=torch.float32, device="cpu"):
    idx = torch.randint(0, m, (1, nnz), device=device)
    jdx = torch.randint(0, n, (1, nnz), device=device)
    indices = torch.cat([idx, jdx], dim=0).to(torch.int64)
    values = torch.randn(nnz, device=device, dtype=dtype)
    return torch.sparse_coo_tensor(
        indices, values, size=(m, n), device=device, dtype=dtype
    ).coalesce()

print("torch:", torch.__version__)

device = "cpu"
dtype = torch.float32
m, k, n = 2, 2, 2

self_sp = make_sparse_coo(m, n, nnz=4, dtype=dtype, device=device)   # sparse (m, n)
mat1_dense = torch.randn(m, k, device=device, dtype=dtype)           # dense  (m, k)
mat2_dense = torch.randn(k, n, device=device, dtype=dtype)           # dense  (k, n)

print("self_sp layout:", self_sp.layout)
print("mat1_dense dim:", mat1_dense.dim(), "shape:", tuple(mat1_dense.shape))
print("mat2_dense dim:", mat2_dense.dim(), "shape:", tuple(mat2_dense.shape))

print("\n=== Case 1: dense mat1 ===")
try:
    y = self_sp.sspaddmm(mat1_dense, mat2_dense)
    print("OK:", y)
except Exception as e:
    print("ERROR type:", type(e).__name__)
    print("ERROR msg :", str(e))

mat1_sp = make_sparse_coo(m, k, nnz=3, dtype=dtype, device=device)   # sparse (m, k)

print("\n=== Case 2: sparse mat1 ===")
try:
    y2 = self_sp.sspaddmm(mat1_sp, mat2_dense)
    print("OK:", y2)
    print("is_sparse:", y2.is_sparse)
except Exception as e:
    print("ERROR type:", type(e).__name__)
    print("ERROR msg :", str(e))
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Summary

According to the current torch.sspaddmm documentation, input and mat1 are expected to be sparse, while mat2 is dense. However, when mat1 is provided as a dense 2D tensor, the operator does not raise a clear layout/type error. Instead, it raises the misleading message:

ERROR type: RuntimeError
ERROR msg : sspaddmm: Argument #2: matrices expected, got 0D tensor

This appears to be an error-reporting issue: the provided mat1_dense is clearly a 2D tensor, not a 0D tensor.

Reproducer

import torch

def make_sparse_coo(m, n, nnz=4, dtype=torch.float32, device="cpu"):
    idx = torch.randint(0, m, (1, nnz), device=device)
    jdx = torch.randint(0, n, (1, nnz), device=device)
    indices = torch.cat([idx, jdx], dim=0).to(torch.int64)
    values = torch.randn(nnz, device=device, dtype=dtype)
    return torch.sparse_coo_tensor(
        indices, values, size=(m, n), device=device, dtype=dtype
    ).coalesce()

print("torch:", torch.__version__)

device = "cpu"
dtype = torch.float32
m, k, n = 2, 2, 2

self_sp = make_sparse_coo(m, n, nnz=4, dtype=dtype, device=device)   # sparse (m, n)
mat1_dense = torch.randn(m, k, device=device, dtype=dtype)           # dense  (m, k)
mat2_dense = torch.randn(k, n, device=device, dtype=dtype)           # dense  (k, n)

print("self_sp layout:", self_sp.layout)
print("mat1_dense dim:", mat1_dense.dim(), "shape:", tuple(mat1_dense.shape))
print("mat2_dense dim:", mat2_dense.dim(), "shape:", tuple(mat2_dense.shape))

print("\n=== Case 1: dense mat1 ===")
try:
    y = self_sp.sspaddmm(mat1_dense, mat2_dense)
    print("OK:", y)
except Exception as e:
    print("ERROR type:", type(e).__name__)
    print("ERROR msg :", str(e))

mat1_sp = make_sparse_coo(m, k, nnz=3, dtype=dtype, device=device)   # sparse (m, k)

print("\n=== Case 2: sparse mat1 ===")
try:
    y2 = self_sp.sspaddmm(mat1_sp, mat2_dense)
    print("OK:", y2)
    print("is_sparse:", y2.is_sparse)
except Exception as e:
    print("ERROR type:", type(e).__name__)
    print("ERROR msg :", str(e))

Observed behavior

Case 1 (mat1 is dense 2D) fails with:

RuntimeError: sspaddmm: Argument #2: matrices expected, got 0D tensor

Case 2 (mat1 is sparse 2D) succeeds.

This matches the documented contract that:

  • input / self: sparse
  • mat1: sparse
  • mat2: dense

Expected behavior

If mat1 is required to be a sparse 2D tensor, passing a dense 2D tensor should fail with a clear and accurate error message indicating that mat1 must be sparse.

For example, an error along the lines of:

sspaddmm: expected mat1 to be a sparse 2D tensor and mat2 to be a dense 2D tensor

would be much clearer than reporting got 0D tensor.

Why this seems like an error-reporting issue

In the failing case, mat1_dense is definitely a 2D tensor:

  • dim == 2
  • shape == (2, 2)

So the current error message is misleading and does not accurately describe the provided input, which makes the failure harder to understand and debug.

Suggested fix

Improve the runtime error raised for dense mat1 so that it explicitly reports the expected layout/type requirements for mat1 and mat2.

Versions

PyTorch version: 2.10.0a0+gitf2bb22f Is debug build: False CUDA used to build PyTorch: Could not collect ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64) GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0 Clang version: 14.0.0-1ubuntu1.1 CMake version: version 4.1.2 Libc version: glibc-2.35

Python version: 3.10.12 (main, Aug 15 2025, 14:32:43) [GCC 11.4.0] (64-bit runtime) Python platform: Linux-5.4.0-200-generic-x86_64-with-glibc2.35 Is CUDA available: False CUDA runtime version: Could not collect CUDA_MODULE_LOADING set to: N/A GPU models and configuration: Nvidia driver version: Could not collect cuDNN version: Could not collect Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: False Caching allocator config: N/A

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 48 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 128 On-line CPU(s) list: 0-127 Vendor ID: AuthenticAMD Model name: AMD Ryzen Threadripper PRO 5995WX 64-Cores CPU family: 25 Model: 8 Thread(s) per core: 2 Core(s) per socket: 64 Socket(s): 1 Stepping: 2 Frequency boost: enabled CPU max MHz: 2700.0000 CPU min MHz: 1800.0000 BogoMIPS: 5389.77 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca Virtualization: AMD-V L1d cache: 2 MiB (64 instances) L1i cache: 2 MiB (64 instances) L2 cache: 32 MiB (64 instances) L3 cache: 256 MiB (8 instances) NUMA node(s): 1 NUMA node0 CPU(s): 0-127 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: 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; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

Versions of relevant libraries: [pip3] numpy==2.2.6 [pip3] optree==0.17.0 [pip3] torch==2.10.0a0+gitf2bb22f [conda] Could not collect

cc @nikitaved @pearu @cpuhrsch @amjames @bhosmer @jcaip

extent analysis

Fix Plan

To address the error-reporting issue, we need to modify the sspaddmm function to raise a clear and accurate error message when mat1 is a dense 2D tensor.

Here are the steps:

  • Check the type and layout of mat1 at the beginning of the sspaddmm function.
  • If mat1 is not a sparse 2D tensor, raise a RuntimeError with a message indicating the expected layout and type requirements for mat1 and mat2.

Example code:

def sspaddmm(self, mat1, mat2):
    if not mat1.is_sparse or mat1.dim() != 2:
        raise RuntimeError("sspaddmm: expected mat1 to be a sparse 2D tensor and mat2 to be a dense 2D tensor")
    # rest of the function remains the same

Verification

To verify that the fix worked, you can run the reproducer code again and check that the error message is now clear and accurate when mat1 is a dense 2D tensor.

Extra Tips

  • Make sure to test the sspaddmm function with different types and layouts of mat1 and mat2 to ensure that it behaves correctly in all cases.
  • Consider adding additional checks and error messages to handle other potential edge cases, such as mat2 not being a dense 2D tensor.

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…

FAQ

Expected behavior

If mat1 is required to be a sparse 2D tensor, passing a dense 2D tensor should fail with a clear and accurate error message indicating that mat1 must be sparse.

For example, an error along the lines of:

sspaddmm: expected mat1 to be a sparse 2D tensor and mat2 to be a dense 2D tensor

would be much clearer than reporting got 0D tensor.

Still need to ship something?

×6

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

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix sspaddmm rejects dense mat1 with misleading error message ("got 0D tensor") [1 pull requests, 1 comments, 2 participants]