Repository: pyg-team/pytorch_geometric
Pull request #8752 (2024-01-12 11:46:34)

Return an empty tensor from `utils.group_argsort` if its input tensor is empty

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pyg-team/pytorch_geometric/pull/8752?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`59e9a4e`)](https://app.codecov.io/gh/pyg-team/pytorch_geometric/commit/59e9a4e11e2b5ff57bb75282da8e1260357c59f0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) 89.56% compared to head [(`3d33b47`)](https://app.codecov.io/gh/pyg-team/pytorch_geometric/pull/8752?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team) 89.56%.

> :exclamation: Current head 3d33b47 differs from pull request most recent head 563c007. Consider uploading reports for the commit 563c007 to get more accurate results


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #8752   +/-   ##
=======================================
  Coverage   89.56%   89.56%           
=======================================
  Files         482      482           
  Lines       31113    31115    +2     
=======================================
+ Hits        27865    27867    +2     
  Misses       3248     3248           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pyg-team/pytorch_geometric/pull/8752?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pyg-team).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ba9ff52eb..e65b8ea63 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -45,6 +45,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 
 ### Changed
 
+- Return an empty tensor from `utils.group_argsort` if its input tensor is empty ([#8752](https://github.com/pyg-team/pytorch_geometric/pull/8752))
 - GNN layers are now jittable by default ([#8745](https://github.com/pyg-team/pytorch_geometric/pull/8745))
 - Sparse node features in `NELL` and `AttributedGraphDataset` are now represented as `torch.sparse_csr_tensor` instead of `torch_sparse.SparseTensor` ([#8679](https://github.com/pyg-team/pytorch_geometric/pull/8679))
 - Accelerated mini-batching of `torch.sparse` tensors ([#8670](https://github.com/pyg-team/pytorch_geometric/pull/8670))
diff --git a/torch_geometric/utils/_scatter.py b/torch_geometric/utils/_scatter.py
index 069654f35..9f738b8b7 100644
--- a/torch_geometric/utils/_scatter.py
+++ b/torch_geometric/utils/_scatter.py
@@ -243,11 +243,20 @@ def group_argsort(
             (default: :obj:`False`)
         stable (bool, optional): Controls the relative order of equivalent
             elements. (default: :obj:`False`)
+
+    Example:
+        >>> src = torch.tensor([0, 1, 5, 4, 3, 2, 6, 7, 8])
+        >>> index = torch.tensor([0, 0, 1, 1, 1, 1, 2, 2, 2])
+        >>> group_argsort(src, index)
+        tensor([0, 1, 3, 2, 1, 0, 0, 1, 2])
     """
     # Only implemented under certain conditions for now :(
     assert src.dim() == 1 and index.dim() == 1
     assert dim == 0 or dim == -1
-    assert src.numel() == index.numel() and src.numel() > 0
+    assert src.numel() == index.numel()
+
+    if src.numel() == 0:
+        return torch.zeros_like(src)
 
     # Normalize `src` to range [0, 1]:
     src = src - src.min()


Test patch:
diff --git a/test/utils/test_scatter.py b/test/utils/test_scatter.py
index 1659ecede..c9a32fa03 100644
--- a/test/utils/test_scatter.py
+++ b/test/utils/test_scatter.py
@@ -94,6 +94,10 @@ def test_group_argsort(num_groups, descending, device):
 
     assert torch.equal(out, expected)
 
+    empty_tensor = torch.tensor([], device=device)
+    out = group_argsort(empty_tensor, empty_tensor)
+    assert out.numel() == 0
+
 
 @withCUDA
 def test_scatter_argmax(device):