Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / api / CPP / pooling.hpp
1 /*
2 // Copyright (c) 2016-2018 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/pooling.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 Select method for the @ref pooling layer.
32 enum class pooling_mode : int32_t
33 {
34     /// @brief Maximum-pooling method.
35     max     = cldnn_pooling_max,
36     /// @brief Average-pooling method - values 
37     average = cldnn_pooling_average,
38     /// @brief Average-pooling method without values which are outside of the input.
39     average_no_padding = cldnn_pooling_average_no_padding,
40     /// @brief Maximum-pooling method with additional buffer to store argmax indices.
41     max_with_argmax = cldnn_pooling_max_with_argmax,
42     /// @brief Pooling with bilinear interpolation
43     bilinear = cldnn_pooling_bilinear
44 };
45
46 /// @brief Performs "pooling" operation which is a form of non-linear down-sampling.
47 /// @details Pools the input image by taking the max, average, etc. within regions.
48 struct pooling : public primitive_base<pooling, CLDNN_PRIMITIVE_DESC(pooling)>
49 {
50     CLDNN_DECLARE_PRIMITIVE(pooling)
51
52     /// @brief Constructs pooling primitive.
53     /// @param id This primitive id.
54     /// @param input Input primitive id.
55     /// @param mode Pooling mode.
56     /// @param stride Defines shift in input buffer between adjacent calculations of output values.
57     /// @param size Pooling kernel size.
58     /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the pooling window should start calculations.
59     pooling(
60         const primitive_id& id,
61         const primitive_id& input,
62         pooling_mode mode,
63         const tensor& size,
64         const tensor& stride,
65         const tensor& input_offset = { 0,0,0,0 },
66         const padding& output_padding = padding()
67         )
68         : primitive_base(id, {input}, output_padding)
69         , argmax("")
70         , mode(static_cast<pooling_mode>(mode))
71         , global_pooling(false)
72         , input_offset(input_offset)
73         , stride(stride)
74         , size(size)
75         , with_output_size(false)
76     {}
77
78     /// @brief Constructs pooling primitive with argmax.
79     /// @param id This primitive id.
80     /// @param input Input primitive id.
81     /// @param argmax Primitive id which contains indices of each max pooling region. Indices must be in flattened bfyx format with no padding. Needs to be fp32 data type.
82     /// @param mode Pooling mode.
83     /// @param stride Defines shift in input buffer between adjacent calculations of output values.
84     /// @param size Pooling kernel size.
85     /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the pooling window should start calculations.
86     pooling(
87         const primitive_id& id,
88         const primitive_id& input,
89         const primitive_id& argmax,
90         pooling_mode mode,
91         const tensor& size,
92         const tensor& stride,
93         const tensor& input_offset = { 0,0,0,0 },
94         const padding& output_padding = padding()
95     )
96         : primitive_base(id, { input }, output_padding)
97         , argmax(argmax)
98         , mode(static_cast<pooling_mode>(mode))
99         , global_pooling(false)
100         , input_offset(input_offset)
101         , stride(stride)
102         , size(size)
103         , with_output_size(false)
104     {}
105
106     /// @brief Constructs pooling primitive (computes input paddings to match output size).
107     /// @param id This primitive id.
108     /// @param input Input primitive id.
109     /// @param mode Pooling mode.
110     /// @param stride Defines shift in input buffer between adjacent calculations of output values.
111     /// @param size Pooling kernel size.
112     /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the pooling window should start calculations.
113     /// @param output_size User-defined output data size of the primitive (w/o padding).
114     pooling(
115         const primitive_id& id,
116         const primitive_id& input,
117         pooling_mode mode,
118         const tensor& size,
119         const tensor& stride,
120         const tensor& input_offset,
121         tensor output_size,
122         const padding& output_padding = padding()
123         )
124         : primitive_base(id, {input}, output_padding)
125         , argmax("")
126         , mode(static_cast<pooling_mode>(mode))
127         , global_pooling(false)
128         , input_offset(input_offset)
129         , stride(stride)
130         , size(size)
131         , with_output_size(true)
132         , output_size(output_size)
133     {}
134
135     /// @brief Constructs pooling primitive with argmax (computes input paddings to match output size).
136     /// @param id This primitive id.
137     /// @param input Input primitive id.
138     /// @param argmax Primitive id which contains indices of each max pooling region. Indices must be in flattened bfyx format with no padding. Needs to be fp32 data type.
139     /// @param mode Pooling mode.
140     /// @param stride Defines shift in input buffer between adjacent calculations of output values.
141     /// @param size Pooling kernel size.
142     /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the pooling window should start calculations.
143     /// @param output_size User-defined output data size of the primitive (w/o padding).
144     pooling(
145         const primitive_id& id,
146         const primitive_id& input,
147         const primitive_id& argmax,
148         pooling_mode mode,
149         const tensor& size,
150         const tensor& stride,
151         const tensor& input_offset,
152         tensor output_size,
153         const padding& output_padding = padding()
154     )
155         : primitive_base(id, { input }, output_padding)
156         , argmax(argmax)
157         , mode(static_cast<pooling_mode>(mode))
158         , global_pooling(false)
159         , input_offset(input_offset)
160         , stride(stride)
161         , size(size)
162         , with_output_size(true)
163         , output_size(output_size)
164     {}
165
166     /// @brief Constructs pooling primitive with kernel size equal to the spatial dimension of input tensor.
167     /// @param id This primitive id.
168     /// @param input Input primitive id.
169     /// @param mode Pooling mode.
170     pooling(
171         const primitive_id& id,
172         const primitive_id& input,
173         pooling_mode mode,
174         const padding& output_padding = padding()
175     )
176         : primitive_base(id, { input }, output_padding)
177         , argmax("")
178         , mode(static_cast<pooling_mode>(mode))
179         , global_pooling(true)
180         , input_offset(0, 0, 0, 0)
181         , stride(1, 1, 1, 1)
182         , size(0, 0, 0, 0)
183         , with_output_size(false)
184     {}
185
186     /// @brief Constructs a copy from C API @CLDNN_PRIMITIVE_DESC{pooling}
187     pooling(const dto* dto)
188         : primitive_base(dto)
189         , argmax(dto->argmax)
190         , mode(static_cast<pooling_mode>(dto->mode))
191         , global_pooling(dto->global_pooling != 0)
192         , input_offset(dto->input_offset)
193         , stride(dto->stride)
194         , size(dto->size)
195         , with_output_size(dto->with_output_size != 0)
196         , output_size(dto->output_size)
197     {}
198
199     /// @brief Constructs pooling primitive (computes input paddings to match output size).
200     /// @param id This primitive id.
201     /// @param input Input primitive id.
202     /// @param mode Pooling mode.
203     /// @param stride Defines shift in input buffer between adjacent calculations of output values.
204     /// @param size Pooling kernel size.
205     /// @param output_size User-defined output data size of the primitive (w/o padding).
206     /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the pooling window should start calculations.
207     /// @return Pooling primitive with specified settings.
208     static pooling create_with_output_size(
209         const primitive_id& id,
210         const primitive_id& input,
211         tensor output_size,
212         pooling_mode mode,
213         const tensor& size,
214         const tensor& stride,
215         const tensor& input_offset = { 0,0,0,0 },
216         const padding& output_padding = padding()
217     )
218     {
219         return pooling(id, input, mode, size, stride, input_offset, output_size, output_padding);
220     }
221
222     /// @brief Constructs pooling primitive with argmax (computes input paddings to match output size).
223     /// @param id This primitive id.
224     /// @param input Input primitive id.
225     /// @param argmax Primitive id which contains indices of each max pooling region. Indices must be in flattened bfyx format with no padding. Needs to be fp32 data type.
226     /// @param mode Pooling mode.
227     /// @param stride Defines shift in input buffer between adjacent calculations of output values.
228     /// @param size Pooling kernel size.
229     /// @param output_size User-defined output data size of the primitive (w/o padding).
230     /// @param input_offset Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the pooling window should start calculations.
231     /// @return Pooling primitive with specified settings.
232     static pooling create_with_output_size(
233         const primitive_id& id,
234         const primitive_id& input,
235         const primitive_id& argmax,
236         tensor output_size,
237         pooling_mode mode,
238         const tensor& size,
239         const tensor& stride,
240         const tensor& input_offset = { 0,0,0,0 },
241         const padding& output_padding = padding()
242     )
243     {
244         return pooling(id, input, argmax, mode, size, stride, input_offset, output_size, output_padding);
245     }
246
247     /// @brief Primitive id which contains indices of each max pooling region. Indices must be in flattened bfyx format with no padding. Needs to be fp32 data type.
248     primitive_id argmax;
249     /// @brief Pooling mode.
250     pooling_mode mode;
251     /// @brief Global pooling (kernel size is equal to the spatial dimension of input tensor)
252     bool global_pooling;
253     /// @brief Defines a shift, relative to (0,0) position of the input buffer, where (0,0) point of the pooling window should start calculations.
254     tensor input_offset;
255     /// @brief Defines shift in input buffer between adjacent calculations of output values.
256     tensor stride;
257     /// @brief Pooling kernel size.
258     tensor size;
259     /// @brief Indicates that the primitive has user-defined output size (non-zero value).
260     bool with_output_size;
261     /// @brief User-defined output data size of the primitive (w/o padding).
262     tensor output_size;
263
264 protected:
265     std::vector<std::reference_wrapper<const primitive_id>> get_dependencies() const override
266     {
267         if (argmax.empty())
268             return{};
269         return{ argmax };
270     }
271
272     void update_dto(dto& dto) const override
273     {
274         dto.mode = static_cast<int32_t>(mode);
275         dto.argmax = argmax.c_str();
276         dto.input_offset = input_offset;
277         dto.stride = stride;
278         dto.size = size;
279         dto.with_output_size = with_output_size;
280         dto.output_size = output_size;
281         dto.global_pooling = global_pooling;
282     }
283 };
284 /// @}
285 /// @}
286 /// @}
287 }