Skip to content

Commit 115a494

Browse files
authored
Fix scalar check for sparse tensors. (#8197)
* Fix scalar check for sparse tensors. As discovered in #8152 If `t` is a scalar sparse tensor, `t._indices` used to return a sparse empty tensor because the scalar check was incorrect. This PR modifies the scalar check to return a dense tensor instead of a sparse tensor. i.e. ``` tensor = torch.sparse_coo_tensor([], [], torch.Size([]), device=device) out = tensor._indices() # was a sparse tensor, now is dense. ``` * Fix typos
1 parent 8e6f7a1 commit 115a494

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

aten/src/ATen/Declarations.cwrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3885,7 +3885,7 @@
38853885
aten_custom_call: |
38863886
if (self_->isScalar()) {
38873887
// Empty tensor
3888-
return self_->type().toScalarType(kLong).tensor({0});
3888+
return toBackend(toDense(backend())).toScalarType(kLong).tensor({0});
38893889
}
38903890
]]
38913891

test/test_sparse.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,12 @@ def test_factory_size_check(self):
910910
with self.assertRaisesRegex(RuntimeError, "values and sizes are inconsistent"):
911911
self.SparseTensor(indices, values, sizes)
912912

913+
def test_factory_empty_indices(self):
914+
device = 'cuda' if self.is_cuda else 'cpu'
915+
tensor = torch.sparse_coo_tensor([], [], torch.Size([]), device=device)
916+
expected_indices = torch.tensor([], dtype=torch.long, device=device)
917+
self.assertEqual(tensor._indices(), expected_indices)
918+
913919
@cpu_only
914920
def test_factory_type_inference(self):
915921
t = torch.sparse_coo_tensor(torch.tensor(([0], [2])), torch.tensor([1.], dtype=torch.float32))

0 commit comments

Comments
 (0)