python_binding.cc 4.3 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
14
#ifdef GRAPHBOLT_USE_CUDA
#include "./cuda/max_uva_threads.h"
#endif
15
#include "./expand_indptr.h"
16
#include "./index_select.h"
17
#include "./random.h"
18

19
20
21
22
#ifdef GRAPHBOLT_USE_CUDA
#include "./cuda/gpu_cache.h"
#endif

23
24
25
26
namespace graphbolt {
namespace sampling {

TORCH_LIBRARY(graphbolt, m) {
27
  m.class_<FusedSampledSubgraph>("FusedSampledSubgraph")
28
      .def(torch::init<>())
29
30
      .def_readwrite("indptr", &FusedSampledSubgraph::indptr)
      .def_readwrite("indices", &FusedSampledSubgraph::indices)
31
      .def_readwrite(
32
          "original_row_node_ids", &FusedSampledSubgraph::original_row_node_ids)
33
      .def_readwrite(
34
          "original_column_node_ids",
35
36
37
38
          &FusedSampledSubgraph::original_column_node_ids)
      .def_readwrite(
          "original_edge_ids", &FusedSampledSubgraph::original_edge_ids)
      .def_readwrite("type_per_edge", &FusedSampledSubgraph::type_per_edge);
39
40
41
42
43
44
45
  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)
46
47
      .def("node_type_to_id", &FusedCSCSamplingGraph::NodeTypeToID)
      .def("edge_type_to_id", &FusedCSCSamplingGraph::EdgeTypeToID)
48
      .def("node_attributes", &FusedCSCSamplingGraph::NodeAttributes)
49
50
51
52
53
      .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)
54
55
      .def("set_node_type_to_id", &FusedCSCSamplingGraph::SetNodeTypeToID)
      .def("set_edge_type_to_id", &FusedCSCSamplingGraph::SetEdgeTypeToID)
56
      .def("set_node_attributes", &FusedCSCSamplingGraph::SetNodeAttributes)
57
58
59
      .def("set_edge_attributes", &FusedCSCSamplingGraph::SetEdgeAttributes)
      .def("in_subgraph", &FusedCSCSamplingGraph::InSubgraph)
      .def("sample_neighbors", &FusedCSCSamplingGraph::SampleNeighbors)
60
61
62
      .def(
          "temporal_sample_neighbors",
          &FusedCSCSamplingGraph::TemporalSampleNeighbors)
63
      .def("copy_to_shared_memory", &FusedCSCSamplingGraph::CopyToSharedMemory)
64
65
      .def_pickle(
          // __getstate__
66
          [](const c10::intrusive_ptr<FusedCSCSamplingGraph>& self)
67
68
69
70
71
72
              -> torch::Dict<
                  std::string, torch::Dict<std::string, torch::Tensor>> {
            return self->GetState();
          },
          // __setstate__
          [](torch::Dict<std::string, torch::Dict<std::string, torch::Tensor>>
73
74
                 state) -> c10::intrusive_ptr<FusedCSCSamplingGraph> {
            auto g = c10::make_intrusive<FusedCSCSamplingGraph>();
75
76
77
            g->SetState(state);
            return g;
          });
78
79
80
81
82
83
#ifdef GRAPHBOLT_USE_CUDA
  m.class_<cuda::GpuCache>("GpuCache")
      .def("query", &cuda::GpuCache::Query)
      .def("replace", &cuda::GpuCache::Replace);
  m.def("gpu_cache", &cuda::GpuCache::Create);
#endif
84
  m.def("fused_csc_sampling_graph", &FusedCSCSamplingGraph::Create);
85
86
  m.def(
      "load_from_shared_memory", &FusedCSCSamplingGraph::LoadFromSharedMemory);
87
  m.def("unique_and_compact", &UniqueAndCompact);
88
  m.def("isin", &IsIn);
89
  m.def("index_select", &ops::IndexSelect);
90
  m.def("index_select_csc", &ops::IndexSelectCSC);
91
  m.def("set_seed", &RandomEngine::SetManualSeed);
92
93
94
#ifdef GRAPHBOLT_USE_CUDA
  m.def("set_max_uva_threads", &cuda::set_max_uva_threads);
#endif
95
96
97
98
99
100
101
102
103
104
105
#ifdef HAS_IMPL_ABSTRACT_PYSTUB
  m.impl_abstract_pystub("dgl.graphbolt.base", "//dgl.graphbolt.base");
#endif
  m.def(
      "expand_indptr(Tensor indptr, ScalarType dtype, Tensor? node_ids, "
      "SymInt? output_size) -> Tensor"
#ifdef HAS_PT2_COMPLIANT_TAG
      ,
      {at::Tag::pt2_compliant_tag}
#endif
  );
106
107
108
109
}

}  // namespace sampling
}  // namespace graphbolt