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