From 5e96be7707a571b136dc64256af399dbbb0fdfe0 Mon Sep 17 00:00:00 2001 From: Vidhya Sudhan Loganathan Date: Tue, 18 Dec 2018 14:17:00 +0000 Subject: COMPMID-1722 : CL: Implement Range Change-Id: I88da6eb5289c303b1dc91606c1560ce629746058 Reviewed-on: https://review.mlplatform.org/381 Reviewed-by: Gian Marco Iodice Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas --- arm_compute/core/CL/CLHelpers.h | 9 ++ arm_compute/core/CL/CLKernels.h | 1 + arm_compute/core/CL/kernels/CLRangeKernel.h | 83 ++++++++++++++ arm_compute/core/utils/misc/Utility.h | 6 +- arm_compute/runtime/CL/CLFunctions.h | 1 + arm_compute/runtime/CL/functions/CLRange.h | 62 +++++++++++ src/core/CL/CLHelpers.cpp | 26 +++++ src/core/CL/CLKernelLibrary.cpp | 6 ++ src/core/CL/cl_kernels/range.cl | 145 +++++++++++++++++++++++++ src/core/CL/kernels/CLRangeKernel.cpp | 159 +++++++++++++++++++++++++++ src/runtime/CL/functions/CLRange.cpp | 49 +++++++++ tests/validation/CL/Range.cpp | 162 ++++++++++++++++++++++++++++ tests/validation/fixtures/RangeFixture.h | 140 ++++++++++++++++++++++++ tests/validation/reference/Range.cpp | 77 +++++++++++++ tests/validation/reference/Range.h | 45 ++++++++ utils/Utils.h | 49 +++++++++ 16 files changed, 1018 insertions(+), 2 deletions(-) create mode 100644 arm_compute/core/CL/kernels/CLRangeKernel.h create mode 100644 arm_compute/runtime/CL/functions/CLRange.h create mode 100644 src/core/CL/cl_kernels/range.cl create mode 100644 src/core/CL/kernels/CLRangeKernel.cpp create mode 100644 src/runtime/CL/functions/CLRange.cpp create mode 100644 tests/validation/CL/Range.cpp create mode 100644 tests/validation/fixtures/RangeFixture.h create mode 100644 tests/validation/reference/Range.cpp create mode 100644 tests/validation/reference/Range.h diff --git a/arm_compute/core/CL/CLHelpers.h b/arm_compute/core/CL/CLHelpers.h index a86870a250..78427c3738 100644 --- a/arm_compute/core/CL/CLHelpers.h +++ b/arm_compute/core/CL/CLHelpers.h @@ -135,5 +135,14 @@ bool dot8_acc_supported(const cl::Device &device); * @return True if the configuration is supported */ bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout); + +/** Helper function to get the preferred native vector width size for built-in scalar types that can be put into vectors + * + * @param[in] device A CL device + * @param[in] dt data type + * + * @return preferred vector width + */ +size_t preferred_vector_width(const cl::Device &device, DataType dt); } #endif /* __ARM_COMPUTE_CLHELPERS_H__ */ diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h index d89426dd32..cfcfb7400b 100644 --- a/arm_compute/core/CL/CLKernels.h +++ b/arm_compute/core/CL/CLKernels.h @@ -113,6 +113,7 @@ #include "arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h" #include "arm_compute/core/CL/kernels/CLROIAlignLayerKernel.h" #include "arm_compute/core/CL/kernels/CLROIPoolingLayerKernel.h" +#include "arm_compute/core/CL/kernels/CLRangeKernel.h" #include "arm_compute/core/CL/kernels/CLReductionOperationKernel.h" #include "arm_compute/core/CL/kernels/CLRemapKernel.h" #include "arm_compute/core/CL/kernels/CLReorgLayerKernel.h" diff --git a/arm_compute/core/CL/kernels/CLRangeKernel.h b/arm_compute/core/CL/kernels/CLRangeKernel.h new file mode 100644 index 0000000000..2da21175ce --- /dev/null +++ b/arm_compute/core/CL/kernels/CLRangeKernel.h @@ -0,0 +1,83 @@ +/* + * 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_CLRANGEKERNEL_H__ +#define __ARM_COMPUTE_CLRANGEKERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Kernel class for Range() + * + * range generates a 1-D tensor containing a sequence of numbers that begins at 'start' and extends by increments + * of 'step' up to but not including 'end'. + */ +class CLRangeKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLRangeKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLRangeKernel(const CLRangeKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLRangeKernel &operator=(const CLRangeKernel &) = delete; + /** Allow instances of this class to be moved */ + CLRangeKernel(CLRangeKernel &&) = default; + /** Allow instances of this class to be moved */ + CLRangeKernel &operator=(CLRangeKernel &&) = default; + /** Default destructor */ + ~CLRangeKernel() = default; + /** Initialise the kernel's output tensor, start, end and step of the sequence. + * + * @param[out] output Output tensor. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[in] start The starting value of the sequence. + * @param[in] end The ending (not including) value of the sequence. + * @param[in] step The gap between each pair of values in the sequence. + */ + void configure(ICLTensor *output, float start, float end, float step); + /** Static function to check if given info will lead to a valid configuration of @ref CLRangeKernel + * + * @param[in] output Output tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[in] start The starting value of the sequence. + * @param[in] end The ending (not including) value of the sequence. + * @param[in] step The gap between each pair of values in the sequence. + * + * @return a status + */ + static Status validate(const ITensorInfo *output, float start, float end, float step); + + // Inherited methods overridden: + void run(const Window &window, cl::CommandQueue &queue) override; + +private: + float _start; /**< Start of sequence */ + float _end; /**< End of sequence */ + float _step; /**< Increment/step value */ + ICLTensor *_output; /**< Destination tensor */ +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_CLRANGEKERNEL_H__ */ diff --git a/arm_compute/core/utils/misc/Utility.h b/arm_compute/core/utils/misc/Utility.h index 37c8b66e06..8dd9afd5cd 100644 --- a/arm_compute/core/utils/misc/Utility.h +++ b/arm_compute/core/utils/misc/Utility.h @@ -80,8 +80,10 @@ std::array::value_type, N> make_array(It * * @return Clamped value. */ -template -inline T clamp(const T &n, const T &lower, const T &upper) +template +inline DataType clamp(const DataType &n, + const DataType &lower = std::numeric_limits::lowest(), + const DataType &upper = std::numeric_limits::max()) { return std::max(lower, std::min(n, upper)); } diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h index d4827af88a..303a8989a9 100644 --- a/arm_compute/runtime/CL/CLFunctions.h +++ b/arm_compute/runtime/CL/CLFunctions.h @@ -111,6 +111,7 @@ #include "arm_compute/runtime/CL/functions/CLRNNLayer.h" #include "arm_compute/runtime/CL/functions/CLROIAlignLayer.h" #include "arm_compute/runtime/CL/functions/CLROIPoolingLayer.h" +#include "arm_compute/runtime/CL/functions/CLRange.h" #include "arm_compute/runtime/CL/functions/CLReduceMean.h" #include "arm_compute/runtime/CL/functions/CLReductionOperation.h" #include "arm_compute/runtime/CL/functions/CLRemap.h" diff --git a/arm_compute/runtime/CL/functions/CLRange.h b/arm_compute/runtime/CL/functions/CLRange.h new file mode 100644 index 0000000000..2614534f14 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLRange.h @@ -0,0 +1,62 @@ +/* + * 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_CLRANGE_H__ +#define __ARM_COMPUTE_CLRANGE_H__ + +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/ICLSimpleFunction.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Basic function to run @ref CLRangeKernel + * + * @note The tensor data type for the output must be U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @note The function performs generates a sequence with the given start, end and step. + */ +class CLRange : public ICLSimpleFunction +{ +public: + /** Initialise the kernel's start, end, step and output tensor. + * + * @param[out] output Output tensor. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[in] start The starting value of the sequence. + * @param[in] end The ending (not including) value of the sequence. + * @param[in] step The gap between each pair of values in the sequence. Default is 1. + */ + void configure(ICLTensor *output, float start, float end, float step = 1.f); + /** Static function to check if given info will lead to a valid configuration of @ref CLRange + * + * @param[in] output Output tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32. + * @param[in] start The starting value of the sequence. + * @param[in] end The ending (not including) value of the sequence. + * @param[in] step The gap between each pair of values in the sequence. Default is 1. + * + * @return a status + */ + static Status validate(const ITensorInfo *output, float start, float end, float step = 1.f); +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_CLRANGE_H__ */ diff --git a/src/core/CL/CLHelpers.cpp b/src/core/CL/CLHelpers.cpp index 0947d58973..924fb1d322 100644 --- a/src/core/CL/CLHelpers.cpp +++ b/src/core/CL/CLHelpers.cpp @@ -230,4 +230,30 @@ bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Si return (std::find(winograd_configs_nhwc.begin(), winograd_configs_nhwc.end(), p) != winograd_configs_nhwc.end()); } } + +size_t preferred_vector_width(const cl::Device &device, const DataType dt) +{ + switch(dt) + { + case DataType::U8: + case DataType::S8: + case DataType::QASYMM8: + return device.getInfo(); + case DataType::U16: + case DataType::S16: + return device.getInfo(); + case DataType::U32: + case DataType::S32: + return device.getInfo(); + case DataType::F16: + case DataType::F32: + return device.getInfo(); + case DataType::U64: + case DataType::S64: + return device.getInfo(); + default: + return 1; + } +} + } // namespace arm_compute diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 03bc8d15db..ea5342c19d 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -382,6 +382,8 @@ const std::map CLKernelLibrary::_kernel_program_map = { "pooling_layer_MxN_quantized_nchw", "pooling_layer_quantized.cl" }, { "prior_box_layer_nchw", "prior_box_layer.cl" }, { "quantization_layer", "quantization_layer.cl" }, + { "range", "range.cl" }, + { "range_quantized", "range.cl" }, { "reduction_operation_x", "reduction_operation.cl" }, { "reduction_operation_non_parallel_x", "reduction_operation.cl" }, { "reduction_operation_y", "reduction_operation.cl" }, @@ -815,6 +817,10 @@ const std::map CLKernelLibrary::_program_source_map = { "quantization_layer.cl", #include "./cl_kernels/quantization_layer.clembed" + }, + { + "range.cl", +#include "./cl_kernels/range.clembed" }, { "reduction_operation.cl", diff --git a/src/core/CL/cl_kernels/range.cl b/src/core/CL/cl_kernels/range.cl new file mode 100644 index 0000000000..d122c9ac89 --- /dev/null +++ b/src/core/CL/cl_kernels/range.cl @@ -0,0 +1,145 @@ +/* + * 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(VECTOR_SIZE) && defined(START) && defined(STEP) && defined(DATA_TYPE) +/** Generates a sequence of numbers starting from START and extends by increments of 'STEP' up to but not including 'END'. + * + * @note starting value of the sequence must be given as a preprocessor argument using -DSTART=value. e.g. -DSTART=0 + * @note difference between consequtive elements of the sequence must be given as a preprocessor argument using -DSTEP=value. e.g. -DSTEP=1 + * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short + * @note vector size supported by the device must be given as a preprocessor argument using -DVECTOR_SIZE=value. e.g. -DDATA_TYPE=4 + * + * @param[out] out_ptr Pointer to the destination tensor. Supported data types: U8/S8/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 dst_stride_x * number of elements along X 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 range( + VECTOR_DECLARATION(out)) +{ + uint id = get_global_id(0) * VECTOR_SIZE; + __global void *dst_ptr = out_ptr + out_offset_first_element_in_bytes + id * sizeof(DATA_TYPE); +#if VECTOR_SIZE == 1 + DATA_TYPE seq; + seq = (DATA_TYPE)START + (DATA_TYPE)id * (DATA_TYPE)STEP; + + *((__global DATA_TYPE *)dst_ptr) = seq; +#else // VECTOR_SIZE == 1 + VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE) + seq; + + seq.s0 = ((DATA_TYPE)START + (DATA_TYPE)id * (DATA_TYPE)STEP); +#if VECTOR_SIZE > 1 + seq.s1 = seq.s0 + (DATA_TYPE)STEP; +#if VECTOR_SIZE > 2 + seq.s2 = seq.s1 + (DATA_TYPE)STEP; +#if VECTOR_SIZE > 3 + seq.s3 = seq.s2 + (DATA_TYPE)STEP; +#if VECTOR_SIZE > 4 + seq.s4 = seq.s3 + (DATA_TYPE)STEP; +#if VECTOR_SIZE > 5 + seq.s5 = seq.s4 + (DATA_TYPE)STEP; +#if VECTOR_SIZE > 6 + seq.s6 = seq.s5 + (DATA_TYPE)STEP; +#if VECTOR_SIZE > 7 + seq.s7 = seq.s6 + (DATA_TYPE)STEP; +#endif // VECTOR_SIZE > 7 +#endif // VECTOR_SIZE > 6 +#endif // VECTOR_SIZE > 5 +#endif // VECTOR_SIZE > 4 +#endif // VECTOR_SIZE > 3 +#endif // VECTOR_SIZE > 2 +#endif // VECTOR_SIZE > 1 + VSTORE(VECTOR_SIZE) + (seq, 0, ((__global DATA_TYPE *)dst_ptr)); +#endif //VECTOR_SIZE == 1 +} + +#if defined(OFFSET_OUT) && defined(SCALE_OUT) + +#define CONVERT_RTE(x, type) (convert_##type##_rte((x))) +#define CONVERT_DOWN(x, type) CONVERT_RTE(x, type) + +/** Generates a sequence of numbers starting from START and extends by increments of 'STEP' up to but not including 'END'. + * + * @note starting value of the sequence must be given as a preprocessor argument using -DSTART=value. e.g. -DSTART=0 + * @note difference between consequtive elements of the sequence must be given as a preprocessor argument using -DSTEP=value. e.g. -DSTEP=1 + * @note Datatype must be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short + * @note vector size supported by the device must be given as a preprocessor argument using -DVECTOR_SIZE=vector_size. e.g. -DDATA_TYPE=4 + * @note The quantization offset of the output must be passed at compile time using -DOFFSET_OUT, i.e. -DOFFSET_OUT=10 + * @note The quantization scale of the output must be passed at compile time using -DSCALE_OUT, i.e. -DSCALE_OUT=10 + * + * @param[out] out_ptr Pointer to the destination tensor. Supported data types: QASYMM8. + * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes) + * @param[in] out_step_x dst_stride_x * number of elements along X 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 range_quantized( + VECTOR_DECLARATION(out)) +{ + size_t id = get_global_id(0) * VECTOR_SIZE; + __global void *dst_ptr = out_ptr + out_offset_first_element_in_bytes + id * sizeof(DATA_TYPE); +#if VECTOR_SIZE == 1 + float seq; + seq = (float)START + (float)id * (float)STEP; + seq = (DATA_TYPE)(int)(seq / ((float)SCALE_OUT) + (float)OFFSET_OUT); + seq = max(0.0f, min(seq, 255.0f)); + *((__global uchar *)dst_ptr) = CONVERT_SAT(CONVERT_DOWN(seq, int), uchar); +#else // VECTOR_SIZE == 1 + VEC_DATA_TYPE(float, VECTOR_SIZE) + seq; + seq.s0 = (float)START + id * (float)STEP; +#if VECTOR_SIZE > 1 + seq.s1 = seq.s0 + (float)STEP; +#if VECTOR_SIZE > 2 + seq.s2 = seq.s1 + (float)STEP; +#if VECTOR_SIZE > 3 + seq.s3 = seq.s2 + (float)STEP; +#if VECTOR_SIZE > 4 + seq.s4 = seq.s3 + (float)STEP; +#if VECTOR_SIZE > 5 + seq.s5 = seq.s4 + (float)STEP; +#if VECTOR_SIZE > 6 + seq.s6 = seq.s5 + (float)STEP; +#if VECTOR_SIZE > 7 + seq.s7 = seq.s6 + (float)STEP; +#endif // VECTOR_SIZE > 7 +#endif // VECTOR_SIZE > 6 +#endif // VECTOR_SIZE > 5 +#endif // VECTOR_SIZE > 4 +#endif // VECTOR_SIZE > 3 +#endif // VECTOR_SIZE > 2 +#endif // VECTOR_SIZE > 1 + seq = seq / ((VEC_DATA_TYPE(float, VECTOR_SIZE))((float)SCALE_OUT)) + ((VEC_DATA_TYPE(float, VECTOR_SIZE))((float)OFFSET_OUT)); + seq = max((VEC_DATA_TYPE(float, VECTOR_SIZE))(0.0f), min(seq, (VEC_DATA_TYPE(float, VECTOR_SIZE))(255.0f))); + VEC_DATA_TYPE(uchar, VECTOR_SIZE) + res = CONVERT_SAT(CONVERT_DOWN(seq, VEC_DATA_TYPE(int, VECTOR_SIZE)), VEC_DATA_TYPE(uchar, VECTOR_SIZE)); + VSTORE(VECTOR_SIZE) + (res, 0, ((__global DATA_TYPE *)dst_ptr)); +#endif // VECTOR_SIZE == 1 +} +#endif // defined(OFFSET_OUT) && defined(SCALE_OUT) + +#endif // defined(VECTOR_SIZE) && defined(START) && defined(STEP) && defined(DATA_TYPE) diff --git a/src/core/CL/kernels/CLRangeKernel.cpp b/src/core/CL/kernels/CLRangeKernel.cpp new file mode 100644 index 0000000000..f53fe87a70 --- /dev/null +++ b/src/core/CL/kernels/CLRangeKernel.cpp @@ -0,0 +1,159 @@ +/* + * 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/CLRangeKernel.h" + +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLValidate.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "utils/Utils.h" + +using namespace arm_compute; + +namespace +{ +size_t num_of_elements_in_range(const float start, const float end, const float step) +{ + ARM_COMPUTE_ERROR_ON_MSG(step == 0, "CLRange Step cannot be 0"); + return size_t(std::ceil((end - start) / step)); +} + +unsigned int get_num_elems_processed_per_iteration(const DataType dt) +{ + unsigned int num_elems_processed_per_iteration = preferred_vector_width(CLKernelLibrary::get().get_device(), dt); + if(num_elems_processed_per_iteration > 8) + { + num_elems_processed_per_iteration = 8; //kernel uses only 8 lanes. + } + return num_elems_processed_per_iteration; +} + +Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step) +{ + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, + 1, + DataType::U8, DataType::S8, DataType::QASYMM8, + DataType::U16, DataType::S16, + DataType::U32, DataType::S32, + DataType::F16, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output); + + ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end"); + + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(start, output.data_type(), output.quantization_info()), "start value is outside the range of the data type"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(end, output.data_type(), output.quantization_info()), "end value is outside the range of the data type"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(step, output.data_type(), output.quantization_info()), "step value is outside the range of the data type"); + + ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); + + ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.num_dimensions() != 1, "Output has to be a 1-D tensor"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.tensor_shape().total_size() < num_of_elements_in_range(start, end, step), "Output tensor size is incorrect"); + + return Status{}; +} + +std::pair validate_and_configure_window(ITensorInfo &output, const float start, const float end, const float step) +{ + unsigned int num_elems_processed_per_iteration = get_num_elems_processed_per_iteration(output.data_type()); + // Auto initialize output if not initialized + auto_init_if_empty(output, TensorShape(num_of_elements_in_range(start, end, step)), 1, output.data_type(), output.quantization_info()); + + // Configure kernel window + Window win = calculate_max_window(output, Steps(num_elems_processed_per_iteration)); + + AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration); + bool window_changed = update_window_and_padding(win, output_access); + output_access.set_valid_region(win, ValidRegion(Coordinates(), TensorShape(num_of_elements_in_range(start, end, step)))); + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_pair(err, win); +} +} // namespace + +CLRangeKernel::CLRangeKernel() + : _start(0), _end(1), _step(1), _output(nullptr) +{ +} + +void CLRangeKernel::configure(ICLTensor *output, const float start, const float end, const float step) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step)); + + // Configure kernel window + auto win_config = validate_and_configure_window(*(output->info()), start, end, step); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + + _start = start; + _end = end; + _step = step; + _output = output; + + std::string kernel_name = "range"; + + unsigned int num_elems_processed_per_iteration = get_num_elems_processed_per_iteration(output->info()->data_type()); + // Set build options + CLBuildOptions build_opts; + build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type())); + build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); + build_opts.add_option("-DSTART=" + support::cpp11::to_string(start)); + build_opts.add_option("-DSTEP=" + support::cpp11::to_string(step)); + if(is_data_type_quantized_asymmetric(output->info()->data_type())) + { + build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(output->info()->quantization_info().offset)); + build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(output->info()->quantization_info().scale)); + kernel_name += "_quantized"; + } + // Create kernel + _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); + ICLKernel::configure_internal(win_config.second); + + // Set config_id for enabling LWS tuning + _config_id = kernel_name; + _config_id += "_"; + _config_id += lower_string(string_from_data_type(output->info()->data_type())); + _config_id += "_"; + _config_id += support::cpp11::to_string(output->info()->dimension(0)); +} + +Status CLRangeKernel::validate(const ITensorInfo *output, const float start, const float end, const float step) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); + + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step)); + ARM_COMPUTE_RETURN_ON_ERROR((validate_and_configure_window(*(output->clone()), start, end, step)).first); + + return Status{}; +} + +void CLRangeKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); + unsigned int idx = 0; + add_1D_tensor_argument(idx, _output, window); + + enqueue(queue, *this, window, lws_hint()); +} diff --git a/src/runtime/CL/functions/CLRange.cpp b/src/runtime/CL/functions/CLRange.cpp new file mode 100644 index 0000000000..b2cd472d02 --- /dev/null +++ b/src/runtime/CL/functions/CLRange.cpp @@ -0,0 +1,49 @@ +/* + * 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/CLRange.h" + +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/CL/kernels/CLRangeKernel.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/runtime/CL/CLScheduler.h" +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +void CLRange::configure(ICLTensor *output, const float start, const float end, const float step) +{ + auto k = arm_compute::support::cpp14::make_unique(); + k->set_target(CLScheduler::get().target()); + k->configure(output, start, end, step); + _kernel = std::move(k); + + // Tune kernels + CLScheduler::get().tune_kernel_static(*_kernel); +} + +Status CLRange::validate(const ITensorInfo *output, const float start, const float end, const float step) +{ + return CLRangeKernel::validate(output, start, end, step); +} diff --git a/tests/validation/CL/Range.cpp b/tests/validation/CL/Range.cpp new file mode 100644 index 0000000000..b1804f2184 --- /dev/null +++ b/tests/validation/CL/Range.cpp @@ -0,0 +1,162 @@ +/* + * 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/CLRange.h" +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/ConvertPolicyDataset.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/RangeFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +constexpr RelativeTolerance tolerance(0.01f); +constexpr AbsoluteTolerance abs_tolerance(0.02f); +const auto start_dataset = framework::dataset::make("Start", { float(3), float(-17), float(16) }); +const auto unsigned_start_dataset = framework::dataset::make("Start", { float(3), float(16) }); +const auto float_step_dataset = framework::dataset::make("Step", { float(1), float(-0.2f), float(0.2), float(12.2), float(-12.2), float(-1.2), float(-3), float(3) }); +const auto step_dataset = framework::dataset::make("Step", { float(1), float(12), float(-12), float(-1), float(-3), float(3) }); +const auto unsigned_step_dataset = framework::dataset::make("Step", { float(1), float(12), float(3) }); +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(Range) + +// *INDENT-OFF* +// clang-format off + +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("OutputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), + TensorInfo(TensorShape(32U), 1, DataType::U8), + TensorInfo(TensorShape(27U), 1, DataType::U8), + TensorInfo(TensorShape(32U), 1, DataType::U8), + TensorInfo(TensorShape(32U), 1, DataType::F32), + TensorInfo(TensorShape(27U), 1, DataType::U8), + TensorInfo(TensorShape(27U), 1, DataType::U8), + TensorInfo(TensorShape(10U), 1, DataType::U8), + }), + framework::dataset::make("Start",{ 0.0f, 15.0f, 1500.0f, 100.0f, -15.0f, 0.2f , 2.0f , 10.0f})), + framework::dataset::make("End",{ 100.0f, 15.0f, 2500.0f, -1000.0f, 15.0f, 10.0f, 10.0f,100.0f })), + framework::dataset::make("Step",{ 100.0f, 15.0f, 10.0f, 100.0f, -15.0f, 1.0f, 0.0f, 10.0f })), + framework::dataset::make("Expected", { false, //1-D tensor expected + false, //start == end + false, //output vector size insufficient + false, //sign of step incorrect + false, //sign of step incorrect + false, //data type incompatible + false, //step = 0 + true, + })), + output_info, start, end, step, expected) +{ + ARM_COMPUTE_EXPECT(bool(CLRange::validate(&output_info, start, end, step)) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +template +using CLRangeFixture = RangeFixture; + +TEST_SUITE(U8) +FIXTURE_DATA_TEST_CASE(RunSmall, CLRangeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine( + framework::dataset::make("DataType", DataType::U8), + unsigned_start_dataset), + unsigned_step_dataset), + framework::dataset::make("QuantizationInfo", { QuantizationInfo() }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance, 0.f, abs_tolerance); +} +TEST_SUITE_END() //U8 + +TEST_SUITE(Quantized) +TEST_SUITE(QASYMM8) + +FIXTURE_DATA_TEST_CASE(RunSmall, CLRangeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine( + framework::dataset::make("DataType", DataType::QASYMM8), + start_dataset), + step_dataset), + framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.3457f, 120.0f) }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance, 0.f, abs_tolerance); +} +TEST_SUITE_END() //QASYMM8 +TEST_SUITE_END() //Quantized + +TEST_SUITE(S16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLRangeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine( + framework::dataset::make("DataType", DataType::S16), + start_dataset), + step_dataset), + framework::dataset::make("QuantizationInfo", { QuantizationInfo() }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance, 0.f, abs_tolerance); +} +TEST_SUITE_END() //S16 + +TEST_SUITE(Float) +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLRangeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine( + framework::dataset::make("DataType", DataType::F16), + start_dataset), + float_step_dataset), + framework::dataset::make("QuantizationInfo", { QuantizationInfo() }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance, 0.f, abs_tolerance); +} +TEST_SUITE_END() //FP16 + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLRangeFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine( + framework::dataset::make("DataType", DataType::F32), + start_dataset), + float_step_dataset), + framework::dataset::make("QuantizationInfo", { QuantizationInfo() }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance, 0.f, abs_tolerance); +} +TEST_SUITE_END() //FP32 +TEST_SUITE_END() //Float + +TEST_SUITE_END() //Range +TEST_SUITE_END() //CL +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/RangeFixture.h b/tests/validation/fixtures/RangeFixture.h new file mode 100644 index 0000000000..c192eee14f --- /dev/null +++ b/tests/validation/fixtures/RangeFixture.h @@ -0,0 +1,140 @@ +/* + * 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_RANGE_FIXTURE +#define ARM_COMPUTE_TEST_RANGE_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/Helpers.h" +#include "tests/validation/reference/Range.h" + +#include + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +size_t num_of_elements_in_range(float start, float end, float step) +{ + ARM_COMPUTE_ERROR_ON(step == 0); + return size_t(std::ceil((end - start) / step)); +} +} + +template +class RangeFixture : public framework::Fixture +{ +public: + template + void setup(const DataType data_type0, float start, float step, const QuantizationInfo qinfo0 = QuantizationInfo()) + { + _target = compute_target(data_type0, qinfo0, start, step); + _reference = compute_reference(data_type0, qinfo0, start, step); + } + +protected: + float get_random_end(const DataType output_data_type, const QuantizationInfo qinfo_out, float start, float step) + { + std::uniform_real_distribution<> distribution(1, 100); + std::mt19937 gen(library->seed()); + float end = start; + switch(output_data_type) + { + case DataType::U8: + end += std::max((uint8_t)1, static_cast(distribution(gen))) * step; + return utility::clamp(end); + case DataType::U16: + end += std::max((uint16_t)1, static_cast(distribution(gen))) * step; + return utility::clamp(end); + case DataType::U32: + end += std::max((uint32_t)1, static_cast(distribution(gen))) * step; + return utility::clamp(end); + case DataType::S8: + end += std::max((int8_t)1, static_cast(distribution(gen))) * step; + return utility::clamp(end); + case DataType::S16: + end += std::max((int16_t)1, static_cast(distribution(gen))) * step; + return utility::clamp(end); + case DataType::S32: + end += std::max((int32_t)1, static_cast(distribution(gen))) * step; + return utility::clamp(end); + case DataType::F32: + end += std::max(1.0f, static_cast(distribution(gen))) * step; + return end; + case DataType::F16: + end += std::max(half(1.0f), static_cast(distribution(gen))) * step; + return utility::clamp(end); + case DataType::QASYMM8: + return utility::clamp(end + (float)distribution(gen) * step, qinfo_out.dequantize(0), qinfo_out.dequantize(std::numeric_limits::max())); + default: + return 0; + } + } + + TensorType compute_target(const DataType output_data_type, const QuantizationInfo qinfo_out, float start, float step) + { + float end = get_random_end(output_data_type, qinfo_out, start, step); + size_t num_of_elements = num_of_elements_in_range(start, end, step); + // Create tensor + TensorType dst = create_tensor(TensorShape(num_of_elements), output_data_type, 1, qinfo_out); + // Create and configure function + FunctionType range_func; + range_func.configure(&dst, start, end, step); + + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + // Allocate tensors + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Compute function + range_func.run(); + return dst; + } + + SimpleTensor compute_reference(const DataType output_data_type, const QuantizationInfo qinfo_out, float start, float step) + { + // Create tensor + const float end = get_random_end(output_data_type, qinfo_out, start, step); + size_t num_of_elements = num_of_elements_in_range(start, end, step); + SimpleTensor ref_dst{ TensorShape(num_of_elements ? num_of_elements : 1), output_data_type, 1, qinfo_out }; + return reference::range(ref_dst, start, num_of_elements, step); + } + + TensorType _target{}; + SimpleTensor _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_RANGE_FIXTURE */ diff --git a/tests/validation/reference/Range.cpp b/tests/validation/reference/Range.cpp new file mode 100644 index 0000000000..c24512fa9d --- /dev/null +++ b/tests/validation/reference/Range.cpp @@ -0,0 +1,77 @@ +/* + * 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 "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +template +void generate_range(SimpleTensor &dst, float start, const size_t num_of_elements, float step) +{ + float val = start; + for(size_t index = 0; index < num_of_elements; index++) + { + dst[index] = static_cast(val); + val += step; + } +} +} // namespace + +template +SimpleTensor range(SimpleTensor &dst, float start, const size_t num_of_elements, float step) +{ + generate_range(dst, start, num_of_elements, step); + return dst; +} + +template <> +SimpleTensor range(SimpleTensor &dst, float start, const size_t num_of_elements, float step) +{ + if(dst.data_type() == DataType::QASYMM8) + { + SimpleTensor dst_tmp{ dst.shape(), DataType::F32, 1 }; + generate_range(dst_tmp, start, num_of_elements, step); + return convert_to_asymmetric(dst_tmp, dst.quantization_info()); + } + generate_range(dst, start, num_of_elements, step); + return dst; +} +template SimpleTensor range(SimpleTensor &dst, float start, const size_t num_of_elements, float step); +template SimpleTensor range(SimpleTensor &dst, float start, const size_t num_of_elements, float step); +template SimpleTensor range(SimpleTensor &dst, float start, const size_t num_of_elements, float step); +template SimpleTensor range(SimpleTensor &dst, float start, const size_t num_of_elements, float step); +template SimpleTensor range(SimpleTensor &dst, float start, const size_t num_of_elements, float step); + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/Range.h b/tests/validation/reference/Range.h new file mode 100644 index 0000000000..d31166bb86 --- /dev/null +++ b/tests/validation/reference/Range.h @@ -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. + */ +#ifndef __ARM_COMPUTE_TEST_RANGE_H__ +#define __ARM_COMPUTE_TEST_RANGE_H__ + +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor range(SimpleTensor &dst, float start, size_t num_elements, float step); + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_RANGE_H__ */ diff --git a/utils/Utils.h b/utils/Utils.h index 8cac857178..031a3726a1 100644 --- a/utils/Utils.h +++ b/utils/Utils.h @@ -194,6 +194,55 @@ inline std::string get_typestring(DataType data_type) } } +/** Returns true if the value can be represented by the given data type + * + * @param[in] val value to be checked + * @param[in] dt data type that is checked + * @param[in] quant_info quantization info if the data type is QASYMM8 + * + * @return true if the data type can hold the value. + */ +template +bool check_value_range(T val, DataType dt, QuantizationInfo quant_info = QuantizationInfo()) +{ + switch(dt) + { + case DataType::U8: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::QASYMM8: + { + double min = static_cast(quant_info.dequantize(0)); + double max = static_cast(quant_info.dequantize(std::numeric_limits::max())); + return ((double)val >= min && (double)val <= max); + } + case DataType::S8: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::U16: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::S16: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::U32: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::S32: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::U64: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::S64: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::F16: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::F32: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::F64: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::SIZET: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + default: + ARM_COMPUTE_ERROR("Data type not supported"); + return false; + } +} + /** Maps a tensor if needed * * @param[in] tensor Tensor to be mapped -- cgit v1.2.1