pytorch - 💡(How to fix) Fix `check_invariants` does not silence warning for `torch.sparse_csr_tensor` [5 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#178274Fetched 2026-04-08 01:20:47
View on GitHub
Comments
5
Participants
3
Timeline
47
Reactions
0
Author
Timeline (top)
mentioned ×19subscribed ×19commented ×5labeled ×4

Code Example

import torch

crow_indices = [0, 2, 4]
col_indices = [0, 1, 0, 1]
values = [1, 2, 3, 4]
torch.sparse_csr_tensor(torch.tensor(crow_indices, dtype=torch.int64), torch.tensor(col_indices, dtype=torch.int64), torch.tensor(values), (2, 2), check_invariants=True) # raises a warning

# UserWarning: Sparse invariant checks are implicitly disabled. Memory errors (e.g. SEGFAULT) will occur when operating on a sparse tensor which violates the invariants, but checks incur performance overhead. To silence this warning, explicitly opt in or out. See `torch.sparse.check_sparse_tensor_invariants.__doc__` for guidance.  (Triggered internally at /pytorch/aten/src/ATen/Context.cpp:760.)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

See also: https://github.com/scverse/annbatch/actions/runs/23490172888/job/68356024594?pr=168 Presumably this is 2.11 specific

import torch

crow_indices = [0, 2, 4]
col_indices = [0, 1, 0, 1]
values = [1, 2, 3, 4]
torch.sparse_csr_tensor(torch.tensor(crow_indices, dtype=torch.int64), torch.tensor(col_indices, dtype=torch.int64), torch.tensor(values), (2, 2), check_invariants=True) # raises a warning

# UserWarning: Sparse invariant checks are implicitly disabled. Memory errors (e.g. SEGFAULT) will occur when operating on a sparse tensor which violates the invariants, but checks incur performance overhead. To silence this warning, explicitly opt in or out. See `torch.sparse.check_sparse_tensor_invariants.__doc__` for guidance.  (Triggered internally at /pytorch/aten/src/ATen/Context.cpp:760.)

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

extent analysis

Fix Plan

To fix the warning about sparse invariant checks being implicitly disabled, you can explicitly opt in or out of these checks.

Here are the steps:

  • Explicitly opt in: Use torch.sparse.check_sparse_tensor_invariants(True) before creating the sparse tensor.
  • Explicitly opt out: Use torch.sparse.check_sparse_tensor_invariants(False) before creating the sparse tensor.

Example code:

import torch

# Explicitly opt in
torch.sparse.check_sparse_tensor_invariants(True)
crow_indices = [0, 2, 4]
col_indices = [0, 1, 0, 1]
values = [1, 2, 3, 4]
torch.sparse_csr_tensor(torch.tensor(crow_indices, dtype=torch.int64), torch.tensor(col_indices, dtype=torch.int64), torch.tensor(values), (2, 2), check_invariants=True)

# Explicitly opt out
torch.sparse.check_sparse_tensor_invariants(False)
crow_indices = [0, 2, 4]
col_indices = [0, 1, 0, 1]
values = [1, 2, 3, 4]
torch.sparse_csr_tensor(torch.tensor(crow_indices, dtype=torch.int64), torch.tensor(col_indices, dtype=torch.int64), torch.tensor(values), (2, 2), check_invariants=False)

Verification

To verify that the fix worked, check that the warning about sparse invariant checks being implicitly disabled is no longer raised.

Extra Tips

  • Be aware that disabling sparse invariant checks can improve performance but may also lead to memory errors if the sparse tensor violates the invariants.
  • Make sure to test your code thoroughly after opting in or out of sparse invariant checks.

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