dgl icon indicating copy to clipboard operation
dgl copied to clipboard

Inconsistent Block/hetergraph ndata assignment behavior

Open IzzyPutterman opened this issue 3 years ago • 1 comments

🐛 Bug

To Reproduce

import numpy as np
import torch
import dgl
block = {}
block[('a', 'w', 'p')] = (
    torch.from_numpy(np.arange(4)),
    torch.from_numpy(np.arange(4, 8)))
block = dgl.heterograph(block)
block.ndata['s'] = {}
block.ndata['s']['a'] = torch.zeros(4, 2)
block.ndata['s']['p'] = torch.ones(8, 2)
block.ndata['t'] = {}
block.ndata['t']['a'] = torch.zeros(4, 2)
block.ndata['t']['p'] = torch.ones(8, 2)
block = dgl.to_block(block)
print(block.srcdata)

The output on my end is defaultdict(<class 'dict'>, {'_ID': {'a': tensor([0, 1, 2, 3]), 'p': tensor([4, 5, 6, 7])}}) The only key in the dict printed is "_ID"

Now run a slight variation below
import numpy as np
import torch
import dgl
block = {}
block[('a', 'w', 'p')] = (
    torch.from_numpy(np.arange(4)),
    torch.from_numpy(np.arange(4, 8)))
block = dgl.heterograph(block)
tmp = {}
tmp['a'] = torch.zeros(4, 2)
tmp['p'] = torch.ones(8, 2)
block.ndata['s'] = tmp
block.ndata['t'] = {}
block.ndata['t']['a'] = torch.zeros(4, 2)
block.ndata['t']['p'] = torch.ones(8, 2)
block = dgl.to_block(block)
print(block.srcdata)

The output on my end is

defaultdict(<class 'dict'>, {'s': {'a': tensor([[0., 0.],
        [0., 0.],
        [0., 0.],
        [0., 0.]]), 'p': tensor([[1., 1.],
        [1., 1.],
        [1., 1.],
        [1., 1.]])}, '_ID': {'a': tensor([0, 1, 2, 3]), 'p': tensor([4, 5, 6, 7])}})

Now there should be the 't', 's', and "_ID" keys in the dict.

Expected behavior

To me, the two methods are pythonically equivalent. I dont understand why just the first ndata key needs to have a buffer while the second does not.

Environment

  • DGL Version (e.g., 1.0): 0.8 post 2

IzzyPutterman avatar Jul 11 '22 02:07 IzzyPutterman

This issue has been automatically marked as stale due to lack of activity. It will be closed if no further activity occurs. Thank you

github-actions[bot] avatar Aug 11 '22 01:08 github-actions[bot]

Duplicate of #3670.

BarclayII avatar Sep 07 '22 15:09 BarclayII