Caffe2 - C++ API
A deep learning, cross platform ML framework
square_root_divide_op.h
1 #ifndef CAFFE2_OPERATORS_SQUARE_ROOT_DIVIDE_OP_H_
2 #define CAFFE2_OPERATORS_SQUARE_ROOT_DIVIDE_OP_H_
3 
4 #include "caffe2/core/context.h"
5 #include "caffe2/core/operator.h"
6 #include "caffe2/utils/math.h"
7 
8 namespace caffe2 {
9 
10 template <class Context>
11 class SquareRootDivideOp final : public Operator<Context> {
12  public:
13  USE_OPERATOR_CONTEXT_FUNCTIONS;
14  USE_DISPATCH_HELPER;
15 
16  template <class... Args>
17  explicit SquareRootDivideOp(Args&&... args)
18  : Operator<Context>(std::forward<Args>(args)...) {}
19 
20  bool RunOnDevice() override {
21  return DispatchHelper<TensorTypes<float>>::call(this, Input(DATA));
22  }
23 
24  private:
25  template <typename TData>
26  bool DoRunWithType() {
28  this, Input(SCALE));
29  }
30 
31  template <typename TData, typename TScale>
32  bool DoRunWithType2() {
33  auto& data = Input(DATA);
34  auto& scale = Input(SCALE);
35 
36  auto* Y = Output(0, data.sizes(), at::dtype<TData>());
37  size_t batchSize = data.size(0);
38  size_t exampleSize = data.size_from_dim(1);
39  CAFFE_ENFORCE(batchSize == scale.size(0), batchSize, " != ", scale.size(0));
40  auto* scalePtr = scale.template data<TScale>();
41  auto* dataPtr = data.template data<TData>();
42  auto* yPtr = Y->template mutable_data<TData>();
43  for (auto i = 0; i < batchSize; ++i) {
44  auto scale = scalePtr[i];
45  CAFFE_ENFORCE(scale >= 0, scale, " < 0");
46  auto multiplier = scale == 0 ? 1.0 : 1 / std::sqrt(scale);
47  math::Scale<float, TData, Context>(
48  exampleSize,
49  multiplier,
50  dataPtr + i * exampleSize,
51  yPtr + i * exampleSize,
52  &context_);
53  }
54  return true;
55  }
56 
57  INPUT_TAGS(DATA, SCALE);
58 };
59 
60 } // namespace caffe2
61 
62 #endif // CAFFE2_OPERATORS_SQUARE_ROOT_DIVIDE_OP_H_
const Tensor & Input(int idx, DeviceType type=Context::GetDeviceType())
Retrieve a non-owning reference to the input at position &#39;idx&#39; for this operator. ...
Definition: operator.h:702
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13