arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / cl_kernels / l2_normalize.cl
1 /*
2  * Copyright (c) 2016, 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 kernel performs reduction given an operation.
27  *
28  * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
29  * @note The data size must be passed at compile time using -DDATA_SIZE e.g. -DDATA_SIZE=32
30  *
31  * @param[in]  src_ptr                           Pointer to the source tensor. Supported data types: QS8/F16/F32
32  * @param[in]  src_stride_x                      Stride of the source tensor in X dimension (in bytes)
33  * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
34  * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source tensor
35  * @param[in]  sum_ptr                           Pointer to the source tensor. Supported data types: QS8/F16/F32
36  * @param[in]  sum_stride_x                      Stride of the source tensor in X dimension (in bytes)
37  * @param[in]  sum_step_x                        sum_stride_x * number of elements along X processed per workitem(in bytes)
38  * @param[in]  sum_offset_first_element_in_bytes The offset of the first element in the source tensor
39  * @param[out] dst_ptr                           Pointer to the destination tensor. Supported data types: same as @p src_ptr
40  * @param[in]  dst_stride_x                      Stride of the destination tensor in X dimension (in bytes)
41  * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
42  * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
43  * @param[in]  epsilon                           Epsilon value
44  */
45 __kernel void l2_normalize(
46     VECTOR_DECLARATION(src),
47     VECTOR_DECLARATION(sum),
48     VECTOR_DECLARATION(dst),
49     DATA_TYPE epsilon)
50 {
51     Vector src = CONVERT_TO_VECTOR_STRUCT(src);
52     Vector sum = CONVERT_TO_VECTOR_STRUCT(sum);
53     Vector dst = CONVERT_TO_VECTOR_STRUCT(dst);
54
55     VEC_DATA_TYPE(DATA_TYPE, 16)
56     in = vload16(0, (__global DATA_TYPE *)src.ptr);
57     VEC_DATA_TYPE(DATA_TYPE, 16)
58     normalize_value = (VEC_DATA_TYPE(DATA_TYPE, 16))native_rsqrt(fmax(((__global DATA_TYPE *)sum.ptr)[0], epsilon));
59
60     vstore16(in * normalize_value, 0, (__global DATA_TYPE *)dst.ptr);
61 }