Caffe2 - C++ API
A deep learning, cross platform ML framework
ensure_clipped_op.cc
1 #include "caffe2/operators/ensure_clipped_op.h"
2 #include "caffe2/core/tensor.h"
3 
4 namespace caffe2 {
5 
6 template <>
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);
14  // n: number of sparse embeddings to be normalized
15  auto n = Input(INDICES).numel();
16  if (n == 0) {
17  return true;
18  }
19  // embedding length, e.g. 32, 64, 128
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)
26  .cwiseMax(min_)
27  .cwiseMin(max_);
28  }
29  return true;
30 }
31 
32 REGISTER_CPU_OPERATOR(EnsureClipped, EnsureClippedOp<float, CPUContext>);
33 OPERATOR_SCHEMA(EnsureClipped)
34  .NumInputs(1, 3)
35  .NumOutputs(1)
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}})
41  .SetDoc(R"DOC(
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
44 )DOC");
45 
46 SHOULD_NOT_DO_GRADIENT(EnsureClipped);
47 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13