DLPrimitives
pool.hpp
1 #pragma once
2 #include <dlprim/tensor.hpp>
3 #include <dlprim/context.hpp>
4 namespace dlprim {
5 namespace core {
6 
10  inline int calc_pooling_output_size(int in_size,int kernel,int pad,int stride,bool ceil_mode)
11  {
12  int padded_size = in_size + pad*2;
13  DLPRIM_CHECK(padded_size >= kernel);
14  int offset = ceil_mode ? (stride-1) : 0;
15  int size = (padded_size - kernel + offset) / stride + 1;
16  if((size - 1) * stride >= in_size + pad) {
17  size--;
18  }
19  return size;
20  }
21 
22 
27  public:
28  virtual ~Pooling2DForward() {}
29  // get workspace size
30  virtual size_t workspace() = 0;
31 
37  virtual void enqueue(Tensor &X,Tensor &Y,ExecutionContext const &e) = 0;
38 
42  static std::unique_ptr<Pooling2DForward> create_max_pooling(
43  Context &ctx,
44  int kernel[2],int pad[2],int stride[2],
45  DataType dt=float_data);
46 
53  static std::unique_ptr<Pooling2DForward> create_avg_pooling(
54  Context &ctx,
55  int kernel[2],int pad[2],int stride[2],bool count_include_pad=false,
56  DataType dt=float_data);
57 
59  static std::unique_ptr<Pooling2DForward> create_global_max_pooling(
60  Context &ctx,Shape const &in_shape,DataType dt=float_data);
61 
63  static std::unique_ptr<Pooling2DForward> create_global_avg_pooling(
64  Context &ctx,Shape const &in_shape,DataType dt=float_data);
65  };
66 
71  public:
73  virtual size_t workspace() = 0;
74  virtual ~Pooling2DBackwardBase() {}
81  virtual void enqueue(Tensor &X,Tensor &dX,Tensor &dY,float factor,ExecutionContext const &e) = 0;
82  };
83 
88  public:
89  virtual ~MaxPooling2DBackward() {}
92  static std::unique_ptr<MaxPooling2DBackward> create(
93  Context &ctx,
94  int kernel[2],int pad[2],int stride[2],
95  DataType dt=float_data);
96 
98  static std::unique_ptr<MaxPooling2DBackward> create_global(
99  Context &ctx,Shape const &in_shape,DataType dt=float_data);
100  };
101 
103  public:
104  virtual ~AvgPooling2DBackward() {}
105 
112  virtual void enqueue(Tensor &/*X*/,Tensor &dX,Tensor &dY,float factor,ExecutionContext const &e)
113  {
114  enqueue(dX,dY,factor,e);
115  }
122  virtual void enqueue(Tensor &dX,Tensor &dY,float factor,ExecutionContext const &e) = 0;
123 
125  static std::unique_ptr<AvgPooling2DBackward> create(
126  Context &ctx,
127  int kernel[2],int pad[2],int stride[2],bool count_include_pad=false,
128  DataType dt=float_data);
129 
131  static std::unique_ptr<AvgPooling2DBackward> create_global(
132  Context &ctx,Shape const &in_shape,DataType dt=float_data);
133  };
134 } // core
135 } //dlprim
Tensor shape.
Definition: shape.hpp:18
Backward computation for max pooling.
Definition: pool.hpp:87
2d pooling
Definition: pool.hpp:26
This is main object that represent the pair of OpenCL platform and device all other objects use it...
Definition: context.hpp:302
static std::unique_ptr< Pooling2DForward > create_avg_pooling(Context &ctx, int kernel[2], int pad[2], int stride[2], bool count_include_pad=false, DataType dt=float_data)
Create max pooling for kernel, pad, stride.
DataType
type definition
Definition: definitions.hpp:70
static std::unique_ptr< Pooling2DForward > create_global_avg_pooling(Context &ctx, Shape const &in_shape, DataType dt=float_data)
Avergage global pooling.
virtual void enqueue(Tensor &, Tensor &dX, Tensor &dY, float factor, ExecutionContext const &e)
for Avg pooling we don&#39;t need X so you can call directrly enqueue(dX,dY,factor,e) ...
Definition: pool.hpp:112
Mane namespace.
Definition: context.hpp:9
Central Data Contrainer - Tensor.
Definition: tensor.hpp:99
Backward pooling computation.
Definition: pool.hpp:70
static std::unique_ptr< Pooling2DForward > create_max_pooling(Context &ctx, int kernel[2], int pad[2], int stride[2], DataType dt=float_data)
Create max pooling for kernel, pad, stride.
static std::unique_ptr< Pooling2DForward > create_global_max_pooling(Context &ctx, Shape const &in_shape, DataType dt=float_data)
Max global pooling.
virtual void enqueue(Tensor &X, Tensor &Y, ExecutionContext const &e)=0
when used with kernel based pooling (not global) X and Y dimensions should match at batch and channel...
int calc_pooling_output_size(int in_size, int kernel, int pad, int stride, bool ceil_mode)
Compute output size of the tensor after pooling in specific dimentions.
Definition: pool.hpp:10
Definition: pool.hpp:102
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121