Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any integer tensors when checking edge_index input to message passing #5281

Merged
merged 4 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Do not fill `InMemoryDataset` cache on `dataset.num_features` ([#5264](https://github.com/pyg-team/pytorch_geometric/pull/5264))
- Changed tests relying on `dblp` datasets to instead use synthetic data ([#5250](https://github.com/pyg-team/pytorch_geometric/pull/5250))
- Fixed a bug for the initialization of activation function examples in `custom_graphgym` ([#5243](https://github.com/pyg-team/pytorch_geometric/pull/5243))
- Allow any integer tensors when checking edge_index input to message passing ([5281](https://github.com/pyg-team/pytorch_geometric/pull/5281))
### Removed

## [2.1.0] - 2022-08-17
Expand Down
19 changes: 19 additions & 0 deletions test/nn/conv/test_message_passing.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,22 @@ def test_message_passing_with_aggr_module(aggr_module):
out = conv(x, edge_index)
assert out.size(0) == 4 and out.size(1) in {8, 16}
assert torch.allclose(conv(x, adj.t()), out)


def test_message_passing_int32_edge_index():
# Check that we can dispatch an int32 edge_index up to aggregation
x = torch.randn(4, 8)
edge_index = torch.tensor([[0, 1, 2, 3], [0, 0, 1, 1]], dtype=torch.int32)
edge_weight = torch.randn(edge_index.shape[1])

# Use a hook to promote the edge_index to long to workaround PyTorch CPU
# backend restriction to int64 for the index.
def cast_index_hook(module, inputs):
input_dict = inputs[-1]
input_dict['index'] = input_dict['index'].long()
return (input_dict, )

conv = MyConv(8, 32)
conv.register_aggregate_forward_pre_hook(cast_index_hook)

assert conv(x, edge_index, edge_weight).size() == (4, 32)
10 changes: 6 additions & 4 deletions torch_geometric/nn/conv/message_passing.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ def __check_input__(self, edge_index, size):
the_size: List[Optional[int]] = [None, None]

if isinstance(edge_index, Tensor):
if not edge_index.dtype == torch.long:
raise ValueError(f"Expected 'edge_index' to be of type "
f"'torch.long' (got '{edge_index.dtype}')")
int_dtypes = (torch.uint8, torch.int8, torch.int32, torch.int64)

if edge_index.dtype not in int_dtypes:
raise ValueError(f"Expected 'edge_index' to be of integer "
f"type (got '{edge_index.dtype}')")
if edge_index.dim() != 2:
raise ValueError(f"Expected 'edge_index' to be two-dimensional"
f" (got {edge_index.dim()} dimensions)")
Expand All @@ -211,7 +213,7 @@ def __check_input__(self, edge_index, size):
return the_size

raise ValueError(
('`MessagePassing.propagate` only supports `torch.LongTensor` of '
('`MessagePassing.propagate` only supports integer tensors of '
'shape `[2, num_messages]` or `torch_sparse.SparseTensor` for '
'argument `edge_index`.'))

Expand Down