Caffe2 - C++ API
A deep learning, cross platform ML framework
batch_moments_op.h
1 #ifndef CAFFE2_OPERATORS_BATCH_MOMENTS_OP_H_
2 #define CAFFE2_OPERATORS_BATCH_MOMENTS_OP_H_
3 
4 #include "caffe2/core/context.h"
5 #include "caffe2/core/logging.h"
6 #include "caffe2/core/operator.h"
7 
8 namespace caffe2 {
9 
10 template <typename T, class Context>
11 class BatchMomentsOp final : public Operator<Context> {
12  public:
13  USE_OPERATOR_CONTEXT_FUNCTIONS;
14 
15  template <class... Args>
16  explicit BatchMomentsOp(Args&&... args)
17  : Operator<Context>(std::forward<Args>(args)...),
18  order_(StringToStorageOrder(
19  this->template GetSingleArgument<std::string>("order", "NCHW"))) {
20  CAFFE_ENFORCE_NE(order_, StorageOrder::UNKNOWN);
21  }
22 
23  bool RunOnDevice() override {
24  const auto& X = Input(0);
25 
26  const int ndim = X.dim();
27  const int N = X.dim32(0);
28  const int C = order_ == StorageOrder::NCHW ? X.dim32(1) : X.dim32(ndim - 1);
29  const int HxW = X.numel() / (N * C);
30  auto* mu = Output(0, {C}, at::dtype<T>());
31  auto* var = Output(1, {C}, at::dtype<T>());
32  const T* X_data = X.template data<T>();
33  T* mu_data = mu->template mutable_data<T>();
34  T* var_data = var->template mutable_data<T>();
35  return order_ == StorageOrder::NCHW
36  ? ComputeBatchMomentsNCHW(N, C, HxW, X_data, mu_data, var_data)
37  : ComputeBatchMomentsNHWC(N, C, HxW, X_data, mu_data, var_data);
38  }
39 
40  private:
41  bool ComputeBatchMomentsNCHW(
42  const int N,
43  const int C,
44  const int HxW,
45  const T* X,
46  T* mu,
47  T* var);
48 
49  bool ComputeBatchMomentsNHWC(
50  const int N,
51  const int C,
52  const int HxW,
53  const T* X,
54  T* mu,
55  T* var);
56 
57  const StorageOrder order_;
58 };
59 
60 template <typename T, class Context>
61 class BatchMomentsGradientOp final : public Operator<Context> {
62  public:
63  USE_OPERATOR_CONTEXT_FUNCTIONS;
64 
65  template <class... Args>
66  explicit BatchMomentsGradientOp(Args&&... args)
67  : Operator<Context>(std::forward<Args>(args)...),
68  order_(StringToStorageOrder(
69  this->template GetSingleArgument<std::string>("order", "NCHW"))) {
70  CAFFE_ENFORCE_NE(order_, StorageOrder::UNKNOWN);
71  }
72 
73  bool RunOnDevice() override {
74  const auto& dmu = Input(0);
75  const auto& dvar = Input(1);
76  const auto& X = Input(2);
77 
78  const int ndim = X.dim();
79  const int N = X.dim32(0);
80  const int C = order_ == StorageOrder::NCHW ? X.dim32(1) : X.dim32(ndim - 1);
81  const int HxW = X.numel() / (N * C);
82  auto* dX = Output(0, X.sizes(), at::dtype<T>());
83  const T* dmu_data = dmu.template data<T>();
84  const T* dvar_data = dvar.template data<T>();
85  const T* X_data = X.template data<T>();
86  T* dX_data = dX->template mutable_data<T>();
87  return order_ == StorageOrder::NCHW
88  ? ComputeBatchMomentsGradientNCHW(
89  N, C, HxW, dmu_data, dvar_data, X_data, dX_data)
90  : ComputeBatchMomentsGradientNHWC(
91  N, C, HxW, dmu_data, dvar_data, X_data, dX_data);
92  }
93 
94  private:
95  bool ComputeBatchMomentsGradientNCHW(
96  const int N,
97  const int C,
98  const int HxW,
99  const T* dmu,
100  const T* dvar,
101  const T* X,
102  T* dX);
103 
104  bool ComputeBatchMomentsGradientNHWC(
105  const int N,
106  const int C,
107  const int HxW,
108  const T* dmu,
109  const T* dvar,
110  const T* X,
111  T* dX);
112 
113  const StorageOrder order_;
114 };
115 
116 } // namespace caffe2
117 
118 #endif // CAFFE2_OPERATORS_BATCH_MOMENTS_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
Definition: static.cpp:64