From c54778625ec45b94eb97f52da3ad6c2eb3d3c3c9 Mon Sep 17 00:00:00 2001 From: Jack Francis Dalton Date: Wed, 9 Jul 2025 18:22:14 +0000 Subject: [PATCH] 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 --- torch/_linalg_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torch/_linalg_utils.py b/torch/_linalg_utils.py index fe168cb33c4b..43c8b65767e0 100644 --- a/torch/_linalg_utils.py +++ b/torch/_linalg_utils.py @@ -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