Skip to content

Commit 40693ab

Browse files
authored
Fix empty edge indices handling (#332)
* update * fix empty
1 parent 0355738 commit 40693ab

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

torch_sparse/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def __init__(
146146
self._csr2csc = csr2csc
147147
self._csc2csr = csc2csr
148148

149-
if not is_sorted:
149+
if not is_sorted and self._col.numel() > 0:
150150
idx = self._col.new_zeros(self._col.numel() + 1)
151151
idx[1:] = self.row()
152152
idx[1:] *= self._sparse_sizes[1]

torch_sparse/tensor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ def numel(self) -> int:
357357
return self.nnz()
358358

359359
def density(self) -> float:
360+
if self.sparse_size(0) == 0 or self.sparse_size(1) == 0:
361+
return 0.0
360362
return self.nnz() / (self.sparse_size(0) * self.sparse_size(1))
361363

362364
def sparsity(self) -> float:

0 commit comments

Comments
 (0)