This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathnp_normal_op.h
327 lines (313 loc) · 12.5 KB
/
np_normal_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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file np_normal_op.h
* \brief Operator for numpy sampling from normal distributions
*/
#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_NORMAL_OP_H_
#define MXNET_OPERATOR_NUMPY_RANDOM_NP_NORMAL_OP_H_
#include <mxnet/operator_util.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include "../../../api/operator/op_utils.h"
#include "../../../common/utils.h"
#include "../../elemwise_op_common.h"
#include "../../mshadow_op.h"
#include "../../mxnet_op.h"
#include "../../operator_common.h"
#include "../../tensor/elemwise_binary_broadcast_op.h"
#include "./dist_common.h"
namespace mxnet {
namespace op {
struct NumpyNormalParam : public dmlc::Parameter<NumpyNormalParam> {
dmlc::optional<float> loc;
dmlc::optional<float> scale;
std::string ctx;
int dtype;
dmlc::optional<mxnet::Tuple<index_t>> size;
DMLC_DECLARE_PARAMETER(NumpyNormalParam) {
DMLC_DECLARE_FIELD(loc);
DMLC_DECLARE_FIELD(scale);
DMLC_DECLARE_FIELD(size)
.set_default(dmlc::optional<mxnet::Tuple<index_t>>())
.describe(
"Output shape. If the given shape is, "
"e.g., (m, n, k), then m * n * k samples are drawn. "
"Default is None, in which case a single value is returned.");
DMLC_DECLARE_FIELD(ctx).set_default("cpu").describe(
"Context of output, in format [cpu|gpu|cpu_pinned](n)."
" Only used for imperative calls.");
DMLC_DECLARE_FIELD(dtype)
.add_enum("None", -1)
.add_enum("float32", mshadow::kFloat32)
.add_enum("float64", mshadow::kFloat64)
.add_enum("float16", mshadow::kFloat16)
.set_default(-1)
.describe(
"DType of the output in case this can't be inferred. "
"Defaults to float32 or float64 if not defined (dtype=None).");
}
void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
std::ostringstream loc_s, scale_s, dtype_s, size_s;
loc_s << loc;
scale_s << scale;
dtype_s << dtype;
size_s << size;
(*dict)["loc"] = loc_s.str();
(*dict)["scale"] = scale_s.str();
(*dict)["dtype"] = MXNetTypeWithBool2String(dtype);
(*dict)["size"] = size_s.str();
}
};
inline bool NumpyNormalOpType(const nnvm::NodeAttrs& attrs,
std::vector<int>* in_attrs,
std::vector<int>* out_attrs) {
const NumpyNormalParam& param = nnvm::get<NumpyNormalParam>(attrs.parsed);
int otype = param.dtype;
if (otype != -1) {
(*out_attrs)[0] = otype;
} else {
(*out_attrs)[0] = mxnet::common::GetDefaultDtype();
}
(*out_attrs)[1] = mshadow::kFloat32;
return true;
}
namespace mxnet_op {
template <int ndim, typename IType, typename OType>
struct normal_kernel {
MSHADOW_XINLINE static void Map(index_t i,
const Shape<ndim>& lstride,
const Shape<ndim>& hstride,
const Shape<ndim>& oshape,
IType* loc,
IType* scale,
float* normals,
OType* out) {
Shape<ndim> coord = unravel(i, oshape);
auto lidx = static_cast<index_t>(dot(coord, lstride));
auto hidx = static_cast<index_t>(dot(coord, hstride));
IType loc_value = loc[lidx];
IType scale_value = scale[hidx];
out[i] = loc_value + normals[i] * scale_value;
}
};
template <int ndim, typename IType, typename OType>
struct normal_one_scalar_kernel {
MSHADOW_XINLINE static void Map(index_t i,
int scalar_pos,
const Shape<ndim>& stride,
const Shape<ndim>& oshape,
IType* array,
float scalar,
float* normals,
OType* out) {
Shape<ndim> coord = unravel(i, oshape);
auto idx = static_cast<index_t>(dot(coord, stride));
IType loc_value;
IType scale_value;
if (scalar_pos == 0) {
loc_value = scalar;
scale_value = array[idx];
} else {
loc_value = array[idx];
scale_value = scalar;
}
out[i] = loc_value + normals[i] * scale_value;
}
};
template <typename OType>
struct normal_two_scalar_kernel {
MSHADOW_XINLINE static void Map(index_t i, float loc, float scale, float* normals, OType* out) {
out[i] = loc + normals[i] * scale;
}
};
template <typename IType>
struct check_legal_scale_kernel {
MSHADOW_XINLINE static void Map(index_t i, IType* scalar, float* flag) {
if (scalar[i] < 0) {
*flag = -1.0;
}
}
};
} // namespace mxnet_op
template <typename xpu>
void NumpyNormalForward(const nnvm::NodeAttrs& attrs,
const OpContext& ctx,
const std::vector<TBlob>& inputs,
const std::vector<OpReqType>& req,
const std::vector<TBlob>& outputs) {
using namespace mshadow;
using namespace mxnet_op;
const auto& param = nnvm::get<NumpyNormalParam>(attrs.parsed);
Stream<xpu>* s = ctx.get_stream<xpu>();
// Generate base random number.
Random<xpu, float>* prnd = ctx.requested[0].get_random<xpu, float>(s);
Tensor<xpu, 1, float> workspace = ctx.requested[1].get_space_typed<xpu, 1, float>(Shape1(1), s);
Tensor<xpu, 1, float> normal_tensor = outputs[1].FlatTo1D<xpu, float>(s);
Tensor<xpu, 1, float> indicator_device = workspace;
float indicator_host = 1.0;
float* indicator_device_ptr = indicator_device.dptr_;
Kernel<set_zero, xpu>::Launch(s, 1, indicator_device_ptr);
prnd->SampleGaussian(&normal_tensor, 0.0, 1.0);
mxnet::TShape new_lshape, new_hshape, new_oshape;
// [scalar scalar] case
if (inputs.size() == 0U) {
CHECK_GE(param.scale.value(), 0.0) << "ValueError: scale < 0";
MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
Kernel<normal_two_scalar_kernel<OType>, xpu>::Launch(s,
outputs[0].Size(),
param.loc.value(),
param.scale.value(),
normal_tensor.dptr_,
outputs[0].dptr<OType>());
});
} else if (inputs.size() == 1U) {
// [scalar tensor], [tensor scalar] case
int ndim = FillShape(inputs[0].shape_,
inputs[0].shape_,
outputs[0].shape_,
&new_lshape,
&new_lshape,
&new_oshape);
int scalar_pos;
float scalar_value;
if (param.loc.has_value()) {
scalar_pos = 0;
scalar_value = param.loc.value();
MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
s, inputs[0].Size(), inputs[0].dptr<IType>(), indicator_device_ptr);
});
_copy<xpu>(s, &indicator_host, indicator_device_ptr);
CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
} else {
scalar_pos = 1;
scalar_value = param.scale.value();
CHECK_GE(scalar_value, 0.0) << "ValueError: scale < 0";
}
MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
BROADCAST_NDIM_SWITCH(ndim, NDim, {
Shape<NDim> oshape = new_oshape.get<NDim>();
Shape<NDim> stride = calc_stride(new_lshape.get<NDim>());
Kernel<normal_one_scalar_kernel<NDim, IType, OType>, xpu>::Launch(
s,
outputs[0].Size(),
scalar_pos,
stride,
oshape,
inputs[0].dptr<IType>(),
scalar_value,
normal_tensor.dptr_,
outputs[0].dptr<OType>());
});
});
});
} else if (inputs.size() == 2U) {
// [tensor tensor] case
MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
s, inputs[1].Size(), inputs[1].dptr<IType>(), indicator_device_ptr);
});
_copy<xpu>(s, &indicator_host, indicator_device_ptr);
CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
int ndim = FillShape(inputs[0].shape_,
inputs[1].shape_,
outputs[0].shape_,
&new_lshape,
&new_hshape,
&new_oshape);
MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
BROADCAST_NDIM_SWITCH(ndim, NDim, {
Shape<NDim> oshape = new_oshape.get<NDim>();
Shape<NDim> lstride = calc_stride(new_lshape.get<NDim>());
Shape<NDim> hstride = calc_stride(new_hshape.get<NDim>());
Kernel<normal_kernel<NDim, IType, OType>, xpu>::Launch(s,
outputs[0].Size(),
lstride,
hstride,
oshape,
inputs[0].dptr<IType>(),
inputs[1].dptr<IType>(),
normal_tensor.dptr_,
outputs[0].dptr<OType>());
});
});
});
}
}
// Allow normal sampling to be differentiable,
// using reparameterization trick described in:
// Auto-encoding variational bayes.
// Kingma, D. P., & Welling, M. (2013).
template <typename xpu>
void NormalReparamBackward(const nnvm::NodeAttrs& attrs,
const OpContext& ctx,
const std::vector<TBlob>& inputs,
const std::vector<OpReqType>& req,
const std::vector<TBlob>& outputs) {
// skip kernel launch for zero-size tensors
if (inputs[0].shape_.Size() == 0U) {
return;
}
// [scalar scalar] case
if (outputs.size() == 0U) {
return;
}
const auto& param = nnvm::get<NumpyNormalParam>(attrs.parsed);
// [tensor tensor] case
if (inputs.size() == 6U) {
mxnet::TShape new_lshape, new_rshape, new_oshape;
int ndim = FillShape(outputs[0].shape_,
outputs[1].shape_,
inputs[0].shape_,
&new_lshape,
&new_rshape,
&new_oshape);
MSHADOW_REAL_TYPE_SWITCH(outputs[0].type_flag_, DType, {
BROADCAST_NDIM_SWITCH(ndim, NDim, {
CommonReparamBackwardImpl<xpu, NDim, DType>(
ctx, inputs, req, outputs, new_lshape, new_rshape, new_oshape);
});
});
}
// [tensor scalar], [scalar tensor] case
if (inputs.size() == 5U) {
mxnet::TShape new_ishape, new_oshape;
int ndim = FillShape(outputs[0].shape_,
outputs[0].shape_,
inputs[0].shape_,
&new_ishape,
&new_ishape,
&new_oshape);
bool loc_is_tensor = !param.loc.has_value();
MSHADOW_REAL_TYPE_SWITCH(outputs[0].type_flag_, DType, {
BROADCAST_NDIM_SWITCH(ndim, NDim, {
CommonScalarReparamBackwardImpl<xpu, NDim, DType>(
ctx, inputs, req, outputs, new_ishape, new_oshape, loc_is_tensor);
});
});
}
}
} // namespace op
} // namespace mxnet
#endif // MXNET_OPERATOR_NUMPY_RANDOM_NP_NORMAL_OP_H_