segment_reduce.cc 2.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*!
 *  Copyright (c) 2020 by Contributors
 * \file kernel/cpu/segment_reduce.cc
 * \brief Segment reduce C APIs and definitions.
 */
#include "./segment_reduce.h"
#include <dgl/array.h>
#include <string>
#include "./spmm_binary_ops.h"

namespace dgl {
namespace aten {

/*! \brief Segment Reduce operator. */
15
template <int XPU, typename IdType, int bits>
16
17
18
19
20
21
22
void SegmentReduce(
    const std::string& op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg) {
  if (op == "sum") {
23
24
25
    SWITCH_BITS(bits, DType, {
      cpu::SegmentSum<IdType, DType>(feat, offsets, out);
    });
26
  } else if (op == "max" || op == "min") {
27
28
29
30
31
32
33
34
35
36
37
    if (op == "max") {
      SWITCH_BITS(bits, DType, {
        cpu::SegmentCmp<IdType, DType, cpu::op::Max<DType>>(
            feat, offsets, out, arg);
      });
    } else {
      SWITCH_BITS(bits, DType, {
          cpu::SegmentCmp<IdType, DType, cpu::op::Min<DType>>(
              feat, offsets, out, arg);
      });
    }
38
39
40
41
42
43
  } else {
    LOG(FATAL) << "Unsupported reduce function " << op;
  }
}

/*! \brief Backward function of segment cmp.*/
44
template <int XPU, typename IdType, int bits>
45
46
47
48
void BackwardSegmentCmp(
    NDArray feat,
    NDArray arg,
    NDArray out) {
49
50
51
  SWITCH_BITS(bits, DType, {
    cpu::BackwardSegmentCmp<IdType, DType>(feat, arg, out);
  });
52
53
}

54
template void SegmentReduce<kDLCPU, int32_t, 16>(
55
56
57
58
59
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
60
template void SegmentReduce<kDLCPU, int64_t, 16>(
61
62
63
64
65
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
66
template void SegmentReduce<kDLCPU, int32_t, 32>(
67
68
69
70
71
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
72
template void SegmentReduce<kDLCPU, int64_t, 32>(
73
74
75
76
77
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
template void SegmentReduce<kDLCPU, int32_t, 64>(
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
template void SegmentReduce<kDLCPU, int64_t, 64>(
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
template void BackwardSegmentCmp<kDLCPU, int32_t, 16>(
    NDArray feat,
    NDArray arg,
    NDArray out);
template void BackwardSegmentCmp<kDLCPU, int64_t, 16>(
    NDArray feat,
    NDArray arg,
    NDArray out);
template void BackwardSegmentCmp<kDLCPU, int32_t, 32>(
99
100
101
    NDArray feat,
    NDArray arg,
    NDArray out);
102
template void BackwardSegmentCmp<kDLCPU, int64_t, 32>(
103
104
105
    NDArray feat,
    NDArray arg,
    NDArray out);
106
template void BackwardSegmentCmp<kDLCPU, int32_t, 64>(
107
108
109
    NDArray feat,
    NDArray arg,
    NDArray out);
110
template void BackwardSegmentCmp<kDLCPU, int64_t, 64>(
111
112
113
114
115
116
    NDArray feat,
    NDArray arg,
    NDArray out);

}  // namespace aten
}  // namespace dgl