Message Passing with UDF

Dear DGL-Forum,
i am new to DGL and currently working on a way to compute alle paths of a fixed length leading to all nodes. As a first step i would like each node to store a tensor containing the edge_id that leads to the node, as well as the source node of the path. So the result should look like this:

[
    [predecessor_node_id, edge_id connecting predecessor and this node, predecessor_node_id], 
    [predecessor_node_id, edge_id connecting predecessor and this node, predecessor_node_id],
    ...
]

I have tried the following approach which leads to an error message (see below), so i am thinking i miss something fundamental about the API. Please enlighten me about my wrong doings :slight_smile:

import dgl
import torch
import numpy as np

def initial_send(edges):
    helper = torch.stack((edges._eid, g.find_edges(edges._eid)[0], edges._eid))
    return {"send_message": helper.T}

def initial_reduce(nodes):
    return {"recieved message": nodes.mailbox["send_message"]}

if __name__ == '__main__':
    src = np.array([0, 1, 2, 2, 3, 3, 4, 3])
    dst = np.array([1, 2, 3, 1, 1, 2, 2, 2])
    g = dgl.graph((src, dst))
    g.update_all(initial_send,initial_reduce)

RuntimeError: Sizes of tensors must match except in dimension 0. Got 1 and 3 in dimension 1 (The offending index is 1)

In your case, after calling message func initial_send, some nodes receives multiple messages. you could try to add print(nodes.mailbox["send_message"]) at the beginning of initial_reduce to check this. You need to reduce these messages into one for each node. For example, use fn.sum(): dgl.function — DGL 0.7.2 documentation

Hi, thank you very much for your answer! In my case i would like to have a reduce function that only collects the input messages without reducing them to the same dimensionality. So for example, node 1: holds a tensor [[1,0,1],[2,1,2]] while node two holds [[2,2,2],[3,5,3],[1,7,1]]. Is this possible using the message passing API? Since my post i have been playing around with this a bit further and tried to concatenate all the received messages into one tensor, but i still run into an indexing issue.

so you want nodes hold different shape of tensor as feature? I don’t it’s feasible as the dimension for each node should be same.

Yeah, they should have different shapes. The idea is (most likely was) to repeatedly pass the node-edge pairs to finally obtain all paths of a certain length leading to each node. But if this is not possible i think i need to improvise. Nevertheless, thank you for your advice.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.