Skip to content

Function to convert Program Graph to PyTorch Geometric Graph #174

@sniarchos

Description

@sniarchos

🚀 Feature

It would be nice to have a programl.to_pyg function to convert one or more Program Graphs to torch_geometric.data.Data, i.e. to PyTorch Geometric graphs.

Motivation

This would be extremely helpful in order to set up ML/DL pipelines with custom GNNs using the PyTorch Geometric library, which offers a lot of utilities regarding machine/deep learning tasks on graphs and it is a library that seems to gain a lot of popularity lately, especially in research.

Pitch

My idea is a 1-1 map between the nodes, edges and node features of the Program Graph to the PyG Graph, as well as turning the edge type of Program Graph (i.e., the CONTROL / DATA / CALL enum values) into a single edge feature of PyG Graph. Unfortunately, PyTorch Geometric does not (yet) explicitly support graph-level features. They seem to support only node-level features, node-level targets and graph-level targets for the time being. Therefore, a reasonable thing to do is to extend the torch_geometric.data.Data object with an additional attribute, as proposed in the documentation. Extending the first introductory example from the docs:

>>> import torch
>>> from torch_geometric.data import Data
>>> 
>>> edge_index = torch.tensor([[0, 1, 1, 2],
...                            [1, 0, 2, 1]], dtype=torch.long)
>>> x = torch.tensor([[-1], [0], [1]], dtype=torch.float)
>>> 
>>> data = Data(x=x, edge_index=edge_index)
>>> data
Data(edge_index=[2, 4], x=[3, 1])
>>>
>>> data.graph_y = torch.tensor([42]) # adding a graph-level target
>>> data
Data(edge_index=[2, 4], graph_y=[1], x=[3, 1])

I believe I am not forgetting anything (feel free to remind me if I do!).

If you don't have something like that in the works and you are interested, I would love to work on it and send a PR eventually. I intend to write such a tool anyway (i.e. Program Graph -> PyG Graph), so I would love to contribute it to the project as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions