From aaa27189e0e75c3ebad57854ac8901d0140677ac Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Wed, 21 Nov 2018 16:32:15 +0000 Subject: COMPMID-1734: Implement CLSelect Change-Id: I49b2e8b4200c9ed654736d9451e4ab9c073b4b10 --- arm_compute/core/CL/CLKernels.h | 1 + arm_compute/core/CL/kernels/CLSelectKernel.h | 85 +++++++++ arm_compute/runtime/CL/CLFunctions.h | 1 + arm_compute/runtime/CL/functions/CLSelect.h | 60 ++++++ src/core/CL/CLKernelLibrary.cpp | 7 + src/core/CL/cl_kernels/select.cl | 216 ++++++++++++++++++++++ src/core/CL/kernels/CLSelectKernel.cpp | 203 +++++++++++++++++++++ src/runtime/CL/functions/CLSelect.cpp | 45 +++++ tests/validation/CL/Select.cpp | 262 +++++++++++++++++++++++++++ tests/validation/fixtures/SelectFixture.h | 148 +++++++++++++++ tests/validation/reference/Select.cpp | 84 +++++++++ tests/validation/reference/Select.h | 43 +++++ 12 files changed, 1155 insertions(+) create mode 100644 arm_compute/core/CL/kernels/CLSelectKernel.h create mode 100644 arm_compute/runtime/CL/functions/CLSelect.h create mode 100644 src/core/CL/cl_kernels/select.cl create mode 100644 src/core/CL/kernels/CLSelectKernel.cpp create mode 100644 src/runtime/CL/functions/CLSelect.cpp create mode 100644 tests/validation/CL/Select.cpp create mode 100644 tests/validation/fixtures/SelectFixture.h create mode 100644 tests/validation/reference/Select.cpp create mode 100644 tests/validation/reference/Select.h diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h index cc76231345..1457d7b3ac 100644 --- a/arm_compute/core/CL/CLKernels.h +++ b/arm_compute/core/CL/CLKernels.h @@ -116,6 +116,7 @@ #include "arm_compute/core/CL/kernels/CLReshapeLayerKernel.h" #include "arm_compute/core/CL/kernels/CLScaleKernel.h" #include "arm_compute/core/CL/kernels/CLScharr3x3Kernel.h" +#include "arm_compute/core/CL/kernels/CLSelectKernel.h" #include "arm_compute/core/CL/kernels/CLSobel3x3Kernel.h" #include "arm_compute/core/CL/kernels/CLSobel5x5Kernel.h" #include "arm_compute/core/CL/kernels/CLSobel7x7Kernel.h" diff --git a/arm_compute/core/CL/kernels/CLSelectKernel.h b/arm_compute/core/CL/kernels/CLSelectKernel.h new file mode 100644 index 0000000000..031166c37c --- /dev/null +++ b/arm_compute/core/CL/kernels/CLSelectKernel.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_CLSELECTKERNEL_H__ +#define __ARM_COMPUTE_CLSELECTKERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +// Forward declarations +class ICLTensor; + +/** OpenCL interface for executing the select kernel + * + * Select is computed by: + * @f[ output(i) = condition(i) ? x(i) : y(i) @f] + **/ +class CLSelectKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLSelectKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLSelectKernel(const CLSelectKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLSelectKernel &operator=(const CLSelectKernel &) = delete; + /** Allow instances of this class to be moved */ + CLSelectKernel(CLSelectKernel &&) = default; + /** Allow instances of this class to be moved */ + CLSelectKernel &operator=(CLSelectKernel &&) = default; + /** Default destructor */ + ~CLSelectKernel() = default; + /** Initialise the kernel's inputs and output. + * + * @param[in] c Condition input tensor. Data types supported: U8. + * @param[in] x First input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[out] y Second input tensor. Data types supported: Same as @p x + * @param[in] output Output tensor. Data types supported: Same as @p x. + */ + void configure(const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output); + /** Static function to check if given info will lead to a valid configuration of @ref CLSelectKernel + * + * @param[in] c Condition input tensor. Data types supported: U8. + * @param[in] x First input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[in] y Second input tensor. Data types supported: Same as @p x + * @param[in] output Output tensor. Data types supported: Same as @p x. + * + * @return a status + */ + static Status validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output); + + // Inherited methods overridden: + void run(const Window &window, cl::CommandQueue &queue) override; + +private: + const ICLTensor *_c; /**< Condition tensor */ + const ICLTensor *_x; /**< Source tensor 1 */ + const ICLTensor *_y; /**< Source tensor 2 */ + ICLTensor *_output; /**< Destination tensor */ + bool _has_same_rank; /**< Flag that indicates if condition tensor and other inputs have the same rank */ +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_CLWHEREKERNEL_H__ */ diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h index 2a8294f28b..b4c5c2eb80 100644 --- a/arm_compute/runtime/CL/CLFunctions.h +++ b/arm_compute/runtime/CL/CLFunctions.h @@ -117,6 +117,7 @@ #include "arm_compute/runtime/CL/functions/CLReshapeLayer.h" #include "arm_compute/runtime/CL/functions/CLScale.h" #include "arm_compute/runtime/CL/functions/CLScharr3x3.h" +#include "arm_compute/runtime/CL/functions/CLSelect.h" #include "arm_compute/runtime/CL/functions/CLSlice.h" #include "arm_compute/runtime/CL/functions/CLSobel3x3.h" #include "arm_compute/runtime/CL/functions/CLSobel5x5.h" diff --git a/arm_compute/runtime/CL/functions/CLSelect.h b/arm_compute/runtime/CL/functions/CLSelect.h new file mode 100644 index 0000000000..dab62a7ec1 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLSelect.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_CLSELECT_H__ +#define __ARM_COMPUTE_CLSELECT_H__ + +#include "arm_compute/runtime/CL/ICLSimpleFunction.h" + +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +// Forward declarations +class ICLTensor; + +/** Basic function to run @ref CLSelect */ +class CLSelect : public ICLSimpleFunction +{ +public: + /** Initialise the kernel's inputs and output. + * + * @param[in] c Condition input tensor. Data types supported: U8. + * @param[in] x First input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[in] y Second input tensor. Data types supported: Same as @p x + * @param[out] output Output tensor. Data types supported: Same as @p x. + */ + void configure(const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output); + /** Static function to check if given info will lead to a valid configuration of @ref CLSelect + * + * @param[in] c Condition input tensor. Data types supported: U8. + * @param[in] x First input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[in] y Second input tensor. Data types supported: Same as @p x + * @param[in] output Output tensor. Data types supported: Same as @p x. + * + * @return a status + */ + static Status validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output); +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_CLSELECT_H__ */ diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 3a002e808d..12944061a9 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -386,6 +386,9 @@ const std::map CLKernelLibrary::_kernel_program_map = { "scale_bilinear_quantized_nchw", "scale_quantized.cl" }, { "scale_bilinear_quantized_nhwc", "scale_quantized.cl" }, { "scharr3x3", "scharr_filter.cl" }, + { "select_same_rank", "select.cl" }, + { "select_different_rank_2", "select.cl" }, + { "select_different_rank_n", "select.cl" }, { "sobel3x3", "sobel_filter.cl" }, { "sobel_separable5x1", "sobel_filter.cl" }, { "sobel_separable1x5", "sobel_filter.cl" }, @@ -812,6 +815,10 @@ const std::map CLKernelLibrary::_program_source_map = { "scharr_filter.cl", #include "./cl_kernels/scharr_filter.clembed" + }, + { + "select.cl", +#include "./cl_kernels/select.clembed" }, { "sobel_filter.cl", diff --git a/src/core/CL/cl_kernels/select.cl b/src/core/CL/cl_kernels/select.cl new file mode 100644 index 0000000000..d783ae212e --- /dev/null +++ b/src/core/CL/cl_kernels/select.cl @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "helpers.h" + +#if defined(DATA_TYPE) && defined(SELECT_DATA_TYPE) && defined(VEC_SIZE) +/** This function perform a select operation between two tensors when condition tensor has the same rank. + * + * @attention The data_type need to be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=uchar + * @attention The select operation data_type need to be passed at compile time using -DSELECT_DATA_TYPE: e.g. -DSELECT_DATA_TYPE=uchar + * @attention Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 + * + * @param[in] c_ptr Pointer to the source tensor. Supported data types: U8 + * @param[in] c_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] c_step_x c_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] c_stride_y Stride of the source tensor in Y dimension (in bytes) + * @param[in] c_step_y c_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] c_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] c_step_z c_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] c_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[in] x_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] x_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] x_step_x x_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] x_stride_y Stride of the source tensor in Y dimension (in bytes) + * @param[in] x_step_y x_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] x_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] x_step_z x_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] x_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[in] y_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] y_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] y_step_x y_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] y_stride_y Stride of the source tensor in Y dimension (in bytes) + * @param[in] y_step_y y_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] y_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] y_step_z y_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] y_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[out] out_ptr Pointer to the destination tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) + * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes) + * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] out_step_z out_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor + */ +__kernel void select_same_rank( + TENSOR3D_DECLARATION(c), + TENSOR3D_DECLARATION(x), + TENSOR3D_DECLARATION(y), + TENSOR3D_DECLARATION(out)) +{ + // Get pixels pointer + Tensor3D c_t = CONVERT_TO_TENSOR3D_STRUCT(c); + Tensor3D x_t = CONVERT_TO_TENSOR3D_STRUCT(x); + Tensor3D y_t = CONVERT_TO_TENSOR3D_STRUCT(y); + Tensor3D out_t = CONVERT_TO_TENSOR3D_STRUCT(out); + + // Load values + VEC_DATA_TYPE(SELECT_DATA_TYPE, VEC_SIZE) + in_c = CONVERT((VLOAD(VEC_SIZE)(0, (__global uchar *)c_t.ptr)), VEC_DATA_TYPE(SELECT_DATA_TYPE, VEC_SIZE)); + VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + in_x = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)x_t.ptr); + VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + in_y = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)y_t.ptr); + + // Calculate and store result + VSTORE(VEC_SIZE) + (select(in_y, in_x, in_c > (SELECT_DATA_TYPE)0), 0, (__global DATA_TYPE *)out_t.ptr); +} + +/** This function perform a select operation between two tensors when condition tensor has a different rank. + * + * @attention The data_type need to be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=uchar + * @attention The select operation data_type need to be passed at compile time using -DSELECT_DATA_TYPE: e.g. -DSELECT_DATA_TYPE=uchar + * @attention Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 + * + * @param[in] c_ptr Pointer to the source tensor. Supported data types: U8 + * @param[in] c_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] c_step_x c_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] c_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[in] x_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] x_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] x_step_x x_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] x_stride_y Stride of the source tensor in Y dimension (in bytes) + * @param[in] x_step_y x_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] x_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] x_step_z x_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] x_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[in] y_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] y_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] y_step_x y_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] y_stride_y Stride of the source tensor in Y dimension (in bytes) + * @param[in] y_step_y y_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] y_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] y_step_z y_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] y_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[out] out_ptr Pointer to the destination tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) + * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes) + * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] out_step_z out_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor + */ +__kernel void select_different_rank_2( + VECTOR_DECLARATION(c), + TENSOR3D_DECLARATION(x), + TENSOR3D_DECLARATION(y), + TENSOR3D_DECLARATION(out)) +{ + const int c_idx = get_global_id(1); + + // Get pixels pointer + Vector c_t = CONVERT_TO_VECTOR_STRUCT_NO_STEP(c); + Tensor3D x_t = CONVERT_TO_TENSOR3D_STRUCT(x); + Tensor3D y_t = CONVERT_TO_TENSOR3D_STRUCT(y); + Tensor3D out_t = CONVERT_TO_TENSOR3D_STRUCT(out); + + // Load values + VEC_DATA_TYPE(SELECT_DATA_TYPE, VEC_SIZE) + in_c = *((__global uchar *)(c_t.ptr + c_idx * c_t.stride_x)); + VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + in_x = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)x_t.ptr); + VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + in_y = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)y_t.ptr); + + // Calculate and store result + VSTORE(VEC_SIZE) + (select(in_y, in_x, in_c > (SELECT_DATA_TYPE)0), 0, (__global DATA_TYPE *)out_t.ptr); +} +#endif /* defined(DATA_TYPE) && defined(SELECT_DATA_TYPE) && defined(VEC_SIZE) */ + +#if defined(DATA_TYPE) && defined(SELECT_DATA_TYPE) && defined(VEC_SIZE) && defined(DEPTH_SIZE) +/** This function perform a select operation between two tensors when condition tensor has a different rank. + * + * @attention The data_type need to be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=uchar + * @attention The select operation data_type need to be passed at compile time using -DSELECT_DATA_TYPE: e.g. -DSELECT_DATA_TYPE=uchar + * @attention Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 + * + * @param[in] c_ptr Pointer to the source tensor. Supported data types: U8 + * @param[in] c_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] c_step_x c_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] c_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[in] x_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] x_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] x_step_x x_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] x_stride_y Stride of the source tensor in Y dimension (in bytes) + * @param[in] x_step_y x_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] x_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] x_step_z x_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] x_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[in] y_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] y_stride_x Stride of the source tensor in X dimension (in bytes) + * @param[in] y_step_x y_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] y_stride_y Stride of the source tensor in Y dimension (in bytes) + * @param[in] y_step_y y_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] y_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] y_step_z y_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] y_offset_first_element_in_bytes The offset of the first element in the source tensor + * @param[out] out_ptr Pointer to the destination tensor. Supported data types: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32 + * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) + * @param[in] out_step_x out_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes) + * @param[in] out_step_y out_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] out_step_z out_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor + */ +__kernel void select_different_rank_n( + VECTOR_DECLARATION(c), + TENSOR3D_DECLARATION(x), + TENSOR3D_DECLARATION(y), + TENSOR3D_DECLARATION(out)) +{ + const int c_idx = get_global_id(2) / DEPTH_SIZE; + + // Get pixels pointer + Vector c_t = CONVERT_TO_VECTOR_STRUCT_NO_STEP(c); + Tensor3D x_t = CONVERT_TO_TENSOR3D_STRUCT(x); + Tensor3D y_t = CONVERT_TO_TENSOR3D_STRUCT(y); + Tensor3D out_t = CONVERT_TO_TENSOR3D_STRUCT(out); + + // Load values + VEC_DATA_TYPE(SELECT_DATA_TYPE, VEC_SIZE) + in_c = *((__global uchar *)(c_t.ptr + c_idx * c_t.stride_x)); + VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + in_x = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)x_t.ptr); + VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + in_y = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)y_t.ptr); + + // Calculate and store result + VSTORE(VEC_SIZE) + (select(in_y, in_x, in_c > (SELECT_DATA_TYPE)0), 0, (__global DATA_TYPE *)out_t.ptr); +} +#endif /* defined(DATA_TYPE) && defined(SELECT_DATA_TYPE) && defined(VEC_SIZE) && defined(DEPTH_SIZE) */ \ No newline at end of file diff --git a/src/core/CL/kernels/CLSelectKernel.cpp b/src/core/CL/kernels/CLSelectKernel.cpp new file mode 100644 index 0000000000..c9e5da0670 --- /dev/null +++ b/src/core/CL/kernels/CLSelectKernel.cpp @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/CL/kernels/CLSelectKernel.h" + +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/CLValidate.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Window.h" + +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +namespace +{ +Status validate_arguments(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) +{ + ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(x); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(x, + 1, + DataType::U8, DataType::S8, DataType::QASYMM8, + DataType::U16, DataType::S16, + DataType::U32, DataType::S32, + DataType::F16, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, y); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, y); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::U8); + + const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions()); + ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape())); + ARM_COMPUTE_RETURN_ERROR_ON(!is_same_rank && ((c->tensor_shape().num_dimensions() > 1) || (c->tensor_shape().x() != x->tensor_shape()[x->tensor_shape().num_dimensions() - 1]))); + + if(output != nullptr && output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, output); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, output); + } + + return Status{}; +} + +std::pair validate_and_configure_window(ITensorInfo *c, ITensorInfo *x, ITensorInfo *y, ITensorInfo *output) +{ + if(output != nullptr) + { + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output, *x->clone()); + } + + const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions()); + + const unsigned int num_elems_processed_per_iteration = 16 / x->element_size(); + + // Configure kernel window + Window win = calculate_max_window(*x, Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal x_access(x, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal y_access(y, 0, num_elems_processed_per_iteration); + bool window_changed = update_window_and_padding(win, x_access, y_access); + + // Update window for condition + if(is_same_rank) + { + AccessWindowHorizontal c_access(c, 0, num_elems_processed_per_iteration); + window_changed = window_changed || update_window_and_padding(win, c_access); + } + + // Update window for output + if(output != nullptr) + { + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + window_changed = window_changed || update_window_and_padding(win, output_access); + output_access.set_valid_region(win, x->valid_region()); + } + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_pair(err, win); +} +} // namespace + +CLSelectKernel::CLSelectKernel() + : _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false) +{ +} +void CLSelectKernel::configure(const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(c->info(), x->info(), y->info(), output->info())); + + _c = c; + _x = x; + _y = y; + _output = output; + _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions()); + + const unsigned int num_elems_processed_per_iteration = 16 / x->info()->element_size(); + + // Set build options + CLBuildOptions build_opts; + build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(x->info()->data_type())); + build_opts.add_option("-DSELECT_DATA_TYPE=" + get_cl_select_type_from_data_type(x->info()->data_type())); + build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); + + // Create kernel + std::string kernel_name = "select"; + if(_has_same_rank) + { + kernel_name += "_same_rank"; + } + else + { + const bool is_input_rank_greater_than_two = x->info()->tensor_shape().num_dimensions() > 2; + if(is_input_rank_greater_than_two) + { + const size_t width = x->info()->tensor_shape().x(); + const size_t height = x->info()->tensor_shape().y(); + const size_t outer_size = x->info()->tensor_shape()[x->info()->tensor_shape().num_dimensions() - 1]; + const size_t depth_size = x->info()->tensor_shape().total_size() / (width * height * outer_size); + build_opts.add_option("-DDEPTH_SIZE=" + support::cpp11::to_string(depth_size)); + } + kernel_name += "_different_rank"; + kernel_name += is_input_rank_greater_than_two ? "_n" : "_2"; + } + _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); + + // Configure kernel window + auto win_config = validate_and_configure_window(c->info(), x->info(), y->info(), output->info()); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + ICLKernel::configure_internal(win_config.second); + + _config_id = "select_"; + _config_id += string_from_data_type(x->info()->data_type()); + _config_id += "_"; + _config_id += support::cpp11::to_string(x->info()->dimension(0)); + _config_id += "_"; + _config_id += support::cpp11::to_string(x->info()->dimension(1)); + _config_id += "_"; + _config_id += support::cpp11::to_string(x->info()->dimension(2)); +} + +Status CLSelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(c, x, y, output)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(c->clone().get(), x->clone().get(), y->clone().get(), output->clone().get()).first); + return Status{}; +} + +void CLSelectKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); + + Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); + Window slice = collapsed.first_slice_window_3D(); + + if(!_has_same_rank) + { + Window vector_slice = window.first_slice_window_1D(); + vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); + unsigned int idx = 0; + add_1D_tensor_argument(idx, _c, vector_slice); + } + + do + { + unsigned int idx = _has_same_rank ? 0 : num_arguments_per_1D_tensor(); + if(_has_same_rank) + { + add_3D_tensor_argument(idx, _c, slice); + } + add_3D_tensor_argument(idx, _x, slice); + add_3D_tensor_argument(idx, _y, slice); + add_3D_tensor_argument(idx, _output, slice); + + enqueue(queue, *this, slice, lws_hint()); + } + while(collapsed.slide_window_slice_3D(slice)); +} +} // namespace arm_compute diff --git a/src/runtime/CL/functions/CLSelect.cpp b/src/runtime/CL/functions/CLSelect.cpp new file mode 100644 index 0000000000..90c368e9b8 --- /dev/null +++ b/src/runtime/CL/functions/CLSelect.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/functions/CLSelect.h" + +#include "arm_compute/core/CL/kernels/CLSelectKernel.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/CLScheduler.h" + +using namespace arm_compute; + +namespace arm_compute +{ +void CLSelect::configure(const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output) +{ + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(c, x, y, output); + _kernel = std::move(k); +} + +Status CLSelect::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) +{ + return CLSelectKernel::validate(c, x, y, output); +} +} // namespace arm_compute diff --git a/tests/validation/CL/Select.cpp b/tests/validation/CL/Select.cpp new file mode 100644 index 0000000000..e3159caa93 --- /dev/null +++ b/tests/validation/CL/Select.cpp @@ -0,0 +1,262 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLSelect.h" +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/ShapeDatasets.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/SelectFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +auto configuration_dataset = combine(framework::dataset::concat(datasets::SmallShapes(), datasets::LargeShapes()), + framework::dataset::make("has_same_rank", { false, true })); +auto run_small_dataset = combine(datasets::SmallShapes(), framework::dataset::make("has_same_rank", { false, true })); +auto run_large_dataset = combine(datasets::LargeShapes(), framework::dataset::make("has_same_rank", { false, true })); + +} // namespace +TEST_SUITE(CL) +TEST_SUITE(Select) + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("CInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S8), // Invalid condition datatype + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), // Invalid output datatype + TensorInfo(TensorShape(13U), 1, DataType::U8), // Invalid c shape + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), // Mismatching shapes + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(2U), 1, DataType::U8), + }), + framework::dataset::make("XInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U, 10U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), + })), + framework::dataset::make("YInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), + })), + framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S8), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), + })), + framework::dataset::make("Expected", { false, false, false, false, true, true})), + c_info, x_info, y_info, output_info, expected) +{ + Status s = CLSelect::validate(&c_info.clone()->set_is_resizable(false), + &x_info.clone()->set_is_resizable(false), + &y_info.clone()->set_is_resizable(false), + &output_info.clone()->set_is_resizable(false)); + ARM_COMPUTE_EXPECT(bool(s) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +template +using CLSelectFixture = SelectValidationFixture; + +TEST_SUITE(Float) +TEST_SUITE(F16) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, configuration_dataset, + shape, same_rank) +{ + const DataType dt = DataType::F16; + + // Create tensors + CLTensor ref_c = create_tensor(detail::select_condition_shape(shape, same_rank), DataType::U8); + CLTensor ref_x = create_tensor(shape, dt); + CLTensor ref_y = create_tensor(shape, dt); + CLTensor dst = create_tensor(shape, dt); + + // Create and Configure function + CLSelect select; + select.configure(&ref_c, &ref_x, &ref_y, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const int step = 16 / arm_compute::data_size_from_type(dt); + const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding(); + if(same_rank) + { + validate(ref_c.info()->padding(), padding); + } + validate(ref_x.info()->padding(), padding); + validate(ref_y.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +FIXTURE_DATA_TEST_CASE(RunSmall, + CLSelectFixture, + framework::DatasetMode::PRECOMMIT, + combine(run_small_dataset, framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, + CLSelectFixture, + framework::DatasetMode::NIGHTLY, + combine(run_large_dataset, framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() // F16 + +TEST_SUITE(FP32) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, configuration_dataset, + shape, same_rank) +{ + const DataType dt = DataType::F32; + + // Create tensors + CLTensor ref_c = create_tensor(detail::select_condition_shape(shape, same_rank), DataType::U8); + CLTensor ref_x = create_tensor(shape, dt); + CLTensor ref_y = create_tensor(shape, dt); + CLTensor dst = create_tensor(shape, dt); + + // Create and Configure function + CLSelect select; + select.configure(&ref_c, &ref_x, &ref_y, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const int step = 16 / arm_compute::data_size_from_type(dt); + const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding(); + if(same_rank) + { + validate(ref_c.info()->padding(), padding); + } + validate(ref_x.info()->padding(), padding); + validate(ref_y.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +FIXTURE_DATA_TEST_CASE(RunSmall, + CLSelectFixture, + framework::DatasetMode::PRECOMMIT, + combine(run_small_dataset, framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, + CLSelectFixture, + framework::DatasetMode::NIGHTLY, + combine(run_large_dataset, framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() // F32 +TEST_SUITE_END() // Float + +TEST_SUITE(Quantized) +TEST_SUITE(QASYMM8) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, configuration_dataset, + shape, same_rank) +{ + const DataType dt = DataType::QASYMM8; + + // Create tensors + CLTensor ref_c = create_tensor(detail::select_condition_shape(shape, same_rank), DataType::U8); + CLTensor ref_x = create_tensor(shape, dt); + CLTensor ref_y = create_tensor(shape, dt); + CLTensor dst = create_tensor(shape, dt); + + // Create and Configure function + CLSelect select; + select.configure(&ref_c, &ref_x, &ref_y, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const int step = 16 / arm_compute::data_size_from_type(dt); + const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding(); + if(same_rank) + { + validate(ref_c.info()->padding(), padding); + } + validate(ref_x.info()->padding(), padding); + validate(ref_y.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +FIXTURE_DATA_TEST_CASE(RunSmall, + CLSelectFixture, + framework::DatasetMode::PRECOMMIT, + combine(run_small_dataset, framework::dataset::make("DataType", DataType::QASYMM8))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, + CLSelectFixture, + framework::DatasetMode::NIGHTLY, + combine(run_large_dataset, framework::dataset::make("DataType", DataType::QASYMM8))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() // QASYMM8 +TEST_SUITE_END() // Quantized + +TEST_SUITE_END() // Select +TEST_SUITE_END() // CL +} // namespace validation +} // namespace test +} // namespace arm_compute \ No newline at end of file diff --git a/tests/validation/fixtures/SelectFixture.h b/tests/validation/fixtures/SelectFixture.h new file mode 100644 index 0000000000..2ddc97a6cf --- /dev/null +++ b/tests/validation/fixtures/SelectFixture.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_SELECT_FIXTURE +#define ARM_COMPUTE_TEST_SELECT_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/reference/Select.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace detail +{ +/** Get the expected shape of the condition tensor + * + * @param shape Shape of the other input tensor to select + * @param has_same_same_rank Boolean that flags if the condition has the same rank with the other tensors + * + * @return the expected condition shape + */ +inline TensorShape select_condition_shape(TensorShape shape, bool has_same_same_rank) +{ + TensorShape condition_shape = shape; + if(!has_same_same_rank) + { + condition_shape = TensorShape(shape[shape.num_dimensions() - 1]); + } + return condition_shape; +} +} // namespace detail + +template +class SelectValidationFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape shape, bool has_same_same_rank, DataType data_type) + { + TensorShape condition_shape = detail::select_condition_shape(shape, has_same_same_rank); + + _target = compute_target(shape, condition_shape, data_type); + _reference = compute_reference(shape, condition_shape, data_type); + } + +protected: + template + void fill(U &&tensor, int i) + { + library->fill_tensor_uniform(tensor, i); + } + template + void fill_bool(U &&tensor, int i) + { + ARM_COMPUTE_ERROR_ON(tensor.data_type() != DataType::U8); + library->fill_tensor_uniform(tensor, i, static_cast(0), static_cast(1)); + } + + TensorType compute_target(const TensorShape &shape, const TensorShape &condition_shape, DataType data_type) + { + // Create tensors + TensorType c_t = create_tensor(condition_shape, DataType::U8); + TensorType x_t = create_tensor(shape, data_type); + TensorType y_t = create_tensor(shape, data_type); + TensorType dst_t = create_tensor(shape, data_type); + + // Create and configure function + FunctionType select; + select.configure(&c_t, &x_t, &y_t, &dst_t); + + ARM_COMPUTE_EXPECT(c_t.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(x_t.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(y_t.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst_t.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + c_t.allocator()->allocate(); + x_t.allocator()->allocate(); + y_t.allocator()->allocate(); + dst_t.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!c_t.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!x_t.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!y_t.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst_t.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill_bool(AccessorType(c_t), 0); + fill(AccessorType(x_t), 1); + fill(AccessorType(y_t), 2); + + // Compute function + select.run(); + + return dst_t; + } + + SimpleTensor compute_reference(const TensorShape &shape, const TensorShape &condition_shape, DataType data_type) + { + // Create reference + SimpleTensor ref_c{ condition_shape, DataType::U8 }; + SimpleTensor ref_x{ shape, data_type }; + SimpleTensor ref_y{ shape, data_type }; + + // Fill reference + fill_bool(ref_c, 0); + fill(ref_x, 1); + fill(ref_y, 2); + + return reference::select(ref_c, ref_x, ref_y); + } + + TensorType _target{}; + SimpleTensor _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_SELECT_FIXTURE */ diff --git a/tests/validation/reference/Select.cpp b/tests/validation/reference/Select.cpp new file mode 100644 index 0000000000..91b6b66c42 --- /dev/null +++ b/tests/validation/reference/Select.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "Select.h" + +#include "arm_compute/core/Types.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor select(const SimpleTensor &c, const SimpleTensor &x, const SimpleTensor &y) +{ + // Check if condition has the same rank as c + const bool is_same_rank = (c.shape().num_dimensions() == x.shape().num_dimensions()); + + // Check shapes + ARM_COMPUTE_ERROR_ON(x.shape() != y.shape()); + ARM_COMPUTE_ERROR_ON(is_same_rank && (x.shape() != c.shape())); + ARM_COMPUTE_ERROR_ON(!is_same_rank && (c.shape().num_dimensions() > 1) && (c.shape().x() != x.shape()[x.shape().num_dimensions() - 1])); + + // Create reference + SimpleTensor dst{ x.shape(), x.data_type(), 1 }; + + // Run select core + if(is_same_rank) + { + for(int i = 0; i < x.num_elements(); ++i) + { + dst[i] = c[i] > 0 ? x[i] : y[i]; + } + } + else + { + T *output_ptr = dst.data(); + + const int outer_size = c.num_elements(); + const int inner_size = x.num_elements() / outer_size; + size_t offset = 0; + + for(int i = 0; i < outer_size; ++i) + { + const T *input_ptr = c[i] > 0 ? x.data() : y.data(); + memcpy(output_ptr + offset, input_ptr + offset, inner_size * sizeof(T)); + offset += inner_size; + } + } + + return dst; +} + +template SimpleTensor select(const SimpleTensor &c, const SimpleTensor &x, const SimpleTensor &y); +template SimpleTensor select(const SimpleTensor &c, const SimpleTensor &x, const SimpleTensor &y); +template SimpleTensor select(const SimpleTensor &c, const SimpleTensor &x, const SimpleTensor &y); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/Select.h b/tests/validation/reference/Select.h new file mode 100644 index 0000000000..394f36748d --- /dev/null +++ b/tests/validation/reference/Select.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_SELECT_H__ +#define __ARM_COMPUTE_TEST_SELECT_H__ + +#include "tests/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor select(const SimpleTensor &c, const SimpleTensor &x, const SimpleTensor &y); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_SELECT_H__ */ -- cgit v1.2.1