blob: 80ea54012f8fad3a20519471e722fb8fa62328ab [file] [log] [blame]
Michele Di Giorgio56dd7262017-07-27 09:53:49 +01001/*
2 * Copyright (c) 2017 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#include "helpers.h"
25
26/** This performs the quantization of floating point inputs to 8-bit unsigned integers.
27 *
28 * @param[in] input_ptr Pointer to the source image. Supported data types: F32
29 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
30 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
31 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
32 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
33 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
34 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
35 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image
36 * @param[out] output_ptr Pointer to the destination image. Supported data types: U8
37 * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes)
38 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
39 * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes)
40 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
41 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
42 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
43 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
44 * @param[in] min_max_ptr Pointer to the min/max vector. Minimum value in position 0, maximum value in position 1. Supported data types: F32.
45 * @param[in] min_max_stride_x Stride of the min/max vector in X dimension (in bytes)
46 * @param[in] min_max_step_x min_max_stride_x * number of elements along X processed per workitem(in bytes)
47 * @param[in] min_max_offset_first_element_in_bytes The offset of the first element in the min/max vector
48 */
49__kernel void quantization_layer(
50 TENSOR3D_DECLARATION(input),
51 TENSOR3D_DECLARATION(output),
52 VECTOR_DECLARATION(min_max))
53{
54 // Get pixels pointer
55 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
56 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
57
58 // min_max_value.s0 = min, min_max_value.s1 = max
59 const float2 min_max_value = vload2(0, (__global float *)(min_max_ptr + min_max_offset_first_element_in_bytes));
60
61 const float4 vmin = (float4)min_max_value.s0;
62 const float4 vrange = (float4)(min_max_value.s1 - min_max_value.s0);
63
64 // Load data
65 float4 data = vload4(0, (__global float *)input.ptr);
66
67 // Map float values to range [0.0, 1.0]
68 data = (data - vmin) / vrange;
69
70 // Quantize and saturate
71 uchar4 res = convert_uchar4_sat(data * 256.0f);
72
73 // Store result
74 vstore4(res, 0, (__global uchar *)output.ptr);
75}