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

7
#include <graphbolt/fused_csc_sampling_graph.h>
8
#include <graphbolt/isin.h>
9
#include <graphbolt/serialize.h>
10
#include <graphbolt/unique_and_compact.h>
11

12
13
#include "./index_select.h"

14
15
16
17
namespace graphbolt {
namespace sampling {

TORCH_LIBRARY(graphbolt, m) {
18
  m.class_<FusedSampledSubgraph>("FusedSampledSubgraph")
19
      .def(torch::init<>())
20
21
      .def_readwrite("indptr", &FusedSampledSubgraph::indptr)
      .def_readwrite("indices", &FusedSampledSubgraph::indices)
22
      .def_readwrite(
23
          "original_row_node_ids", &FusedSampledSubgraph::original_row_node_ids)
24
      .def_readwrite(
25
          "original_column_node_ids",
26
27
28
29
          &FusedSampledSubgraph::original_column_node_ids)
      .def_readwrite(
          "original_edge_ids", &FusedSampledSubgraph::original_edge_ids)
      .def_readwrite("type_per_edge", &FusedSampledSubgraph::type_per_edge);
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  m.class_<FusedCSCSamplingGraph>("FusedCSCSamplingGraph")
      .def("num_nodes", &FusedCSCSamplingGraph::NumNodes)
      .def("num_edges", &FusedCSCSamplingGraph::NumEdges)
      .def("csc_indptr", &FusedCSCSamplingGraph::CSCIndptr)
      .def("indices", &FusedCSCSamplingGraph::Indices)
      .def("node_type_offset", &FusedCSCSamplingGraph::NodeTypeOffset)
      .def("type_per_edge", &FusedCSCSamplingGraph::TypePerEdge)
      .def("edge_attributes", &FusedCSCSamplingGraph::EdgeAttributes)
      .def("set_csc_indptr", &FusedCSCSamplingGraph::SetCSCIndptr)
      .def("set_indices", &FusedCSCSamplingGraph::SetIndices)
      .def("set_node_type_offset", &FusedCSCSamplingGraph::SetNodeTypeOffset)
      .def("set_type_per_edge", &FusedCSCSamplingGraph::SetTypePerEdge)
      .def("set_edge_attributes", &FusedCSCSamplingGraph::SetEdgeAttributes)
      .def("in_subgraph", &FusedCSCSamplingGraph::InSubgraph)
      .def("sample_neighbors", &FusedCSCSamplingGraph::SampleNeighbors)
45
46
      .def(
          "sample_negative_edges_uniform",
47
48
          &FusedCSCSamplingGraph::SampleNegativeEdgesUniform)
      .def("copy_to_shared_memory", &FusedCSCSamplingGraph::CopyToSharedMemory)
49
50
      .def_pickle(
          // __getstate__
51
          [](const c10::intrusive_ptr<FusedCSCSamplingGraph>& self)
52
53
54
55
56
57
              -> torch::Dict<
                  std::string, torch::Dict<std::string, torch::Tensor>> {
            return self->GetState();
          },
          // __setstate__
          [](torch::Dict<std::string, torch::Dict<std::string, torch::Tensor>>
58
59
                 state) -> c10::intrusive_ptr<FusedCSCSamplingGraph> {
            auto g = c10::make_intrusive<FusedCSCSamplingGraph>();
60
61
62
            g->SetState(state);
            return g;
          });
63
64
65
66
67
  m.def("from_fused_csc", &FusedCSCSamplingGraph::FromCSC);
  m.def("load_fused_csc_sampling_graph", &LoadFusedCSCSamplingGraph);
  m.def("save_fused_csc_sampling_graph", &SaveFusedCSCSamplingGraph);
  m.def(
      "load_from_shared_memory", &FusedCSCSamplingGraph::LoadFromSharedMemory);
68
  m.def("unique_and_compact", &UniqueAndCompact);
69
  m.def("isin", &IsIn);
70
  m.def("index_select", &ops::IndexSelect);
71
72
73
74
}

}  // namespace sampling
}  // namespace graphbolt