2 // Copyright (c) 2016-2018 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/pooling.h"
20 #include "primitive.hpp"
24 /// @addtogroup cpp_api C++ API
26 /// @addtogroup cpp_topology Network Topology
28 /// @addtogroup cpp_primitives Primitives
31 /// @brief Select method for the @ref pooling layer.
32 enum class pooling_mode : int32_t
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
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)>
50 CLDNN_DECLARE_PRIMITIVE(pooling)
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.
60 const primitive_id& id,
61 const primitive_id& input,
65 const tensor& input_offset = { 0,0,0,0 },
66 const padding& output_padding = padding()
68 : primitive_base(id, {input}, output_padding)
70 , mode(static_cast<pooling_mode>(mode))
71 , global_pooling(false)
72 , input_offset(input_offset)
75 , with_output_size(false)
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.
87 const primitive_id& id,
88 const primitive_id& input,
89 const primitive_id& argmax,
93 const tensor& input_offset = { 0,0,0,0 },
94 const padding& output_padding = padding()
96 : primitive_base(id, { input }, output_padding)
98 , mode(static_cast<pooling_mode>(mode))
99 , global_pooling(false)
100 , input_offset(input_offset)
103 , with_output_size(false)
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).
115 const primitive_id& id,
116 const primitive_id& input,
119 const tensor& stride,
120 const tensor& input_offset,
122 const padding& output_padding = padding()
124 : primitive_base(id, {input}, output_padding)
126 , mode(static_cast<pooling_mode>(mode))
127 , global_pooling(false)
128 , input_offset(input_offset)
131 , with_output_size(true)
132 , output_size(output_size)
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).
145 const primitive_id& id,
146 const primitive_id& input,
147 const primitive_id& argmax,
150 const tensor& stride,
151 const tensor& input_offset,
153 const padding& output_padding = padding()
155 : primitive_base(id, { input }, output_padding)
157 , mode(static_cast<pooling_mode>(mode))
158 , global_pooling(false)
159 , input_offset(input_offset)
162 , with_output_size(true)
163 , output_size(output_size)
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.
171 const primitive_id& id,
172 const primitive_id& input,
174 const padding& output_padding = padding()
176 : primitive_base(id, { input }, output_padding)
178 , mode(static_cast<pooling_mode>(mode))
179 , global_pooling(true)
180 , input_offset(0, 0, 0, 0)
183 , with_output_size(false)
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)
195 , with_output_size(dto->with_output_size != 0)
196 , output_size(dto->output_size)
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,
214 const tensor& stride,
215 const tensor& input_offset = { 0,0,0,0 },
216 const padding& output_padding = padding()
219 return pooling(id, input, mode, size, stride, input_offset, output_size, output_padding);
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,
239 const tensor& stride,
240 const tensor& input_offset = { 0,0,0,0 },
241 const padding& output_padding = padding()
244 return pooling(id, input, argmax, mode, size, stride, input_offset, output_size, output_padding);
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.
249 /// @brief Pooling mode.
251 /// @brief Global pooling (kernel size is equal to the spatial dimension of input tensor)
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.
255 /// @brief Defines shift in input buffer between adjacent calculations of output values.
257 /// @brief Pooling kernel 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).
265 std::vector<std::reference_wrapper<const primitive_id>> get_dependencies() const override
272 void update_dto(dto& dto) const override
274 dto.mode = static_cast<int32_t>(mode);
275 dto.argmax = argmax.c_str();
276 dto.input_offset = input_offset;
279 dto.with_output_size = with_output_size;
280 dto.output_size = output_size;
281 dto.global_pooling = global_pooling;