DLPrimitives
pooling.hpp
1 #pragma once
2 #include <dlprim/operator.hpp>
3 
4 namespace dlprim {
5  namespace json { class value; }
6  namespace core {
7  class Pooling2DBackwardBase;
8  class Pooling2DForward;
9  }
10  struct PoolingBase {
11  enum Mode {
12  max = 0,
13  avg = 1
14  };
15 
16  Mode mode = max;
17 
18  static PoolingBase from_json(json::value const &v);
19  };
20 
21  struct Pooling2DConfig : public PoolingBase {
22  int kernel[2] = {1,1};
23  int pad[2] = {0,0};
24  int stride[2]={1,1};
25  bool ceil_mode=false;
26  bool count_include_pad = false;
27  static Pooling2DConfig from_json(json::value const &v);
28  };
29 
30  class Pooling2D : public Operator {
31  public:
33  virtual ~Pooling2D();
34 
35  virtual char const *operator_type() const
36  {
37  return "Pooling2D";
38  }
39  virtual void setup(std::vector<TensorSpecs> const &in,
40  std::vector<TensorSpecs> &out,
41  std::vector<TensorSpecs> &parameters,
42  size_t &workspace);
43 
44  virtual void reshape(std::vector<Shape> const &in,
45  std::vector<Shape> &out,
46  size_t &ws);
47 
48  virtual void forward(std::vector<Tensor> &input,
49  std::vector<Tensor> &output,
50  std::vector<Tensor> &parameters,
51  Tensor &workspace,
52  ExecutionContext const &ctx);
53 
54  virtual void backward(std::vector<TensorAndGradient> &input,
55  std::vector<TensorAndGradient> &output,
56  std::vector<TensorAndGradient> &parameters,
57  Tensor &workspace,
58  ExecutionContext const &ctx);
59 
60  private:
61  Shape calc_shape(Shape ins);
62  int calc_output_size(int in_size,int dim);
63  void forward_gpu(Tensor &in,Tensor &output,ExecutionContext const &ctx);
64  void backward_gpu(Tensor &x,Tensor &dx,Tensor &dy,float factor,ExecutionContext const &ctx);
65 
66  template<typename Dtype,typename ReduceOpts>
67  void forward_cpu(Tensor &in,Tensor &output,ReduceOpts rop);
68 
69  void backward_cpu_max(Tensor &x,Tensor &dx,Tensor &dy,float factor);
70  template<typename ReduceOpts>
71  void backward_cpu_ave(Tensor &dx,Tensor &dy,float factor,ReduceOpts rop);
72 
73  template<typename T>
74  struct MaxRedcue;
75  template<typename T>
76  struct AveReduceFull;
77  template<typename T>
78  struct AveReduceValid;
79 
80 
81  struct ReduceMax;
82  struct ReduceAve;
83  struct NormIdent;
84  struct NormPartIdent;
85  struct NormAve;
86  struct NormPartAve;
87 
88  Pooling2DConfig config_;
89  DataType dtype_;
90 
91  std::unique_ptr<core::Pooling2DForward> fwd_;
92  std::unique_ptr<core::Pooling2DBackwardBase> bwd_;
93  };
94 
95  struct GlobalPoolingConfig : public PoolingBase {
96  static GlobalPoolingConfig from_json(json::value const &v)
97  {
99  static_cast<PoolingBase &>(cfg) = PoolingBase::from_json(v);
100  return cfg;
101  }
102  };
103 
104  class GlobalPooling : public Operator {
105  public:
107  virtual ~GlobalPooling();
108 
109  virtual char const *operator_type() const
110  {
111  return "GlobalPooling";
112  }
113  virtual void setup(std::vector<TensorSpecs> const &in,
114  std::vector<TensorSpecs> &out,
115  std::vector<TensorSpecs> &parameters,
116  size_t &workspace);
117 
118  virtual void reshape(std::vector<Shape> const &in,
119  std::vector<Shape> &out,
120  size_t &ws);
121 
122  virtual void forward(std::vector<Tensor> &input,
123  std::vector<Tensor> &output,
124  std::vector<Tensor> &parameters,
125  Tensor &workspace,
126  ExecutionContext const &ctx);
127 
128  virtual void backward(std::vector<TensorAndGradient> &input,
129  std::vector<TensorAndGradient> &output,
130  std::vector<TensorAndGradient> &parameters,
131  Tensor &workspace,
132  ExecutionContext const &ctx);
133 
134  private:
135  void forward_gpu(Tensor &input,Tensor &output,ExecutionContext const &ctx);
136  void forward_cpu(Tensor &input,Tensor &output);
137  void backward_cpu(Tensor &x,Tensor &dx,Tensor &dy,float factor);
138  void backward_gpu(Tensor &x,Tensor &dx,Tensor &dy,float factor,ExecutionContext const &ctx);
139  size_t setup_kernel(Shape const &sp);
140 
141  GlobalPoolingConfig cfg_;
142  DataType dtype_;
143  std::unique_ptr<core::Pooling2DForward> fwd_;
144  std::unique_ptr<core::Pooling2DBackwardBase> bwd_;
145  };
146 
147 } // namespace
Definition: pooling.hpp:21
Tensor shape.
Definition: shape.hpp:18
Definition: pooling.hpp:104
Base class for backward/forward propogation calculations for internal network.
Definition: operator.hpp:15
This is main object that represent the pair of OpenCL platform and device all other objects use it...
Definition: context.hpp:302
DataType
type definition
Definition: definitions.hpp:70
virtual char const * operator_type() const
name of the operator type
Definition: pooling.hpp:109
Definition: pooling.hpp:30
Definition: pooling.hpp:95
This class is central representation of json objects.
Definition: json.hpp:652
virtual char const * operator_type() const
name of the operator type
Definition: pooling.hpp:35
Mane namespace.
Definition: context.hpp:9
Definition: pooling.hpp:10
Central Data Contrainer - Tensor.
Definition: tensor.hpp:99
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121