2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 #include "../C/deconvolution.h"
20 #include "primitive.hpp"
24 /// @addtogroup cpp_api C++ API
26 /// @addtogroup cpp_topology Network Topology
28 /// @addtogroup cpp_primitives Primitives
31 /// @brief Performs transposed convolution.
32 /// Also supports built-in Relu @ref activation available by setting it in arguments.
33 /// @details Deconvolution is similar to convolution layer with the weights flipped on the axis and stride and input padding parameters used in opposite sense as in convolution.
34 struct deconvolution : public primitive_base<deconvolution, CLDNN_PRIMITIVE_DESC(deconvolution)>
36 CLDNN_DECLARE_PRIMITIVE(deconvolution)
38 /// @brief Constructs deconvolution primitive.
39 /// @param id This primitive id.
40 /// @param input Input primitive id.
41 /// @param weights List of primitive ids containing weights data.
42 /// @param bias List of primitive ids containing bias data. Provide empty vector if using next parameters without bias.
43 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
44 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
45 /// @param with_activation Enables Relu activation.
46 /// @param activation_slp Relu activation slope.
48 const primitive_id& id,
49 const primitive_id& input,
50 const std::vector<primitive_id>& weights,
51 const std::vector<primitive_id>& bias,
52 tensor stride = { 1, 1, 1, 1 },
53 tensor input_offset = { 0,0,0,0 },
54 bool with_activation = false,
55 float activation_slp = 0.0f,
56 const padding& output_padding = padding()
58 :primitive_base(id, { input }, output_padding)
59 , weights(_weights.cpp_ids)
61 , input_offset(input_offset)
63 , with_activation(with_activation)
64 , activation_negative_slope(activation_slp)
65 , with_output_size(false)
72 /// @brief Constructs deconvolution primitive.
73 /// @param id This primitive id.
74 /// @param input Input primitive id.
75 /// @param groups Number of filter groups.
76 /// @param weights List of primitive ids containing weights data.
77 /// @param bias List of primitive ids containing bias data. Provide empty vector if using next parameters without bias.
78 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
79 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
80 /// @param with_activation Enables Relu activation.
81 /// @param activation_slp Relu activation slope.
83 const primitive_id& id,
84 const primitive_id& input,
85 const std::vector<primitive_id>& weights,
86 const std::vector<primitive_id>& bias,
88 tensor stride = { 1, 1, 1, 1 },
89 tensor input_offset = { 0,0,0,0 },
90 bool with_activation = false,
91 float activation_slp = 0.0f,
92 const padding& output_padding = padding()
94 :primitive_base(id, { input }, output_padding)
95 , weights(_weights.cpp_ids)
97 , input_offset(input_offset)
99 , with_activation(with_activation)
100 , activation_negative_slope(activation_slp)
101 , with_output_size(false)
109 /// @brief Constructs deconvolution primitive (w/o bias).
110 /// @param id This primitive id.
111 /// @param input Input primitive id.
112 /// @param weights List of primitive ids containing weights data.
113 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
114 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
115 /// @param with_activation Enables Relu activation.
116 /// @param activation_slp Relu activation slope.
118 const primitive_id& id,
119 const primitive_id& input,
120 const std::vector<primitive_id>& weights,
121 tensor stride = { 1, 1, 1, 1 },
122 tensor input_offset = { 0,0,0,0 },
123 bool with_activation = false,
124 float activation_slp = 0.0f,
125 const padding& output_padding = padding(),
126 bool gradient = false
128 :primitive_base(id, { input }, output_padding)
129 , weights(_weights.cpp_ids)
130 , bias(_bias.cpp_ids)
131 , input_offset(input_offset)
133 , with_activation(with_activation)
134 , activation_negative_slope(activation_slp)
135 , with_output_size(false)
138 , _bias(std::vector<primitive_id>(0))
139 , _gradient(gradient)
143 /// @brief Constructs deconvolution primitive (w/o bias).
144 /// @param id This primitive id.
145 /// @param input Input primitive id.
146 /// @param weights List of primitive ids containing weights data.
147 /// @param groups Number of filter groups.
148 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
149 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
150 /// @param with_activation Enables Relu activation.
151 /// @param activation_slp Relu activation slope.
153 const primitive_id& id,
154 const primitive_id& input,
155 const std::vector<primitive_id>& weights,
157 tensor stride = { 1, 1, 1, 1 },
158 tensor input_offset = { 0,0,0,0 },
159 bool with_activation = false,
160 float activation_slp = 0.0f,
161 const padding& output_padding = padding(),
162 bool gradient = false
164 :primitive_base(id, { input }, output_padding)
165 , weights(_weights.cpp_ids)
166 , bias(_bias.cpp_ids)
167 , input_offset(input_offset)
169 , with_activation(with_activation)
170 , activation_negative_slope(activation_slp)
171 , with_output_size(false)
174 , _bias(std::vector<primitive_id>(0))
175 , _gradient(gradient)
179 /// @brief Constructs deconvolution primitive (computes input paddings to match output size).
180 /// @param id This primitive id.
181 /// @param input Input primitive id.
182 /// @param weights List of primitive ids containing weights data.
183 /// @param bias List of primitive ids containing bias data. Provide empty vector if using next parameters without bias.
184 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
185 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
186 /// @param with_activation Enables Relu activation.
187 /// @param activation_slp Relu activation slope.
188 /// @param output_size User-defined output data size of the primitive (w/o padding).
190 const primitive_id& id,
191 const primitive_id& input,
192 const std::vector<primitive_id>& weights,
193 const std::vector<primitive_id>& bias,
196 bool with_activation,
197 float activation_slp,
199 const padding& output_padding = padding()
201 :primitive_base(id, { input }, output_padding)
202 , weights(_weights.cpp_ids)
203 , bias(_bias.cpp_ids)
204 , input_offset(input_offset)
206 , with_activation(with_activation)
207 , activation_negative_slope(activation_slp)
208 , with_output_size(true)
209 , output_size(output_size)
217 /// @brief Constructs deconvolution primitive (computes input paddings to match output size).
218 /// @param id This primitive id.
219 /// @param input Input primitive id.
220 /// @param weights List of primitive ids containing weights data.
221 /// @param bias List of primitive ids containing bias data. Provide empty vector if using next parameters without bias.
222 /// @param groups Number of filter groups.
223 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
224 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
225 /// @param with_activation Enables Relu activation.
226 /// @param activation_slp Relu activation slope.
227 /// @param output_size User-defined output data size of the primitive (w/o padding).
229 const primitive_id& id,
230 const primitive_id& input,
231 const std::vector<primitive_id>& weights,
232 const std::vector<primitive_id>& bias,
236 bool with_activation,
237 float activation_slp,
239 const padding& output_padding = padding()
241 :primitive_base(id, { input }, output_padding)
242 , weights(_weights.cpp_ids)
243 , bias(_bias.cpp_ids)
244 , input_offset(input_offset)
246 , with_activation(with_activation)
247 , activation_negative_slope(activation_slp)
248 , with_output_size(true)
249 , output_size(output_size)
258 /// @brief Constructs deconvolution primitive (w/o bias, computes input paddings to match output size).
259 /// @param id This primitive id.
260 /// @param input Input primitive id.
261 /// @param weights List of primitive ids containing weights data.
262 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
263 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
264 /// @param with_activation Enables Relu activation.
265 /// @param activation_slp Relu activation slope.
266 /// @param output_size User-defined output data size of the primitive (w/o padding).
268 const primitive_id& id,
269 const primitive_id& input,
270 const std::vector<primitive_id>& weights,
273 bool with_activation,
274 float activation_slp,
276 const padding& output_padding = padding(),
277 bool gradient = false
279 :primitive_base(id, { input }, output_padding)
280 , weights(_weights.cpp_ids)
281 , bias(_bias.cpp_ids)
282 , input_offset(input_offset)
284 , with_activation(with_activation)
285 , activation_negative_slope(activation_slp)
286 , with_output_size(true)
287 , output_size(output_size)
290 , _bias(std::vector<primitive_id>(0))
291 , _gradient(gradient)
295 /// @brief Constructs a copy from C API @CLDNN_PRIMITIVE_DESC{deconvolution}
296 deconvolution(const dto* dto)
298 , weights(_weights.cpp_ids)
299 , bias(_bias.cpp_ids)
300 , input_offset(dto->input_offset)
301 , stride(dto->stride)
302 , with_activation(dto->with_activation != 0)
303 , activation_negative_slope(dto->activation_negative_slope)
304 , with_output_size(dto->with_output_size != 0)
305 , output_size(dto->output_size)
306 , groups(dto->groups)
307 , _weights(dto->weights)
309 , _gradient(dto->gradient != 0)
311 if (!dto->split || (weights.size() != bias.size() && bias.size() != 0) || dto->split != weights.size())
312 throw std::invalid_argument("Invalid deconvolution dto: bad split value");
315 /// @brief Constructs deconvolution primitive (computes input paddings to match output size).
316 /// @param id This primitive id.
317 /// @param input Input primitive id.
318 /// @param weights List of primitive ids containing weights data.
319 /// @param bias List of primitive ids containing bias data. Provide empty vector if using next parameters without bias.
320 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
321 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
322 /// @param with_activation Enables Relu activation.
323 /// @param activation_slp Relu activation slope.
324 /// @param output_size User-defined output data size of the primitive (w/o padding).
325 /// @return Deconvolution primitive with specified settings.
326 static deconvolution create_with_output_size(
327 const primitive_id& id,
328 const primitive_id& input,
329 const std::vector<primitive_id>& weights,
330 const std::vector<primitive_id>& bias,
332 tensor stride = { 1, 1, 1, 1 },
333 tensor input_offset = { 0,0,0,0 },
334 bool with_activation = false,
335 float activation_slp = 0.0f,
336 const padding& output_padding = padding()
339 return deconvolution(id, input, weights, bias, stride, input_offset, with_activation,
340 activation_slp, output_size, output_padding);
343 /// @brief Constructs deconvolution primitive (w/o bias; computes input paddings to match output size).
344 /// @param id This primitive id.
345 /// @param input Input primitive id.
346 /// @param weights List of primitive ids containing weights data.
347 /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
348 /// @param stride Defines shift in input buffer between adjacent calculations of output values.
349 /// @param with_activation Enables Relu activation.
350 /// @param activation_slp Relu activation slope.
351 /// @param output_size User-defined output data size of the primitive (w/o padding).
352 /// @return Deconvolution primitive with specified settings.
353 static deconvolution create_with_output_size(
354 const primitive_id& id,
355 const primitive_id& input,
356 const std::vector<primitive_id>& weights,
358 tensor stride = { 1, 1, 1, 1 },
359 tensor input_offset = { 0,0,0,0 },
360 bool with_activation = false,
361 float activation_slp = 0.0f,
362 const padding& output_padding = padding()
365 return deconvolution(id, input, weights, stride, input_offset, with_activation,
366 activation_slp, output_size, output_padding);
369 /// @brief List of primitive ids containing weights data.
370 fixed_size_vector_ref weights;
371 /// @brief List of primitive ids containing bias data.
372 fixed_size_vector_ref bias;
373 /// @brief Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the deconvolution window should start calculations.
375 /// @brief Defines shift in input buffer between adjacent calculations of output values.
377 /// @brief Enables Relu activation.
378 bool with_activation;
379 /// @brief Relu activation slope.
380 float activation_negative_slope;
381 /// @brief Indicates that the primitive has user-defined output size (non-zero value).
382 bool with_output_size;
383 /// @brief User-defined output data size of the primitive (w/o padding).
385 /// @brief Number of feature groups (grouped convolution). If more than 1 then weights/bias count needs to be 1.
388 /// @brief On how many cards split the computation to.
389 int32_t split() const { return static_cast<int32_t>(weights.size()); }
390 /// @brief Indicates that deconvolution is used for convolution backward computation (convolution_grad_input)
391 bool gradient() const { return _gradient; }
394 primitive_id_arr _weights;
395 primitive_id_arr _bias;
398 std::vector<std::reference_wrapper<const primitive_id>> get_dependencies() const override
400 std::vector<std::reference_wrapper<const primitive_id>> ret;
401 ret.reserve(weights.size() + bias.size());
402 for (auto& w : weights)
410 void update_dto(dto& dto) const override
412 dto.weights = _weights.ref();
413 dto.bias = _bias.ref();
414 dto.input_offset = input_offset;
417 dto.with_activation = with_activation;
418 dto.activation_negative_slope = activation_negative_slope;
419 dto.with_output_size = with_output_size;
420 dto.output_size = output_size;
421 dto.gradient = _gradient;