forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moments_op.h
134 lines (118 loc) · 3.67 KB
/
moments_op.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef CAFFE2_OPERATORS_MOMENTS_OP_H_
#define CAFFE2_OPERATORS_MOMENTS_OP_H_
#include <algorithm>
#include <vector>
#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
#include "caffe2/utils/math.h"
namespace caffe2 {
template <typename T, class Context>
class MomentsOp final : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
template <class... Args>
explicit MomentsOp(Args&&... args)
: Operator<Context>(std::forward<Args>(args)...),
axes_(this->template GetRepeatedArgument<int>("axes")),
OP_SINGLE_ARG(bool, "keepdims", keep_dims_, true) {}
bool RunOnDevice() override {
const auto& X = Input(0);
const int ndim = X.dim();
if (axes_.empty()) {
axes_.resize(ndim);
std::iota(axes_.begin(), axes_.end(), 0);
} else {
std::sort(axes_.begin(), axes_.end());
CAFFE_ENFORCE_GE(axes_.front(), 0, "Axes ids must be non-negative.");
CAFFE_ENFORCE_LT(
axes_.back(),
ndim,
"Axes ids must be smaller than the dimensions of input.");
}
const std::vector<int> X_dims(X.sizes().cbegin(), X.sizes().cend());
std::vector<int> Y_dims = X_dims;
for (const int axis : axes_) {
Y_dims[axis] = 1;
}
std::vector<std::int64_t> output_dims;
output_dims.reserve(ndim);
std::size_t cur_axis = 0;
for (int i = 0; i < ndim; ++i) {
if (cur_axis < axes_.size() && i == axes_[cur_axis]) {
if (keep_dims_) {
output_dims.push_back(1);
}
++cur_axis;
} else {
output_dims.push_back(X_dims[i]);
}
}
auto* mean = Output(0, output_dims, at::dtype<T>());
auto* var = Output(1, output_dims, at::dtype<T>());
math::Moments<float, Context>(
X_dims.size(),
X_dims.data(),
Y_dims.data(),
X.template data<T>(),
mean->template mutable_data<T>(),
var->template mutable_data<T>(),
&context_);
return true;
}
private:
std::vector<int> axes_;
const int keep_dims_;
};
template <typename T, class Context>
class MomentsGradientOp final : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
template <class... Args>
explicit MomentsGradientOp(Args&&... args)
: Operator<Context>(std::forward<Args>(args)...),
axes_(this->template GetRepeatedArgument<int>("axes")) {}
bool RunOnDevice() override {
const auto& dmean = Input(0);
const auto& dvariance = Input(1);
const auto& X = Input(2);
const auto& mean = Input(3);
const int ndim = X.dim();
if (axes_.empty()) {
axes_.resize(ndim);
std::iota(axes_.begin(), axes_.end(), 0);
} else {
std::sort(axes_.begin(), axes_.end());
CAFFE_ENFORCE_GE(axes_.front(), 0, "Axes ids must be non-negative.");
CAFFE_ENFORCE_LT(
axes_.back(),
ndim,
"Axes ids must be smaller than the dimensions of input.");
}
const std::vector<int> dX_dims(X.sizes().cbegin(), X.sizes().cend());
std::vector<int> dY_dims = dX_dims;
for (const int axis : axes_) {
dY_dims[axis] = 1;
}
auto* dX = Output(0, X.sizes(), at::dtype<T>());
return Compute(
dY_dims,
dX_dims,
dmean.template data<T>(),
dvariance.template data<T>(),
X.template data<T>(),
mean.template data<T>(),
dX->template mutable_data<T>());
}
private:
bool Compute(
const std::vector<int>& dY_dims,
const std::vector<int>& dX_dims,
const T* dmean_data,
const T* dvariance_data,
const T* X_data,
const T* mean_data,
T* dX_data);
std::vector<int> axes_;
};
} // namespace caffe2
#endif // CAFFE2_OPERATORS_MOMENTS_OP_H_