index_select.cc 1.48 KB
Newer Older
1
2
3
4
5
/**
 *  Copyright (c) 2023 by Contributors
 * @file index_select.cc
 * @brief Index select operators.
 */
6
7
#include <graphbolt/cuda_ops.h>
#include <graphbolt/fused_csc_sampling_graph.h>
8
9

#include "./macro.h"
10
#include "./utils.h"
11
12
13
14
15

namespace graphbolt {
namespace ops {

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

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

43
44
}  // namespace ops
}  // namespace graphbolt