Skip to content

Commit

Permalink
Fix EdgeIndex.resize_ linting issues (#8993)
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty1s committed Feb 29, 2024
1 parent bb58158 commit dba9659
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Added

- Added `EdgeIndex.resize_` functionality ([#8983](https://github.com/pyg-team/pytorch_geometric/pull/8983))
- Added `EdgeIndex.sparse_resize_` functionality ([#8983](https://github.com/pyg-team/pytorch_geometric/pull/8983))
- Added approximate `faiss`-based KNN-search ([#8952](https://github.com/pyg-team/pytorch_geometric/pull/8952))

### Changed
Expand Down
10 changes: 5 additions & 5 deletions test/test_edge_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,18 +1024,18 @@ def test_sparse_narrow(device):


@withCUDA
def test_resize(device):
def test_sparse_resize(device):
adj = EdgeIndex([[0, 1, 1, 2], [1, 0, 2, 1]], device=device)

out = adj.sort_by('row')[0].fill_cache_()
assert out.sparse_size() == (3, 3)
assert out._indptr.equal(tensor([0, 1, 3, 4], device=device))
assert out._T_indptr.equal(tensor([0, 1, 3, 4], device=device))
out = out.resize_((4, 5))
out = out.sparse_resize_((4, 5))
assert out.sparse_size() == (4, 5)
assert out._indptr.equal(tensor([0, 1, 3, 4, 4], device=device))
assert out._T_indptr.equal(tensor([0, 1, 3, 4, 4, 4], device=device))
out = out.resize_((3, 3))
out = out.sparse_resize_((3, 3))
assert out.sparse_size() == (3, 3)
assert out._indptr is None
assert out._T_indptr is None
Expand All @@ -1044,11 +1044,11 @@ def test_resize(device):
assert out.sparse_size() == (3, 3)
assert out._indptr.equal(tensor([0, 1, 3, 4], device=device))
assert out._T_indptr.equal(tensor([0, 1, 3, 4], device=device))
out = out.resize_((4, 5))
out = out.sparse_resize_((4, 5))
assert out.sparse_size() == (4, 5)
assert out._indptr.equal(tensor([0, 1, 3, 4, 4, 4], device=device))
assert out._T_indptr.equal(tensor([0, 1, 3, 4, 4], device=device))
out = out.resize_((3, 3))
out = out.sparse_resize_((3, 3))
assert out.sparse_size() == (3, 3)
assert out._indptr is None
assert out._T_indptr is None
Expand Down
12 changes: 9 additions & 3 deletions torch_geometric/edge_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,10 @@ def get_sparse_size(

return torch.Size((self.get_sparse_size(0), self.get_sparse_size(1)))

def resize_(self, sparse_size: Tuple[int, int]) -> 'EdgeIndex':
def sparse_resize_( # type: ignore
self,
sparse_size: Tuple[int, int],
) -> 'EdgeIndex':
r"""Assigns or re-assigns the size of the underlying sparse matrix.
Args:
Expand All @@ -538,8 +541,11 @@ def _modify_ptr(ptr: Optional[Tensor], size: int) -> Optional[Tensor]:
if ptr.numel() - 1 > size:
return None

val = ptr.new_full((size - ptr.numel() + 1, ), fill_value=ptr[-1])
return torch.cat([ptr, val], dim=0)
fill_value = ptr.new_full(
(size - ptr.numel() + 1, ),
fill_value=ptr[-1], # type: ignore
)
return torch.cat([ptr, fill_value], dim=0)

if self.is_sorted_by_row:
self._indptr = _modify_ptr(self._indptr, num_rows)
Expand Down

0 comments on commit dba9659

Please sign in to comment.