Caffe2 - C++ API
A deep learning, cross platform ML framework
layer_norm_op.h
1 #ifndef CAFFE2_OPERATORS_LAYER_NORM_OP_H
2 #define CAFFE2_OPERATORS_LAYER_NORM_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 LayerNormOp : public Operator<Context> {
12  public:
13  USE_OPERATOR_CONTEXT_FUNCTIONS;
14  LayerNormOp(const OperatorDef& operator_def, Workspace* ws)
15  : Operator<Context>(operator_def, ws),
16  axis_(OperatorBase::GetSingleArgument<int>("axis", 1)),
17  epsilon_(OperatorBase::GetSingleArgument<float>("epsilon", 1e-5f)) {}
18  ~LayerNormOp() {}
19 
20  template <typename T>
21  bool DoRunWithType();
22 
23  bool RunOnDevice() override {
24  return DoRunWithType<float>();
25  }
26 
27  protected:
28  int axis_;
29  float epsilon_;
30 
31  Tensor<Context> scratch_;
32  Tensor<Context> seg_indices_;
33 };
34 
35 template <class Context>
36 class LayerNormGradientOp : public Operator<Context> {
37  public:
38  USE_OPERATOR_CONTEXT_FUNCTIONS;
39  LayerNormGradientOp(const OperatorDef& operator_def, Workspace* ws)
40  : Operator<Context>(operator_def, ws),
41  axis_(OperatorBase::GetSingleArgument<int>("axis", 1)),
42  epsilon_(OperatorBase::GetSingleArgument<float>("epsilon", 0.001f)) {}
44 
45  template <typename T>
46  bool DoRunWithType();
47 
48  bool RunOnDevice() override {
49  return DoRunWithType<float>();
50  }
51 
52  protected:
53  int axis_;
54  float epsilon_;
55 
56  Tensor<Context> scratch_;
57  Tensor<Context> gscratch_;
58  Tensor<Context> seg_indices_;
59  Tensor<Context> dstdev_;
60  Tensor<Context> dmean_;
61 };
62 
63 } // namespace caffe2
64 
65 #endif /* CAFFE2_OPERATORS_LAYER_NORM_OP_H */
Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information...
Definition: tensor.h:93
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...