Caffe2 - C++ API
A deep learning, cross platform ML framework
local_response_normalization_op.cc
1 #include <caffe2/ideep/ideep_utils.h>
2 
3 namespace caffe2 {
4 
5 class IDEEPLRNOp final : public IDEEPOperator {
6  public:
7  USE_IDEEP_DEF_ALIASES();
8  USE_IDEEP_OPERATOR_FUNCTIONS();
9 
10  IDEEPLRNOp(const OperatorDef& operator_def, Workspace* ws)
11  : IDEEPOperator(operator_def, ws),
12  size_(OperatorBase::GetSingleArgument<int>("size", 0)),
13  alpha_(OperatorBase::GetSingleArgument<float>("alpha", 0)),
14  beta_(OperatorBase::GetSingleArgument<float>("beta", 0)),
15  bias_(OperatorBase::GetSingleArgument<float>("bias", 1)),
16  pre_pad_((size_ - 1) / 2) {
17  DCHECK_GT(size_, 0);
18  DCHECK_EQ(size_ % 2, 1);
19  DCHECK_GT(alpha_, 0);
20  DCHECK_GT(beta_, 0);
21  }
22  ~IDEEPLRNOp() override {}
23 
24  bool RunOnDevice() override {
25  auto& X = Input(INPUT);
26  auto* Y = Output(OUTPUT);
27 
28  ideep::lrn_forward::compute(X, *Y, size_, alpha_, beta_, bias_);
29 
30  return true;
31  }
32 
33  private:
34  const int size_;
35  const float alpha_;
36  const float beta_;
37  const float bias_;
38  const int pre_pad_;
39 
40  INPUT_TAGS(INPUT);
41  OUTPUT_TAGS(OUTPUT);
42 };
43 
44 class IDEEPLRNGradientOp final : public IDEEPOperator {
45  public:
46  USE_IDEEP_DEF_ALIASES();
47  USE_IDEEP_OPERATOR_FUNCTIONS();
48 
49  IDEEPLRNGradientOp(const OperatorDef& operator_def, Workspace* ws)
50  : IDEEPOperator(operator_def, ws),
51  size_(OperatorBase::GetSingleArgument<int>("size", 0)),
52  alpha_(OperatorBase::GetSingleArgument<float>("alpha", 0)),
53  beta_(OperatorBase::GetSingleArgument<float>("beta", 0)),
54  bias_(OperatorBase::GetSingleArgument<float>("bias", 1)),
55  pre_pad_((size_ - 1) / 2) {
56  DCHECK_GT(size_, 0);
57  DCHECK_EQ(size_ % 2, 1);
58  DCHECK_GT(alpha_, 0);
59  DCHECK_GT(beta_, 0);
60  }
61  ~IDEEPLRNGradientOp() override {}
62 
63  bool RunOnDevice() override {
64  const auto& X = Input(INPUT);
65  const auto& Y = Input(FILTER);
66  const auto& dY = Input(OUTPUT_GRAD);
67  auto* dX = Output(INPUT_GRAD);
68 
69  ideep::lrn_backward::compute(X, dY, Y, *dX, size_, alpha_, beta_, bias_);
70 
71  return true;
72  }
73 
74  private:
75  const int size_;
76  const float alpha_;
77  const float beta_;
78  const float bias_;
79  const int pre_pad_;
80 
81  INPUT_TAGS(INPUT, FILTER, OUTPUT_GRAD);
82  OUTPUT_TAGS(INPUT_GRAD);
83 };
84 
85 
86 REGISTER_IDEEP_OPERATOR(LRN, IDEEPLRNOp);
87 REGISTER_IDEEP_OPERATOR(LRNGradient, IDEEPLRNGradientOp);
88 
89 } // namespace caffe2
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 ...
Definition: blob.h:13