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 --- 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 +++++++++ 9 files changed, 707 insertions(+) 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 (limited to 'src') 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); +} -- cgit v1.2.1