dmlc / dmlc/dgl

Issues in converting block to homo graph to use HGTConv

Open
#4,482 2 comments 0 reactions 0 assignees View on GitHub
feature request
Dominant language
Python
Stars
14.3k
Forks
3.1k
PR merge metrics
No merged PRs in 30d

Description

## 🐛 Bug

Hi, there.

I wish to use HGTConv on blocks generated by dataloader.

My graph actually has 2 type of nodes, named A and B. It seems HGTConv accepts only homo graph input, as it will assign node embedding tensor (not dict) directly to graph ndata.

So i tried to convert my block to homo graph using dgl.to_homogeneous(), but then it has 4 node types and doubled node numbers. Tries to make it work, as node numbers get doubled, i need to concat twice my node input like h = torch.cat([h, h]) and adjust num_ntypes = num_ntypes * 2, which is quite weird.

I know in a block source nodes and dest nodes are marked differently, but i have no clue how can i use HGTConv properly when input is a block.

Any help is appreciated.

## To Reproduce

```import dgl
import torch
from dgl.nn.pytorch.conv import HGTConv

src = torch.randint(0, 1000, (100000,)).int()
dst = torch.randint(0, 1000, (100000,)).int()

hg = {('A', 'AB', 'B'): (src, dst),
('B', 'BA', 'A'): (dst, src)}
hg = dgl.heterograph(hg)

sampler = dgl.dataloading.MultiLayerNeighborSampler([10, 5])
sampler = dgl.dataloading.as_edge_prediction_sampler(
sampler, negative_sampler=dgl.dataloading.negative_sampler.Uniform(5))
edge_indices = {etype: torch.arange(0, end=hg.num_edges(etype), dtype=torch.int32) for etype in hg.canonical_etypes}

hg.ndata['x'] = {'A': torch.rand(1000, 64), 'B': torch.rand(1000, 64)}

loader = dgl.dataloading.DataLoader(
hg, edge_indices, sampler,
batch_size=1024,
shuffle=False,
drop_last=False,
num_workers=0)

hgt_layer = HGTConv(in_size=64,
head_size=16,
num_heads=4,
num_ntypes=4, # 4 works, not 2
num_etypes=4,
dropout=0.2,
use_norm=False)

for input_nodes, positive_graph, negative_graph, blocks in loader:
input_features = blocks[0].srcdata['x']
block = dgl.to_homogeneous(blocks[0])
x = torch.cat(list(input_features.values()), dim=0)
x = hgt_layer(g=block, ntype=block.ndata[dgl.NTYPE], etype=block.edata[dgl.ETYPE], x=torch.cat([x, x]), presorted=True) # weird
```

## Expected behavior

## Environment

- DGL Version (e.g., 1.0): 0.8.1
- Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3): pytorch 1.8.1
- OS (e.g., Linux): Mac OS
- How you installed DGL (`conda`, `pip`, source): pip
- Build command you used (if compiling from source):
- Python version: 3.7
- CUDA/cuDNN version (if applicable): N/A
- GPU models and configuration (e.g. V100): N/A
- Any other relevant information:

## Additional context

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.