index_select.cc 1.61 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 "./index_select.h"

9
10
#include <graphbolt/cuda_ops.h>
#include <graphbolt/fused_csc_sampling_graph.h>
11

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

namespace graphbolt {
namespace ops {

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

27
std::tuple<torch::Tensor, torch::Tensor> IndexSelectCSC(
28
29
    torch::Tensor indptr, torch::Tensor indices, torch::Tensor nodes,
    torch::optional<int64_t> output_size) {
30
31
  TORCH_CHECK(
      indices.sizes().size() == 1, "IndexSelectCSC only supports 1d tensors");
32
33
  if (utils::is_on_gpu(nodes) && utils::is_accessible_from_gpu(indptr) &&
      utils::is_accessible_from_gpu(indices)) {
34
    GRAPHBOLT_DISPATCH_CUDA_ONLY_DEVICE(
35
        c10::DeviceType::CUDA, "IndexSelectCSCImpl",
36
        { return IndexSelectCSCImpl(indptr, indices, nodes, output_size); });
37
38
39
40
41
  }
  // @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.");
42
  sampling::FusedCSCSamplingGraph g(indptr, indices);
43
44
45
46
  const auto res = g.InSubgraph(nodes);
  return std::make_tuple(res->indptr, res->indices);
}

47
48
}  // namespace ops
}  // namespace graphbolt