segment_reduce.cc 3.67 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
  } else {
    LOG(FATAL) << "Unsupported reduce function " << op;
  }
}

43
44
45
46
47
48
49
50
51
52
/*! \brief Scatter Add.*/
template <int XPU, typename IdType, int bits>
void ScatterAdd(NDArray feat,
                NDArray idx,
                NDArray out) {
  SWITCH_BITS(bits, DType, {
    cpu::ScatterAdd<IdType, DType>(feat, idx, out);
  });
}

53
/*! \brief Backward function of segment cmp.*/
54
template <int XPU, typename IdType, int bits>
55
56
57
58
void BackwardSegmentCmp(
    NDArray feat,
    NDArray arg,
    NDArray out) {
59
60
61
  SWITCH_BITS(bits, DType, {
    cpu::BackwardSegmentCmp<IdType, DType>(feat, arg, out);
  });
62
63
}

64
template void SegmentReduce<kDLCPU, int32_t, 16>(
65
66
67
68
69
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
70
template void SegmentReduce<kDLCPU, int64_t, 16>(
71
72
73
74
75
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
76
template void SegmentReduce<kDLCPU, int32_t, 32>(
77
78
79
80
81
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
82
template void SegmentReduce<kDLCPU, int64_t, 32>(
83
84
85
86
87
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
88
89
90
91
92
93
94
95
96
97
98
99
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);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
template void ScatterAdd<kDLCPU, int32_t, 16>(
    NDArray feat,
    NDArray idx,
    NDArray out);
template void ScatterAdd<kDLCPU, int64_t, 16>(
    NDArray feat,
    NDArray idx,
    NDArray out);
template void ScatterAdd<kDLCPU, int32_t, 32>(
    NDArray feat,
    NDArray idx,
    NDArray out);
template void ScatterAdd<kDLCPU, int64_t, 32>(
    NDArray feat,
    NDArray idx,
    NDArray out);
template void ScatterAdd<kDLCPU, int32_t, 64>(
    NDArray feat,
    NDArray idx,
    NDArray out);
template void ScatterAdd<kDLCPU, int64_t, 64>(
    NDArray feat,
    NDArray arg,
    NDArray out);
124
125
126
127
128
129
130
131
132
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>(
133
134
135
    NDArray feat,
    NDArray arg,
    NDArray out);
136
template void BackwardSegmentCmp<kDLCPU, int64_t, 32>(
137
138
139
    NDArray feat,
    NDArray arg,
    NDArray out);
140
template void BackwardSegmentCmp<kDLCPU, int32_t, 64>(
141
142
143
    NDArray feat,
    NDArray arg,
    NDArray out);
144
template void BackwardSegmentCmp<kDLCPU, int64_t, 64>(
145
146
147
148
149
150
    NDArray feat,
    NDArray arg,
    NDArray out);

}  // namespace aten
}  // namespace dgl