DLPrimitives
conv2d.hpp
1 #pragma once
2 #include <dlprim/operator.hpp>
3 
4 namespace dlprim {
5 
6  namespace gpu { class GEMM; }
7  namespace core { class Conv2DForward; class Conv2DBackwardData; class Conv2DBackwardFilter; }
8  class BWBias;
9  class Scal;
10 
12  bool bias = true;
13 
14  std::string fwd_algo="auto";
15  std::string bwd_data_algo="auto";
16  std::string bwd_filter_algo="auto";
17 
18  StandardActivations activation=StandardActivations::identity;
19  static Convolution2DConfig from_json(json::value const &v);
20  };
21 
23  protected:
24  template<typename Op,typename DType>
25  static void im2col(Shape const &in,Shape const &outs,DType *img_in,DType *mat_in,Convolution2DConfig const &config);
26  static void fwd_bwd_cpu(GemmOpMode mode,Tensor &in,Tensor &out,Tensor &W,Tensor *bias_tensor,void *ws,Convolution2DConfig const &config,float fwd_beta=0.0f);
27  static void scale_cpu(Tensor &t,float v);
28  };
29 
30  class Convolution2D : public Operator, public Convolution2DBase {
31  public:
32 
33  Convolution2D(Context &ctx,Convolution2DConfig const &cfg);
34  virtual ~Convolution2D();
35 
36  virtual char const *operator_type() const
37  {
38  return "Convolution2D";
39  }
40 
41  void initialize_params(std::vector<Tensor> &parameters,ExecutionContext const &e);
42 
43  virtual void setup(std::vector<TensorSpecs> const &in,
44  std::vector<TensorSpecs> &out,
45  std::vector<TensorSpecs> &parameters,
46  size_t &workspace);
47 
48  virtual void reshape(std::vector<Shape> const &in,
49  std::vector<Shape> &out,
50  size_t &ws);
51 
52  virtual void forward(std::vector<Tensor> &input,
53  std::vector<Tensor> &output,
54  std::vector<Tensor> &parameters,
55  Tensor &workspace,
56  ExecutionContext const &ctx);
57 
58  virtual void backward(std::vector<TensorAndGradient> &input,
59  std::vector<TensorAndGradient> &output,
60  std::vector<TensorAndGradient> &parameters,
61  Tensor &workspace,
62  ExecutionContext const &ctx);
63 
64 
65  Shape get_output_shape(Shape const &in);
66  protected:
67 
68  size_t calc_workspace(Shape const &in);
69  void setup_algo(Shape const &in);
70  int get_im2col_width();
71  void forward_gpu(Tensor &in,Tensor &out,Tensor &M,Tensor *bias,ExecutionContext const &ctx);
72  void forward_cpu(Tensor &in,Tensor &out,Tensor &M,Tensor *bias,void *ws);
73 
74  void backward_filter_cpu(Tensor &dy,Tensor &x,Tensor &dK,Tensor &ws,float factor);
75  void backward_data_cpu(Tensor &dy,Tensor &K,Tensor &dx,Tensor &ws,float factor);
76 
77 
78 
79 
80  Convolution2DConfig config_;
81  DataType dtype_;
82  std::unique_ptr<core::Conv2DForward> conv_;
83  std::unique_ptr<core::Conv2DBackwardData> conv_bwd_data_;
84  std::unique_ptr<core::Conv2DBackwardFilter> conv_bwd_filter_;
85 
86  std::unique_ptr<Operator> activation_;
87  std::unique_ptr<BWBias> bwd_bias_;
88  size_t ws_size_;
89  size_t out_h_,out_w_;
90  size_t in_h_,in_w_;
91  size_t bs_;
92 
93  };
94 
96  int output_pad[2] = {0,0}; // Output padding - due to possible ambiguity of input/output size calcuulations
97 
98  static TransposedConvolution2DConfig from_json(json::value const &v);
99  };
100 
102  public:
103 
105  virtual ~TransposedConvolution2D();
106 
107  virtual char const *operator_type() const
108  {
109  return "TransposedConvolution2D";
110  }
111 
112  void initialize_params(std::vector<Tensor> &parameters,ExecutionContext const &e);
113 
114  virtual void setup(std::vector<TensorSpecs> const &in,
115  std::vector<TensorSpecs> &out,
116  std::vector<TensorSpecs> &parameters,
117  size_t &workspace);
118 
119  virtual void reshape(std::vector<Shape> const &in,
120  std::vector<Shape> &out,
121  size_t &ws);
122 
123  virtual void forward(std::vector<Tensor> &input,
124  std::vector<Tensor> &output,
125  std::vector<Tensor> &parameters,
126  Tensor &workspace,
127  ExecutionContext const &ctx);
128 
129  virtual void backward(std::vector<TensorAndGradient> &input,
130  std::vector<TensorAndGradient> &output,
131  std::vector<TensorAndGradient> &parameters,
132  Tensor &workspace,
133  ExecutionContext const &ctx);
134 
135 
136  Shape get_output_shape(Shape const &in);
137  protected:
138 
139  size_t calc_workspace(Shape const &in);
140  void setup_algo(Shape const &in);
141  int get_im2col_width();
142  void forward_gpu(Tensor &in,Tensor &out,Tensor &M,Tensor *bias,ExecutionContext const &ctx);
143  void forward_cpu(Tensor &in,Tensor &out,Tensor &M,Tensor *bias,void *ws);
144 
145  void backward_filter_cpu(Tensor &dy,Tensor &x,Tensor &dK,Tensor &ws,float factor);
146  void backward_data_cpu(Tensor &dy,Tensor &K,Tensor &dx,Tensor &ws,float factor);
147 
148 
150  // in/out channels switched
151  Convolution2DConfig conv_config_;
152 
153 
154  DataType dtype_;
155  std::unique_ptr<core::Conv2DForward> conv_bwd_data_;
156  std::unique_ptr<core::Conv2DBackwardData> conv_fwd_;
157  std::unique_ptr<core::Conv2DBackwardFilter> conv_bwd_filter_;
158 
159  std::unique_ptr<Operator> activation_;
160  std::unique_ptr<BWBias> bwd_bias_;
161  size_t ws_size_;
162  size_t out_h_,out_w_;
163  size_t in_h_,in_w_;
164  size_t bs_;
165 
166  };
167 
168 
169 
170 } // namespace
171 
Definition: conv2d.hpp:95
Tensor shape.
Definition: shape.hpp:18
Definition: conv2d.hpp:30
Definition: conv2d.hpp:101
Definition: conv2d.hpp:11
Convolution settings.
Definition: definitions.hpp:301
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: conv2d.hpp:107
virtual char const * operator_type() const
name of the operator type
Definition: conv2d.hpp:36
This class is central representation of json objects.
Definition: json.hpp:652
Mane namespace.
Definition: context.hpp:9
Central Data Contrainer - Tensor.
Definition: tensor.hpp:99
GemmOpMode
internal GEMM mode
Definition: definitions.hpp:292
StandardActivations
Parameterless Activations that can be embedded to general kernels like inner product or convolution...
Definition: definitions.hpp:266
Definition: conv2d.hpp:22
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121