pyg-team / pytorch_geometric

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

Segmentation Fault when using LabelPropagation class #7044

Open manukastratta opened 1 year ago

manukastratta commented 1 year ago

πŸ› Describe the bug

There is a bug with the LabelPropagation class defined in this file which causes a segfault: https://github.com/pyg-team/pytorch_geometric/blob/0ed30b44a5668bc7715e8470f1f469b5e3aa3cbb/torch_geometric/nn/models/label_prop.py When I tried running the example script using this LabelPropagation class, the program segfaulted. Here is the LabelPropagation example I ran: https://github.com/pyg-team/pytorch_geometric/blob/0ed30b44a5668bc7715e8470f1f469b5e3aa3cbb/examples/label_prop.py

To reproduce: Simply run the file https://github.com/pyg-team/pytorch_geometric/blob/0ed30b44a5668bc7715e8470f1f469b5e3aa3cbb/examples/label_prop.py

Separately, I also tried instantiating an instance of the LabelPropagation class and calling its forward function with my custom dataset, and my program also segfaulted in the forward function. My suspicion (after digging through the stacktrace a bit) is that it's a problem with the way propagate is called, but I'm unsure and wasn't able to fix the bug.

out = self.propagate(edge_index, x=out, edge_weight=edge_weight, size=None); https://github.com/pyg-team/pytorch_geometric/blob/master/torch_geometric/nn/models/label_prop.py#L80

It seems like the bug does not originate from the example code of (examples/label_prop.py), but originates in the actual LabelPropagation class (models/label_prop.py), particularly in its forward function.

Stack trace:

(myPythonEnv) manukastratta@Manukas-MacBook-Air Downloads % python label_prop.py 
zsh: segmentation fault  python label_prop.py

Environment

rusty1s commented 1 year ago

Mh, this works just fine for me :( Do other PyG examples run for me? Do you know where the error might occur? Does it work when dropping the ToSparseTensor transform?


import os.path as osp

from ogb.nodeproppred import Evaluator, PygNodePropPredDataset

import torch_geometric.transforms as T
from torch_geometric.nn import LabelPropagation

root = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', 'OGB')
dataset = PygNodePropPredDataset('ogbn-arxiv', root, transform=T.Compose([
    T.ToUndirected(),
]))
split_idx = dataset.get_idx_split()
evaluator = Evaluator(name='ogbn-arxiv')
data = dataset[0]

model = LabelPropagation(num_layers=3, alpha=0.9)
out = model(data.y, data.edge_index, mask=split_idx['train'])

y_pred = out.argmax(dim=-1, keepdim=True)

val_acc = evaluator.eval({
    'y_true': data.y[split_idx['valid']],
    'y_pred': y_pred[split_idx['valid']],
})['acc']
test_acc = evaluator.eval({
    'y_true': data.y[split_idx['test']],
    'y_pred': y_pred[split_idx['test']],
})['acc']

print(f'Val: {val_acc:.4f}, Test: {test_acc:.4f}')