clDNN
convolution.hpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
18 #pragma once
19 #include "../C/convolution.h"
20 #include "primitive.hpp"
21 
22 namespace cldnn
23 {
30 
34 struct convolution : public primitive_base<convolution, CLDNN_PRIMITIVE_DESC(convolution)>
35 {
36  CLDNN_DECLATE_PRIMITIVE(convolution)
37 
38 
51  const primitive_id& id,
52  const primitive_id& input,
53  const std::vector<primitive_id>& weights,
54  const std::vector<primitive_id>& bias,
55  tensor stride = { 1, 1, 1, 1 },
56  tensor input_offset = { 0,0,0,0 },
57  tensor dilation = { 1, 1, 1, 1 },
58  bool with_activation = false,
59  float activation_slp = 0.0f,
60  const padding& output_padding = padding()
61  )
63  , weights(_weights.cpp_ids)
64  , bias(_bias.cpp_ids)
66  , stride(stride)
69  , activation_negative_slope(activation_slp)
70  , with_output_size(false)
71  , _weights(weights)
72  , _bias(bias)
73  {
74  if ((bias.size() != 0) && (weights.size() != bias.size()))
75  throw std::runtime_error("convolution's weights/bias count does not match");
76  }
77 
90  const primitive_id& id,
91  const primitive_id& input,
92  const std::vector<primitive_id>& weights,
93  tensor stride = { 1, 1, 1, 1 },
94  tensor input_offset = { 0,0,0,0 },
95  tensor dilation = { 1, 1, 1, 1 },
96  bool with_activation = false,
97  float activation_slp = 0.0f,
98  const padding& output_padding = padding()
99  )
100  :primitive_base(id, { input }, output_padding)
101  , weights(_weights.cpp_ids)
102  , bias(_bias.cpp_ids)
104  , stride(stride)
105  , dilation(dilation)
107  , activation_negative_slope(activation_slp)
108  , with_output_size(false)
109  , _weights(weights)
110  , _bias(std::vector<primitive_id>(0))
111  {
112  }
113 
128  const primitive_id& id,
129  const primitive_id& input,
130  const std::vector<primitive_id>& weights,
131  const std::vector<primitive_id>& bias,
132  tensor stride,
135  bool with_activation,
136  float activation_slp,
138  const padding& output_padding = padding()
139  )
141  , weights(_weights.cpp_ids)
142  , bias(_bias.cpp_ids)
144  , stride(stride)
145  , dilation(dilation)
147  , activation_negative_slope(activation_slp)
148  , with_output_size(true)
150  , _weights(weights)
151  , _bias(bias)
152  {
153  if ((bias.size() != 0) && (weights.size() != bias.size()))
154  throw std::runtime_error("convolution's weights/bias count does not match");
155  }
156 
170  const primitive_id& id,
171  const primitive_id& input,
172  const std::vector<primitive_id>& weights,
173  tensor stride,
176  bool with_activation,
177  float activation_slp,
179  const padding& output_padding = padding()
180  )
182  , weights(_weights.cpp_ids)
183  , bias(_bias.cpp_ids)
185  , stride(stride)
186  , dilation(dilation)
188  , activation_negative_slope(activation_slp)
189  , with_output_size(true)
191  , _weights(weights)
192  , _bias(std::vector<primitive_id>(0))
193  {
194  }
195 
199  , weights(_weights.cpp_ids)
200  , bias(_bias.cpp_ids)
202  , stride(dto->stride)
203  , dilation(dto->dilation)
208  , _weights(dto->weights)
209  , _bias(dto->bias)
210  {
211  if (!dto->split || (weights.size() != bias.size() && bias.size() != 0) || dto->split != weights.size())
212  throw std::invalid_argument("Invalid convolution dto: bad split value");
213  }
214 
230  const primitive_id& id,
231  const primitive_id& input,
232  const std::vector<primitive_id>& weights,
233  const std::vector<primitive_id>& bias,
235  tensor stride = { 1, 1, 1, 1 },
236  tensor input_offset = { 0,0,0,0 },
237  tensor dilation = { 1, 1, 1, 1 },
238  bool with_activation = false,
239  float activation_slp = 0.0f,
240  const padding& output_padding = padding()
241  )
242  {
244  activation_slp, output_size, output_padding);
245  }
246 
261  const primitive_id& id,
262  const primitive_id& input,
263  const std::vector<primitive_id>& weights,
265  tensor stride = { 1, 1, 1, 1 },
266  tensor input_offset = { 0,0,0,0 },
267  tensor dilation = { 1, 1, 1, 1 },
268  bool with_activation = false,
269  float activation_slp = 0.0f,
270  const padding& output_padding = padding()
271  )
272  {
274  activation_slp, output_size, output_padding);
275  }
276 
297 
299  int32_t split() const { return static_cast<int32_t>(weights.size()); }
300 
301 protected:
302  primitive_id_arr _weights;
303  primitive_id_arr _bias;
304 
305  std::vector<std::reference_wrapper<const primitive_id>> get_dependencies() const override
306  {
307  std::vector<std::reference_wrapper<const primitive_id>> ret;
308  ret.reserve(weights.size() + bias.size());
309  for (auto& w : weights)
310  ret.push_back(w);
311  for (auto& b : bias)
312  ret.push_back(b);
313 
314  return ret;
315  }
316 
317  void update_dto(dto& dto) const override
318  {
319  dto.weights = _weights.ref();
320  dto.bias = _bias.ref();
321  dto.input_offset = input_offset;
322  dto.stride = stride;
323  dto.split = split();
324  dto.with_activation = with_activation;
325  dto.activation_negative_slope = activation_negative_slope;
326  dto.dilation = dilation;
327  dto.with_output_size = with_output_size;
328  dto.output_size = output_size;
329  }
330 };
334 }
convolution(const primitive_id &id, const primitive_id &input, const std::vector< primitive_id > &weights, tensor stride, tensor input_offset, tensor dilation, bool with_activation, float activation_slp, tensor output_size, const padding &output_padding=padding())
Constructs convolution primitive (w/o bias; computes input paddings to match output size)...
fixed_size_vector_ref bias
List of primitive ids containing bias data.
tensor output_size
User-defined output data size of the primitive (w/o padding).
Represents data padding information.
Definition: layout.hpp:125
static convolution create_with_output_size(const primitive_id &id, const primitive_id &input, const std::vector< primitive_id > &weights, tensor output_size, tensor stride={ 1, 1, 1, 1 }, tensor input_offset={ 0, 0, 0, 0 }, tensor dilation={ 1, 1, 1, 1 }, bool with_activation=false, float activation_slp=0.0f, const padding &output_padding=padding())
Constructs convolution primitive (w/o bias; computes input paddings to match output size)...
convolution(const primitive_id &id, const primitive_id &input, const std::vector< primitive_id > &weights, tensor stride={ 1, 1, 1, 1 }, tensor input_offset={ 0, 0, 0, 0 }, tensor dilation={ 1, 1, 1, 1 }, bool with_activation=false, float activation_slp=0.0f, const padding &output_padding=padding())
Constructs convolution primitive (w/o bias).
Definition: convolution.hpp:89
N-dimensional vector. Mostly used to represent memory size.
Definition: tensor.hpp:256
tensor dilation
Defines gaps in the input - dilation rate k=1 is normal convolution, k=2 means skipping one pixel per...
convolution(const primitive_id &id, const primitive_id &input, const std::vector< primitive_id > &weights, const std::vector< primitive_id > &bias, tensor stride, tensor input_offset, tensor dilation, bool with_activation, float activation_slp, tensor output_size, const padding &output_padding=padding())
Constructs convolution primitive (computes input paddings to match output size).
float activation_negative_slope
Relu activation slope.
const primitive_id id
Primitive&#39;s id.
Definition: primitive.hpp:143
tensor input_offset
Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the convolution...
uint32_t split
On how many cards split the computation to.
Definition: convolution.h:50
convolution(const dto *dto)
Constructs a copy from C API cldnn_convolution_desc.
tensor stride
Defines shift in input buffer between adjacent calculations of output values.
int32_t split() const
On how many cards split the computation to.
convolution(const primitive_id &id, const primitive_id &input, const std::vector< primitive_id > &weights, const std::vector< primitive_id > &bias, tensor stride={ 1, 1, 1, 1 }, tensor input_offset={ 0, 0, 0, 0 }, tensor dilation={ 1, 1, 1, 1 }, bool with_activation=false, float activation_slp=0.0f, const padding &output_padding=padding())
Constructs convolution primitive.
Definition: convolution.hpp:50
fixed_size_vector_ref weights
List of primitive ids containing weights data.
std::string primitive_id
Unique id of a primitive within a topology.
Definition: primitive.hpp:42
static convolution create_with_output_size(const primitive_id &id, const primitive_id &input, const std::vector< primitive_id > &weights, const std::vector< primitive_id > &bias, tensor output_size, tensor stride={ 1, 1, 1, 1 }, tensor input_offset={ 0, 0, 0, 0 }, tensor dilation={ 1, 1, 1, 1 }, bool with_activation=false, float activation_slp=0.0f, const padding &output_padding=padding())
Constructs convolution primitive (computes input paddings to match output size).
Performs forward spatial convolution with weight sharing. Also supports built-in Relu cldnn_activatio...
Definition: convolution.h:36
bool with_output_size
Indicates that the primitive has user-defined output size (non-zero value).
fixed_size_vector_ref input
List of ids of input primitives.
Definition: primitive.hpp:146
bool with_activation
Enable Relu activation.
base class for all primitives implementations.
Definition: primitive.hpp:190
padding output_padding
Requested output padding.
Definition: primitive.hpp:149
Initialize fields common for all primitives.
Definition: primitive.hpp:64
Performs forward spatial convolution with weight sharing. Also supports built-in Relu cldnn_activatio...
Definition: convolution.hpp:34