Data type and device context throw error since update to DGL Version 0.5.1

Hi there!

I have updated to the new version of DGL 0.5.1 (thanks so much for the GPU support!) and am getting an error with the data type torch.int64 and the device context coda:0 when I am trying to build a subgraph.

Here is a code snippet:

data_dict = {
('user', 'plays', 'game'): (torch.tensor([0, 1, 2, 3]), torch.tensor([0, 0, 1, 1])),
('person', 'watches', 'movie'): (torch.tensor([0, 1,  2]), torch.tensor([0, 0, 0]))
}
num_nodes_dict = {'user': 4, 'game': 2, 'person': 3, 'movie': 1}
device = torch.device('cuda')
parent_graph = dgl.heterograph(data_dict=graph_dict, num_nodes_dict=num_nodes_dict, device=device)
nodes_dict_tensor = {'user': torch.tensor([0,1]), 'game': torch.tensor([0])}
subgraph = dgl.node_subgraph(graph=parent_graph, nodes=nodes_dict_tensor)

The error I am getting is as follows:
dgl._ffi.base.DGLError: Expect argument "nodes["user"]" to have data type torch.int64 and device context cpu. But got torch.int64 and cuda:0.

What should the input format of the data_dict or the nodes_dict_tensor be like to avoid this error?

I would be thankful for a hint!

Best,
Sophia

Try replacing

nodes_dict_tensor = {'user': torch.tensor([0,1]), 'game': torch.tensor([0])}

with

nodes_dict_tensor = {'user': torch.tensor([0,1]).to(device), 'game': torch.tensor([0]).to(device)}

In generally, once your graph is on GPU, the arguments for graph-related operations should be on GPU as well.

1 Like

@mufeili I added the .to(device) to the tensors, but the error still remains. What I was wondering for the DGLHeteroGraph subgraph is if the subgraph is maybe not recognised to be on GPU, could that be ?

That’s strange. I just tried running the code snippet below and it seems to be working well.

data_dict = {
('user', 'plays', 'game'): (torch.tensor([0, 1, 2, 3]), torch.tensor([0, 0, 1, 1])),
('person', 'watches', 'movie'): (torch.tensor([0, 1,  2]), torch.tensor([0, 0, 0]))
}
num_nodes_dict = {'user': 4, 'game': 2, 'person': 3, 'movie': 1}
device = torch.device('cuda')
parent_graph = dgl.heterograph(data_dict=data_dict, num_nodes_dict=num_nodes_dict, device=device)
nodes_dict_tensor = {'user': torch.tensor([0,1]).to(device), 'game': torch.tensor([0]).to(device)}
subgraph = dgl.node_subgraph(graph=parent_graph, nodes=nodes_dict_tensor)
1 Like

@mufeili It turns out I didn’t pass the device to construct the parent_graph, so it automatically constructed it on CPU. Problem solved now :wink:

1 Like