Skip to content

Conversation

DhyeyMavani2003
Copy link

@DhyeyMavani2003 DhyeyMavani2003 commented Oct 4, 2025

Description

This PR fixes a bug where maybe_num_nodes_dict would crash with a RuntimeError when processing heterogeneous graphs containing empty edge indices.

Problem

The function calls .max() on edge index tensors without checking if they are empty. PyTorch requires specifying a reduction dimension when calling .max() on empty tensors, causing the following error:

RuntimeError: max(): Expected reduction dim to be specified for input.numel() == 0. Specify the reduction dim with the 'dim' argument.

Reproducer

import torch
from torch_geometric.utils.num_nodes import maybe_num_nodes_dict

edge_index_dict = {
    ('user', 'rates', 'movie'): torch.tensor([[], []], dtype=torch.long),
    ('user', 'follows', 'user'): torch.tensor([[0, 1], [1, 2]], dtype=torch.long),
}

# This would crash before the fix
result = maybe_num_nodes_dict(edge_index_dict)

Solution

Added checks for empty tensors (numel() > 0) before calling .max(), returning 0 for empty edge indices, which is the correct behavior for graphs with no edges of a particular type.

Impact

This bug affects users working with heterogeneous graphs where some edge types have no edges - a common scenario in real-world applications:

  • Recommendation systems (users who haven't rated certain items)
  • Knowledge graphs (missing relationship types)
  • Social networks (disconnected components)

Testing

Added comprehensive test coverage in test/utils/test_num_nodes.py:

  • Mixed empty and non-empty edge indices
  • All empty edge indices
  • Empty edge indices with provided num_nodes_dict

All tests pass successfully.

Checklist

  • Bug fix identified and reproduced
  • Fix implemented with proper edge case handling
  • Comprehensive tests added
  • All tests pass
  • Commit message follows repository conventions

This commit fixes a bug where maybe_num_nodes_dict would crash with a
RuntimeError when processing heterogeneous graphs containing empty edge
indices. The error occurred because calling .max() on an empty tensor
requires specifying a reduction dimension.

The fix adds checks for empty tensors (numel() > 0) before calling .max(),
returning 0 for empty edge indices, which is the correct behavior for
graphs with no edges of a particular type.

Added comprehensive test coverage for:
- Mixed empty and non-empty edge indices
- All empty edge indices
- Empty edge indices with provided num_nodes_dict

This bug would affect users working with heterogeneous graphs where some
edge types have no edges, which is a common scenario in real-world
applications like recommendation systems or knowledge graphs.

Co-authored-by: Ona <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant