1 #include "caffe2/operators/ensure_clipped_op.h" 2 #include "caffe2/core/tensor.h" 7 template <
typename SIndex>
8 bool EnsureClippedOp<float, CPUContext>::DoRunWithType() {
9 Output(OUTPUT_PARAM)->ResizeLike(Input(PARAM));
10 const auto* indices = Input(INDICES).template data<SIndex>();
11 const auto* paramIn = Input(PARAM).template data<float>();
12 auto* paramOut = Output(OUTPUT_PARAM)->template mutable_data<float>();
13 CAFFE_ENFORCE_EQ(paramIn, paramOut);
15 auto n = Input(INDICES).numel();
20 auto block_size = Input(GRAD).numel() / n;
21 for (
int i = 0; i < n; ++i) {
22 auto idx = indices[i];
23 auto offsetIdx = idx * block_size;
24 EigenVectorMap<float>(paramOut + offsetIdx, block_size) =
25 ConstEigenVectorMap<float>(paramIn + offsetIdx, block_size)
32 REGISTER_CPU_OPERATOR(EnsureClipped, EnsureClippedOp<float, CPUContext>);
33 OPERATOR_SCHEMA(EnsureClipped)
36 .Input(0,
"param",
"Parameters to be normalized")
37 .Input(1,
"indices",
"Sparse indices, only needed for sparse param")
38 .Input(2,
"grad",
"Gradient computed, only needed for sparse param")
39 .Output(0,
"output_param",
"param ensured to be clipped within range")
40 .AllowInplace({{0, 0}})
42 Given a tensor, apply clip after gradient is applied; when the param is sparse as 43 indicated by valid indices and grad, in-place is required 46 SHOULD_NOT_DO_GRADIENT(EnsureClipped); A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...