blob: 2aa7f185c6e5c855ae10e90916b9471f2f2df866 [file] [log] [blame]
Michalis Spyrou17220e22018-09-12 13:35:38 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018 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
Michalis Spyrou17220e22018-09-12 13:35:38 +010096 *
97 * @param[in] in_ptr Pointer to the source image. Supported data types: QASYMM8.
98 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
99 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
100 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
101 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
102 * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes)
103 * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
104 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
105 * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr
106 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
107 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
108 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
109 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
110 * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes)
111 * @param[in] out_step_z dst_stride_y * number of elements along Z processed per workitem(in bytes)
112 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
113 * @param[in] input_width Input image width
114 * @param[in] input_height Input image height
115 * @param[in] scale_x The scale factor along x dimension
116 * @param[in] scale_y The scale factor along y dimension
117 */
118__kernel void scale_bilinear_quantized_nhwc(
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000119 TENSOR4D_DECLARATION(in),
120 TENSOR4D_DECLARATION(out),
Michalis Spyrou17220e22018-09-12 13:35:38 +0100121 const float input_width,
122 const float input_height,
123 const float scale_x,
124 const float scale_y)
125{
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000126 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0);
127 Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100128
129#ifdef SAMPLING_POLICY_TOP_LEFT
130 const float new_x = get_global_id(1) * scale_x;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000131 const float new_y = (get_global_id(2) % DEPTH_OUT) * scale_y;
Michalis Spyrou17220e22018-09-12 13:35:38 +0100132#elif SAMPLING_POLICY_CENTER
133 const float new_x = (get_global_id(1) + 0.5f) * scale_x - 0.5f;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000134 const float new_y = ((get_global_id(2) % DEPTH_OUT) + 0.5f) * scale_y - 0.5f;
Michalis Spyrou17220e22018-09-12 13:35:38 +0100135#else /* SAMPLING_POLICY */
136#error("Unsupported sampling policy");
137#endif /* SAMPLING_POLICY */
138
139 const float new_xf = floor(new_x);
140 const float new_yf = floor(new_y);
141 float clamped_x = clamp(new_xf, 0.0f, input_width - 1);
142 float clamped_x1 = clamp(new_xf + 1, 0.0f, input_width - 1);
143 float clamped_x_ = clamped_x;
144 float clamped_x1_ = clamped_x1;
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);
147
148#ifndef BORDER_MODE_REPLICATE
149 clamped_x1 = select(clamped_x1, 0.0f - BORDER_SIZE, new_yf + 1 < 0.f || new_yf + 1 > input_height - 1 || new_xf + 1 < 0.f || new_xf + 1 > input_width - 1);
150 clamped_x_ = select(clamped_x_, 0.0f - BORDER_SIZE, new_yf + 1 > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1);
151 clamped_x = select(clamped_x, 0.0f - BORDER_SIZE, new_yf < 0.f || new_yf > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1);
152 clamped_x1_ = select(clamped_x1_, 0.0f - BORDER_SIZE, new_xf + 1 < 0.f || new_xf + 1 > input_width - 1 || new_yf < 0.f || new_yf > input_height - 1);
153#endif /* BORDER_MODE_REPLICATE */
154
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000155 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))),
156 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1_), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))),
157 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x_), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))),
158 *((__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 +0100159
160 const float a = new_x - new_xf;
161 const float b = 1.f - a;
162 const float a1 = new_y - new_yf;
163 const float b1 = 1.f - a1;
164 const float4 insf32 = convert_float4(ins - (int4)OFFSET) * (float4)SCALE;
165
166 const float fr = ((insf32.s0 * b * b1) + (insf32.s1 * a * b1) + (insf32.s2 * b * a1) + (insf32.s3 * a * a1));
167
Manuel Bottini8481d832019-12-10 15:28:40 +0000168 DATA_TYPE res = CONVERT_SAT(convert_int_sat_rtp(fr / SCALE) + OFFSET, DATA_TYPE);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100169
170 *((__global DATA_TYPE *)out.ptr) = res;
171}
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000172#endif /* defined(DEPTH_OUT) */