From 75eea338eb232ebdafa2fb84d22e711b5f964785 Mon Sep 17 00:00:00 2001 From: Sang-Hoon Park Date: Fri, 13 Nov 2020 13:44:13 +0000 Subject: COMPMID-3961: Add Logical OR/AND/NOT operator on CL Change-Id: I612aeed6affa17624fb9044964dd59c41a5c9888 Signed-off-by: Sang-Hoon Park Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/4448 Reviewed-by: Pablo Marquez Tello Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins --- Android.bp | 3 + arm_compute/core/Types.h | 15 +- arm_compute/runtime/CL/CLFunctions.h | 3 + arm_compute/runtime/CL/functions/CLLogicalAnd.h | 121 ++++++++++++++ arm_compute/runtime/CL/functions/CLLogicalNot.h | 112 +++++++++++++ arm_compute/runtime/CL/functions/CLLogicalOr.h | 121 ++++++++++++++ src/core/CL/CLKernelLibrary.cpp | 2 + src/core/CL/cl_kernels/elementwise_operation.cl | 5 + src/core/CL/cl_kernels/elementwise_unary.cl | 6 +- .../CL/kernels/CLElementWiseUnaryLayerKernel.cpp | 27 +++- .../CL/kernels/CLElementwiseOperationKernel.cpp | 69 ++++++++ src/core/CL/kernels/CLElementwiseOperationKernel.h | 44 ++++++ src/runtime/CL/functions/CLLogicalAnd.cpp | 97 ++++++++++++ src/runtime/CL/functions/CLLogicalNot.cpp | 95 +++++++++++ src/runtime/CL/functions/CLLogicalOr.cpp | 97 ++++++++++++ tests/validation/CL/Logical.cpp | 176 +++++++++++++++++++++ tests/validation/NEON/Logical.cpp | 4 +- tests/validation/fixtures/LogicalFixture.h | 37 ++--- tests/validation/reference/Logical.cpp | 31 ++-- tests/validation/reference/Logical.h | 9 +- 20 files changed, 1017 insertions(+), 57 deletions(-) create mode 100644 arm_compute/runtime/CL/functions/CLLogicalAnd.h create mode 100644 arm_compute/runtime/CL/functions/CLLogicalNot.h create mode 100644 arm_compute/runtime/CL/functions/CLLogicalOr.h create mode 100644 src/runtime/CL/functions/CLLogicalAnd.cpp create mode 100644 src/runtime/CL/functions/CLLogicalNot.cpp create mode 100644 src/runtime/CL/functions/CLLogicalOr.cpp create mode 100644 tests/validation/CL/Logical.cpp diff --git a/Android.bp b/Android.bp index 64eedc9229..a4ca5c79e8 100644 --- a/Android.bp +++ b/Android.bp @@ -531,6 +531,9 @@ cc_library_static { "src/runtime/CL/functions/CLLaplacianPyramid.cpp", "src/runtime/CL/functions/CLLaplacianReconstruct.cpp", "src/runtime/CL/functions/CLLocallyConnectedLayer.cpp", + "src/runtime/CL/functions/CLLogicalAnd.cpp", + "src/runtime/CL/functions/CLLogicalNot.cpp", + "src/runtime/CL/functions/CLLogicalOr.cpp", "src/runtime/CL/functions/CLMagnitude.cpp", "src/runtime/CL/functions/CLMaxUnpoolingLayer.cpp", "src/runtime/CL/functions/CLMeanStdDev.cpp", diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index 2e639c4be4..39cc29b0da 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -546,13 +546,14 @@ enum class ArithmeticOperation /** Available element wise unary operations */ enum class ElementWiseUnary { - RSQRT, /**< Reverse square root */ - EXP, /**< Exponential */ - NEG, /**< Negate */ - LOG, /**< Natural Logarithm */ - ABS, /**< Absolute value */ - SIN, /**< Sine */ - ROUND, /**< Round */ + RSQRT, /**< Reverse square root */ + EXP, /**< Exponential */ + NEG, /**< Negate */ + LOG, /**< Natural Logarithm */ + ABS, /**< Absolute value */ + SIN, /**< Sine */ + ROUND, /**< Round */ + LOGICAL_NOT, /**< Logical Not */ }; /** The normalization type used for the normalization layer */ diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h index f909cc30c5..4e32831bc6 100644 --- a/arm_compute/runtime/CL/CLFunctions.h +++ b/arm_compute/runtime/CL/CLFunctions.h @@ -99,6 +99,9 @@ #include "arm_compute/runtime/CL/functions/CLLaplacianPyramid.h" #include "arm_compute/runtime/CL/functions/CLLaplacianReconstruct.h" #include "arm_compute/runtime/CL/functions/CLLocallyConnectedLayer.h" +#include "arm_compute/runtime/CL/functions/CLLogicalAnd.h" +#include "arm_compute/runtime/CL/functions/CLLogicalNot.h" +#include "arm_compute/runtime/CL/functions/CLLogicalOr.h" #include "arm_compute/runtime/CL/functions/CLMagnitude.h" #include "arm_compute/runtime/CL/functions/CLMaxUnpoolingLayer.h" #include "arm_compute/runtime/CL/functions/CLMeanStdDev.h" diff --git a/arm_compute/runtime/CL/functions/CLLogicalAnd.h b/arm_compute/runtime/CL/functions/CLLogicalAnd.h new file mode 100644 index 0000000000..1a6ccf35a5 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLLogicalAnd.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2020 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_CLLOGICALAND_H +#define ARM_COMPUTE_CLLOGICALAND_H + +#include "arm_compute/core/Error.h" +#include "arm_compute/runtime/CL/ICLOperator.h" +#include "arm_compute/runtime/IFunction.h" + +namespace arm_compute +{ +class CLCompileContext; +class ICLTensor; +class ITensorInfo; + +namespace experimental +{ +class CLLogicalAnd : public ICLOperator +{ +public: + /** Default Constructor */ + CLLogicalAnd() = default; + /** Initialise the kernel's inputs, output and conversion policy. + * + * @param[in] compile_context The compile context to be used. + * @param[in, out] input1 First tensor input. Data types supported: U8. + * The input tensor is [in, out] because its TensorInfo might be modified inside the kernel in case of broadcasting of dimension 0. + * @param[in, out] input2 Second tensor input. Data types supported: U8. + * The input tensor is [in, out] because its TensorInfo might be modified inside the kernel in case of broadcasting of dimension 0. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output); + /** Static function to check if given info will lead to a valid configuration of @ref CLLogicalBinaryKernel + * + * @param[in] input1 First tensor input info. Data types supported: U8. + * @param[in] input2 Second tensor input info. Data types supported: U8. + * @param[in] output Output tensor info. Data types supported: U8. + * + * @return a status + */ + static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); + // Inherited methods overridden: + void run(ITensorPack &tensors) override; +}; +} // namespace experimental + +/** Basic function to run @ref CLLogicalBinaryKernel. + * + * @note The tensor data type for the inputs must be U8. + * @note The function performs a logical AND operation using the two input tensors. + */ +class CLLogicalAnd : public IFunction +{ +public: + /** Default Constructor */ + CLLogicalAnd(); + /** Default Destructor */ + ~CLLogicalAnd(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLLogicalAnd(const CLLogicalAnd &) = delete; + /** Default move constructor */ + CLLogicalAnd(CLLogicalAnd &&); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLLogicalAnd &operator=(const CLLogicalAnd &) = delete; + /** Default move assignment operator */ + CLLogicalAnd &operator=(CLLogicalAnd &&); + /** Initialize the function + * + * @param[in] input1 Input tensor. Data types supported: U8. + * @param[in] input2 Input tensor. Data types supported: U8. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output); + /** Initialize the function + * + * @param[in] compile_context The compile context to be used. + * @param[in] input1 Input tensor. Data types supported: U8. + * @param[in] input2 Input tensor. Data types supported: U8. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(const CLCompileContext &compile_context, ICLTensor *input1, ICLTensor *input2, ICLTensor *output); + /** Static function to check if given info will lead to a valid configuration + * + * @param[in] input1 First tensor input info. Data types supported: U8. + * @param[in] input2 Second tensor input info. Data types supported: U8. + * @param[in] output Output tensor info. Data types supported: U8. + * + * @return a status + */ + static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); + + // Inherited methods overridden: + void run() override; + +private: + struct Impl; + std::unique_ptr _impl; +}; +} // namespace arm_compute +#endif /* ARM_COMPUTE_CLLOGICALAND_H */ diff --git a/arm_compute/runtime/CL/functions/CLLogicalNot.h b/arm_compute/runtime/CL/functions/CLLogicalNot.h new file mode 100644 index 0000000000..4fdf39ed70 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLLogicalNot.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2020 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_CLLOGICALNOT_H +#define ARM_COMPUTE_CLLOGICALNOT_H + +#include "arm_compute/core/Error.h" +#include "arm_compute/runtime/CL/ICLOperator.h" +#include "arm_compute/runtime/IFunction.h" + +namespace arm_compute +{ +class CLCompileContext; +class ICLTensor; +class ITensorInfo; + +namespace experimental +{ +class CLLogicalNot : public ICLOperator +{ +public: + /** Default Constructor */ + CLLogicalNot() = default; + /** Initialise the kernel's inputs, output and conversion policy. + * + * @param[in] compile_context The compile context to be used. + * @param[in, out] input Tensor input. Data types supported: U8. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output); + /** Static function to check if given info will lead to a valid configuration of @ref CLElementWiseUnaryLayerKernel + * + * @param[in] input Tensor input info. Data types supported: U8. + * @param[in] output Output tensor info. Data types supported: U8. + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output); + // Inherited methods overridden: + void run(ITensorPack &tensors) override; +}; +} // namespace experimental + +/** Basic function to do logical NOT operation + * + * @note The tensor data type for the inputs must be U8. + * @note The function performs a logical NOT operation on input tensor. + */ +class CLLogicalNot : public IFunction +{ +public: + /** Default Constructor */ + CLLogicalNot(); + /** Default Destructor */ + ~CLLogicalNot(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLLogicalNot(const CLLogicalNot &) = delete; + /** Default move constructor */ + CLLogicalNot(CLLogicalNot &&); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLLogicalNot &operator=(const CLLogicalNot &) = delete; + /** Default move assignment operator */ + CLLogicalNot &operator=(CLLogicalNot &&); + /** Initialize the function + * + * @param[in] input Input tensor. Data types supported: U8. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(const ICLTensor *input, ICLTensor *output); + /** Initialize the function + * + * @param[in] compile_context The compile context to be used. + * @param[in] input Input tensor. Data types supported: U8. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output); + /** Static function to check if given info will lead to a valid configuration + * + * @param[in] input Tensor input info. Data types supported: U8. + * @param[in] output Output tensor info. Data types supported: U8. + * + * @return a status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *output); + void run() override; + +private: + struct Impl; + std::unique_ptr _impl; +}; +} // namespace arm_compute +#endif /* ARM_COMPUTE_CLLOGICALNOT_H */ diff --git a/arm_compute/runtime/CL/functions/CLLogicalOr.h b/arm_compute/runtime/CL/functions/CLLogicalOr.h new file mode 100644 index 0000000000..a50551e1dd --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLLogicalOr.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2020 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_CLLOGICALOR_H +#define ARM_COMPUTE_CLLOGICALOR_H + +#include "arm_compute/core/Error.h" +#include "arm_compute/runtime/CL/ICLOperator.h" +#include "arm_compute/runtime/IFunction.h" + +namespace arm_compute +{ +class CLCompileContext; +class ICLTensor; +class ITensorInfo; + +namespace experimental +{ +class CLLogicalOr : public ICLOperator +{ +public: + /** Default Constructor */ + CLLogicalOr() = default; + /** Initialise the kernel's inputs, output and conversion policy. + * + * @param[in] compile_context The compile context to be used. + * @param[in, out] input1 First tensor input. Data types supported: U8. + * The input tensor is [in, out] because its TensorInfo might be modified inside the kernel in case of broadcasting of dimension 0. + * @param[in, out] input2 Second tensor input. Data types supported: U8. + * The input tensor is [in, out] because its TensorInfo might be modified inside the kernel in case of broadcasting of dimension 0. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output); + /** Static function to check if given info will lead to a valid configuration of @ref CLLogicalBinaryKernel + * + * @param[in] input1 First tensor input info. Data types supported: U8. + * @param[in] input2 Second tensor input info. Data types supported: U8. + * @param[in] output Output tensor info. Data types supported: U8. + * + * @return a status + */ + static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); + // Inherited methods overridden: + void run(ITensorPack &tensors) override; +}; +} // namespace experimental + +/** Basic function to run @ref CLLogicalBinaryKernel. + * + * @note The tensor data type for the inputs must be U8. + * @note The function performs a logical OR operation using the two input tensors. + */ +class CLLogicalOr : public IFunction +{ +public: + /** Default Constructor */ + CLLogicalOr(); + /** Default Destructor */ + ~CLLogicalOr(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLLogicalOr(const CLLogicalOr &) = delete; + /** Default move constructor */ + CLLogicalOr(CLLogicalOr &&); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLLogicalOr &operator=(const CLLogicalOr &) = delete; + /** Default move assignment operator */ + CLLogicalOr &operator=(CLLogicalOr &&); + /** Initialize the function + * + * @param[in] input1 Input tensor. Data types supported: U8. + * @param[in] input2 Input tensor. Data types supported: U8. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output); + /** Initialize the function + * + * @param[in] compile_context The compile context to be used. + * @param[in] input1 Input tensor. Data types supported: U8. + * @param[in] input2 Input tensor. Data types supported: U8. + * @param[out] output Output tensor. Data types supported: U8. + */ + void configure(const CLCompileContext &compile_context, ICLTensor *input1, ICLTensor *input2, ICLTensor *output); + /** Static function to check if given info will lead to a valid configuration + * + * @param[in] input1 First tensor input info. Data types supported: U8. + * @param[in] input2 Second tensor input info. Data types supported: U8. + * @param[in] output Output tensor info. Data types supported: U8. + * + * @return a status + */ + static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); + + // Inherited methods overridden: + void run() override; + +private: + struct Impl; + std::unique_ptr _impl; +}; +} // namespace arm_compute +#endif /* ARM_COMPUTE_CLLOGICALOR_H */ diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 33f0da0aa1..ae8b879be3 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -156,6 +156,8 @@ const std::map CLKernelLibrary::_kernel_program_map = { "elementwise_operation_SQUARED_DIFF", "elementwise_operation.cl" }, { "elementwise_operation_POWER", "elementwise_operation.cl" }, { "elementwise_operation_PRELU", "elementwise_operation.cl" }, + { "elementwise_operation_AND", "elementwise_operation.cl" }, + { "elementwise_operation_OR", "elementwise_operation.cl" }, { "elementwise_operation_ADD_quantized", "elementwise_operation_quantized.cl" }, { "elementwise_operation_SUB_quantized", "elementwise_operation_quantized.cl" }, { "elementwise_operation_MAX_quantized", "elementwise_operation_quantized.cl" }, diff --git a/src/core/CL/cl_kernels/elementwise_operation.cl b/src/core/CL/cl_kernels/elementwise_operation.cl index f6c09b4ec7..ea25082a6c 100644 --- a/src/core/CL/cl_kernels/elementwise_operation.cl +++ b/src/core/CL/cl_kernels/elementwise_operation.cl @@ -40,6 +40,11 @@ #define POWER(x, y) pow(x, y) #define PRELU(x, y) (select(y * x, x, CONVERT((x > (DATA_TYPE_OUT)0), SELECT_VEC_DATA_TYPE(DATA_TYPE_OUT, VEC_SIZE_OUT)))) +#if defined(VEC_SIZE_OUT) && defined(DATA_TYPE_OUT) +#define AND(x, y) (CONVERT((x && y), VEC_DATA_TYPE(DATA_TYPE_OUT, VEC_SIZE_OUT)) & 1) +#define OR(x, y) (CONVERT((x || y), VEC_DATA_TYPE(DATA_TYPE_OUT, VEC_SIZE_OUT)) & 1) +#endif // defined(VEC_SIZE_OUT) && defined(DATA_TYPE_OUT) + #define OP_FUN_NAME_STR(op) elementwise_operation_##op #define OP_FUN_NAME(op) OP_FUN_NAME_STR(op) diff --git a/src/core/CL/cl_kernels/elementwise_unary.cl b/src/core/CL/cl_kernels/elementwise_unary.cl index 3e557c0550..54c81fb04e 100644 --- a/src/core/CL/cl_kernels/elementwise_unary.cl +++ b/src/core/CL/cl_kernels/elementwise_unary.cl @@ -41,9 +41,11 @@ // Calculate round (Cannot use round function as it rounds halfway cases away from zero). #if defined(VEC_SIZE) #define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, VEC_DATA_TYPE(int, VEC_SIZE), rte), VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)) -#else // defined(VEC_SIZE +#else // defined(VEC_SIZE) #define round_op(input) CONVERT(CONVERT_SAT_ROUND(input, int, rte), DATA_TYPE) -#endif // defined(VEC_SIZE +#endif // defined(VEC_SIZE) +// Calculate logical NOT +#define logical_not_op(input) ((!input) & 0x1) /** Applies element wise unary operator in a tensor. * diff --git a/src/core/CL/kernels/CLElementWiseUnaryLayerKernel.cpp b/src/core/CL/kernels/CLElementWiseUnaryLayerKernel.cpp index 38a7f1bae1..0a641adcd2 100644 --- a/src/core/CL/kernels/CLElementWiseUnaryLayerKernel.cpp +++ b/src/core/CL/kernels/CLElementWiseUnaryLayerKernel.cpp @@ -34,16 +34,30 @@ namespace arm_compute { namespace { -Status validate_arguments(const ITensorInfo &input, const ITensorInfo &output) +Status validate_arguments(const ITensorInfo &input, const ITensorInfo &output, const ElementWiseUnary op) { ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32); + if(op == ElementWiseUnary::LOGICAL_NOT) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::U8); + } + else + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32); + } // Validate in case of configured output if(output.total_size() > 0) { ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::F16, DataType::F32); + if(op == ElementWiseUnary::LOGICAL_NOT) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::U8); + } + else + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::F16, DataType::F32); + } ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output); ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&input, &output); } @@ -63,7 +77,7 @@ void CLElementWiseUnaryLayerKernel::configure(const CLCompileContext &compile_co auto padding_info = get_padding_info({ input, output }); - ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input, *output)); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input, *output, op)); const std::string kernel_name = "elementwise_unary"; const int vec_size_x = 16 / output->element_size(); @@ -98,6 +112,9 @@ void CLElementWiseUnaryLayerKernel::configure(const CLCompileContext &compile_co case ElementWiseUnary::ROUND: build_opts.add_option("-DOPERATION=round_op"); break; + case ElementWiseUnary::LOGICAL_NOT: + build_opts.add_option("-DOPERATION=logical_not_op"); + break; default: ARM_COMPUTE_ERROR("Not implemented"); } @@ -121,7 +138,7 @@ Status CLElementWiseUnaryLayerKernel::validate(const ITensorInfo *input, const I { ARM_COMPUTE_UNUSED(op); ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input, *output)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input, *output, op)); return Status{}; } diff --git a/src/core/CL/kernels/CLElementwiseOperationKernel.cpp b/src/core/CL/kernels/CLElementwiseOperationKernel.cpp index efb3fe79e3..47439e15ab 100644 --- a/src/core/CL/kernels/CLElementwiseOperationKernel.cpp +++ b/src/core/CL/kernels/CLElementwiseOperationKernel.cpp @@ -26,6 +26,7 @@ #include "arm_compute/core/CL/CLHelpers.h" #include "arm_compute/core/CL/ICLTensor.h" #include "src/core/CL/CLValidate.h" +#include "src/core/common/Validate.h" #include "src/core/helpers/AutoConfiguration.h" #include "src/core/helpers/WindowHelpers.h" #include "support/Cast.h" @@ -220,6 +221,18 @@ std::pair validate_and_configure_window_for_arithmetic_operators return configure_window_arithmetic_common(output); } +std::pair validate_and_configure_window_for_logical_binary_operators(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) +{ + const std::pair broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2); + const TensorShape &out_shape = broadcast_pair.first; + + set_shape_if_empty(output, out_shape); + set_data_type_if_unknown(output, DataType::U8); + + // The arithmetic utility functions can be share + return configure_window_arithmetic_common(output); +} + std::pair validate_and_configure_window_for_division(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) { const std::pair broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2); @@ -319,6 +332,62 @@ void CLElementwiseOperationKernel::run_op(ITensorPack &tensors, const Window &wi while(collapsed.slide_window_slice_3D(slice)); } +/** Logical binary */ +void CLLogicalBinaryKernel::configure(const CLCompileContext &compile_context, kernels::LogicalOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); + ARM_COMPUTE_ERROR_THROW_ON(CLLogicalBinaryKernel::validate(op, input1, input2, output)); + _op = op; + configure_common(compile_context, input1, input2, output); +} + +Status CLLogicalBinaryKernel::validate(kernels::LogicalOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) +{ + ARM_COMPUTE_UNUSED(op); + ARM_COMPUTE_ASSERT(op != kernels::LogicalOperation::Unknown && op != kernels::LogicalOperation::Not); + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); + + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2); + + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_logical_binary_operators(*input1->clone(), *input2->clone(), *output->clone()).first); + + return Status{}; +} + +std::string CLLogicalBinaryKernel::name() +{ + switch(_op) + { + case kernels::LogicalOperation::And: + return "AND"; + case kernels::LogicalOperation::Or: + return "OR"; + case kernels::LogicalOperation::Not: + /* fall through */ + default: + ARM_COMPUTE_ASSERT(true); + } + return ""; +} + +std::pair CLLogicalBinaryKernel::validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) +{ + return validate_and_configure_window_for_logical_binary_operators(input1, input2, output); +} + +CLBuildOptions CLLogicalBinaryKernel::generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) +{ + // The arithmetic utility functions can be share + return generate_build_options_with_arithmetic_rules(input1, input2, output, name()); +} + +std::string CLLogicalBinaryKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) +{ + return generate_id_for_tuning_common(kernel_name, input1, output); +} + /** Arithmetic operations with saturation*/ void CLSaturatedArithmeticOperationKernel::configure(ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy, diff --git a/src/core/CL/kernels/CLElementwiseOperationKernel.h b/src/core/CL/kernels/CLElementwiseOperationKernel.h index 75030cf3a3..e24d1564a8 100644 --- a/src/core/CL/kernels/CLElementwiseOperationKernel.h +++ b/src/core/CL/kernels/CLElementwiseOperationKernel.h @@ -26,6 +26,7 @@ #include "arm_compute/core/Types.h" #include "src/core/CL/ICLKernel.h" +#include "src/core/KernelTypes.h" namespace arm_compute { @@ -99,6 +100,49 @@ private: ITensorInfo *_output; /**< Destination tensor info */ }; +class CLLogicalBinaryKernel : public CLElementwiseOperationKernel +{ +public: + /** Default constructor */ + CLLogicalBinaryKernel() = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLLogicalBinaryKernel(const CLLogicalBinaryKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLLogicalBinaryKernel &operator=(const CLLogicalBinaryKernel &) = delete; + /** Allow instances of this class to be moved */ + CLLogicalBinaryKernel(CLLogicalBinaryKernel &&) = default; + /** Allow instances of this class to be moved */ + CLLogicalBinaryKernel &operator=(CLLogicalBinaryKernel &&) = default; + /** Default destructor */ + ~CLLogicalBinaryKernel() = default; + /** Function to configure kernel + * + * @param[in] compile_context The compile context to be used. + * @param[in] op Logical binary operation to be executed. + * @param[in] input1 First tensor input info. Data types supported: U8. + * @param[in] input2 Second tensor input info. Data types supported: U8. + * @param[in] output Output tensor info. Data types supported: U8. + */ + void configure(const CLCompileContext &compile_context, kernels::LogicalOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output); + /** Static function to check if the given configuration is valid for this kernel + * + * @param[in] op Logical binary operation to be executed. + * @param[in] input1 First tensor input info. Data types supported: U8. + * @param[in] input2 Second tensor input info. Data types supported: U8. + * @param[in] output Output tensor info. Data types supported: U8. + */ + static Status validate(kernels::LogicalOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); + +private: + // Inherited methods overridden: + std::string name() override; + std::pair validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override; + CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; + std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override; + + kernels::LogicalOperation _op{ kernels::LogicalOperation::Unknown }; +}; + /** Addition operation */ class CLSaturatedArithmeticOperationKernel : public CLElementwiseOperationKernel { diff --git a/src/runtime/CL/functions/CLLogicalAnd.cpp b/src/runtime/CL/functions/CLLogicalAnd.cpp new file mode 100644 index 0000000000..55d3dc523b --- /dev/null +++ b/src/runtime/CL/functions/CLLogicalAnd.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2020 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/CLLogicalAnd.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "src/core/CL/kernels/CLElementwiseOperationKernel.h" +#include "support/MemorySupport.h" + +#include + +namespace arm_compute +{ +namespace experimental +{ +void CLLogicalAnd::configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) +{ + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(compile_context, kernels::LogicalOperation::And, input1, input2, output); + _kernel = std::move(k); +} + +Status CLLogicalAnd::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) +{ + return CLLogicalBinaryKernel::validate(kernels::LogicalOperation::And, input1, input2, output); +} + +void CLLogicalAnd::run(ITensorPack &tensors) +{ + ICLOperator::run(tensors); +} +} // namespace experimental + +struct CLLogicalAnd::Impl +{ + const ICLTensor *src0{ nullptr }; + const ICLTensor *src1{ nullptr }; + ICLTensor *dst{ nullptr }; + std::unique_ptr op{ nullptr }; +}; + +CLLogicalAnd::CLLogicalAnd() + : _impl(support::cpp14::make_unique()) +{ +} +CLLogicalAnd::CLLogicalAnd(CLLogicalAnd &&) = default; +CLLogicalAnd &CLLogicalAnd::operator=(CLLogicalAnd &&) = default; +CLLogicalAnd::~CLLogicalAnd() = default; + +void CLLogicalAnd::configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output) +{ + configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output); +} + +void CLLogicalAnd::configure(const CLCompileContext &compile_context, ICLTensor *input1, ICLTensor *input2, ICLTensor *output) +{ + _impl->src0 = input1; + _impl->src1 = input2; + _impl->dst = output; + _impl->op = arm_compute::support::cpp14::make_unique(); + _impl->op->configure(compile_context, input1->info(), input2->info(), output->info()); +} + +Status CLLogicalAnd::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) +{ + return experimental::CLLogicalAnd::validate(input1, input2, output); +} + +void CLLogicalAnd::run() +{ + ITensorPack pack; + pack.add_tensor(TensorType::ACL_SRC_0, _impl->src0); + pack.add_tensor(TensorType::ACL_SRC_1, _impl->src1); + pack.add_tensor(TensorType::ACL_DST, _impl->dst); + + _impl->op->run(pack); +} +} // namespace arm_compute diff --git a/src/runtime/CL/functions/CLLogicalNot.cpp b/src/runtime/CL/functions/CLLogicalNot.cpp new file mode 100644 index 0000000000..67aa3192f8 --- /dev/null +++ b/src/runtime/CL/functions/CLLogicalNot.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2020 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/CLLogicalNot.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "src/core/CL/kernels/CLElementWiseUnaryLayerKernel.h" +#include "support/MemorySupport.h" + +#include + +namespace arm_compute +{ +namespace experimental +{ +void CLLogicalNot::configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output) +{ + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(compile_context, input, output, ElementWiseUnary::LOGICAL_NOT); + _kernel = std::move(k); +} + +Status CLLogicalNot::validate(const ITensorInfo *input, const ITensorInfo *output) +{ + return CLElementWiseUnaryLayerKernel::validate(input, output, ElementWiseUnary::LOGICAL_NOT); +} + +void CLLogicalNot::run(ITensorPack &tensors) +{ + ICLOperator::run(tensors); +} +} // namespace experimental + +struct CLLogicalNot::Impl +{ + const ICLTensor *src{ nullptr }; + ICLTensor *dst{ nullptr }; + std::unique_ptr op{ nullptr }; +}; + +CLLogicalNot::CLLogicalNot() + : _impl(support::cpp14::make_unique()) +{ +} +CLLogicalNot::CLLogicalNot(CLLogicalNot &&) = default; +CLLogicalNot &CLLogicalNot::operator=(CLLogicalNot &&) = default; +CLLogicalNot::~CLLogicalNot() = default; + +void CLLogicalNot::configure(const ICLTensor *input, ICLTensor *output) +{ + configure(CLKernelLibrary::get().get_compile_context(), input, output); +} + +void CLLogicalNot::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output) +{ + _impl->src = input; + _impl->dst = output; + _impl->op = arm_compute::support::cpp14::make_unique(); + _impl->op->configure(compile_context, input->info(), output->info()); +} + +Status CLLogicalNot::validate(const ITensorInfo *input, const ITensorInfo *output) +{ + return experimental::CLLogicalNot::validate(input, output); +} + +void CLLogicalNot::run() +{ + ITensorPack pack; + pack.add_tensor(TensorType::ACL_SRC, _impl->src); + pack.add_tensor(TensorType::ACL_DST, _impl->dst); + + _impl->op->run(pack); +} + +} // namespace arm_compute \ No newline at end of file diff --git a/src/runtime/CL/functions/CLLogicalOr.cpp b/src/runtime/CL/functions/CLLogicalOr.cpp new file mode 100644 index 0000000000..4681083fd5 --- /dev/null +++ b/src/runtime/CL/functions/CLLogicalOr.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2020 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/CLLogicalOr.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "src/core/CL/kernels/CLElementwiseOperationKernel.h" +#include "support/MemorySupport.h" + +#include + +namespace arm_compute +{ +namespace experimental +{ +void CLLogicalOr::configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) +{ + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(compile_context, kernels::LogicalOperation::Or, input1, input2, output); + _kernel = std::move(k); +} + +Status CLLogicalOr::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) +{ + return CLLogicalBinaryKernel::validate(kernels::LogicalOperation::Or, input1, input2, output); +} + +void CLLogicalOr::run(ITensorPack &tensors) +{ + ICLOperator::run(tensors); +} +} /* namespace experimental */ + +struct CLLogicalOr::Impl +{ + const ICLTensor *src0{ nullptr }; + const ICLTensor *src1{ nullptr }; + ICLTensor *dst{ nullptr }; + std::unique_ptr op{ nullptr }; +}; + +CLLogicalOr::CLLogicalOr() + : _impl(support::cpp14::make_unique()) +{ +} +CLLogicalOr::CLLogicalOr(CLLogicalOr &&) = default; +CLLogicalOr &CLLogicalOr::operator=(CLLogicalOr &&) = default; +CLLogicalOr::~CLLogicalOr() = default; + +void CLLogicalOr::configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output) +{ + configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output); +} + +void CLLogicalOr::configure(const CLCompileContext &compile_context, ICLTensor *input1, ICLTensor *input2, ICLTensor *output) +{ + _impl->src0 = input1; + _impl->src1 = input2; + _impl->dst = output; + _impl->op = arm_compute::support::cpp14::make_unique(); + _impl->op->configure(compile_context, input1->info(), input2->info(), output->info()); +} + +Status CLLogicalOr::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) +{ + return experimental::CLLogicalOr::validate(input1, input2, output); +} + +void CLLogicalOr::run() +{ + ITensorPack pack; + pack.add_tensor(TensorType::ACL_SRC_0, _impl->src0); + pack.add_tensor(TensorType::ACL_SRC_1, _impl->src1); + pack.add_tensor(TensorType::ACL_DST, _impl->dst); + + _impl->op->run(pack); +} +} // namespace arm_compute diff --git a/tests/validation/CL/Logical.cpp b/tests/validation/CL/Logical.cpp new file mode 100644 index 0000000000..ecdb7c8f53 --- /dev/null +++ b/tests/validation/CL/Logical.cpp @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2020 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/functions/CLLogicalAnd.h" +#include "arm_compute/runtime/CL/functions/CLLogicalNot.h" +#include "arm_compute/runtime/CL/functions/CLLogicalOr.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.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/LogicalFixture.h" + +namespace +{ +using namespace arm_compute; + +const auto correct_shape = TensorShape(1, 2, 3, 4); // target shape to check against +const auto wrong_shape = TensorShape(1, 2, 2, 4); // wrong shape to check validate logic +const auto correct_dt = DataType::U8; // correct data type to check against +const auto wrong_dt = DataType::F32; // wrong data type to check validate logic +} + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +TEST_SUITE(CL) +TEST_SUITE(LogicalOr) +TEST_SUITE(Validate) +TEST_CASE(NullPtr, framework::DatasetMode::ALL) +{ + Status s = CLLogicalOr::validate(nullptr, nullptr, nullptr); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); +} + +TEST_CASE(WrongDataType, framework::DatasetMode::ALL) +{ + TensorInfo in1{ correct_shape, 1, correct_dt }; + TensorInfo in2{ correct_shape, 1, wrong_dt }; + TensorInfo out{ correct_shape, 1, correct_dt }; + + Status s = CLLogicalOr::validate(&in1, &in2, &out); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); +} +TEST_SUITE_END() // Validate +template +using CLLogicalOrFixture = LogicalOrValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, CLLogicalOrFixture, framework::DatasetMode::ALL, zip(datasets::SmallShapes(), datasets::SmallShapes())) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunSmallBroadcast, CLLogicalOrFixture, framework::DatasetMode::ALL, datasets::SmallShapesBroadcast()) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() // LogicalOr + +TEST_SUITE(LogicalAnd) +TEST_SUITE(Validate) +TEST_CASE(NullPtr, framework::DatasetMode::ALL) +{ + Status s = CLLogicalAnd::validate(nullptr, nullptr, nullptr); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); +} + +TEST_CASE(WrongDataType, framework::DatasetMode::ALL) +{ + TensorInfo in1{ correct_shape, 1, correct_dt }; + TensorInfo in2{ correct_shape, 1, wrong_dt }; + TensorInfo out{ correct_shape, 1, correct_dt }; + + Status s = CLLogicalAnd::validate(&in1, &in2, &out); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); +} +TEST_SUITE_END() // Validate +template +using CLLogicalAndFixture = LogicalAndValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, CLLogicalAndFixture, framework::DatasetMode::ALL, zip(datasets::SmallShapes(), datasets::SmallShapes())) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} + +FIXTURE_DATA_TEST_CASE(RunSmallBroadcast, CLLogicalAndFixture, framework::DatasetMode::ALL, datasets::SmallShapesBroadcast()) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() // LogicalAnd +TEST_SUITE(LogicalNot) + +TEST_SUITE(Validate) +TEST_CASE(NullPtr, framework::DatasetMode::ALL) +{ + Status s = CLLogicalNot::validate(nullptr, nullptr); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); +} + +TEST_CASE(WrongDataType, framework::DatasetMode::ALL) +{ + TensorInfo in{ correct_shape, 1, correct_dt }; + TensorInfo out{ correct_shape, 1, wrong_dt }; + + Status s = CLLogicalNot::validate(&in, &out); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); + + in = TensorInfo{ correct_shape, 1, wrong_dt }; + out = TensorInfo{ correct_shape, 1, correct_dt }; + + s = CLLogicalNot::validate(&in, &out); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); + + in = TensorInfo{ correct_shape, 1, wrong_dt }; + out = TensorInfo{ correct_shape, 1, wrong_dt }; + + s = CLLogicalNot::validate(&in, &out); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); +} + +TEST_CASE(WrongShape, framework::DatasetMode::ALL) +{ + TensorInfo in{ correct_shape, 1, correct_dt }; + TensorInfo out{ wrong_shape, 1, correct_dt }; + + Status s = CLLogicalNot::validate(&in, &out); + ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS); +} +TEST_SUITE_END() // Validate + +template +using CLLogicalNotFixture = LogicalNotValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, CLLogicalNotFixture, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), framework::dataset::make("DataType", + DataType::U8))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() // LogicalNot +TEST_SUITE_END() // CL +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/NEON/Logical.cpp b/tests/validation/NEON/Logical.cpp index f721e3cbc5..6f1c55b33c 100644 --- a/tests/validation/NEON/Logical.cpp +++ b/tests/validation/NEON/Logical.cpp @@ -41,7 +41,7 @@ TEST_SUITE(NEON) TEST_SUITE(LogicalAnd) template -using NELogicalAndFixture = LogicalBinaryOperationValidationFixture; +using NELogicalAndFixture = LogicalAndValidationFixture; FIXTURE_DATA_TEST_CASE(RunSmall, NELogicalAndFixture, framework::DatasetMode::ALL, zip(datasets::SmallShapes(), datasets::SmallShapes())) { @@ -58,7 +58,7 @@ TEST_SUITE_END() // LogicalAnd TEST_SUITE(LogicalOr) template -using NELogicalOrFixture = LogicalBinaryOperationValidationFixture; +using NELogicalOrFixture = LogicalOrValidationFixture; FIXTURE_DATA_TEST_CASE(RunSmall, NELogicalOrFixture, framework::DatasetMode::ALL, zip(datasets::SmallShapes(), datasets::SmallShapes())) { diff --git a/tests/validation/fixtures/LogicalFixture.h b/tests/validation/fixtures/LogicalFixture.h index a4817cf785..4bedb378bb 100644 --- a/tests/validation/fixtures/LogicalFixture.h +++ b/tests/validation/fixtures/LogicalFixture.h @@ -46,10 +46,10 @@ protected: template void fill(U &&tensor, int i) { - constexpr uint8_t zero = 0; - constexpr uint8_t one = 0x1; - constexpr uint8_t mixed = 0xAA; - constexpr uint8_t mixed_bitwise_not = ~(0xAA); + constexpr auto zero = (uint8_t)0; + constexpr auto one = (uint8_t)0x1; + constexpr auto mixed = (uint8_t)0xAA; + constexpr auto mixed_bitwise_not = (uint8_t) ~(0xAA); library->fill_static_values(tensor, i == 0 ? std::vector { zero, one, zero, one, mixed, zero, mixed } : @@ -70,7 +70,10 @@ protected: SimpleTensor _reference{}; }; -template +template +using LogicalBinaryRefFunctionPtrType = SimpleTensor(const SimpleTensor &, const SimpleTensor &); + +template RefFunction> class LogicalBinaryOperationValidationFixture : public LogicalOperationValidationFixtureBase { using Parent = LogicalOperationValidationFixtureBase; @@ -114,24 +117,18 @@ private: Parent::fill(src0, 0); Parent::fill(src1, 1); - switch(Op) - { - case reference::LogicalBinaryOperation::OR: - return reference::logical_or(src0, src1); - case reference::LogicalBinaryOperation::AND: - return reference::logical_and(src0, src1); - case reference::LogicalBinaryOperation::UNKNOWN: - /* fall-through */ - default: - ARM_COMPUTE_ASSERT_FAIL("unknown logical binary operator is given"); - } - - return SimpleTensor {}; + return RefFunction(src0, src1); } - static constexpr auto _data_type{ DataType::U8 }; + static constexpr auto _data_type = DataType::U8; }; +template +using LogicalOrValidationFixture = LogicalBinaryOperationValidationFixture>; + +template +using LogicalAndValidationFixture = LogicalBinaryOperationValidationFixture>; + template class LogicalNotValidationFixture : public LogicalOperationValidationFixtureBase { @@ -178,4 +175,4 @@ private: } // namespace validation } // namespace test } // namespace arm_compute -#endif /* ARM_COMPUTE_TEST_LOGICAL_FIXTURE */ \ No newline at end of file +#endif /* ARM_COMPUTE_TEST_LOGICAL_FIXTURE */ diff --git a/tests/validation/reference/Logical.cpp b/tests/validation/reference/Logical.cpp index 394525c392..9989ec841e 100644 --- a/tests/validation/reference/Logical.cpp +++ b/tests/validation/reference/Logical.cpp @@ -22,6 +22,8 @@ * SOFTWARE. */ #include "tests/validation/reference/Logical.h" +#include "src/core/KernelTypes.h" +#include "tests/framework/Asserts.h" namespace arm_compute { @@ -32,27 +34,30 @@ namespace validation namespace reference { template -T logical_op(LogicalBinaryOperation op, T src1, T src2) +T logical_binary_op(arm_compute::kernels::LogicalOperation op, T src1, T src2) { switch(op) { - case LogicalBinaryOperation::AND: + case arm_compute::kernels::LogicalOperation::And: return src1 && src2; - case LogicalBinaryOperation::OR: + case arm_compute::kernels::LogicalOperation::Or: return src1 || src2; - case LogicalBinaryOperation::UNKNOWN: + // The following operators are either invalid or not binary operator + case arm_compute::kernels::LogicalOperation::Not: + /* fall through */ + case arm_compute::kernels::LogicalOperation::Unknown: + /* fall through */ default: - ARM_COMPUTE_ERROR_ON_MSG(true, "unknown logical binary operation is given"); + ARM_COMPUTE_ASSERT(true); } - return false; + return T{}; } template struct BroadcastUnroll { template - static void unroll(LogicalBinaryOperation op, - const SimpleTensor &src1, const SimpleTensor &src2, SimpleTensor &dst, + static void unroll(arm_compute::kernels::LogicalOperation op, const SimpleTensor &src1, const SimpleTensor &src2, SimpleTensor &dst, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst) { const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]); @@ -79,10 +84,10 @@ template <> struct BroadcastUnroll<0> { template - static void unroll(LogicalBinaryOperation op, const SimpleTensor &src1, const SimpleTensor &src2, SimpleTensor &dst, + static void unroll(arm_compute::kernels::LogicalOperation op, const SimpleTensor &src1, const SimpleTensor &src2, SimpleTensor &dst, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst) { - dst[coord2index(dst.shape(), id_dst)] = logical_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)]); + dst[coord2index(dst.shape(), id_dst)] = logical_binary_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)]); } }; @@ -94,7 +99,7 @@ SimpleTensor logical_or(const SimpleTensor &src1, const SimpleTensor &s Coordinates id_dst{}; SimpleTensor dst{ TensorShape::broadcast_shape(src1.shape(), src2.shape()), src1.data_type() }; - BroadcastUnroll::unroll(LogicalBinaryOperation::OR, src1, src2, dst, id_src1, id_src2, id_dst); + BroadcastUnroll::unroll(arm_compute::kernels::LogicalOperation::Or, src1, src2, dst, id_src1, id_src2, id_dst); return dst; } @@ -107,7 +112,7 @@ SimpleTensor logical_and(const SimpleTensor &src1, const SimpleTensor & Coordinates id_dst{}; SimpleTensor dst{ TensorShape::broadcast_shape(src1.shape(), src2.shape()), src1.data_type() }; - BroadcastUnroll::unroll(LogicalBinaryOperation::AND, src1, src2, dst, id_src1, id_src2, id_dst); + BroadcastUnroll::unroll(arm_compute::kernels::LogicalOperation::And, src1, src2, dst, id_src1, id_src2, id_dst); return dst; } @@ -133,4 +138,4 @@ template SimpleTensor logical_not(const SimpleTensor &src1); } // namespace reference } // namespace validation } // namespace test -} // namespace arm_compute \ No newline at end of file +} // namespace arm_compute diff --git a/tests/validation/reference/Logical.h b/tests/validation/reference/Logical.h index fb906b70b6..0d2bef9a43 100644 --- a/tests/validation/reference/Logical.h +++ b/tests/validation/reference/Logical.h @@ -34,13 +34,6 @@ namespace validation { namespace reference { -enum class LogicalBinaryOperation -{ - UNKNOWN = 0, - AND = 1, - OR = 2 -}; - template SimpleTensor logical_or(const SimpleTensor &src1, const SimpleTensor &src2); template @@ -51,4 +44,4 @@ SimpleTensor logical_not(const SimpleTensor &src1); } // namespace validation } // namespace test } // namespace arm_compute -#endif /* ARM_COMPUTE_TEST_LOGICAL_H */ \ No newline at end of file +#endif /* ARM_COMPUTE_TEST_LOGICAL_H */ -- cgit v1.2.1