Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / api / CPP / deconvolution.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
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #pragma once
19 #include "../C/deconvolution.h"
20 #include "primitive.hpp"
21
22 namespace cldnn
23 {
24 /// @addtogroup cpp_api C++ API
25 /// @{
26 /// @addtogroup cpp_topology Network Topology
27 /// @{
28 /// @addtogroup cpp_primitives Primitives
29 /// @{
30
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)>
35 {
36     CLDNN_DECLARE_PRIMITIVE(deconvolution)
37
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.
47     deconvolution(
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()
57     )
58         :primitive_base(id, { input }, output_padding)
59         , weights(_weights.cpp_ids)
60         , bias(_bias.cpp_ids)
61         , input_offset(input_offset)
62         , stride(stride)
63         , with_activation(with_activation)
64         , activation_negative_slope(activation_slp)
65         , with_output_size(false)
66         , groups(1)
67         , _weights(weights)
68         , _bias(bias)
69         , _gradient(false)
70     {
71     }
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.
82     deconvolution(
83         const primitive_id& id,
84         const primitive_id& input,
85         const std::vector<primitive_id>& weights,
86         const std::vector<primitive_id>& bias,
87         uint32_t groups,
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()
93     )
94         :primitive_base(id, { input }, output_padding)
95         , weights(_weights.cpp_ids)
96         , bias(_bias.cpp_ids)
97         , input_offset(input_offset)
98         , stride(stride)
99         , with_activation(with_activation)
100         , activation_negative_slope(activation_slp)
101         , with_output_size(false)
102         , groups(groups)
103         , _weights(weights)
104         , _bias(bias)
105         , _gradient(false)
106     {
107     }
108
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.
117     deconvolution(
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
127     )
128         :primitive_base(id, { input }, output_padding)
129         , weights(_weights.cpp_ids)
130         , bias(_bias.cpp_ids)
131         , input_offset(input_offset)
132         , stride(stride)
133         , with_activation(with_activation)
134         , activation_negative_slope(activation_slp)
135         , with_output_size(false)
136         , groups(1)
137         , _weights(weights)
138         , _bias(std::vector<primitive_id>(0))
139         , _gradient(gradient)
140     {
141     }
142
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.
152     deconvolution(
153         const primitive_id& id,
154         const primitive_id& input,
155         const std::vector<primitive_id>& weights,
156         uint32_t groups,
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
163     )
164         :primitive_base(id, { input }, output_padding)
165         , weights(_weights.cpp_ids)
166         , bias(_bias.cpp_ids)
167         , input_offset(input_offset)
168         , stride(stride)
169         , with_activation(with_activation)
170         , activation_negative_slope(activation_slp)
171         , with_output_size(false)
172         , groups(groups)
173         , _weights(weights)
174         , _bias(std::vector<primitive_id>(0))
175         , _gradient(gradient)
176     {
177     }
178
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).
189     deconvolution(
190         const primitive_id& id,
191         const primitive_id& input,
192         const std::vector<primitive_id>& weights,
193         const std::vector<primitive_id>& bias,
194         tensor stride,
195         tensor input_offset,
196         bool with_activation,
197         float activation_slp,
198         tensor output_size,
199         const padding& output_padding = padding()
200     )
201         :primitive_base(id, { input }, output_padding)
202         , weights(_weights.cpp_ids)
203         , bias(_bias.cpp_ids)
204         , input_offset(input_offset)
205         , stride(stride)
206         , with_activation(with_activation)
207         , activation_negative_slope(activation_slp)
208         , with_output_size(true)
209         , output_size(output_size)
210         , groups(1)
211         , _weights(weights)
212         , _bias(bias)
213         , _gradient(false)
214     {
215     }
216
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).
228     deconvolution(
229         const primitive_id& id,
230         const primitive_id& input,
231         const std::vector<primitive_id>& weights,
232         const std::vector<primitive_id>& bias,
233         uint32_t groups,
234         tensor stride,
235         tensor input_offset,
236         bool with_activation,
237         float activation_slp,
238         tensor output_size,
239         const padding& output_padding = padding()
240     )
241         :primitive_base(id, { input }, output_padding)
242         , weights(_weights.cpp_ids)
243         , bias(_bias.cpp_ids)
244         , input_offset(input_offset)
245         , stride(stride)
246         , with_activation(with_activation)
247         , activation_negative_slope(activation_slp)
248         , with_output_size(true)
249         , output_size(output_size)
250         , groups(groups)
251         , _weights(weights)
252         , _bias(bias)
253         , _gradient(false)
254     {
255     }
256
257
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).
267     deconvolution(
268         const primitive_id& id,
269         const primitive_id& input,
270         const std::vector<primitive_id>& weights,
271         tensor stride,
272         tensor input_offset,
273         bool with_activation,
274         float activation_slp,
275         tensor output_size,
276         const padding& output_padding = padding(),
277         bool gradient = false
278     )
279         :primitive_base(id, { input }, output_padding)
280         , weights(_weights.cpp_ids)
281         , bias(_bias.cpp_ids)
282         , input_offset(input_offset)
283         , stride(stride)
284         , with_activation(with_activation)
285         , activation_negative_slope(activation_slp)
286         , with_output_size(true)
287         , output_size(output_size)
288         , groups(1)
289         , _weights(weights)
290         , _bias(std::vector<primitive_id>(0))
291         , _gradient(gradient)
292     {
293     }
294
295     /// @brief Constructs a copy from C API @CLDNN_PRIMITIVE_DESC{deconvolution}
296     deconvolution(const dto* dto)
297         :primitive_base(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)
308         , _bias(dto->bias)
309         , _gradient(dto->gradient != 0)
310     {
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");
313     }
314
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,
331         tensor output_size,
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()
337     )
338     {
339         return deconvolution(id, input, weights, bias, stride, input_offset, with_activation,
340             activation_slp, output_size, output_padding);
341     }
342
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,
357         tensor output_size,
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()
363     )
364     {
365         return deconvolution(id, input, weights, stride, input_offset, with_activation,
366             activation_slp, output_size, output_padding);
367     }
368
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.
374     tensor input_offset;
375     /// @brief Defines shift in input buffer between adjacent calculations of output values.
376     tensor stride;
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).
384     tensor output_size;
385     /// @brief Number of feature groups (grouped convolution). If more than 1 then weights/bias count needs to be 1.
386     uint32_t groups;
387
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; }
392
393 protected:
394     primitive_id_arr _weights;
395     primitive_id_arr _bias;
396     bool _gradient;
397
398     std::vector<std::reference_wrapper<const primitive_id>> get_dependencies() const override
399     {
400         std::vector<std::reference_wrapper<const primitive_id>> ret;
401         ret.reserve(weights.size() + bias.size());
402         for (auto& w : weights)
403             ret.push_back(w);
404         for (auto& b : bias)
405             ret.push_back(b);
406
407         return ret;
408     }
409
410     void update_dto(dto& dto) const override
411     {
412         dto.weights = _weights.ref();
413         dto.bias = _bias.ref();
414         dto.input_offset = input_offset;
415         dto.split = split();
416         dto.stride = stride;
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;
422         dto.groups = groups;
423     }
424 };
425 /// @}
426 /// @}
427 /// @}
428 }