Skip to content

Commit 3316bdc

Browse files
committed
treat empty inputs gracefully
1 parent 5bb8d17 commit 3316bdc

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

torch_cluster/knn.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def knn(x: torch.Tensor, y: torch.Tensor, k: int,
4545
batch_y = torch.tensor([0, 0])
4646
assign_index = knn(x, y, 2, batch_x, batch_y)
4747
"""
48+
if x.numel() == 0 or y.numel() == 0:
49+
return torch.empty(2, 0, dtype=torch.long, device=x.device)
4850

4951
x = x.view(-1, 1) if x.dim() == 1 else x
5052
y = y.view(-1, 1) if y.dim() == 1 else y

torch_cluster/radius.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def radius(x: torch.Tensor, y: torch.Tensor, r: float,
4545
batch_y = torch.tensor([0, 0])
4646
assign_index = radius(x, y, 1.5, batch_x, batch_y)
4747
"""
48+
if x.numel() == 0 or y.numel() == 0:
49+
return torch.empty(2, 0, dtype=torch.long, device=x.device)
4850

4951
x = x.view(-1, 1) if x.dim() == 1 else x
5052
y = y.view(-1, 1) if y.dim() == 1 else y

0 commit comments

Comments
 (0)