forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
affine_channel_op.cc
198 lines (178 loc) · 5.79 KB
/
affine_channel_op.cc
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "caffe2/operators/affine_channel_op.h"
#include "caffe2/utils/eigen_utils.h"
#include <vector>
namespace caffe2 {
namespace {
template <typename T>
void AffineChannelScaleBiasBackwardNCHW(
const int N,
const int C,
const int HxW,
const T* dY,
const T* X,
T* dscale,
T* dbias) {
const T* dY_ptr = dY;
const T* X_ptr = X;
const int stride = C * HxW;
EigenVectorArrayMap<T> dscale_arr(dscale, C);
EigenVectorArrayMap<T> dbias_arr(dbias, C);
dscale_arr.setZero();
dbias_arr.setZero();
for (int i = 0; i < N; ++i) {
ConstEigenArrayMap<T> dY_arr(dY_ptr, HxW, C);
ConstEigenArrayMap<T> X_arr(X_ptr, HxW, C);
dscale_arr += (dY_arr * X_arr).colwise().sum();
dbias_arr += dY_arr.colwise().sum();
dY_ptr += stride;
X_ptr += stride;
}
}
template <typename T>
void AffineChannelScaleBiasBackwardNHWC(
const int N,
const int C,
const int HxW,
const T* dY,
const T* X,
T* dscale,
T* dbias) {
ConstEigenArrayMap<T> dY_arr(dY, C, N * HxW);
ConstEigenArrayMap<T> X_arr(X, C, N * HxW);
EigenVectorMap<T>(dscale, C) = (dY_arr * X_arr).rowwise().sum();
EigenVectorMap<T>(dbias, C) = dY_arr.rowwise().sum();
}
} // namespace
template <>
bool AffineChannelGradientOp<float, CPUContext>::RunOnDeviceWithOrderNCHW() {
const auto& dY = Input(0);
const auto& scale = is_learnable_ ? Input(2) : Input(1);
auto* dX = Output(0, dY.sizes(), at::dtype<float>());
const int N = dY.dim32(0);
const int C = dY.dim32(1);
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
const int HxW = dY.numel() / (N * C);
const float* dY_data = dY.data<float>();
const float* scale_data = scale.data<float>();
const std::array<int, 3> X_dims = {N, C, HxW};
const std::array<int, 3> scale_dims = {1, C, 1};
math::Mul<float, CPUContext>(
3,
X_dims.data(),
3,
scale_dims.data(),
dY_data,
scale_data,
dX->template mutable_data<float>(),
&context_);
if (is_learnable_) {
const auto& X = Input(1);
const float* X_data = X.data<float>();
auto* dscale = Output(1, scale.sizes(), at::dtype<float>());
auto* dbias = Output(2, scale.sizes(), at::dtype<float>());
AffineChannelScaleBiasBackwardNCHW<float>(
N,
C,
HxW,
dY_data,
X_data,
dscale->template mutable_data<float>(),
dbias->template mutable_data<float>());
}
return true;
}
template <>
bool AffineChannelGradientOp<float, CPUContext>::RunOnDeviceWithOrderNHWC() {
const auto& dY = Input(0);
const auto& scale = is_learnable_ ? Input(2) : Input(1);
auto* dX = Output(0, dY.sizes(), at::dtype<float>());
const int ndim = dY.dim();
const int C = dY.dim32(ndim - 1);
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
const int rows = dY.numel() / C;
const int cols = C;
const float* dY_data = dY.data<float>();
const float* scale_data = scale.data<float>();
math::RowwiseMul<float, CPUContext>(
rows,
cols,
dY_data,
scale_data,
dX->template mutable_data<float>(),
&context_);
if (is_learnable_) {
const auto& X = Input(1);
const float* X_data = X.data<float>();
const int N = X.dim32(0);
const int HxW = rows / N;
auto* dscale = Output(1, scale.sizes(), at::dtype<float>());
auto* dbias = Output(2, scale.sizes(), at::dtype<float>());
AffineChannelScaleBiasBackwardNHWC<float>(
N,
C,
HxW,
dY_data,
X_data,
dscale->template mutable_data<float>(),
dbias->template mutable_data<float>());
}
return true;
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
REGISTER_CPU_OPERATOR(AffineChannel, AffineChannelOp<float, CPUContext>);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
REGISTER_CPU_OPERATOR(
AffineChannelGradient,
AffineChannelGradientOp<float, CPUContext>);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
OPERATOR_SCHEMA(AffineChannel)
.NumInputs(3)
.NumOutputs(1)
.AllowInplace({{0, 0}})
.SetDoc(R"DOC(
Applies a separate affine transformation to each channel of the input. Useful
for replacing spatial batch norm with its equivalent fixed transformation.
)DOC")
.Input(0, "X", "Feature map input with order NCHW or NHWC.")
.Input(
1,
"scale",
"1D input of shape (C); the c-th element is the scale factor of the "
"affine transformation for the c-th channel of the input.")
.Input(
2,
"bias",
"1D input of shape (C); the c-th element is the bias of the affine "
"transformation for the c-th channel of the input.")
.Output(0, "Y", "Output with the same order of Input.");
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
OPERATOR_SCHEMA(AffineChannelGradient)
.NumInputs({2, 3})
.NumOutputs({1, 3})
.AllowInplace({{0, 0}});
namespace {
class GetAffineChannelGradient : public GradientMakerBase {
using GradientMakerBase::GradientMakerBase;
std::vector<OperatorDef> GetGradientDefs() override {
ArgumentHelper arg_helper(def_);
const bool is_learnable =
arg_helper.GetSingleArgument("is_learnable", false);
if (is_learnable) {
return SingleGradientDef(
"AffineChannelGradient",
"",
std::vector<std::string>{GO(0), I(0), I(1)},
std::vector<std::string>{GI(0), GI(1), GI(2)});
} else {
return SingleGradientDef(
"AffineChannelGradient",
"",
std::vector<std::string>{GO(0), I(1)},
std::vector<std::string>{GI(0)});
}
}
};
} // namespace
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
REGISTER_GRADIENT(AffineChannel, GetAffineChannelGradient);
} // namespace caffe2