dmlc / dmlc/dgl

HeteroGraphConv documentation sample code crash

Open
#7,687 1 comment 0 reactions 0 assignees View on GitHub
bug:confirmed topic: doc
Dominant language
Python
Stars
14.3k
Forks
3.1k
PR merge metrics
No merged PRs in 30d

Description

## 🐛 Bug

## To Reproduce

Steps to reproduce the behavior:
Following the code snippet example in HeteroGraphConv documentation with an actual implementation results in a crash
[HeteroGraphConv](https://docs.dgl.ai/en/latest/generated/dgl.nn.pytorch.HeteroGraphConv.html)

```python
import dgl
import dgl.graphbolt as gb
import dgl.nn as dglnn

import torch as th
import numpy as np

n_users = 50
n_games = 10
n_stores = 5

follows_src = np.random.randint(0, n_users, 100)
follows_dst = np.random.randint(0, n_users, 100)

plays_src = np.random.randint(0, n_users, 50)
plays_dst = np.random.randint(0, n_games, 50)

sells_src = np.random.randint(0, n_stores, 20)
sells_dst = np.random.randint(0, n_games, 20)

g = dgl.heterograph(
{
("user", "follows", "user"): (follows_src, follows_dst),
("user", "plays", "game"): (plays_src, plays_dst),
("store", "sells", "game"): (sells_src, sells_dst),
}
)

input_dim = 16
out_dim = 8

hetero_conv = dglnn.HeteroGraphConv(
{
"follows": dglnn.GraphConv(input_dim, out_dim),
"plays": dglnn.GraphConv(input_dim, out_dim),
"sells": dglnn.GraphConv(input_dim, out_dim),
},
aggregate="sum",
)

h1 = {"user": th.randn((g.num_nodes("user"), input_dim))}

h2 = hetero_conv(g, h1)
print(h2.keys())
```

```shell
python heterographconv_example.py
Traceback (most recent call last):
File "/Users/yuri/remix/projects/easy-platform/platform-python/pinsage-recommender/heterographconv_example.py", line 44, in
h2 = hetero_conv(g, h1)
^^^^^^^^^^^^^^^^^^
File "/Users/yuri/remix/projects/easy-platform/platform-python/pinsage-recommender/.venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/yuri/remix/projects/easy-platform/platform-python/pinsage-recommender/.venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/yuri/remix/projects/easy-platform/platform-python/pinsage-recommender/.venv/lib/python3.11/site-packages/dgl-2.3.0-py3.11-macosx-11.1-arm64.egg/dgl/nn/pytorch/hetero.py", line 212, in forward
(inputs[stype], inputs[dtype]),
~~~~~~^^^^^^^
KeyError: 'game'
```
## Expected behavior
```shell
dict_keys(['user', 'game'])
```

## Environment

- DGL Version: 2.3.0
- OS: macOS Sonoma 14.5
- How you installed DGL: source (HEAD detached at v2.3.0)
- Build command you used:
```shell
cmake -DBUILD_CPP_TEST=OFF -DUSE_OPENMP=OFF -DUSE_LIBXSMM=OFF ..
make -j4
```
- Python version: Python 3.11.5

## Additional context

When 'game' features are added to **h1** the example works
Not sure, if this is an implementation bug or a documentation issue.
It seems reasonable to expect the example code to work as shown in documentation in case 'game' nodes don't have any intrinsic features
Also, when a block is passed into the forward function instead of a graph, the failure is silent and the call returns an empty dictionary

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.