python_binding.cc 2.53 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/isin.h>
9
#include <graphbolt/serialize.h>
10
#include <graphbolt/unique_and_compact.h>
11
12
13
14
15

namespace graphbolt {
namespace sampling {

TORCH_LIBRARY(graphbolt, m) {
16
17
18
19
20
  m.class_<SampledSubgraph>("SampledSubgraph")
      .def(torch::init<>())
      .def_readwrite("indptr", &SampledSubgraph::indptr)
      .def_readwrite("indices", &SampledSubgraph::indices)
      .def_readwrite(
21
          "original_row_node_ids", &SampledSubgraph::original_row_node_ids)
22
      .def_readwrite(
23
24
25
          "original_column_node_ids",
          &SampledSubgraph::original_column_node_ids)
      .def_readwrite("original_edge_ids", &SampledSubgraph::original_edge_ids)
26
      .def_readwrite("type_per_edge", &SampledSubgraph::type_per_edge);
27
28
29
30
31
32
  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)
33
      .def("type_per_edge", &CSCSamplingGraph::TypePerEdge)
34
      .def("edge_attributes", &CSCSamplingGraph::EdgeAttributes)
35
      .def("in_subgraph", &CSCSamplingGraph::InSubgraph)
36
      .def("sample_neighbors", &CSCSamplingGraph::SampleNeighbors)
37
38
39
      .def(
          "sample_negative_edges_uniform",
          &CSCSamplingGraph::SampleNegativeEdgesUniform)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
      .def("copy_to_shared_memory", &CSCSamplingGraph::CopyToSharedMemory)
      .def_pickle(
          // __getstate__
          [](const c10::intrusive_ptr<CSCSamplingGraph>& self)
              -> torch::Dict<
                  std::string, torch::Dict<std::string, torch::Tensor>> {
            return self->GetState();
          },
          // __setstate__
          [](torch::Dict<std::string, torch::Dict<std::string, torch::Tensor>>
                 state) -> c10::intrusive_ptr<CSCSamplingGraph> {
            auto g = c10::make_intrusive<CSCSamplingGraph>();
            g->SetState(state);
            return g;
          });
55
  m.def("from_csc", &CSCSamplingGraph::FromCSC);
56
57
  m.def("load_csc_sampling_graph", &LoadCSCSamplingGraph);
  m.def("save_csc_sampling_graph", &SaveCSCSamplingGraph);
58
  m.def("load_from_shared_memory", &CSCSamplingGraph::LoadFromSharedMemory);
59
  m.def("unique_and_compact", &UniqueAndCompact);
60
  m.def("isin", &IsIn);
61
62
63
64
}

}  // namespace sampling
}  // namespace graphbolt