neighbor_sample.cpp 2.64 KB
Newer Older
1
#ifdef WITH_PYTHON
rusty1s's avatar
rusty1s committed
2
#include <Python.h>
3
#endif
rusty1s's avatar
rusty1s committed
4
5
6
7
8
#include <torch/script.h>

#include "cpu/neighbor_sample_cpu.h"

#ifdef _WIN32
9
#ifdef WITH_PYTHON
rusty1s's avatar
rusty1s committed
10
11
12
13
14
15
#ifdef WITH_CUDA
PyMODINIT_FUNC PyInit__neighbor_sample_cuda(void) { return NULL; }
#else
PyMODINIT_FUNC PyInit__neighbor_sample_cpu(void) { return NULL; }
#endif
#endif
16
#endif
rusty1s's avatar
rusty1s committed
17
18

// Returns 'output_node', 'row', 'col', 'output_edge'
19
20
SPARSE_API
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>
rusty1s's avatar
rusty1s committed
21
22
23
24
25
26
27
28
neighbor_sample(const torch::Tensor &colptr, const torch::Tensor &row,
                const torch::Tensor &input_node,
                const std::vector<int64_t> num_neighbors, const bool replace,
                const bool directed) {
  return neighbor_sample_cpu(colptr, row, input_node, num_neighbors, replace,
                             directed);
}

29
30
SPARSE_API
std::tuple<c10::Dict<node_t, torch::Tensor>, c10::Dict<rel_t, torch::Tensor>,
rusty1s's avatar
rusty1s committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
           c10::Dict<rel_t, torch::Tensor>, c10::Dict<rel_t, torch::Tensor>>
hetero_neighbor_sample(
    const std::vector<node_t> &node_types,
    const std::vector<edge_t> &edge_types,
    const c10::Dict<rel_t, torch::Tensor> &colptr_dict,
    const c10::Dict<rel_t, torch::Tensor> &row_dict,
    const c10::Dict<node_t, torch::Tensor> &input_node_dict,
    const c10::Dict<rel_t, std::vector<int64_t>> &num_neighbors_dict,
    const int64_t num_hops, const bool replace, const bool directed) {
  return hetero_neighbor_sample_cpu(
      node_types, edge_types, colptr_dict, row_dict, input_node_dict,
      num_neighbors_dict, num_hops, replace, directed);
}

Rex Ying's avatar
Rex Ying committed
45
46
std::tuple<c10::Dict<node_t, torch::Tensor>, c10::Dict<rel_t, torch::Tensor>,
           c10::Dict<rel_t, torch::Tensor>, c10::Dict<rel_t, torch::Tensor>>
47
hetero_temporal_neighbor_sample(
Rex Ying's avatar
Rex Ying committed
48
49
50
51
52
53
54
55
    const std::vector<node_t> &node_types,
    const std::vector<edge_t> &edge_types,
    const c10::Dict<rel_t, torch::Tensor> &colptr_dict,
    const c10::Dict<rel_t, torch::Tensor> &row_dict,
    const c10::Dict<node_t, torch::Tensor> &input_node_dict,
    const c10::Dict<rel_t, std::vector<int64_t>> &num_neighbors_dict,
    const c10::Dict<node_t, torch::Tensor> &node_time_dict,
    const int64_t num_hops, const bool replace, const bool directed) {
56
  return hetero_temporal_neighbor_sample_cpu(
Rex Ying's avatar
Rex Ying committed
57
58
59
60
      node_types, edge_types, colptr_dict, row_dict, input_node_dict,
      num_neighbors_dict, node_time_dict, num_hops, replace, directed);
}

rusty1s's avatar
rusty1s committed
61
62
63
static auto registry =
    torch::RegisterOperators()
        .op("torch_sparse::neighbor_sample", &neighbor_sample)
Rex Ying's avatar
Rex Ying committed
64
        .op("torch_sparse::hetero_neighbor_sample", &hetero_neighbor_sample)
65
66
        .op("torch_sparse::hetero_temporal_neighbor_sample",
            &hetero_temporal_neighbor_sample);