blob: 010e4ed57ac72f6a8691b96437cd2d0414650c2c [file] [log] [blame]
Michalis Spyrou17220e22018-09-12 13:35:38 +01001/*
Manuel Bottini0c58f992020-12-03 16:26:35 +00002 * Copyright (c) 2018-2020 Arm Limited.
Michalis Spyrou17220e22018-09-12 13:35:38 +01003 *
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#include "helpers_asymm.h"
25#include "warp_helpers_quantized.h"
26
27/** Transforms four 2D coordinates. This is used to map the output coordinates to the input coordinates.
28 *
29 * @param[in] coord 2D coordinates to transform.
30 * @param[in] scale input/output scale ratio
31 *
32 * @return a float8 containing 4 2D transformed values in the input image.
33 */
34inline const float8 transform_bilinear_quantized(const float2 coord, const float2 scale)
35{
36 const float4 in_x_coords = (float4)(coord.s0, 1 + coord.s0, 2 + coord.s0, 3 + coord.s0);
37#ifdef SAMPLING_POLICY_TOP_LEFT
38 const float4 new_x = in_x_coords * (float4)(scale.s0);
39 const float4 new_y = (float4)(coord.s1 * scale.s1);
40 return (float8)(new_x.s0, new_y.s0, new_x.s1, new_y.s1, new_x.s2, new_y.s2, new_x.s3, new_y.s3);
41#elif SAMPLING_POLICY_CENTER
42 const float4 new_x = (in_x_coords + ((float4)(0.5f))) * (float4)(scale.s0) - (float4)(0.5f);
43 const float4 new_y = (float4)((coord.s1 + 0.5f) * scale.s1 - 0.5f);
44 return (float8)(new_x.s0, new_y.s0, new_x.s1, new_y.s1, new_x.s2, new_y.s2, new_x.s3, new_y.s3);
45#else /* SAMPLING_POLICY */
46#error("Unsupported sampling policy");
47#endif /* SAMPLING_POLICY */
48}
49
50/** Performs an affine transformation on an image interpolating with the BILINEAR method.
51 *
52 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
53 * @note Scale value for QASYMM8 data type to used is passed as -DSCALE=<VALUE> e.g. -DSCALE=0.5
54 * @note Offset value for QASYMM8 data type to used is passed as -DOFFSET=<VALUE> e.g. -DOFFSET=1
55 *
56 * @param[in] in_ptr Pointer to the source image. Supported data types: QASYMM8.
57 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
58 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
59 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
60 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
61 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
62 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16. (Must be the same as the input)
63 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
64 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
65 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
66 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
67 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
68 * @param[in] input_width Input image width
69 * @param[in] input_height Input image height
70 * @param[in] scale_x The scale factor along x dimension
71 * @param[in] scale_y The scale factor along y dimension
72 */
73__kernel void scale_bilinear_quantized_nchw(
74 IMAGE_DECLARATION(in),
75 IMAGE_DECLARATION(out),
76 const float input_width,
77 const float input_height,
78 const float scale_x,
79 const float scale_y)
80{
81 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
82 Image out = CONVERT_TO_IMAGE_STRUCT(out);
83 const float2 r = (float2)(scale_x, scale_y);
84 const float8 tc = transform_bilinear_quantized(get_current_coords_quantized(), r);
85 vstore4(bilinear_interpolate_with_border_quantized(&in, tc, input_width, input_height, BORDER_SIZE, SCALE, OFFSET), 0, (__global DATA_TYPE *)out.ptr);
86}
87
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +000088#if defined(DEPTH_OUT)
Michalis Spyrou17220e22018-09-12 13:35:38 +010089/** Performs scale on an image interpolating with the BILINEAR method. (NHWC)
90 *
91 * @note Sampling policy to be used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
92 * @note Scale value for QASYMM8 data type to used is passed as -DSCALE=<VALUE> e.g. -DSCALE=0.5
93 * @note Offset value for QASYMM8 data type to used is passed as -DOFFSET=<VALUE> e.g. -DOFFSET=1
94 * @note If border mode replicate is used, is should be passed as -DBORDER_MODE_REPLICATE
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +000095 * @note Output tensor's depth should be given as a preprocessor argument using -DDEPTH_OUT=size. e.g. -DDEPTH=16
Manuel Bottini0c58f992020-12-03 16:26:35 +000096 * @note The value to be used at the edges of the images shoud be given as a preprocessor argument using -DCONSTANT_VALUE=value.
Michalis Spyrou17220e22018-09-12 13:35:38 +010097 *
98 * @param[in] in_ptr Pointer to the source image. Supported data types: QASYMM8.
99 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
100 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
101 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
102 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
103 * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes)
104 * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
105 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
106 * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr
107 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
108 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
109 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
110 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
111 * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes)
112 * @param[in] out_step_z dst_stride_y * number of elements along Z processed per workitem(in bytes)
113 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
114 * @param[in] input_width Input image width
115 * @param[in] input_height Input image height
116 * @param[in] scale_x The scale factor along x dimension
117 * @param[in] scale_y The scale factor along y dimension
Manuel Bottini0c58f992020-12-03 16:26:35 +0000118 * @param[in] constant_border_value Constant border value to use
Michalis Spyrou17220e22018-09-12 13:35:38 +0100119 */
120__kernel void scale_bilinear_quantized_nhwc(
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000121 TENSOR4D_DECLARATION(in),
122 TENSOR4D_DECLARATION(out),
Michalis Spyrou17220e22018-09-12 13:35:38 +0100123 const float input_width,
124 const float input_height,
125 const float scale_x,
126 const float scale_y)
127{
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000128 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0);
129 Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100130
131#ifdef SAMPLING_POLICY_TOP_LEFT
132 const float new_x = get_global_id(1) * scale_x;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000133 const float new_y = (get_global_id(2) % DEPTH_OUT) * scale_y;
Michalis Spyrou17220e22018-09-12 13:35:38 +0100134#elif SAMPLING_POLICY_CENTER
135 const float new_x = (get_global_id(1) + 0.5f) * scale_x - 0.5f;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000136 const float new_y = ((get_global_id(2) % DEPTH_OUT) + 0.5f) * scale_y - 0.5f;
Michalis Spyrou17220e22018-09-12 13:35:38 +0100137#else /* SAMPLING_POLICY */
138#error("Unsupported sampling policy");
139#endif /* SAMPLING_POLICY */
140
Manuel Bottini0c58f992020-12-03 16:26:35 +0000141 const float new_xf = floor(new_x);
142 const float new_yf = floor(new_y);
143 const float clamped_x = clamp(new_xf, 0.0f, input_width - 1);
144 const float clamped_x1 = clamp(new_xf + 1, 0.0f, input_width - 1);
145 const float clamped_y = clamp(new_yf, 0.0f, input_height - 1);
146 const float clamped_y1 = clamp(new_yf + 1, 0.0f, input_height - 1);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100147
148#ifndef BORDER_MODE_REPLICATE
Manuel Bottini0c58f992020-12-03 16:26:35 +0000149 const bool check_x = (0.f <= new_xf && new_xf < input_width);
150 const bool check_x1 = (-1.f <= new_xf && new_xf < input_width - 1);
151 const bool check_y = (0.f <= new_yf && new_yf < input_height);
152 const bool check_y1 = (-1.f <= new_yf && new_yf < input_height - 1);
153 const int ins_0 = select((int)(CONSTANT_VALUE), (int)(*((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y),
154 (get_global_id(2) / DEPTH_OUT)))),
155 check_x && check_y);
156 const int ins_1 = select((int)(CONSTANT_VALUE), (int)(*((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y),
157 (get_global_id(2) / DEPTH_OUT)))),
158 check_x1 && check_y);
159 const int ins_2 = select((int)(CONSTANT_VALUE), (int)(*((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y1),
160 (get_global_id(2) / DEPTH_OUT)))),
161 check_x && check_y1);
162 const int ins_3 = select((int)(CONSTANT_VALUE), (int)(*((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y1),
163 (get_global_id(2) / DEPTH_OUT)))),
164 check_x1 && check_y1);
165 int4 ins = (int4)(ins_0, ins_1, ins_2, ins_3);
166#else /* BORDER_MODE_REPLICATE */
167 int4 ins = (int4)(*((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))),
168 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))),
169 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))),
170 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))));
Michalis Spyrou17220e22018-09-12 13:35:38 +0100171#endif /* BORDER_MODE_REPLICATE */
172
Michalis Spyrou17220e22018-09-12 13:35:38 +0100173 const float a = new_x - new_xf;
174 const float b = 1.f - a;
175 const float a1 = new_y - new_yf;
176 const float b1 = 1.f - a1;
177 const float4 insf32 = convert_float4(ins - (int4)OFFSET) * (float4)SCALE;
178
179 const float fr = ((insf32.s0 * b * b1) + (insf32.s1 * a * b1) + (insf32.s2 * b * a1) + (insf32.s3 * a * a1));
180
Manuel Bottini8481d832019-12-10 15:28:40 +0000181 DATA_TYPE res = CONVERT_SAT(convert_int_sat_rtp(fr / SCALE) + OFFSET, DATA_TYPE);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100182
183 *((__global DATA_TYPE *)out.ptr) = res;
184}
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000185#endif /* defined(DEPTH_OUT) */