segment_reduce.cc 5.71 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
54
55
56
57
58
59
60
61
62
63
64
65
/*! \brief Update gradients for reduce operator max/min on heterogeneous graph.*/
template <int XPU, typename IdType, int bits>
void UpdateGradMinMax_hetero(const HeteroGraphPtr& g,
                const std::string& op,
                const std::vector<NDArray>& feat,
                const std::vector<NDArray>& idx,
                const std::vector<NDArray>& idx_etype,
                std::vector<NDArray>* out) {
  SWITCH_BITS(bits, DType, {
    cpu::UpdateGradMinMax_hetero<IdType, DType>(g, op, feat, idx, idx_etype, out);
  });
}

66
/*! \brief Backward function of segment cmp.*/
67
template <int XPU, typename IdType, int bits>
68
69
70
71
void BackwardSegmentCmp(
    NDArray feat,
    NDArray arg,
    NDArray out) {
72
73
74
  SWITCH_BITS(bits, DType, {
    cpu::BackwardSegmentCmp<IdType, DType>(feat, arg, out);
  });
75
76
}

77
template void SegmentReduce<kDGLCPU, int32_t, 16>(
78
79
80
81
82
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
83
template void SegmentReduce<kDGLCPU, int64_t, 16>(
84
85
86
87
88
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
89
template void SegmentReduce<kDGLCPU, int32_t, 32>(
90
91
92
93
94
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
95
template void SegmentReduce<kDGLCPU, int64_t, 32>(
96
97
98
99
100
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
101
template void SegmentReduce<kDGLCPU, int32_t, 64>(
102
103
104
105
106
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
107
template void SegmentReduce<kDGLCPU, int64_t, 64>(
108
109
110
111
112
    const std::string &op,
    NDArray feat,
    NDArray offsets,
    NDArray out,
    NDArray arg);
113
template void ScatterAdd<kDGLCPU, int32_t, 16>(
114
115
116
    NDArray feat,
    NDArray idx,
    NDArray out);
117
template void ScatterAdd<kDGLCPU, int64_t, 16>(
118
119
120
    NDArray feat,
    NDArray idx,
    NDArray out);
121
template void ScatterAdd<kDGLCPU, int32_t, 32>(
122
123
124
    NDArray feat,
    NDArray idx,
    NDArray out);
125
template void ScatterAdd<kDGLCPU, int64_t, 32>(
126
127
128
    NDArray feat,
    NDArray idx,
    NDArray out);
129
template void ScatterAdd<kDGLCPU, int32_t, 64>(
130
131
132
    NDArray feat,
    NDArray idx,
    NDArray out);
133
template void ScatterAdd<kDGLCPU, int64_t, 64>(
134
135
136
    NDArray feat,
    NDArray arg,
    NDArray out);
137

138
template void UpdateGradMinMax_hetero<kDGLCPU, int32_t, 16>(
139
140
141
    const HeteroGraphPtr& g, const std::string& op,
    const std::vector<NDArray>& feat, const std::vector<NDArray>& idx,
    const std::vector<NDArray>& idx_etype, std::vector<NDArray>* out);
142
template void UpdateGradMinMax_hetero<kDGLCPU, int64_t, 16>(
143
144
145
    const HeteroGraphPtr& g, const std::string& op,
    const std::vector<NDArray>& feat, const std::vector<NDArray>& idx,
    const std::vector<NDArray>& idx_etype, std::vector<NDArray>* out);
146
template void UpdateGradMinMax_hetero<kDGLCPU, int32_t, 32>(
147
148
149
    const HeteroGraphPtr& g, const std::string& op,
    const std::vector<NDArray>& feat, const std::vector<NDArray>& idx,
    const std::vector<NDArray>& idx_etype, std::vector<NDArray>* out);
150
template void UpdateGradMinMax_hetero<kDGLCPU, int64_t, 32>(
151
152
153
    const HeteroGraphPtr& g, const std::string& op,
    const std::vector<NDArray>& feat, const std::vector<NDArray>& idx,
    const std::vector<NDArray>& idx_etype, std::vector<NDArray>* out);
154
template void UpdateGradMinMax_hetero<kDGLCPU, int32_t, 64>(
155
156
157
    const HeteroGraphPtr& g, const std::string& op,
    const std::vector<NDArray>& feat, const std::vector<NDArray>& idx,
    const std::vector<NDArray>& idx_etype, std::vector<NDArray>* out);
158
template void UpdateGradMinMax_hetero<kDGLCPU, int64_t, 64>(
159
160
161
162
    const HeteroGraphPtr& g, const std::string& op,
    const std::vector<NDArray>& feat, const std::vector<NDArray>& idx,
    const std::vector<NDArray>& idx_etype, std::vector<NDArray>* out);

163
template void BackwardSegmentCmp<kDGLCPU, int32_t, 16>(
164
165
166
    NDArray feat,
    NDArray arg,
    NDArray out);
167
template void BackwardSegmentCmp<kDGLCPU, int64_t, 16>(
168
169
170
    NDArray feat,
    NDArray arg,
    NDArray out);
171
template void BackwardSegmentCmp<kDGLCPU, int32_t, 32>(
172
173
174
    NDArray feat,
    NDArray arg,
    NDArray out);
175
template void BackwardSegmentCmp<kDGLCPU, int64_t, 32>(
176
177
178
    NDArray feat,
    NDArray arg,
    NDArray out);
179
template void BackwardSegmentCmp<kDGLCPU, int32_t, 64>(
180
181
182
    NDArray feat,
    NDArray arg,
    NDArray out);
183
template void BackwardSegmentCmp<kDGLCPU, int64_t, 64>(
184
185
186
187
188
189
    NDArray feat,
    NDArray arg,
    NDArray out);

}  // namespace aten
}  // namespace dgl