Try to construct DGLGraph in C++

Hello,

I hope this message finds you well. I am currently attempting to construct a heterograph in C++ to accelerate my data processing pipeline. Could you please advise me on how to pass the generated DGLGraph back to Python? Below is my current attempt:

struct MetaGraph{
    dgl::HeteroGraphRef hgidx;
    py::object get_hgidx(){
        return py::cast(hgidx);
    }
};

dgl::HeteroGraphRef transformToMetaGraph(Graph& g){
    
    std::vector<int> from_vec{1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
    std::vector<int> to_vec{0, 1, 2, 1, 2, 1, 2, 3, 1, 2};

    int agn;
    int bn;

    auto from = dgl::IdArray::FromVector(from_vec);
    auto to = dgl::IdArray::FromVector(to_vec);
    auto metagraph = dgl::GraphRef(dgl::ImmutableGraph::CreateFromCOO(4, from, to));

    std::vector<dgl::HeteroGraphRef> rel_graphs;
    dgl::dgl_format_code_t code = 1;

    auto row = dgl::IdArray::FromVector(g.ba.first);
    auto col = dgl::IdArray::FromVector(g.ba.second);
    auto hgptr = dgl::CreateFromCOO(
          2, bn, agn, row, col, false, false, code);
    rel_graphs.emplace_back(dgl::HeteroGraphRef(hgptr));    
    // ... 

    std::vector<dgl::HeteroGraphPtr> rel_ptrs;
    rel_ptrs.reserve(rel_graphs.size());
    for (const auto& ref : rel_graphs) {
      rel_ptrs.push_back(ref.sptr());
    }
    auto hgptr_f = dgl::CreateHeteroGraph(
        metagraph.sptr(), rel_ptrs, {agn, bn, sn, spn});
    return dgl::HeteroGraphRef(hgptr_f);
}

PYBIND11_MODULE(GraphExtract, m) {

    m.def("extractFeature", &extractFeature, "A func");
    py::class_<MetaGraph, std::shared_ptr<MetaGraph>>(m, "MetaGraph")
        .def(py::init<>())
        .def("get_hgidx", &MetaGraph::get_hgidx);
}

When using get_hgidx in Python, the following prompt occurs:

g.get_hgidx()
TypeError: Unregistered type : dgl::HeteroGraphRef

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: Unable to convert function return value to a Python type! The signature was
	(self: GraphExtract.MetaGraph) -> object

How can I conveniently read the DGLGraph constructed in C++ in Python? Alternatively, could you tell me any methods to accelerate the construction of heterographs?

Thank you very much for your assistance.

Best regards.

The main DGL code currently does not support PyBind11. But starting from DGL 2.0 we have shipped GraphBolt which supports LibTorch-like Python bindings:
dgl/python/dgl/graphbolt at master · dmlc/dgl · GitHub (Python)
dgl/graphbolt at master · dmlc/dgl · GitHub (C++)

That’s helpful. Thank you for your reply!

Hello, I apologize for bothering you again. I have read through the code and documentation of Graphbolt. Are you suggesting creating a FusedCSCSamplingGraph in C++ and then passing it to Python via binding?
If so, I have another question. My current task is graph-level, so I don’t intend to sample nodes and edges. I noticed that the provided examples are all for tasks involving nodes and edges, and they also involve sampling. Is there a way to convert FusedCSCSamplingGraph to a DGLGraph directly?

Besides, an error was thrown when running the example provided in dgl/notebooks/graphbolt/walkthrough.ipynb:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[6], line 14
     11 eid = torch.tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
     12                     14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
     13 edge_attributes = {gb.ORIGINAL_EDGE_ID: eid}
---> 14 graph = gb.from_fused_csc(indptr, indices, None, None, edge_attributes, None)
     16 num_nodes = 10
     17 num_edges = 25

AttributeError: module 'dgl.graphbolt' has no attribute 'from_fused_csc'

Thank you for your generous assistance.

The tutorial may need to be updated. The actual API name is fused_csc_sampling_graph and its documentation is here:
https://docs.dgl.ai/en/latest/generated/dgl.graphbolt.fused_csc_sampling_graph.html#dgl.graphbolt.fused_csc_sampling_graph

1 Like