Caffe2 - C++ API
A deep learning, cross platform ML framework
minmax_ops.h
1 #ifndef CAFFE2_OPERATORS_MINMAX_OPS_H_
2 #define CAFFE2_OPERATORS_MINMAX_OPS_H_
3 
4 #include "caffe2/core/common_omp.h"
5 #include "caffe2/core/context.h"
6 #include "caffe2/core/logging.h"
7 #include "caffe2/core/operator.h"
8 #include "caffe2/core/types.h"
9 #include "caffe2/utils/math.h"
10 
11 namespace caffe2 {
12 
13 template <typename T, class Context>
14 class MaxMinOpBase : public Operator<Context> {
15  public:
16  USE_OPERATOR_CONTEXT_FUNCTIONS;
17  USE_SIMPLE_CTOR_DTOR(MaxMinOpBase)
18 
19  bool RunOnDevice() override {
20  auto& input0 = Input(0);
21  auto* output = Output(0);
22 
23  output->ResizeLike(input0);
24  output->CopyFrom(input0, &context_);
25 
26  if (InputSize() == 1) {
27  return true;
28  }
29 
30  // Dimension checking
31  for (int i = 1; i < InputSize(); ++i) {
32  CAFFE_ENFORCE_EQ(
33  output->dims(),
34  Input(i).dims(),
35  "Description: Input #",
36  i,
37  ", input dimension:",
38  Input(i).dims(),
39  " should match output dimension: ",
40  output->dims());
41  }
42 
43  return this->Compute();
44  }
45 
46  virtual bool Compute() = 0;
47 };
48 
49 template <typename T, class Context>
50 class MaxOp : public MaxMinOpBase<T, Context> {
51  public:
52  USE_OPERATOR_CONTEXT_FUNCTIONS;
53  MaxOp(const OperatorDef& operator_def, Workspace* ws)
54  : MaxMinOpBase<T, Context>(operator_def, ws) {}
55  virtual ~MaxOp() noexcept {}
56  bool Compute() override;
57 };
58 
59 template <typename T, class Context>
60 class SelectGradientOpBase : public Operator<Context> {
61  public:
62  USE_OPERATOR_CONTEXT_FUNCTIONS;
63  USE_SIMPLE_CTOR_DTOR(SelectGradientOpBase)
64 
65  bool RunOnDevice() override;
66 };
67 
68 template <typename T, class Context>
69 class MaxGradientOp : public SelectGradientOpBase<T, Context> {
70  public:
71  MaxGradientOp(const OperatorDef& operator_def, Workspace* ws)
72  : SelectGradientOpBase<T, Context>(operator_def, ws) {}
73  virtual ~MaxGradientOp() noexcept {}
74 };
75 
76 template <typename T, class Context>
77 class MinOp : public MaxMinOpBase<T, Context> {
78  public:
79  USE_OPERATOR_CONTEXT_FUNCTIONS;
80  MinOp(const OperatorDef& operator_def, Workspace* ws)
81  : MaxMinOpBase<T, Context>(operator_def, ws) {}
82  virtual ~MinOp() noexcept {}
83  bool Compute() override;
84 };
85 
86 template <typename T, class Context>
87 class MinGradientOp : public SelectGradientOpBase<T, Context> {
88  public:
89  MinGradientOp(const OperatorDef& operator_def, Workspace* ws)
90  : SelectGradientOpBase<T, Context>(operator_def, ws) {}
91  virtual ~MinGradientOp() noexcept {}
92 };
93 
94 } // namespace caffe2
95 
96 #endif // CAFFE2_OPERATORS_MINMAX_OPS_H_
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 ...