blob: 008cf651e136d378ad8aa76df3c10150b818f8bf [file] [log] [blame]
Pablo Marquez Tello68b6dce2023-10-05 11:28:15 +01001/*
2 * Copyright (c) 2023 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#ifndef ACL_SRC_CPU_KERNELS_POOL2D_NEON_IMPL_H
26#define ACL_SRC_CPU_KERNELS_POOL2D_NEON_IMPL_H
27
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/utils/misc/Traits.h"
32
33#include "src/core/helpers/WindowHelpers.h"
34#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
35#include "src/cpu/kernels/pool2d/neon/list.h"
36
37#include <limits>
38
39#ifdef ENABLE_NCHW_KERNELS
40namespace arm_compute
41{
42namespace cpu
43{
44
45namespace
46{
47template <typename T>
48auto read_2_boundary_aware_as_f32(int srcw, int srch, int pad_l, int pad_t, int x, int y, const T *ptr, T fval)
49{
50 T vec[2];
51 const bool row_in_bounds((y >= pad_t) && (y < (srch + pad_t)));
52 for (int i = 0; i < 2; i++)
53 {
54 if (row_in_bounds && (x + i >= pad_l) && (x + i < (srcw + pad_l)))
55 {
56 vec[i] = *(ptr + i);
57 }
58 else
59 {
60 vec[i] = fval;
61 }
62 }
63 float32_t vec_f32[2] = {vec[0], vec[1]};
64 return wrapper::vload(vec_f32);
65}
66} // namespace
67
68template <typename T>
69void pooling2_nchw_maxpool_indices(const ITensor *src,
70 ITensor *dst0,
71 ITensor *dst1,
72 PoolingLayerInfo &pool_info,
73 const Window &window_src,
74 const Window &window)
75{
76 Iterator in(src, window_src);
77 Iterator out(dst0, window);
78 Iterator indices(dst1, window);
79 const int pool_pad_top = pool_info.pad_stride_info.pad_top();
80 const int pool_pad_left = pool_info.pad_stride_info.pad_left();
81 int pool_stride_x = 0;
82 int pool_stride_y = 0;
83 std::tie(pool_stride_x, pool_stride_y) = pool_info.pad_stride_info.stride();
84 const int src_w = src->info()->dimension(0);
85 const int src_h = src->info()->dimension(1);
86 const uint8_t *const src_top_ptr =
87 src->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top)));
88 const uint8_t *const src_bottom_ptr =
89 src->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1));
90 const int pad_left = src->info()->padding().left;
91 const int pad_right = src->info()->padding().right;
92 const int in_stride_y = static_cast<int>(src->info()->strides_in_bytes().y());
93 const T float_min = get_initial_min<T>(pool_info.use_inf_as_limit);
94 const T fill_value = (pool_info.pool_type == PoolingType::MAX) ? float_min : 0.f;
95
96 execute_window_loop(
97 window,
98 [&](const Coordinates &id)
99 {
100 const auto x_val = id.x() * pool_stride_x;
101 const auto y_val_0 = id.y() * pool_stride_y;
102 const auto y_val_1 = (id.y() * pool_stride_y) + 1;
103 auto top_data =
104 read_2_boundary_aware_as_f32(src_w, src_h, pool_pad_left, pool_pad_top, x_val, y_val_0,
105 reinterpret_cast<const T *>(src_top_ptr + in.offset()), fill_value);
106 auto bottom_data =
107 read_2_boundary_aware_as_f32(src_w, src_h, pool_pad_left, pool_pad_top, x_val, y_val_1,
108 reinterpret_cast<const T *>(src_bottom_ptr + in.offset()), fill_value);
109
110 // Calculate max data, compare top first, then bottom, to make sue the first max is recorded.
111 const float32x2_t max_data_top = vpmax_f32(top_data, top_data);
112 const float32x2_t max_data_bottom = vpmax_f32(bottom_data, bottom_data);
113 const float32x2_t max_data = vmax_f32(max_data_top, max_data_bottom);
114 *(reinterpret_cast<T *>(out.ptr())) = static_cast<T>(vget_lane_f32(max_data, 0));
115
116 // Calculate max data indice, which will be used in max unpool.
117 const uint32_t offset_base =
118 offset_no_padding<T>(in.offset(), id, *src->info(), pool_stride_x, pool_stride_y, DataLayout::NCHW);
119 const uint32_t offset_top = (uint32_t)(offset_base / sizeof(T));
120 const uint32_t offset_bottom = offset_top + in_stride_y / sizeof(T) - pad_right - pad_left;
121 const uint32x2_t voffset_top = {offset_top, offset_top + 1u};
122 const uint32x2_t voffset_bottom = {offset_bottom, offset_bottom + 1u};
123 const uint32x2_t tmp_indices_top =
124 vbsl_u32(vcge_f32(top_data, vrev64_f32(top_data)), voffset_top, vrev64_u32(voffset_top));
125 const uint32x2_t tmp_indices_bottom =
126 vbsl_u32(vcge_f32(bottom_data, vrev64_f32(bottom_data)), voffset_bottom, vrev64_u32(voffset_bottom));
127 *(reinterpret_cast<int *>(indices.ptr())) = vget_lane_u32(
128 vbsl_u32(vcge_f32(max_data_top, max_data_bottom), tmp_indices_top, tmp_indices_bottom), 0);
129 },
130 in, out, indices);
131}
132
133} // namespace cpu
134} // namespace arm_compute
135
136#endif // ENABLE_NCHW_KERNELS
137
138#endif // ACL_SRC_CPU_KERNELS_POOL2D_NEON_IMPL_H