index_select.cc 1.58 KB
Newer Older
sangwzh's avatar
sangwzh committed
1
// !!! This is a file automatically generated by hipify!!!
2
3
4
5
6
/**
 *  Copyright (c) 2023 by Contributors
 * @file index_select.cc
 * @brief Index select operators.
 */
7
8
#include <graphbolt/cuda_ops.h>
#include <graphbolt/fused_csc_sampling_graph.h>
9

sangwzh's avatar
sangwzh committed
10
11
#include "macro.h"
#include "utils.h"
12
13
14
15
16

namespace graphbolt {
namespace ops {

torch::Tensor IndexSelect(torch::Tensor input, torch::Tensor index) {
17
  if (utils::is_on_gpu(index) && input.is_pinned()) {
18
19
20
21
22
23
24
    GRAPHBOLT_DISPATCH_CUDA_ONLY_DEVICE(
        c10::DeviceType::CUDA, "UVAIndexSelect",
        { return UVAIndexSelectImpl(input, index); });
  }
  return input.index({index.to(torch::kLong)});
}

25
std::tuple<torch::Tensor, torch::Tensor> IndexSelectCSC(
26
27
    torch::Tensor indptr, torch::Tensor indices, torch::Tensor nodes,
    torch::optional<int64_t> output_size) {
28
29
  TORCH_CHECK(
      indices.sizes().size() == 1, "IndexSelectCSC only supports 1d tensors");
30
31
  if (utils::is_on_gpu(nodes) && utils::is_accessible_from_gpu(indptr) &&
      utils::is_accessible_from_gpu(indices)) {
32
    GRAPHBOLT_DISPATCH_CUDA_ONLY_DEVICE(
33
        c10::DeviceType::CUDA, "IndexSelectCSCImpl",
34
        { return IndexSelectCSCImpl(indptr, indices, nodes, output_size); });
35
36
37
38
39
  }
  // @todo: The CPU supports only integer dtypes for indices tensor.
  TORCH_CHECK(
      c10::isIntegralType(indices.scalar_type(), false),
      "IndexSelectCSC is not implemented to slice noninteger types yet.");
40
  sampling::FusedCSCSamplingGraph g(indptr, indices);
41
42
43
44
  const auto res = g.InSubgraph(nodes);
  return std::make_tuple(res->indptr, res->indices);
}

45
46
}  // namespace ops
}  // namespace graphbolt