python_binding.cc 2.25 KB
Newer Older
1
2
3
4
5
6
7
/**
 *  Copyright (c) 2023 by Contributors
 * @file python_binding.cc
 * @brief Graph bolt library Python binding.
 */

#include <graphbolt/csc_sampling_graph.h>
8
#include <graphbolt/serialize.h>
9
10
11
12
13

namespace graphbolt {
namespace sampling {

TORCH_LIBRARY(graphbolt, m) {
14
15
16
17
18
19
20
21
22
  m.class_<SampledSubgraph>("SampledSubgraph")
      .def(torch::init<>())
      .def_readwrite("indptr", &SampledSubgraph::indptr)
      .def_readwrite("indices", &SampledSubgraph::indices)
      .def_readwrite(
          "reverse_row_node_ids", &SampledSubgraph::reverse_row_node_ids)
      .def_readwrite(
          "reverse_column_node_ids", &SampledSubgraph::reverse_column_node_ids)
      .def_readwrite("reverse_edge_ids", &SampledSubgraph::reverse_edge_ids)
23
24
25
26
27
28
29
30
31
32
33
34
      .def_readwrite("type_per_edge", &SampledSubgraph::type_per_edge)
      .def_pickle(
          // __getstate__
          [](const c10::intrusive_ptr<SampledSubgraph>& self)
              -> std::vector<torch::Tensor> { return self->GetState(); },
          // __setstate__
          [](std::vector<torch::Tensor> state)
              -> c10::intrusive_ptr<SampledSubgraph> {
            auto g = c10::make_intrusive<SampledSubgraph>();
            g->SetState(state);
            return g;
          });
35
36
37
38
39
40
  m.class_<CSCSamplingGraph>("CSCSamplingGraph")
      .def("num_nodes", &CSCSamplingGraph::NumNodes)
      .def("num_edges", &CSCSamplingGraph::NumEdges)
      .def("csc_indptr", &CSCSamplingGraph::CSCIndptr)
      .def("indices", &CSCSamplingGraph::Indices)
      .def("node_type_offset", &CSCSamplingGraph::NodeTypeOffset)
41
      .def("type_per_edge", &CSCSamplingGraph::TypePerEdge)
42
      .def("edge_attributes", &CSCSamplingGraph::EdgeAttributes)
43
      .def("in_subgraph", &CSCSamplingGraph::InSubgraph)
44
      .def("sample_neighbors", &CSCSamplingGraph::SampleNeighbors)
45
46
47
      .def(
          "sample_negative_edges_uniform",
          &CSCSamplingGraph::SampleNegativeEdgesUniform)
48
      .def("copy_to_shared_memory", &CSCSamplingGraph::CopyToSharedMemory);
49
  m.def("from_csc", &CSCSamplingGraph::FromCSC);
50
51
  m.def("load_csc_sampling_graph", &LoadCSCSamplingGraph);
  m.def("save_csc_sampling_graph", &SaveCSCSamplingGraph);
52
  m.def("load_from_shared_memory", &CSCSamplingGraph::LoadFromSharedMemory);
53
54
55
56
}

}  // namespace sampling
}  // namespace graphbolt