pytorch - ✅(Solved) Fix `torch.sparse_csr_tensor` with `from_numpy` arrays fails when array is empty for `col_indices` with strict invariant checking [2 pull requests, 9 comments, 3 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#178309Fetched 2026-04-08 01:26:10
View on GitHub
Comments
9
Participants
3
Timeline
89
Reactions
0
Author
Timeline (top)
mentioned ×31subscribed ×31commented ×9referenced ×8

Error Message

RuntimeError: expected col_indices to be a contiguous tensor per batch

PR fix notes

PR #178404: Fix sparse_csr_tensor with empty numpy arrays

Description (problem / solution / changelog)

Summary

Fixes #178309 - torch.sparse_csr_tensor fails with empty from_numpy arrays when col_indices is empty due to stride validation checking.

Root Cause

The check in SparseCsrTensor.cpp fails for empty tensors because they have no elements to determine stride from. When is empty (size [0]), the stride check is meaningless and incorrectly fails.

Fix

Added a check to skip the stride validation when the tensor is empty ():

1 "<stdin>"

1 "<built-in>" 1

1 "<built-in>" 3

465 "<built-in>" 3

1 "<command line>" 1

1 "<built-in>" 2

1 "<stdin>" 2

Test

Added to verify the fix works with both torch.tensor and numpy array inputs.

Authored with Claude

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @Lucaskabela @jataylo

Changed files

  • aten/src/ATen/native/sparse/SparseCsrTensor.cpp (modified, +4/-2)
  • test/dynamo/test_misc.py (modified, +20/-0)
  • test/test_sparse_csr.py (modified, +31/-0)
  • torch/_dynamo/guards.py (modified, +6/-1)

PR #178419: Fix: treat empty tensors as contiguous in sparse validation

Description (problem / solution / changelog)

Fixes #178309

Problem

Empty numpy arrays have stride 0 while empty torch tensors have stride 1. The sparse tensor validation expects contiguity (stride 1), causing: expected col_indices to be a contiguous tensor per batch

Solution

Skip the stride check for tensors with numel() == 0 since empty tensors have no elements and are contiguous by definition.

Key improvements:

  • Uses numel() == 0 instead of size(-1) == 0 for robust empty detection
  • Applies to both plain_indices and compressed_indices

Testing

import numpy as np
import torch

crow_indices = [0, 0]
col_indices = []
values = []

# This now works
t = torch.sparse_csr_tensor(
    torch.from_numpy(np.array(crow_indices, dtype="int32")),
    torch.from_numpy(np.array(col_indices, dtype="int32")),
    torch.from_numpy(np.array(values, dtype="int32")),
    (1, 100)
)

## Changed files

- `aten/src/ATen/native/sparse/SparseCsrTensor.cpp` (modified, +4/-2)
- `test/test_sparse_csr.py` (modified, +10/-0)

Code Example

import numpy as np
import torch

crow_indices = [0, 0]
col_indices = []
values = []
with torch.sparse.check_sparse_tensor_invariants(enable=True):
    # works
    torch.sparse_csr_tensor(torch.tensor(crow_indices, dtype=torch.int32), torch.tensor(col_indices, dtype=torch.int32), torch.tensor(values, dtype=torch.int32), (1, 100))
    # fails
    torch.sparse_csr_tensor(torch.from_numpy(np.array(crow_indices, dtype="int32")).contiguous(), torch.from_numpy(np.array(col_indices, dtype="int32")).contiguous(), torch.from_numpy(np.array(values, dtype="int32")).contiguous(), (1, 100))

---

RuntimeError: expected col_indices to be a contiguous tensor per batch
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Presumably this is a 2.11.0 thing since it only just came up in our CI and is definitely an edge case, but despite the contiguous calls I get this error

import numpy as np
import torch

crow_indices = [0, 0]
col_indices = []
values = []
with torch.sparse.check_sparse_tensor_invariants(enable=True):
    # works
    torch.sparse_csr_tensor(torch.tensor(crow_indices, dtype=torch.int32), torch.tensor(col_indices, dtype=torch.int32), torch.tensor(values, dtype=torch.int32), (1, 100))
    # fails
    torch.sparse_csr_tensor(torch.from_numpy(np.array(crow_indices, dtype="int32")).contiguous(), torch.from_numpy(np.array(col_indices, dtype="int32")).contiguous(), torch.from_numpy(np.array(values, dtype="int32")).contiguous(), (1, 100))

Error:

RuntimeError: expected col_indices to be a contiguous tensor per batch

Versions

Collecting environment information... PyTorch version: 2.11.0 Is debug build: False CUDA used to build PyTorch: None ROCM used to build PyTorch: N/A

OS: macOS 15.1 (arm64) GCC version: Could not collect Clang version: 16.0.0 (clang-1600.0.26.4) CMake version: version 4.2.3 Libc version: N/A

Python version: 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:54:21) [Clang 16.0.6 ] (64-bit runtime) Python platform: macOS-15.1-arm64-arm-64bit 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 Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

CPU: Apple M1 Pro

Versions of relevant libraries: [pip3] Could not collect [conda] numpy 2.2.6 pypi_0 pypi [conda] numpy-groupies 0.11.2 pypi_0 pypi

cc @nikitaved @pearu @cpuhrsch @amjames @bhosmer @jcaip @malfet @mruberry @rgommers

extent analysis

Fix Plan

The fix involves ensuring that the input tensors to torch.sparse_csr_tensor are contiguous in memory.

  • Step 1: Convert the numpy arrays to PyTorch tensors using torch.from_numpy.
  • Step 2: Ensure the tensors are contiguous by calling the contiguous() method.

Here's the corrected code snippet:

import numpy as np
import torch

crow_indices = np.array([0, 0], dtype="int32")
col_indices = np.array([], dtype="int32")
values = np.array([], dtype="int32")

crow_indices_tensor = torch.from_numpy(crow_indices).contiguous()
col_indices_tensor = torch.from_numpy(col_indices).contiguous()
values_tensor = torch.from_numpy(values).contiguous()

with torch.sparse.check_sparse_tensor_invariants(enable=True):
    torch.sparse_csr_tensor(crow_indices_tensor, col_indices_tensor, values_tensor, (1, 100))

Verification

To verify that the fix worked, run the corrected code snippet. If it executes without raising a RuntimeError, the fix was successful.

Extra Tips

When working with PyTorch and numpy, it's essential to ensure that tensors are contiguous in memory to avoid errors. The contiguous() method can be used to achieve this. Additionally, always verify that the fix worked by running the corrected code snippet.

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