pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
21.01k stars 3.62k forks source link

to_dgl does not convert graph with isolated nodes or zero edges graph #9171

Closed david6l34 closed 5 months ago

david6l34 commented 5 months ago

🐛 Describe the bug

import torch
from torch_geometric.data import Data
from torch_geometric.utils.convert import to_dgl

# 1nd example zero edge

data = Data()
data['x'] = torch.tensor([[0, 1, 2], [2,3,4]])
data.validate()
to_dgl(data)
# AttributeError: 'GlobalStorage' object has no attribute 'adj_t'

# 2nd example, isolated nodes
data = Data()
data['x'] = torch.tensor([[0, 1, 2], [2,3,4]])
data['edge_index'] = torch.tensor([[0],[0]])
data.validate()
to_dgl(data)`
# DGLError: Expect number of features to match number of nodes (len(u)). Got 2 and 1 instead.

The function initialized the dgl graph using data from the edges first. Hence any isolated nodes afterwards will cause node number mismatch.

Versions

Collecting environment information... PyTorch version: 2.2.2+cpu Is debug build: False CUDA used to build PyTorch: None ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 Enterprise GCC version: Could not collect Clang version: Could not collect CMake version: Could not collect Libc version: N/A

Python version: 3.9.19 | packaged by conda-forge | (main, Mar 20 2024, 12:38:46) [MSC v.1929 64 bit (AMD64)] (64-bit runtime) Python platform: Windows-10-10.0.19045-SP0 Is CUDA available: False CUDA runtime version: No CUDA CUDA_MODULE_LOADING set to: N/A GPU models and configuration: No CUDA Nvidia driver version: No CUDA cuDNN version: No CUDA HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True

CPU:

Revision=

Versions of relevant libraries: [pip3] mypy-extensions==1.0.0 [pip3] numpy==1.26.4 [pip3] numpydoc==1.7.0 [pip3] torch==2.2.2 [pip3] torch_geometric==2.5.2 [pip3] torchdata==0.7.1 [conda] libblas 3.9.0 22_win64_mkl conda-forge [conda] libcblas 3.9.0 22_win64_mkl conda-forge [conda] liblapack 3.9.0 22_win64_mkl conda-forge [conda] mkl 2024.1.0 h66d3029_692 conda-forge [conda] numpy 1.26.4 py39hddb5d58_0 conda-forge [conda] numpydoc 1.7.0 pypi_0 pypi [conda] torch 2.2.2 pypi_0 pypi [conda] torch-geometric 2.5.2 pypi_0 pypi [conda] torchdata 0.7.1 pypi_0 pypi

rusty1s commented 5 months ago

Thanks for reporting. This will be fixed in https://github.com/pyg-team/pytorch_geometric/pull/9188.