From 56dd726ee074cb145612d03240b710f8adb82ddd Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Thu, 27 Jul 2017 09:53:49 +0100 Subject: COMPMID-448: Implement CL Quantization/Dequantization Layer. Change-Id: Id002e23a2ac48af3d245416dc6411d9a04a1e513 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/81827 Reviewed-by: Gian Marco Iodice Reviewed-by: Georgios Pinitas Tested-by: Kaizen --- .../core/CL/kernels/CLDequantizationLayerKernel.h | 71 +++++++++++ arm_compute/core/CL/kernels/CLMinMaxLayerKernel.h | 70 +++++++++++ .../core/CL/kernels/CLQuantizationLayerKernel.h | 70 +++++++++++ .../runtime/CL/functions/CLDequantizationLayer.h | 68 +++++++++++ .../runtime/CL/functions/CLQuantizationLayer.h | 66 ++++++++++ src/core/CL/CLKernelLibrary.cpp | 15 +++ src/core/CL/cl_kernels/dequantization_layer.cl | 73 +++++++++++ src/core/CL/cl_kernels/minmax_layer.cl | 101 +++++++++++++++ src/core/CL/cl_kernels/quantization_layer.cl | 75 ++++++++++++ .../CL/kernels/CLDequantizationLayerKernel.cpp | 101 +++++++++++++++ src/core/CL/kernels/CLMinMaxLayerKernel.cpp | 136 +++++++++++++++++++++ src/core/CL/kernels/CLQuantizationLayerKernel.cpp | 101 +++++++++++++++ src/runtime/CL/functions/CLDequantizationLayer.cpp | 45 +++++++ src/runtime/CL/functions/CLQuantizationLayer.cpp | 60 +++++++++ tests/validation/CL/DequantizationLayer.cpp | 119 ++++++++++++++++++ tests/validation/CL/QuantizationLayer.cpp | 103 ++++++++++++++++ tests/validation/CPP/QuantizationLayer.cpp | 23 ---- 17 files changed, 1274 insertions(+), 23 deletions(-) create mode 100644 arm_compute/core/CL/kernels/CLDequantizationLayerKernel.h create mode 100644 arm_compute/core/CL/kernels/CLMinMaxLayerKernel.h create mode 100644 arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h create mode 100644 arm_compute/runtime/CL/functions/CLDequantizationLayer.h create mode 100644 arm_compute/runtime/CL/functions/CLQuantizationLayer.h create mode 100644 src/core/CL/cl_kernels/dequantization_layer.cl create mode 100644 src/core/CL/cl_kernels/minmax_layer.cl create mode 100644 src/core/CL/cl_kernels/quantization_layer.cl create mode 100644 src/core/CL/kernels/CLDequantizationLayerKernel.cpp create mode 100644 src/core/CL/kernels/CLMinMaxLayerKernel.cpp create mode 100644 src/core/CL/kernels/CLQuantizationLayerKernel.cpp create mode 100644 src/runtime/CL/functions/CLDequantizationLayer.cpp create mode 100644 src/runtime/CL/functions/CLQuantizationLayer.cpp create mode 100644 tests/validation/CL/DequantizationLayer.cpp create mode 100644 tests/validation/CL/QuantizationLayer.cpp diff --git a/arm_compute/core/CL/kernels/CLDequantizationLayerKernel.h b/arm_compute/core/CL/kernels/CLDequantizationLayerKernel.h new file mode 100644 index 0000000000..87a3fb41a7 --- /dev/null +++ b/arm_compute/core/CL/kernels/CLDequantizationLayerKernel.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2017 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_CLDEQUANTIZATIONLAYERKERNEL_H__ +#define __ARM_COMPUTE_CLDEQUANTIZATIONLAYERKERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Interface for the dequantization layer kernel. + * + * @note The implementation supports only 3D input tensors. + * + */ +class CLDequantizationLayerKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLDequantizationLayerKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLDequantizationLayerKernel(const CLDequantizationLayerKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLDequantizationLayerKernel &operator=(const CLDequantizationLayerKernel &) = delete; + /** Default Move Constructor. */ + CLDequantizationLayerKernel(CLDequantizationLayerKernel &&) = default; + /** Default move assignment operator. */ + CLDequantizationLayerKernel &operator=(CLDequantizationLayerKernel &&) = default; + /** Default destructor */ + ~CLDequantizationLayerKernel() = default; + /** Set the input, output, min and max. + * + * @param[in] input Source tensor. Data types supported: U8. + * @param[out] output Destination tensor. Data types supported: F32. + * @param[in] min_max Pointer to the tensor with shape [2, batches] which stores the minimum and maximum value for each 3D input tensor. + * The dimensions over the second must match the batched dimensions of the input tensor. Data type supported: F32. + */ + void configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *min_max); + + // Inherited methods overridden: + void run(const Window &window, cl::CommandQueue &queue) override; + +private: + const ICLTensor *_input; + ICLTensor *_output; + const ICLTensor *_min_max; +}; +} +#endif /*__ARM_COMPUTE_CLDEQUANTIZATIONLAYERKERNEL_H__ */ diff --git a/arm_compute/core/CL/kernels/CLMinMaxLayerKernel.h b/arm_compute/core/CL/kernels/CLMinMaxLayerKernel.h new file mode 100644 index 0000000000..ed8459ae23 --- /dev/null +++ b/arm_compute/core/CL/kernels/CLMinMaxLayerKernel.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017 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_CLMINMAXLAYERKERNEL_H__ +#define __ARM_COMPUTE_CLMINMAXLAYERKERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Interface for the kernel to perform min max search on a 3D tensor. + */ +class CLMinMaxLayerKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLMinMaxLayerKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLMinMaxLayerKernel(const CLMinMaxLayerKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLMinMaxLayerKernel &operator=(const CLMinMaxLayerKernel &) = delete; + /** Allow instances of this class to be moved */ + CLMinMaxLayerKernel(CLMinMaxLayerKernel &&) = default; + /** Allow instances of this class to be moved */ + CLMinMaxLayerKernel &operator=(CLMinMaxLayerKernel &&) = default; + /** Initialise the kernel's input and output. + * + * @param[in] input Input tensor with at least 3 dimensions. The dimensions over the third will be interpreted as batches.Data types supported: F32. + * @param[out] output Output tensor with shape [2, batches, ...] which stores the minimum and maximum values for each 3D input tensor. + * The dimensions over the second must match the batched dimensions of the input tensor. Data types supported: F32. + */ + void configure(const ICLTensor *input, ICLTensor *output); + + /** Resets global minimum and maximum + * + * @param[in,out] queue Command queue on which to map and unmap the min_max tensor + */ + void reset(cl::CommandQueue &queue); + + // Inherited methods overridden: + void run(const Window &window, cl::CommandQueue &queue) override; + +private: + const ICLTensor *_input; + ICLTensor *_output; +}; +} +#endif /*__ARM_COMPUTE_CLMINMAXLAYERKERNEL_H__ */ diff --git a/arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h b/arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h new file mode 100644 index 0000000000..427219f740 --- /dev/null +++ b/arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017 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_CLQUANTIZATIONLAYERKERNEL_H__ +#define __ARM_COMPUTE_CLQUANTIZATIONLAYERKERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Interface for the quantization layer kernel. + * + * @note The implementation supports only 3D input tensors. + */ +class CLQuantizationLayerKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLQuantizationLayerKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLQuantizationLayerKernel(const CLQuantizationLayerKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLQuantizationLayerKernel &operator=(const CLQuantizationLayerKernel &) = delete; + /** Default Move Constructor. */ + CLQuantizationLayerKernel(CLQuantizationLayerKernel &&) = default; + /** Default move assignment operator. */ + CLQuantizationLayerKernel &operator=(CLQuantizationLayerKernel &&) = default; + /** Default destructor */ + ~CLQuantizationLayerKernel() = default; + /** Set the input, output, min and max. + * + * @param[in] input Source tensor. Data types supported: F32. + * @param[out] output Destination tensor. Data types supported: U8. + * @param[in] min_max Pointer to the tensor with shape [2, batches] which stores the minimum and maximum value for each 3D input tensor. + * The dimensions over the second must match the batched dimensions of the input tensor. Data type supported: F32. + */ + void configure(const ICLTensor *input, ICLTensor *output, ICLTensor *min_max); + + // Inherited methods overridden: + void run(const Window &window, cl::CommandQueue &queue) override; + +private: + const ICLTensor *_input; + ICLTensor *_output; + const ICLTensor *_min_max; +}; +} +#endif /*__ARM_COMPUTE_CLQUANTIZATIONLAYERKERNEL_H__ */ diff --git a/arm_compute/runtime/CL/functions/CLDequantizationLayer.h b/arm_compute/runtime/CL/functions/CLDequantizationLayer.h new file mode 100644 index 0000000000..0ba182b475 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLDequantizationLayer.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017 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_CLDEQUANTIZATIONLAYER_H__ +#define __ARM_COMPUTE_CLDEQUANTIZATIONLAYER_H__ + +#include "arm_compute/runtime/IFunction.h" + +#include "arm_compute/core/CL/kernels/CLDequantizationLayerKernel.h" +#include "arm_compute/runtime/Tensor.h" + +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Basic function to simulate a dequantization layer. This function calls the following CL kernels: + * + * -# @ref CLDequantizationLayerKernel + * + */ +class CLDequantizationLayer : public IFunction +{ +public: + /** Default constructor */ + CLDequantizationLayer(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLDequantizationLayer(const CLDequantizationLayer &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLDequantizationLayer &operator=(const CLDequantizationLayer &) = delete; + /** Set the input and output tensors. + * + * @param[in] input Source tensor with at least 3 dimensions. The dimensions over the third will be interpreted as batches. Data types supported: U8. + * @param[out] output Destination tensor with the same dimensions of input. Data type supported: F32. + * @param[in] min_max Pointer to the tensor with shape [2, batches] which stores the minimum and maximum value for each 3D input tensor. + * The dimensions over the second must match the batched dimensions of the input tensor. Data type supported: F32. + */ + void configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *min_max); + + // Inherited methods overridden: + void run() override; + +private: + CLDequantizationLayerKernel _dequantize_kernel; +}; +} +#endif /* __ARM_COMPUTE_CLDEQUANTIZATIONLAYER_H__ */ diff --git a/arm_compute/runtime/CL/functions/CLQuantizationLayer.h b/arm_compute/runtime/CL/functions/CLQuantizationLayer.h new file mode 100644 index 0000000000..b3c4da05d4 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLQuantizationLayer.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2017 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_CLQUANTIZATIONLAYER_H__ +#define __ARM_COMPUTE_CLQUANTIZATIONLAYER_H__ + +#include "arm_compute/runtime/IFunction.h" + +#include "arm_compute/core/CL/kernels/CLMinMaxLayerKernel.h" +#include "arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h" +#include "arm_compute/runtime/CL/CLTensor.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Basic function to simulate a quantization layer. This function calls the following CL kernels: + * + * @note The implementation supports only 3D input tensors. + * + * -# @ref CLMinMaxLayerKernel + * -# @ref CLQuantizationLayerKernel + * + */ +class CLQuantizationLayer : public IFunction +{ +public: + /** Default constructor */ + CLQuantizationLayer(); + /** Set the input and output tensors. + * + * @param[in] input Source tensor with at least 3 dimensions. The dimensions over the third will be interpreted as batches. Data types supported: F32. + * @param[out] output Destination tensor with the same dimensions of input. Output data type must be U8. + */ + void configure(const ICLTensor *input, ICLTensor *output); + + // Inherited methods overridden: + void run() override; + +private: + CLQuantizationLayerKernel _quantize_kernel; + CLMinMaxLayerKernel _min_max_kernel; + CLTensor _min_max; +}; +} +#endif /* __ARM_COMPUTE_CLQUANTIZATIONLAYER_H__ */ diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index dd549f035b..696fcb475c 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -145,6 +145,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "copy_planes_3p", "channel_combine.cl" }, { "copy_to_keypoint", "fast_corners.cl" }, { "depthwise_convolution_3x3", "depthwise_convolution.cl" }, + { "dequantization_layer", "dequantization_layer.cl" }, { "derivative", "derivative.cl" }, { "dilate", "dilate.cl" }, { "direct_convolution1x1", "direct_convolution1x1.cl" }, @@ -212,6 +213,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "mean_stddev_accumulate", "mean_stddev.cl" }, { "minmax", "minmaxloc.cl" }, { "minmax_border", "minmaxloc.cl" }, + { "minmax_layer", "minmax_layer.cl" }, { "minmaxloc", "minmaxloc.cl" }, { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" }, { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" }, @@ -237,6 +239,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "pooling_layer_3", "pooling_layer.cl" }, { "pooling_layer_3_optimized", "pooling_layer.cl" }, { "pooling_layer_7", "pooling_layer.cl" }, + { "quantization_layer", "quantization_layer.cl" }, { "reduction_operation", "reduction_operation.cl" }, { "remap_nearest_neighbour", "remap.cl" }, { "remap_bilinear", "remap.cl" }, @@ -355,6 +358,10 @@ const std::map CLKernelLibrary::_program_source_map = { "depthwise_convolution.cl", #include "./cl_kernels/depthwise_convolution.clembed" + }, + { + "dequantization_layer.cl", +#include "./cl_kernels/dequantization_layer.clembed" }, { "derivative.cl", @@ -439,6 +446,10 @@ const std::map CLKernelLibrary::_program_source_map = { "minmaxloc.cl", #include "./cl_kernels/minmaxloc.clembed" + }, + { + "minmax_layer.cl", +#include "./cl_kernels/minmax_layer.clembed" }, { "non_linear_filter3x3.cl", @@ -479,6 +490,10 @@ const std::map CLKernelLibrary::_program_source_map = { "pooling_layer.cl", #include "./cl_kernels/pooling_layer.clembed" + }, + { + "quantization_layer.cl", +#include "./cl_kernels/quantization_layer.clembed" }, { "reduction_operation.cl", diff --git a/src/core/CL/cl_kernels/dequantization_layer.cl b/src/core/CL/cl_kernels/dequantization_layer.cl new file mode 100644 index 0000000000..21e9c873ac --- /dev/null +++ b/src/core/CL/cl_kernels/dequantization_layer.cl @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2017 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" + +/** This performs the dequantization of 8-bit unsigned integers to floating point. + * + * @param[in] input_ptr Pointer to the source image. Supported data types: QS8/QS16/F16/F32 + * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) + * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) + * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image + * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr + * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) + * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) + * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image + * @param[in] min_max_ptr Pointer to the min/max vector. Minimum value in position 0, maximum value in position 1. Suppported data types: F32. + * @param[in] min_max_stride_x Stride of the min/max vector in X dimension (in bytes) + * @param[in] min_max_step_x min_max_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] min_max_offset_first_element_in_bytes The offset of the first element in the min/max vector + */ +__kernel void dequantization_layer( + TENSOR3D_DECLARATION(input), + TENSOR3D_DECLARATION(output), + VECTOR_DECLARATION(min_max)) +{ + // Get pixels pointer + Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); + Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); + Vector min_max = CONVERT_TO_VECTOR_STRUCT(min_max); + + // min_max_value.s0 = min, min_max_value.s1 = max + const float2 min_max_value = vload2(0, (__global float *)min_max.ptr); + + const float4 vmin = (float4)min_max_value.s0; + const float4 scale = (float4)((min_max_value.s1 - min_max_value.s0) / 255.0f); + + // Load data + const uchar4 data = vload4(0, (__global uchar *)input.ptr); + + // Dequantize + const float4 res = convert_float4(data) * scale + vmin; + + // Store result + vstore4(res, 0, (__global float *)output.ptr); +} diff --git a/src/core/CL/cl_kernels/minmax_layer.cl b/src/core/CL/cl_kernels/minmax_layer.cl new file mode 100644 index 0000000000..1e543b43bd --- /dev/null +++ b/src/core/CL/cl_kernels/minmax_layer.cl @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017 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(WIDTH) && defined(HEIGHT) && defined(DEPTH) +/** This function identifies the min and maximum value of an input 3D tensor. + * + * @note The width, height and depth of the input tensor must be provided at compile time using -DWIDTH, -DHEIGHT and -DDEPTH (e.g. -DWIDTH=320, -DHEIGHT=240, -DDEPTH=3) + * + * @param[in] src_ptr Pointer to the source tensor. Supported data types: F32 + * @param[in] src_stride_x Stride of the source image in X dimension (in bytes) + * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes) + * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] src_stride_z Stride of the source image in Z dimension (in bytes) + * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image + * @param[in] dst_ptr Pointer to the min/max vector. Minimum value in position 0, maximum value in position 1. Supported data types: F32. + * @param[in] dst_stride_x Stride of the min/max vector in X dimension (in bytes) + * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the min/max vector + */ +__kernel void minmax_layer( + TENSOR3D_DECLARATION(src), + VECTOR_DECLARATION(dst)) +{ + Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src); + Vector dst = CONVERT_TO_VECTOR_STRUCT(dst); + + float4 min_value = (float4)FLT_MAX; + float4 max_value = (float4) - FLT_MAX; + float2 min_max_value = (float2)(FLT_MAX, -FLT_MAX); + + for(int z = 0; z < DEPTH; ++z) + { + for(int y = 0; y < HEIGHT; ++y) + { + int x = 0; + __global float *src_addr = (__global float *)(src.ptr + y * src_stride_y + z * src_stride_z); + + for(; x <= (int)(WIDTH - 8); x += 8) + { + float8 value = *(src_addr + x); + + min_value = select(value.s0123, min_value, min_value < value.s0123); + min_value = select(value.s4567, min_value, min_value < value.s4567); + + max_value = select(value.s0123, max_value, max_value > value.s0123); + max_value = select(value.s4567, max_value, max_value > value.s4567); + } + + for(; x < WIDTH; ++x) + { + float value = *(src_addr + x); + + min_max_value.s0 = min(min_max_value.s0, value); + min_max_value.s1 = max(min_max_value.s1, value); + } + } + } + + // Perform min/max reduction + min_value.s01 = min(min_value.s01, min_value.s23); + min_value.s0 = min(min_value.s0, min_value.s1); + max_value.s01 = max(max_value.s01, max_value.s23); + max_value.s0 = max(max_value.s0, max_value.s1); + + min_max_value.s0 = min(min_max_value.s0, min_value.s0); + min_max_value.s1 = max(min_max_value.s1, max_value.s0); + + if(min_max_value.s0 == min_max_value.s1) + { + min_max_value.s0 = 0.0f; + min_max_value.s1 = 1.0f; + } + + // Store min and max + vstore2(min_max_value, 0, (__global float *)dst.ptr); +} +#endif // defined(WIDTH) && defined(HEIGHT) && defined(DEPTH) \ No newline at end of file diff --git a/src/core/CL/cl_kernels/quantization_layer.cl b/src/core/CL/cl_kernels/quantization_layer.cl new file mode 100644 index 0000000000..80ea54012f --- /dev/null +++ b/src/core/CL/cl_kernels/quantization_layer.cl @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2017 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" + +/** This performs the quantization of floating point inputs to 8-bit unsigned integers. + * + * @param[in] input_ptr Pointer to the source image. Supported data types: F32 + * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) + * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) + * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image + * @param[out] output_ptr Pointer to the destination image. Supported data types: U8 + * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) + * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) + * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image + * @param[in] min_max_ptr Pointer to the min/max vector. Minimum value in position 0, maximum value in position 1. Supported data types: F32. + * @param[in] min_max_stride_x Stride of the min/max vector in X dimension (in bytes) + * @param[in] min_max_step_x min_max_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] min_max_offset_first_element_in_bytes The offset of the first element in the min/max vector + */ +__kernel void quantization_layer( + TENSOR3D_DECLARATION(input), + TENSOR3D_DECLARATION(output), + VECTOR_DECLARATION(min_max)) +{ + // Get pixels pointer + Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); + Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); + + // min_max_value.s0 = min, min_max_value.s1 = max + const float2 min_max_value = vload2(0, (__global float *)(min_max_ptr + min_max_offset_first_element_in_bytes)); + + const float4 vmin = (float4)min_max_value.s0; + const float4 vrange = (float4)(min_max_value.s1 - min_max_value.s0); + + // Load data + float4 data = vload4(0, (__global float *)input.ptr); + + // Map float values to range [0.0, 1.0] + data = (data - vmin) / vrange; + + // Quantize and saturate + uchar4 res = convert_uchar4_sat(data * 256.0f); + + // Store result + vstore4(res, 0, (__global uchar *)output.ptr); +} diff --git a/src/core/CL/kernels/CLDequantizationLayerKernel.cpp b/src/core/CL/kernels/CLDequantizationLayerKernel.cpp new file mode 100644 index 0000000000..216fa2757e --- /dev/null +++ b/src/core/CL/kernels/CLDequantizationLayerKernel.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017 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/CLDequantizationLayerKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +using namespace arm_compute; + +CLDequantizationLayerKernel::CLDequantizationLayerKernel() + : _input(nullptr), _output(nullptr), _min_max(nullptr) +{ +} + +void CLDequantizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *min_max) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); + ARM_COMPUTE_ERROR_ON_NULLPTR(output, min_max); + ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); + + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::F32, 0); + + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + + _input = input; + _output = output; + _min_max = min_max; + + constexpr unsigned int num_elems_processed_per_iteration = 4; + + // Create kernel + _kernel = static_cast(CLKernelLibrary::get().create_kernel("dequantization_layer")); + + // Configure window + Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + AccessWindowStatic min_max_access(min_max->info(), 0, 0, 2, min_max->info()->dimension(1)); + + // Update window and padding + update_window_and_padding(win, input_access, output_access, min_max_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + + ICLKernel::configure(win); +} + +void CLDequantizationLayerKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); + + Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3); + Window slice = window_collapsed.first_slice_window_3D(); + + Window min_max_window = window; + min_max_window.set(Window::DimX, Window::Dimension(0, 0, 0)); + min_max_window.set(Window::DimY, Window::Dimension(0, _min_max->info()->dimension(1), 1)); + min_max_window.set(Window::DimZ, Window::Dimension(0, 0, 0)); + + Window min_max_slice = min_max_window.first_slice_window_1D(); + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, slice); + add_3D_tensor_argument(idx, _output, slice); + add_1D_tensor_argument(idx, _min_max, min_max_slice); + enqueue(queue, *this, slice); + } + while(window.slide_window_slice_3D(slice) && min_max_window.slide_window_slice_1D(min_max_slice)); +} diff --git a/src/core/CL/kernels/CLMinMaxLayerKernel.cpp b/src/core/CL/kernels/CLMinMaxLayerKernel.cpp new file mode 100644 index 0000000000..9b4533bd8d --- /dev/null +++ b/src/core/CL/kernels/CLMinMaxLayerKernel.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2017 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/CLMinMaxLayerKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +#include + +using namespace arm_compute; + +CLMinMaxLayerKernel::CLMinMaxLayerKernel() + : _input(nullptr), _output(nullptr) +{ +} + +void CLMinMaxLayerKernel::configure(const ICLTensor *input, ICLTensor *output) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + + TensorShape output_shape{ input->info()->tensor_shape() }; + output_shape.set(Window::DimX, 2); + output_shape.remove_dimension(1); + output_shape.remove_dimension(1); + + // Output auto initialization if not yet initialized + auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); + + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); + + _input = input; + _output = output; + + const unsigned int num_elems_processed_per_iteration = 1; + + std::set build_opts; + build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); + build_opts.emplace("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); + build_opts.emplace("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); + + // Create kernel + _kernel = static_cast(CLKernelLibrary::get().create_kernel("minmax_layer", build_opts)); + + // Configure kernel window + Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); + AccessWindowStatic output_access(output->info(), 0, 0, 2, output->info()->dimension(1)); + + update_window_and_padding(win, input_access, output_access); + + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); + + ICLKernel::configure(win); +} + +void CLMinMaxLayerKernel::reset(cl::CommandQueue &queue) +{ + _output->map(queue, true); + + Window window_output; + window_output.use_tensor_dimensions(_output->info()->tensor_shape()); + window_output.set(Window::DimX, Window::Dimension(0, 1, 1)); + window_output.collapse_if_possible(ICLKernel::window(), 1); + + Iterator output(_output, window_output); + + // Reset output + execute_window_loop(window_output, [&](const Coordinates & id) + { + auto *ptr = reinterpret_cast(output.ptr()); + ptr[0] = std::numeric_limits::max(); + ptr[1] = std::numeric_limits::min(); + }, + output); + + _output->unmap(queue); +} + +void CLMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + // Collapse min/max batches + Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3); + Window slice = window_collapsed.first_slice_window_3D(); + slice.set(Window::DimX, Window::Dimension(0, 1, 1)); + slice.set(Window::DimY, Window::Dimension(0, 1, 1)); + slice.set(Window::DimZ, Window::Dimension(0, 1, 1)); + + Window window_output; + window_output.use_tensor_dimensions(_output->info()->tensor_shape()); + window_output.set(Window::DimX, Window::Dimension(0, 1, 1)); + window_output.collapse_if_possible(ICLKernel::window(), 1); + + Window output_slice = window_output.first_slice_window_1D(); + + do + { + unsigned int idx = 0; + // Set inputs + add_3D_tensor_argument(idx, _input, slice); + add_1D_tensor_argument(idx, _output, output_slice); + enqueue(queue, *this, slice); + } + while(window.slide_window_slice_3D(slice) && window_output.slide_window_slice_1D(output_slice)); +} diff --git a/src/core/CL/kernels/CLQuantizationLayerKernel.cpp b/src/core/CL/kernels/CLQuantizationLayerKernel.cpp new file mode 100644 index 0000000000..47564436a9 --- /dev/null +++ b/src/core/CL/kernels/CLQuantizationLayerKernel.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017 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/CLQuantizationLayerKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +using namespace arm_compute; + +CLQuantizationLayerKernel::CLQuantizationLayerKernel() + : _input(nullptr), _output(nullptr), _min_max(nullptr) +{ +} + +void CLQuantizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, ICLTensor *min_max) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(output, min_max); + ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); + + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::U8, 0); + + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + + _input = input; + _output = output; + _min_max = min_max; + + constexpr unsigned int num_elems_processed_per_iteration = 4; + + // Create kernel + _kernel = static_cast(CLKernelLibrary::get().create_kernel("quantization_layer")); + + // Configure window + Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + AccessWindowStatic min_max_access(min_max->info(), 0, 0, 2, min_max->info()->dimension(1)); + + // Update window and padding + update_window_and_padding(win, input_access, output_access, min_max_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + + ICLKernel::configure(win); +} + +void CLQuantizationLayerKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); + + Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3); + Window slice = window_collapsed.first_slice_window_3D(); + + Window window_min_max; + window_min_max.use_tensor_dimensions(_min_max->info()->tensor_shape()); + window_min_max.set(Window::DimX, Window::Dimension(0, 1, 1)); + window_min_max.collapse_if_possible(ICLKernel::window(), 1); + + Window slice_min_max = window_min_max.first_slice_window_1D(); + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, slice); + add_3D_tensor_argument(idx, _output, slice); + add_1D_tensor_argument(idx, _min_max, slice_min_max); + enqueue(queue, *this, slice); + } + while(window.slide_window_slice_3D(slice) && window_min_max.slide_window_slice_1D(slice_min_max)); +} diff --git a/src/runtime/CL/functions/CLDequantizationLayer.cpp b/src/runtime/CL/functions/CLDequantizationLayer.cpp new file mode 100644 index 0000000000..5559d42c7f --- /dev/null +++ b/src/runtime/CL/functions/CLDequantizationLayer.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2017 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/CLDequantizationLayer.h" + +#include "arm_compute/runtime/CL/CLScheduler.h" + +using namespace arm_compute; + +CLDequantizationLayer::CLDequantizationLayer() + : _dequantize_kernel() +{ +} + +void CLDequantizationLayer::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *min_max) +{ + _dequantize_kernel.configure(input, output, min_max); +} + +void CLDequantizationLayer::run() +{ + // Run dequantization kernel + CLScheduler::get().enqueue(_dequantize_kernel, false); +} diff --git a/src/runtime/CL/functions/CLQuantizationLayer.cpp b/src/runtime/CL/functions/CLQuantizationLayer.cpp new file mode 100644 index 0000000000..ed1f51c714 --- /dev/null +++ b/src/runtime/CL/functions/CLQuantizationLayer.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2017 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/CLQuantizationLayer.h" + +#include "arm_compute/runtime/CL/CLScheduler.h" + +using namespace arm_compute; + +CLQuantizationLayer::CLQuantizationLayer() + : _quantize_kernel(), _min_max_kernel(), _min_max() +{ +} + +void CLQuantizationLayer::configure(const ICLTensor *input, ICLTensor *output) +{ + // Configure min-max kernel. _min_max tensor will be auto-configured within the kernel. + _min_max_kernel.configure(input, &_min_max); + + // Configure quantize kernel + _quantize_kernel.configure(input, output, &_min_max); + + // Allocate min_max tensor + _min_max.allocator()->allocate(); +} + +void CLQuantizationLayer::run() +{ + cl::CommandQueue q = CLScheduler::get().queue(); + + // Reset min and max + _min_max_kernel.reset(q); + + // Run min-max kernel + CLScheduler::get().enqueue(_min_max_kernel, false); + + // Run quantize kernel + CLScheduler::get().enqueue(_quantize_kernel, false); +} diff --git a/tests/validation/CL/DequantizationLayer.cpp b/tests/validation/CL/DequantizationLayer.cpp new file mode 100644 index 0000000000..8e1ba60cf6 --- /dev/null +++ b/tests/validation/CL/DequantizationLayer.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2017 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/CLDequantizationLayer.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/DequantizationLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +const auto DequantizationShapes = concat(concat(concat(datasets::Small3DShapes(), + datasets::Large3DShapes()), + datasets::Small4DShapes()), + datasets::Large4DShapes()); +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(DequantizationLayer) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(DequantizationShapes, framework::dataset::make("DataType", DataType::U8)), shape, data_type) +{ + TensorShape shape_min_max = shape; + shape_min_max.set(Window::DimX, 2); + + // Remove Y and Z dimensions and keep the batches + shape_min_max.remove_dimension(1); + shape_min_max.remove_dimension(1); + + // Create tensors + CLTensor src = create_tensor(shape, data_type); + CLTensor dst = create_tensor(shape, DataType::F32); + CLTensor min_max = create_tensor(shape_min_max, DataType::F32); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(min_max.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + CLDequantizationLayer dequant_layer; + dequant_layer.configure(&src, &dst, &min_max); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(src.info()->valid_region(), valid_region); + validate(dst.info()->valid_region(), valid_region); + + // Validate valid region of min_max tensor + const ValidRegion valid_region_min_max = shape_to_valid_region(shape_min_max); + validate(min_max.info()->valid_region(), valid_region_min_max); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 4).required_padding(); + validate(src.info()->padding(), padding); + validate(dst.info()->padding(), padding); + + // Validate padding of min_max tensor + const PaddingSize padding_min_max = PaddingCalculator(shape_min_max.x(), 2).required_padding(); + validate(min_max.info()->padding(), padding_min_max); +} + +template +using CLDequantizationLayerFixture = DequantizationValidationFixture; + +TEST_SUITE(Integer) +TEST_SUITE(U8) +FIXTURE_DATA_TEST_CASE(RunSmall, CLDequantizationLayerFixture, framework::DatasetMode::PRECOMMIT, combine(concat(datasets::Small3DShapes(), datasets::Small4DShapes()), + framework::dataset::make("DataType", DataType::U8))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLDequantizationLayerFixture, framework::DatasetMode::NIGHTLY, combine(concat(datasets::Large3DShapes(), datasets::Large4DShapes()), + framework::dataset::make("DataType", DataType::U8))) +{ + // Validate output + validate(CLAccessor(_target), _reference); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/CL/QuantizationLayer.cpp b/tests/validation/CL/QuantizationLayer.cpp new file mode 100644 index 0000000000..0747fe9da3 --- /dev/null +++ b/tests/validation/CL/QuantizationLayer.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2017 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/CLQuantizationLayer.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/QuantizationLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +constexpr AbsoluteTolerance tolerance_f32(1.0f); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */ +const auto QuantizationShapes = concat(concat(concat(datasets::Small3DShapes(), + datasets::Large3DShapes()), + datasets::Small4DShapes()), + datasets::Large4DShapes()); +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(QuantizationLayer) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(QuantizationShapes, framework::dataset::make("DataType", DataType::F32)), shape, data_type) +{ + // Create tensors + CLTensor src = create_tensor(shape, data_type); + CLTensor dst = create_tensor(shape, DataType::U8); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + CLQuantizationLayer quant_layer; + quant_layer.configure(&src, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(src.info()->valid_region(), valid_region); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 4).required_padding(); + validate(src.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using CLQuantizationLayerFixture = QuantizationValidationFixture; + +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLQuantizationLayerFixture, framework::DatasetMode::PRECOMMIT, combine(concat(datasets::Small3DShapes(), datasets::Small4DShapes()), + framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLQuantizationLayerFixture, framework::DatasetMode::NIGHTLY, combine(concat(datasets::Large3DShapes(), datasets::Large4DShapes()), + framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/CPP/QuantizationLayer.cpp b/tests/validation/CPP/QuantizationLayer.cpp index 0584d88a37..6909da927e 100644 --- a/tests/validation/CPP/QuantizationLayer.cpp +++ b/tests/validation/CPP/QuantizationLayer.cpp @@ -31,29 +31,6 @@ namespace validation { namespace reference { -void compute_min_max(const SimpleTensor &src, float *min, float *max) -{ - // Set min and max to first pixel - float tmp_min = src[0]; - float tmp_max = src[0]; - - // Look for min and max values - for(int i = 1; i < src.num_elements(); ++i) - { - if(src[i] < tmp_min) - { - tmp_min = src[i]; - } - if(src[i] > tmp_max) - { - tmp_max = src[i]; - } - } - - *min = tmp_min; - *max = tmp_max; -} - template ::value, int>::type> SimpleTensor quantization_layer(const SimpleTensor &src) { -- cgit v1.2.1