Repository: dmlc/dgl

Issue:
bug fix dgl/examples/pytorch/rgcn/entity_classify.py / and dgl/examples/pytorch/rgcn/link_predict.py 
## 🐛 Bug

Both dgl/examples/pytorch/rgcn/entity_classify.py / and dgl/examples/pytorch/rgcn/link_predict.py  throw errors that are corrected when .long() are added to passed variables

I used the following to correct
link_predict.py
test_node_id, test_rel, test_norm =test_node_id.long(), test_rel.long(), test_norm.long()

entity_classify.py
    if use_cuda:
        torch.cuda.set_device(args.gpu)
        feats = feats.cuda()
        edge_type = edge_type.cuda().long()
        edge_norm = edge_norm.cuda()
        labels = labels.cuda().long()


start training...
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-6-3b5ea77cdea8> in <module>
    171     print(args)
    172     args.bfs_level = args.n_layers + 1 # pruning used nodes for memory
--> 173     main(args)

<ipython-input-6-3b5ea77cdea8> in main(args)
    110         optimizer.zero_grad()
    111         t0 = time.time()
--> 112         logits = model(g, feats, edge_type, edge_norm)
    113         loss = F.cross_entropy(logits[train_idx], labels[train_idx])
    114         t1 = time.time()

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~\Jupyter\0TensorFlow2\pytorch\dgl\pytorch\rgcn\model.py in forward(self, g, h, r, norm)
     45     def forward(self, g, h, r, norm):
     46         for layer in self.layers:
---> 47             h = layer(g, h, r, norm)
     48         return h

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~\Anaconda3\lib\site-packages\dgl\nn\pytorch\conv\relgraphconv.py in forward(self, g, x, etypes, norm)
    178             loop_message = utils.matmul_maybe_select(x, self.loop_weight)
    179         # message passing
--> 180         g.update_all(self.message_func, fn.sum(msg='msg', out='h'))
    181         # apply bias and activation
    182         node_repr = g.ndata['h']

~\Anaconda3\lib\site-packages\dgl\graph.py in update_all(self, message_func, reduce_func, apply_node_func)
   2745                                           reduce_func=reduce_func,
   2746                                           apply_func=apply_node_func)
-> 2747             Runtime.run(prog)
   2748 
   2749     def prop_nodes(self,

~\Anaconda3\lib\site-packages\dgl\runtime\runtime.py in run(prog)
      9         for exe in prog.execs:
     10             # prog.pprint_exe(exe)
---> 11             exe.run()

~\Anaconda3\lib\site-packages\dgl\runtime\ir\executor.py in run(self)
    202         edge_data = self.fdedge.data
    203         dst_data = self.fddst.data
--> 204         udf_ret = fn_data(src_data, edge_data, dst_data)
    205         self.ret.data = FrameRef(Frame(udf_ret))
    206 

~\Anaconda3\lib\site-packages\dgl\runtime\scheduler.py in _mfunc_wrapper(src_data, edge_data, dst_data)
    947         ebatch = EdgeBatch((u.data, v.data, eid.data),
    948                            src_data, edge_data, dst_data)
--> 949         return mfunc(ebatch)
    950     _mfunc_wrapper = var.FUNC(_mfunc_wrapper)
    951     msg = ir.EDGE_UDF(_mfunc_wrapper, fdsrc, fdedge, fddst)

~\Anaconda3\lib\site-packages\dgl\nn\pytorch\conv\relgraphconv.py in basis_message_func(self, edges)
    131             weight = self.weight
    132 
--> 133         msg = utils.bmm_maybe_select(edges.src['h'], weight, edges.data['type'])
    134         if 'norm' in edges.data:
    135             msg = msg * edges.data['norm']

~\Anaconda3\lib\site-packages\dgl\nn\pytorch\utils.py in bmm_maybe_select(A, B, index)
     88         return B.index_select(0, flatidx)
     89     else:
---> 90         BB = B.index_select(0, index)
     91         return th.bmm(A.unsqueeze(1), BB).squeeze()
     92 

RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #3 'index' in call to _th_index_select

## Environment

 - DGL Version (e.g., 1.0):   dgl.__version__ = '0.4.2'
 - Backend Library & Version (e.g., PyTorch 0.4.1, MXNet/Gluon 1.3): torch.__version__ = '1.4.0'
 - OS (e.g., Linux): Windows
 - How you installed DGL (`conda`, `pip`, source):  pip install
 - Build command you used (if compiling from source):
 - Python version:   Python 3.7.4
 - CUDA/cuDNN version (if applicable): CUDA v10.1
 - GPU models and configuration (e.g. V100): GTX 980M
 - Any other relevant information:

## Additional context

<!-- Add any other context about the problem here. -->



Discussion:

Resolving patch:
diff --git a/examples/pytorch/rgcn/entity_classify.py b/examples/pytorch/rgcn/entity_classify.py
--- a/examples/pytorch/rgcn/entity_classify.py
+++ b/examples/pytorch/rgcn/entity_classify.py
@@ -63,9 +63,9 @@ def main(args):
     feats = torch.arange(num_nodes)
 
     # edge type and normalization factor
-    edge_type = torch.from_numpy(data.edge_type)
-    edge_norm = torch.from_numpy(data.edge_norm).unsqueeze(1)
-    labels = torch.from_numpy(labels).view(-1)
+    edge_type = torch.from_numpy(data.edge_type).long()
+    edge_norm = torch.from_numpy(data.edge_norm).unsqueeze(1).long()
+    labels = torch.from_numpy(labels).view(-1).long()
 
     # check cuda
     use_cuda = args.gpu >= 0 and torch.cuda.is_available()
diff --git a/examples/pytorch/rgcn/utils.py b/examples/pytorch/rgcn/utils.py
--- a/examples/pytorch/rgcn/utils.py
+++ b/examples/pytorch/rgcn/utils.py
@@ -141,7 +141,7 @@ def build_graph_from_triplets(num_nodes, num_rels, triplets):
     g.add_edges(src, dst)
     norm = comp_deg_norm(g)
     print("# nodes: {}, # edges: {}".format(num_nodes, len(src)))
-    return g, rel, norm
+    return g, rel.astype('int64'), norm.astype('int64')
 
 def build_test_graph(num_nodes, num_rels, edges):
     src, rel, dst = edges.transpose()


Test patch: