Compute Library
 22.11
Pooling3dLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "Pooling3dLayer.h"
28 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 namespace reference
36 {
38 
39 template <typename T>
41 {
42  TensorShape pooled_shape = compute_pool3d_shape(src.shape(), pool3d_info);
43  SimpleTensor<T> dst{ pooled_shape, src.data_type(), 1 };
44 
45  if(indices != nullptr)
46  {
47  *indices = SimpleTensor<uint32_t> { pooled_shape, DataType::U32, 1 };
48  }
49 
50  const int idx_channel = 0;
51  const int idx_width = 1;
52  const int idx_height = 2;
53  const int idx_depth = 3;
54  const int idx_batch = 4;
55 
56  const int pool_size_width = pool3d_info.is_global_pooling ? src.shape()[idx_width] : pool3d_info.pool_size.width;
57  const int pool_size_height = pool3d_info.is_global_pooling ? src.shape()[idx_height] : pool3d_info.pool_size.height;
58  const int pool_size_depth = pool3d_info.is_global_pooling ? src.shape()[idx_depth] : pool3d_info.pool_size.depth;
59 
60  const int pool_stride_width = static_cast<int>(pool3d_info.stride.width);
61  const int pool_stride_height = static_cast<int>(pool3d_info.stride.height);
62  const int pool_stride_depth = static_cast<int>(pool3d_info.stride.depth);
63 
64  const int pad_left = static_cast<int>(pool3d_info.padding.left);
65  const int pad_top = static_cast<int>(pool3d_info.padding.top);
66  const int pad_front = static_cast<int>(pool3d_info.padding.front);
67 
68  const int pad_right = static_cast<int>(pool3d_info.padding.right);
69  const int pad_bottom = static_cast<int>(pool3d_info.padding.bottom);
70  const int pad_back = static_cast<int>(pool3d_info.padding.back);
71 
72  const int num_channels = static_cast<int>(src.shape()[idx_channel]);
73  const int num_batches = static_cast<int>(src.shape()[idx_batch]);
74 
75  ARM_COMPUTE_ERROR_ON(num_channels != static_cast<int>(dst.shape()[idx_channel]));
76  ARM_COMPUTE_ERROR_ON(num_batches != static_cast<int>(dst.shape()[idx_batch]));
77 
78  const int w_src = static_cast<int>(src.shape()[idx_width]);
79  const int h_src = static_cast<int>(src.shape()[idx_height]);
80  const int d_src = static_cast<int>(src.shape()[idx_depth]);
81  const int w_dst = static_cast<int>(dst.shape()[idx_width]);
82  const int h_dst = static_cast<int>(dst.shape()[idx_height]);
83  const int d_dst = static_cast<int>(dst.shape()[idx_depth]);
84 
85  const bool exclude_padding = pool3d_info.exclude_padding;
86 
87  const int height_stride_src = num_channels * w_src;
88  const int depth_stride_src = height_stride_src * h_src;
89  const int batch_stride_src = depth_stride_src * d_src;
90  const int height_stride_dst = num_channels * w_dst;
91  const int depth_stride_dst = height_stride_dst * h_dst;
92  const int batch_stride_dst = depth_stride_dst * d_dst;
93 
94  for(int b = 0; b < num_batches; ++b)
95  {
96  const int batch_offset_dst = b * batch_stride_dst;
97  const int batch_offset_src = b * batch_stride_src;
98  for(int c = 0; c < num_channels; ++c)
99  {
100  for(int d = 0; d < d_dst; ++d)
101  {
102  const int depth_offset_dst = d * depth_stride_dst;
103  for(int h = 0; h < h_dst; ++h)
104  {
105  const int height_offset_dst = h * height_stride_dst;
106  for(int w = 0; w < w_dst; ++w)
107  {
108  int wstart = w * pool_stride_width - pad_left;
109  int hstart = h * pool_stride_height - pad_top;
110  int dstart = d * pool_stride_depth - pad_front;
111  int wend = std::min(wstart + pool_size_width, w_src + pad_right);
112  int hend = std::min(hstart + pool_size_height, h_src + pad_bottom);
113  int dend = std::min(dstart + pool_size_depth, d_src + pad_back);
114 
115  // this may not be equal to pool_w * pool_h * pool_d because of
116  // DimensionRoundingType choice (CEIL)
117  int pool_size = (dend - dstart) * (hend - hstart) * (wend - wstart);
118 
119  // limit [start, end) to [0, w_src)
120  wstart = std::max(wstart, 0);
121  hstart = std::max(hstart, 0);
122  dstart = std::max(dstart, 0);
123  wend = std::min(wend, w_src);
124  hend = std::min(hend, h_src);
125  dend = std::min(dend, d_src);
126 
127  auto max_val = -std::numeric_limits<T>::infinity();
128  int max_index{ 0 };
129  T avg_val = static_cast<T>(0.f);
130  T l2_val = static_cast<T>(0.f);
131 
132  if(exclude_padding)
133  {
134  pool_size = (dend - dstart) * (hend - hstart) * (wend - wstart);
135  }
136 
137  for(int z = dstart; z < dend; ++z)
138  {
139  const int depth_offset_src = z * depth_stride_src;
140  for(int y = hstart; y < hend; ++y)
141  {
142  const int height_offset_src = y * height_stride_src;
143  for(int x = wstart; x < wend; ++x)
144  {
145  const auto val = static_cast<T>(
146  src[batch_offset_src + depth_offset_src + height_offset_src + x * num_channels + c]);
147  if(val > max_val)
148  {
149  max_val = val;
150  max_index = coord2index(src.shape(), Coordinates(c, x, y, z, 0));
151  }
152 
153  avg_val += val;
154  l2_val += val * val;
155  }
156  }
157  }
158 
159  avg_val /= pool_size;
160  l2_val = static_cast<T>(std::sqrt(l2_val / pool_size));
161 
162  int dst_index = batch_offset_dst + depth_offset_dst + height_offset_dst + w * num_channels + c;
163  switch(pool3d_info.pool_type)
164  {
165  case PoolingType::MAX:
166  dst[dst_index] = static_cast<T>(max_val);
167  break;
168  case PoolingType::AVG:
169  dst[dst_index] = static_cast<T>(avg_val);
170  break;
171  case PoolingType::L2:
172  dst[dst_index] = static_cast<T>(l2_val);
173  break;
174  default:
175  ARM_COMPUTE_ERROR("Pooling Type should be either MAX, AVG or L2");
176  }
177 
178  if(indices != nullptr)
179  {
180  (*indices)[dst_index] = max_index;
181  }
182  }
183  }
184  }
185  }
186  }
187 
188  return dst;
189 }
190 
191 template SimpleTensor<float> pooling_3d_layer(const SimpleTensor<float> &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices);
192 template SimpleTensor<half> pooling_3d_layer(const SimpleTensor<half> &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices);
193 
194 template <typename T>
196 {
197  ARM_COMPUTE_UNUSED(output_qinfo);
198  return pooling_3d_layer_internal<T>(src, pool3d_info, indices);
199 }
200 
201 template <>
203 {
205  SimpleTensor<float> dst_tmp = pooling_3d_layer_internal<float>(src_tmp, pool3d_info, indices);
206  return convert_to_asymmetric<int8_t>(dst_tmp, output_qinfo);
207 }
208 
209 template <>
211 {
213  SimpleTensor<float> dst_tmp = pooling_3d_layer_internal<float>(src_tmp, pool3d_info, indices);
214  return convert_to_asymmetric<uint8_t>(dst_tmp, output_qinfo);
215 }
216 
217 } // namespace reference
218 } // namespace validation
219 } // namespace test
220 } // namespace arm_compute
SimpleTensor< uint8_t > pooling_3d_layer< uint8_t >(const SimpleTensor< uint8_t > &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor< uint32_t > *indices)
SimpleTensor< float > w
Definition: DFT.cpp:156
Shape of a tensor.
Definition: TensorShape.h:39
SimpleTensor< float > b
Definition: DFT.cpp:157
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
template SimpleTensor< float > pooling_3d_layer(const SimpleTensor< float > &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor< uint32_t > *indices)
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
SimpleTensor< float > convert_from_asymmetric(const SimpleTensor< uint8_t > &src)
Definition: Helpers.cpp:36
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
TensorShape compute_pool3d_shape(const TensorShape &src, Pooling3dLayerInfo pool3d_info)
Calculate the output pool3d shape of a tensor.
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:388
Quantization information.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
size_t front
Padding across the depth dimenstion on the front, in elements.
Definition: Types.h:820
1 channel, 1 U32 per channel
size_t height
Height of the 3D shape or object.
Definition: Size3D.h:93
Coordinates of an item.
Definition: Coordinates.h:37
Pooling Layer Information struct.
Definition: Types.h:1295
size_t top
Padding across the height dimenstion on the top, in elements.
Definition: Types.h:818
size_t left
Padding across the width dimenstion on the left, in elements.
Definition: Types.h:816
size_t width
Width of the 3D shape or object.
Definition: Size3D.h:92
size_t back
Padding across the depth dimenstion on the back, in elements.
Definition: Types.h:821
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
size_t right
Padding across the width dimenstion on the right, in elements.
Definition: Types.h:817
size_t depth
Depth of the 3D shape or object.
Definition: Size3D.h:94
SimpleTensor< int8_t > pooling_3d_layer< int8_t >(const SimpleTensor< int8_t > &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor< uint32_t > *indices)
SimpleTensor< T > pooling_3d_layer_internal(const SimpleTensor< T > &src, const Pooling3dLayerInfo &pool3d_info, SimpleTensor< uint32_t > *indices)
size_t bottom
Padding across the height dimenstion on the bottom, in elements.
Definition: Types.h:819