Update is_sparse doc to mention that it is sparse_coo specific (#157378)

## Issue being addressed
`is_sparse` presents itself as determining if a tensor is sparse. HOWEVER, it only does checks against the tensor for `sparse_coo`. This has lead to confusion from developers as when non-coo sparse tensors are provided it return false, despite those tensors being sparse.

## Considered Remedy
Fixing this is do-able however would result in complexity as existing systems may depend on this behavior remaining consistent, and even inside of pytorch is_sparse is used by `bform` which states that it supports only `sparse_csr and sparse_coo` meaning additional work/thought would have to go into solving for `sparse_csc` and `sparse_bsr`

## Remedy provided in this PR
In lieu of these complications the lowest risk highest gain action was to add clear warning messaging to the function for now to avoid confusion to developers utilizing the function. The rest of the function behavior remains identical

## Issue content
Addresses issue number: #101385
Original issue: https://github.com/pytorch/pytorch/issues/101385

Pull Request resolved: https://github.com/pytorch/pytorch/pull/157378
Approved by: https://github.com/soulitzer
This commit is contained in:
Jack Francis Dalton
2025-07-09 18:22:14 +00:00
committed by PyTorch MergeBot
parent 81c7445eb9
commit c54778625e

View File

@ -8,7 +8,7 @@ from torch import Tensor
def is_sparse(A):
"""Check if tensor A is a sparse tensor"""
"""Check if tensor A is a sparse COO tensor. All other sparse storage formats (CSR, CSC, etc...) will return False."""
if isinstance(A, torch.Tensor):
return A.layout == torch.sparse_coo