xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/CpuScaleKernel.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2016-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 #ifndef ARM_COMPUTE_CPU_SCALEKERNEL_H
25*c217d954SCole Faust #define ARM_COMPUTE_CPU_SCALEKERNEL_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/KernelDescriptors.h"
28*c217d954SCole Faust #include "src/core/common/Macros.h"
29*c217d954SCole Faust #include "src/cpu/ICpuKernel.h"
30*c217d954SCole Faust 
31*c217d954SCole Faust namespace arm_compute
32*c217d954SCole Faust {
33*c217d954SCole Faust namespace cpu
34*c217d954SCole Faust {
35*c217d954SCole Faust namespace kernels
36*c217d954SCole Faust {
37*c217d954SCole Faust /** Arm(R) Neon(TM) kernel to perform scaling on a tensor */
38*c217d954SCole Faust class CpuScaleKernel : public ICpuKernel<CpuScaleKernel>
39*c217d954SCole Faust {
40*c217d954SCole Faust private:
41*c217d954SCole Faust     /** Scale function to use for the particular function to use */
42*c217d954SCole Faust     using ScaleFunctionPtr = void (CpuScaleKernel::*)(const ITensor *, ITensor *, const ITensor *, const ITensor *, const ITensor *, const Window &window);
43*c217d954SCole Faust     using ScaleKernelPtr   = std::add_pointer<void(const ITensor *, ITensor *, const ITensor *, const ITensor *, const ITensor *,
44*c217d954SCole Faust                                                    InterpolationPolicy, BorderMode, PixelValue, float, bool, const Window &)>::type;
45*c217d954SCole Faust 
46*c217d954SCole Faust public:
47*c217d954SCole Faust     CpuScaleKernel() = default;
48*c217d954SCole Faust     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuScaleKernel);
49*c217d954SCole Faust     /** Initialise the kernel's inputs, output and interpolation policy
50*c217d954SCole Faust      *
51*c217d954SCole Faust      * @note dx, dy and offsets have the same dimensions (width and height) of the output tensor
52*c217d954SCole Faust      * @note Using @p policy Area only supports data layout NCHW and input data type U8.
53*c217d954SCole Faust      * @note Using S8 data type only supports NHWC, @p border_mode Replicate, and @p policy Bilinear
54*c217d954SCole Faust      *
55*c217d954SCole Faust      * @param[in]  src     Source tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/U8/S8/S16/F16/F32.
56*c217d954SCole Faust      * @param[in]  dx      Distance x tensor info. Pixel's distance between the X real coordinate and the smallest X following integer. Data type supported: F32
57*c217d954SCole Faust      * @param[in]  dy      Distance y tensor info. Pixel's distance between the Y real coordinate and the smallest Y following integer. Data type supported: F32
58*c217d954SCole Faust      * @param[in]  offsets Offset tensor info. Offset to access the pixel with NEAREST interpolation or the top-left pixel with BILINEAR interpolation in the input tensor. Data type supported: S32.
59*c217d954SCole Faust      * @param[out] dst     Destination tensor info. Data types supported: Same as @p input. All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane.
60*c217d954SCole Faust      * @param[in]  info    @ref ScaleKernelInfo to use for configuration
61*c217d954SCole Faust      */
62*c217d954SCole Faust     void configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets, ITensorInfo *dst,
63*c217d954SCole Faust                    const ScaleKernelInfo &info);
64*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
65*c217d954SCole Faust      *
66*c217d954SCole Faust      * Similar to CpuScaleKernel::configure()
67*c217d954SCole Faust      *
68*c217d954SCole Faust      * @return a status
69*c217d954SCole Faust      */
70*c217d954SCole Faust     static Status validate(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets, ITensorInfo *dst,
71*c217d954SCole Faust                            const ScaleKernelInfo &info);
72*c217d954SCole Faust 
73*c217d954SCole Faust     // Inherited methods overridden:
74*c217d954SCole Faust     void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override;
75*c217d954SCole Faust     const char *name() const override;
76*c217d954SCole Faust 
77*c217d954SCole Faust     struct ScaleKernel
78*c217d954SCole Faust     {
79*c217d954SCole Faust         const char                                 *name;
80*c217d954SCole Faust         const ScaleKernelDataTypeISASelectorDataPtr is_selected;
81*c217d954SCole Faust         ScaleKernelPtr                              ukernel;
82*c217d954SCole Faust     };
83*c217d954SCole Faust 
84*c217d954SCole Faust     static const std::vector<ScaleKernel> &get_available_kernels();
85*c217d954SCole Faust 
86*c217d954SCole Faust private:
87*c217d954SCole Faust #ifdef ENABLE_NCHW_KERNELS
88*c217d954SCole Faust     /** function to perform scale using area interpolation on the given window
89*c217d954SCole Faust      *
90*c217d954SCole Faust      *  @note Used only in case down-sampling.
91*c217d954SCole Faust      */
92*c217d954SCole Faust     void scale_area_nchw_u8(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window);
93*c217d954SCole Faust 
94*c217d954SCole Faust     /** function to perform scale using bilinear interpolation on the given window */
95*c217d954SCole Faust     template <typename T>
96*c217d954SCole Faust     void scale_bilinear_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window);
97*c217d954SCole Faust     /** function to perform scale using bilinear interpolation on the given window */
98*c217d954SCole Faust     template <typename T>
99*c217d954SCole Faust     void scale_bilinear_qasymm(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window);
100*c217d954SCole Faust 
101*c217d954SCole Faust     /** function to perform scale using nearest neighbour on the given window */
102*c217d954SCole Faust     template <typename T>
103*c217d954SCole Faust     void scale_nearest_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window);
104*c217d954SCole Faust #endif // ENABLE_NCHW_KERNELS
105*c217d954SCole Faust 
106*c217d954SCole Faust     ScaleFunctionPtr    _func{ nullptr };
107*c217d954SCole Faust     InterpolationPolicy _policy{};
108*c217d954SCole Faust     BorderMode          _border_mode{};
109*c217d954SCole Faust     PixelValue          _constant_border_value{};
110*c217d954SCole Faust     float               _sampling_offset{ 0 };
111*c217d954SCole Faust     bool                _align_corners{ false };
112*c217d954SCole Faust     DataLayout          _data_layout{ DataLayout::UNKNOWN };
113*c217d954SCole Faust     ScaleKernelPtr      _run_method{ nullptr };
114*c217d954SCole Faust     std::string         _name{};
115*c217d954SCole Faust };
116*c217d954SCole Faust } // namespace kernels
117*c217d954SCole Faust } // namespace cpu
118*c217d954SCole Faust } // namespace arm_compute
119*c217d954SCole Faust #endif /* ARM_COMPUTE_CPU_SCALEKERNEL_H */
120