xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/scale/sve/fp16.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2021-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 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
26 
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensorPack.h"
29 #include "arm_compute/core/Window.h"
30 #include "src/core/NEON/NEMath.h"
31 #include "src/core/NEON/wrapper/wrapper.h"
32 #include "src/core/helpers/ScaleHelpers.h"
33 #include "src/core/utils/ScaleUtils.h"
34 #include "support/Rounding.h"
35 
36 #include <arm_sve.h>
37 #include <cmath>
38 #include <cstddef>
39 
40 namespace arm_compute
41 {
42 namespace
43 {
fp16_sve_scale_nearest(const ITensor * src,ITensor * dst,const ITensor * offsets,float sampling_offset,bool align_corners,const Window & window)44 void fp16_sve_scale_nearest(const ITensor *src, ITensor *dst, const ITensor *offsets,
45                             float sampling_offset, bool align_corners, const Window &window)
46 {
47     const size_t in_stride_c  = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
48     const size_t in_stride_w  = src->info()->dimension(1) + src->info()->padding().top + src->info()->padding().bottom;
49     const size_t in_stride_wc = in_stride_w * in_stride_c;
50     const size_t in_dim_h     = src->info()->dimension(2);
51 
52     // Compute the ratio between source height and destination height
53     const auto hr             = scale_utils::calculate_resize_ratio(in_dim_h, dst->info()->dimension(2), align_corners);
54     const auto window_start_x = static_cast<int32_t>(window.x().start());
55     const auto window_end_x   = static_cast<int32_t>(window.x().end());
56 
57     Window win(window);
58     win.set(Window::DimX, Window::Dimension(0, 1, 1));
59     Iterator out(dst, win);
60 
61     const uint8_t     *in_ptr_start        = src->buffer() + src->info()->offset_first_element_in_bytes();
62     const unsigned int in_stride_bytes_hwc = src->info()->strides_in_bytes()[3];
63 
64     execute_window_loop(win, [&](const Coordinates & id)
65     {
66         const int32_t offset     = *reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z()))) * in_stride_c;
67         const auto    in_hi      = static_cast<int>(align_corners ? utils::rounding::round_half_away_from_zero((id.z() + sampling_offset) * hr) : std::floor((id.z() + sampling_offset) * hr));
68         const int     offset_row = in_hi * in_stride_wc;
69         const auto    in_ptr     = reinterpret_cast<const float16_t *>(in_ptr_start + in_stride_bytes_hwc * id[3]);
70         const auto    out_ptr    = reinterpret_cast<float16_t *>(out.ptr());
71 
72         // Compute S elements per iteration
73         int      x  = window_start_x;
74         svbool_t pg = svwhilelt_b16(x, window_end_x);
75         do
76         {
77             // Store results
78             svst1_f16(pg, out_ptr + x, svld1_f16(pg, in_ptr + offset + offset_row + x));
79 
80             x += svcntw();
81             pg = svwhilelt_b16(x, window_end_x);
82         }
83         while(svptest_any(svptrue_b16(), pg));
84     },
85     out);
86 }
87 }
88 namespace cpu
89 {
fp16_sve_scale(const ITensor * src,ITensor * dst,const ITensor * offsets,const ITensor * dx,const ITensor * dy,InterpolationPolicy policy,BorderMode border_mode,PixelValue constant_border_value,float sampling_offset,bool align_corners,const Window & window)90 void fp16_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy,
91                     InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset,
92                     bool align_corners, const Window &window)
93 {
94     ARM_COMPUTE_UNUSED(dx, dy, border_mode, constant_border_value);
95     if(policy == InterpolationPolicy::NEAREST_NEIGHBOR)
96     {
97         fp16_sve_scale_nearest(src, dst, offsets, sampling_offset, align_corners, window);
98     }
99     else
100     {
101         ARM_COMPUTE_ERROR("Not implemented");
102     }
103 }
104 } // namespace cpu
105 } // namespace arm_compute
106 #endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */