From 7068f9900d136312318ff430aef588b14e0c87ad Mon Sep 17 00:00:00 2001 From: Anthony Barbier Date: Thu, 26 Oct 2017 15:23:08 +0100 Subject: COMPMID-631: Merge branches/gles_compute branch Last commit: commit b25c5f68042b0c81bf611d59a1bb8535e1c42497 Author: Xinghang Zhou Date: Wed Oct 25 18:48:10 2017 +0800 Synced validation's tolerances of GCSoftmax from cl side Change-Id: Ibe72054205c1c8721845d679a31af7ed0a7c5cf6 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/93283 Reviewed-by: Anthony Barbier Tested-by: Kaizen --- .../kernels/GCAbsoluteDifferenceKernel.cpp | 112 ++++++ .../kernels/GCActivationLayerKernel.cpp | 128 +++++++ .../kernels/GCBatchNormalizationLayerKernel.cpp | 129 +++++++ src/core/GLES_COMPUTE/kernels/GCCol2ImKernel.cpp | 101 ++++++ .../kernels/GCDepthConcatenateKernel.cpp | 145 ++++++++ .../kernels/GCDirectConvolutionLayerKernel.cpp | 394 +++++++++++++++++++++ src/core/GLES_COMPUTE/kernels/GCDropoutKernel.cpp | 110 ++++++ .../GLES_COMPUTE/kernels/GCFillBorderKernel.cpp | 169 +++++++++ .../kernels/GCGEMMInterleave4x4Kernel.cpp | 129 +++++++ .../kernels/GCGEMMMatrixAccumulateBiasesKernel.cpp | 123 +++++++ .../kernels/GCGEMMMatrixAdditionKernel.cpp | 104 ++++++ .../kernels/GCGEMMMatrixMultiplyKernel.cpp | 210 +++++++++++ .../kernels/GCGEMMTranspose1xWKernel.cpp | 128 +++++++ src/core/GLES_COMPUTE/kernels/GCIm2ColKernel.cpp | 230 ++++++++++++ .../kernels/GCNormalizationLayerKernel.cpp | 124 +++++++ .../kernels/GCPixelWiseMultiplicationKernel.cpp | 127 +++++++ .../GLES_COMPUTE/kernels/GCPoolingLayerKernel.cpp | 254 +++++++++++++ .../GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.cpp | 353 ++++++++++++++++++ .../GLES_COMPUTE/kernels/GCTransposeKernel.cpp | 116 ++++++ 19 files changed, 3186 insertions(+) create mode 100644 src/core/GLES_COMPUTE/kernels/GCAbsoluteDifferenceKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCActivationLayerKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCBatchNormalizationLayerKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCCol2ImKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCDepthConcatenateKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCDirectConvolutionLayerKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCDropoutKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCFillBorderKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCGEMMMatrixAccumulateBiasesKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCIm2ColKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCNormalizationLayerKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCPixelWiseMultiplicationKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCPoolingLayerKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.cpp create mode 100644 src/core/GLES_COMPUTE/kernels/GCTransposeKernel.cpp (limited to 'src/core/GLES_COMPUTE/kernels') diff --git a/src/core/GLES_COMPUTE/kernels/GCAbsoluteDifferenceKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCAbsoluteDifferenceKernel.cpp new file mode 100644 index 0000000000..d76ae8ff1c --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCAbsoluteDifferenceKernel.cpp @@ -0,0 +1,112 @@ +/* + * 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/GLES_COMPUTE/kernels/GCAbsoluteDifferenceKernel.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" +#include "support/ToolchainSupport.h" + +#include +#include + +using namespace arm_compute; + +GCAbsoluteDifferenceKernel::GCAbsoluteDifferenceKernel() + : _input1(nullptr), _input2(nullptr), _output(nullptr) +{ +} + +void GCAbsoluteDifferenceKernel::configure(const IGCTensor *input1, const IGCTensor *input2, IGCTensor *output) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2, output); + + _input1 = input1; + _input2 = input2; + _output = output; + + constexpr unsigned int num_elems_processed_per_iteration = 4; + + // Set kernel build options + std::set build_opts; + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("absdiff", build_opts)); + + // Configure kernel window + Window win = calculate_max_window(*input1->info(), Steps(num_elems_processed_per_iteration)); + + AccessWindowRectangle input1_access(input1->info(), 0, 0, 4, 1); + AccessWindowRectangle input2_access(input2->info(), 0, 0, 4, 1); + AccessWindowRectangle output_access(output->info(), 0, 0, 4, 1); + + update_window_and_padding(win, input1_access, input2_access, output_access); + + ValidRegion valid_region = intersect_valid_regions(input1->info()->valid_region(), + input2->info()->valid_region()); + + output_access.set_valid_region(win, valid_region); + + _kernel.clear_params(); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCAbsoluteDifferenceKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_2D(); + do + { + unsigned int idx = 0; + unsigned int binding = 1; // SSBO binding starts from 1. + add_2D_tensor_argument(idx, _input1, binding++, slice); + add_2D_tensor_argument(idx, _input2, binding++, slice); + add_2D_tensor_argument(idx, _output, binding++, slice); + + _kernel.update_shader_params(); + + enqueue(*this, slice); + } + while(window.slide_window_slice_2D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCActivationLayerKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCActivationLayerKernel.cpp new file mode 100644 index 0000000000..42433cf076 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCActivationLayerKernel.cpp @@ -0,0 +1,128 @@ +/* + * 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/GLES_COMPUTE/kernels/GCActivationLayerKernel.h" + +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/IAccessWindow.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" +#include "support/ToolchainSupport.h" + +#include +#include + +using namespace arm_compute; + +GCActivationLayerKernel::GCActivationLayerKernel() + : _input(nullptr), _output(nullptr) +{ +} + +void GCActivationLayerKernel::configure(IGCTensor *input, IGCTensor *output, ActivationLayerInfo act_info) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + + // Make sure _kernel is initialized before calling the parent's configure + _input = input; + _output = input; + + if(output != nullptr) + { + // Output auto inizialitation if not yet initialized + auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); + + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); + + _output = output; + } + + unsigned int num_elems_processed_per_iteration = 4 / input->info()->element_size(); + + // Set build options + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace(("#define " + string_from_activation_func(act_info.activation()))); + build_opts.emplace(("#define " + dt_name)); + build_opts.emplace(("#define A_VAL " + float_to_string_with_full_precision(act_info.a()))); + build_opts.emplace(("#define B_VAL " + float_to_string_with_full_precision(act_info.b()))); + build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1))); + build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1))); + build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1))); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("activation_layer", build_opts)); + + // Configure kernel window + Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); + + if(output != nullptr) + { + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + + update_window_and_padding(win, + AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration), + output_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + } + else + { + update_window_and_padding(win, + AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration)); + } + + _kernel.clear_params(); + + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCActivationLayerKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_3D(); + + do + { + unsigned int idx = 0; + unsigned int binding = 1; + add_3D_tensor_argument(idx, _input, binding++, slice); + add_3D_tensor_argument(idx, _output, binding++, slice); + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCBatchNormalizationLayerKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCBatchNormalizationLayerKernel.cpp new file mode 100644 index 0000000000..9c24d2ef42 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCBatchNormalizationLayerKernel.cpp @@ -0,0 +1,129 @@ +/* + * 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/GLES_COMPUTE/kernels/GCBatchNormalizationLayerKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +GCBatchNormalizationLayerKernel::GCBatchNormalizationLayerKernel() + : _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _beta(nullptr), _gamma(nullptr), _epsilon(0.0f) +{ +} + +void GCBatchNormalizationLayerKernel::configure(const IGCTensor *input, IGCTensor *output, const IGCTensor *mean, const IGCTensor *var, const IGCTensor *beta, const IGCTensor *gamma, + float epsilon) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + + // Output tensor auto initialization if not yet initialized + auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); + + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, mean, var, beta, gamma); + ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output, mean, var, beta, gamma); + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(mean, var, beta, gamma); + ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != mean->info()->dimension(0)); + + _input = input; + _output = output; + _mean = mean; + _var = var; + _beta = beta; + _gamma = gamma; + _epsilon = epsilon; + + const unsigned int num_elems_processed_per_iteration = 4 / input->info()->element_size(); + + // Set build options + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace(("#define " + dt_name)); + build_opts.emplace(("#define ESPILON " + float_to_string_with_full_precision(_epsilon))); + build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1))); + build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1))); + build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1))); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("batchnormalization_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); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + AccessWindowStatic mean_access(mean->info(), 0, 0, mean->info()->dimension(0) + 1, mean->info()->dimension(1)); + AccessWindowStatic var_access(var->info(), 0, 0, var->info()->dimension(0) + 1, var->info()->dimension(1)); + AccessWindowStatic beta_access(beta->info(), 0, 0, beta->info()->dimension(0) + 1, beta->info()->dimension(1)); + AccessWindowStatic gamma_access(gamma->info(), 0, 0, gamma->info()->dimension(0) + 1, gamma->info()->dimension(1)); + + update_window_and_padding(win, input_access, output_access, mean_access, var_access, beta_access, gamma_access); + output_access.set_valid_region(win, input->info()->valid_region()); + + _kernel.clear_params(); + + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCBatchNormalizationLayerKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_3D(); + + Window vector_slice = window.first_slice_window_1D(); + vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); + + unsigned int idx = 2 * num_arguments_per_3D_tensor(); + add_1D_tensor_argument(idx, _mean, 3, vector_slice); + add_1D_tensor_argument(idx, _var, 4, vector_slice); + add_1D_tensor_argument(idx, _beta, 5, vector_slice); + add_1D_tensor_argument(idx, _gamma, 6, vector_slice); + + do + { + idx = 0; + add_3D_tensor_argument(idx, _input, 1, slice); + add_3D_tensor_argument(idx, _output, 2, slice); + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCCol2ImKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCCol2ImKernel.cpp new file mode 100644 index 0000000000..10716232c9 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCCol2ImKernel.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/GLES_COMPUTE/kernels/GCCol2ImKernel.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" + +using namespace arm_compute; + +GCCol2ImKernel::GCCol2ImKernel() + : _input(nullptr), _output(nullptr), _convolved_dims() +{ +} + +void GCCol2ImKernel::configure(const IGCTensor *input, IGCTensor *output, + std::pair convolved_dims) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + + _kernel.clear_params(); + + _input = input; + _output = output; + _convolved_dims = convolved_dims; + + // Create kernel + std::set build_opts; + constexpr unsigned int num_elems_processed_per_iteration = 8; + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(num_elems_processed_per_iteration)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(num_elems_processed_per_iteration)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.insert("#define COL2IM"); + _kernel = static_cast(GCKernelLibrary::get().create_kernel("col2im", build_opts)); + + // Set static kernel arguments + unsigned int idx = num_arguments_per_2D_tensor() + num_arguments_per_3D_tensor(); + _kernel.set_params(idx++, _convolved_dims.first); + + // Configure window + Window win = calculate_max_window(*input->info(), Steps()); + + // The GCCol2ImKernel doesn't need padding so update_window_and_padding() can be skipped + output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCCol2ImKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); + + Window slice_in = window.first_slice_window_2D(); + Window slice_out = window.first_slice_window_3D(); + + _kernel.use(); + + do + { + // Set inputs + unsigned int idx = 0; + unsigned int binding = 1; + add_2D_tensor_argument(idx, _input, binding++, slice_in); + add_3D_tensor_argument(idx, _output, binding++, slice_out); + _kernel.update_shader_params(); + enqueue(*this, slice_in); + } + while(window.slide_window_slice_2D(slice_in) && window.slide_window_slice_3D(slice_out)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCDepthConcatenateKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCDepthConcatenateKernel.cpp new file mode 100644 index 0000000000..7f9f438a46 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCDepthConcatenateKernel.cpp @@ -0,0 +1,145 @@ +/* + * 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/GLES_COMPUTE/kernels/GCDepthConcatenateKernel.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +GCDepthConcatenateKernel::GCDepthConcatenateKernel() + : _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0) +{ +} + +BorderSize GCDepthConcatenateKernel::border_size() const +{ + return BorderSize(_top_bottom, _left_right); +} + +void GCDepthConcatenateKernel::configure(const IGCTensor *input, unsigned int depth_offset, IGCTensor *output) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) + depth_offset > output->info()->dimension(2)); + ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) > output->info()->dimension(0)); + ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) > output->info()->dimension(1)); + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(3, input, output); + + // The gaps between the two lowest dimensions of input and output need to be divisible by 2 + // Otherwise it is not clear how the padding should be added onto the input tensor + ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) - input->info()->dimension(0)) % 2); + ARM_COMPUTE_ERROR_ON((output->info()->dimension(1) - input->info()->dimension(1)) % 2); + + _input = input; + _output = output; + + // Add build options + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace(("#define " + dt_name)); + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + + // Configure kernel window + _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2; + _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2; + + const int offset_to_first_elements_in_bytes = depth_offset * output->info()->strides_in_bytes()[2]; + + build_opts.emplace("#define OFFSETS_X " + support::cpp11::to_string(_left_right)); + build_opts.emplace("#define OFFSETS_Y " + support::cpp11::to_string(_top_bottom)); + build_opts.emplace("#define OFFSETS_Z " + support::cpp11::to_string(offset_to_first_elements_in_bytes)); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("concatenate_depth", build_opts)); + + unsigned int num_elems_processed_per_iteration = 1; + unsigned int num_elems_read_per_iteration = 1; + if(input->info()->data_type() == DataType::F32) + { + num_elems_processed_per_iteration = 1; + num_elems_read_per_iteration = 1; + } + else if(input->info()->data_type() == DataType::F16) + { + num_elems_processed_per_iteration = 4; + num_elems_read_per_iteration = 4; + } + const unsigned int num_rows_read_per_iteration = 1; + + // The window needs to be based on input as we copy all the depths of input + Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); + win.set(Window::DimZ, Window::Dimension(0, input->info()->tensor_shape().z(), 1)); + + AccessWindowRectangle input_access(input->info(), -_left_right, -_top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + update_window_and_padding(win, input_access, output_access); + output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape())); + + _kernel.clear_params(); + _kernel.set_shader_params_binding_point(0); + IGCKernel::configure(win); +} + +void GCDepthConcatenateKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_3D(); + + do + { + if(_input->info()->data_type() == DataType::F32) + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, 1, slice); + add_3D_tensor_argument(idx, _output, 2, slice); + } + else if(_input->info()->data_type() == DataType::F16) + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, BufferParam(1, 3), slice); + add_3D_tensor_argument(idx, _output, BufferParam(2, 3), slice); + } + + _kernel.update_shader_params(); + + enqueue(*this, slice); + } + while(window.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCDirectConvolutionLayerKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCDirectConvolutionLayerKernel.cpp new file mode 100644 index 0000000000..1fa2a71fff --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCDirectConvolutionLayerKernel.cpp @@ -0,0 +1,394 @@ +/* + * 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/GLES_COMPUTE/kernels/GCDirectConvolutionLayerKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/IAccessWindow.h" +#include "arm_compute/core/ITensor.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +template +GCDirectConvolutionLayerKernel::GCDirectConvolutionLayerKernel() + : _input(nullptr), _bias(nullptr), _weights(nullptr), _output(nullptr), _border_size(0), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_x(0), _conv_pad_y(0), _lws(gles::NDRange(1U, 1U, 1U)) +{ +} + +template +BorderSize GCDirectConvolutionLayerKernel::border_size() const +{ + return _border_size; +} + +template +void GCDirectConvolutionLayerKernel::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *bias, IGCTensor *output, const PadStrideInfo &conv_info) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); + ARM_COMPUTE_ERROR_ON(weights->info()->dimension(2) != input->info()->dimension(2)); + ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != weights->info()->dimension(1)); + ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); + ARM_COMPUTE_ERROR_ON_MSG((kernel_size == 3 && std::get<0>(conv_info.stride()) > 2), "Strides larger than 2 not supported in 3x3 direct convolution!"); + ARM_COMPUTE_ERROR_ON(kernel_size != weights->info()->dimension(0)); + + if(bias != nullptr) + { + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, bias); + // FIXME: Bug in framework, workaround it in tests currently. + //ARM_COMPUTE_ERROR_ON(bias->info()->dimension(0) != weights->info()->dimension(3)); + ARM_COMPUTE_ERROR_ON(bias->info()->num_dimensions() > 1); + } + + _conv_stride_x = std::get<0>(conv_info.stride()); + _conv_stride_y = std::get<1>(conv_info.stride()); + _conv_pad_x = std::get<0>(conv_info.pad()); + _conv_pad_y = std::get<1>(conv_info.pad()); + + _input = input; + _weights = weights; + _output = output; + _bias = bias; + _border_size = BorderSize(_conv_pad_y, _conv_pad_x); + + std::set options; + + options.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(_lws[0])); + options.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(_lws[1])); + options.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(_lws[2])); + options.emplace("#define STRIDE_X " + support::cpp11::to_string(_conv_stride_x)); + + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + options.emplace(("#define " + dt_name)); + + unsigned int num_elems_read_per_iteration_x = kernel_size * _conv_stride_x; + unsigned int num_elems_read_per_iteration_y = 1; + unsigned int num_elems_written_per_iteration_x = 1; + unsigned int num_elems_written_per_iteration_y = 1; + unsigned int num_elems_written_per_iteration_z = 1; + + if(kernel_size == 3) + { + if((_conv_stride_x == 1) && (_conv_stride_y == 1)) + { + switch(input->info()->data_type()) + { + // TODO(APPBROWSER-299): Choose the most optimal path and remove others. +#define PROCESS_X_4ELEMENTS_Y_3ELEMENTS_FP16 + + case DataType::F16: +#if defined(PROCESS_X_8ELEMENTS_Y_3ELEMENTS_FP16) + options.emplace("#define PROCESS_X_8ELEMENTS_Y_3ELEMENTS_FP16"); + num_elems_read_per_iteration_x = 16; + num_elems_read_per_iteration_y = 5; + num_elems_written_per_iteration_x = 8; + num_elems_written_per_iteration_y = 3; +#elif defined(PROCESS_X_4ELEMENTS_Y_3ELEMENTS_FP16) + options.emplace("#define PROCESS_X_4ELEMENTS_Y_3ELEMENTS_FP16"); + num_elems_read_per_iteration_x = 8; + num_elems_read_per_iteration_y = 5; + num_elems_written_per_iteration_x = 4; + num_elems_written_per_iteration_y = 3; +#elif defined(PROCESS_X_4ELEMENTS_Y_4ELEMENTS_FP16) + options.emplace("#define PROCESS_X_4ELEMENTS_Y_4ELEMENTS_FP16"); + num_elems_read_per_iteration_x = 8; + num_elems_read_per_iteration_y = 6; + num_elems_written_per_iteration_x = 4; + num_elems_written_per_iteration_y = 4; +#elif defined(PROCESS_X_4ELEMENTS_Y_3ELEMENTS_Z_2ELEMENTS_FP16) + options.emplace("#define PROCESS_X_4ELEMENTS_Y_3ELEMENTS_Z_2ELEMENTS_FP16"); + num_elems_read_per_iteration_x = 8; + num_elems_read_per_iteration_y = 5; + num_elems_written_per_iteration_x = 4; + num_elems_written_per_iteration_y = 3; + num_elems_written_per_iteration_z = 2; +#endif /* PROCESS_X_8ELEMENTS_Y_3ELEMENTS_FP16 */ + break; + + case DataType::F32: + options.emplace("#define PROCESS_X_4ELEMENTS_Y_3ELEMENTS"); + num_elems_read_per_iteration_x = 8; + num_elems_read_per_iteration_y = 5; + num_elems_written_per_iteration_x = 4; + num_elems_written_per_iteration_y = 3; + break; + + default: + ARM_COMPUTE_ERROR("Current data type is not supported"); + break; + } + } + // FIXME: Just keep one in release + else + { + switch(input->info()->data_type()) + { + case DataType::F16: + options.emplace("#define PROCESS_X_4ELEMENTS_FP16"); + num_elems_read_per_iteration_x = 8; + num_elems_written_per_iteration_x = 4; + break; + + case DataType::F32: + // TODO(APPBROWSER-299): Choose the most optimal path and remove others. +#define PROCESS_4_ELEMENT + +#if defined(PROCESS_1_ELEMENT) + options.emplace("#define PROCESS_1_ELEMENT"); + num_elems_read_per_iteration_x = 3; + num_elems_written_per_iteration_x = 1; +#elif defined(PROCESS_4_ELEMENT) + options.emplace("#define PROCESS_4_ELEMENT"); + num_elems_read_per_iteration_x = 8; + num_elems_written_per_iteration_x = 4; +#elif defined(PROCESS_8_ELEMENT) + options.emplace("#define PROCESS_8_ELEMENT"); + num_elems_read_per_iteration_x = 12; + num_elems_written_per_iteration_x = 8; +#else /* PROCESS_1_ELEMENT */ +#error Have to declare how many elements to process in one thread. +#endif /* PROCESS_1_ELEMENT */ + break; + + default: + ARM_COMPUTE_ERROR("Current data type is not supported"); + break; + } + } + } + else if(kernel_size == 1) + { + switch(input->info()->data_type()) + { + case DataType::F16: + num_elems_read_per_iteration_x = 8; + num_elems_written_per_iteration_x = 8; + break; + + case DataType::F32: + num_elems_read_per_iteration_x = 1; + num_elems_written_per_iteration_x = 1; + break; + + default: + break; + } + } + else if(kernel_size == 5) + { + switch(input->info()->data_type()) + { + case DataType::F16: + num_elems_read_per_iteration_x = 8; + num_elems_written_per_iteration_x = 4; + + default: + break; + } + } + else + { + } + + if(_bias != nullptr) + { + options.emplace("#define BIAS"); + } + + std::stringstream kernel_name; + kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size; + + _kernel = static_cast(GCKernelLibrary::get().create_kernel(kernel_name.str(), options)); + + _kernel.clear_params(); + + unsigned int idx = (_bias == nullptr) ? 3 * num_arguments_per_3D_tensor() : (num_arguments_per_1D_tensor() + 3 * num_arguments_per_3D_tensor()); + + // Calculate output right and bottom border + const int output_width = output->info()->dimension(0); + const int output_height = output->info()->dimension(1); + const int output_padding_right = ceil_to_multiple(output_width, num_elems_written_per_iteration_x * _lws[0]) - output_width; + const int output_padding_bottom = ceil_to_multiple(output_height, num_elems_written_per_iteration_y * _lws[1]) - output_height; + + // Calculate input right and bottom border + const int input_width = input->info()->dimension(0); + const int input_height = input->info()->dimension(1); + const int upper_bound_w = ceil_to_multiple(((output_width + output_padding_right) * _conv_stride_x + (kernel_size - 1)), num_elems_read_per_iteration_x * _lws[0]) - _conv_pad_x - input_width; + const int upper_bound_h = ceil_to_multiple(((output_height + output_padding_bottom) * _conv_stride_y + (kernel_size - 1)), num_elems_read_per_iteration_y * _lws[1]) - _conv_pad_y - input_height; + const int padding_right = std::max(upper_bound_w, _conv_pad_x); + const int padding_bottom = std::max(upper_bound_h, _conv_pad_y); + + BorderSize border = BorderSize(0, output_padding_right, output_padding_bottom, 0); + + Window win = calculate_max_enlarged_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y, num_elems_written_per_iteration_z), border); + + AccessWindowStatic input_access(input->info(), -_conv_pad_x, -_conv_pad_y, input_width + padding_right, input_height + padding_bottom); + AccessWindowStatic weights_access = AccessWindowStatic(nullptr, 0, 0, 0, 0); + AccessWindowStatic bias_access = AccessWindowStatic(nullptr, 0, 0, 0, 1); + + switch(weights->info()->data_type()) + { + case DataType::F16: + weights_access = AccessWindowStatic(weights->info(), 0, 0, kernel_size + 1, kernel_size); + if(_bias != nullptr) + { + bias_access = AccessWindowStatic(_bias->info(), 0, 0, _bias->info()->dimension(0) + 1, 1); + } + break; + + case DataType::F32: + weights_access = AccessWindowStatic(weights->info(), 0, 0, kernel_size, kernel_size); + if(_bias != nullptr) + { + bias_access = AccessWindowStatic(_bias->info(), 0, 0, _bias->info()->dimension(0), 1); + } + break; + + default: + ARM_COMPUTE_ERROR("Current data type is not supported"); + break; + } + + AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height + output_padding_bottom); + + if(_bias != nullptr) + { + update_window_and_padding(win, input_access, weights_access, bias_access, output_access); + } + else + { + update_window_and_padding(win, input_access, weights_access, output_access); + } + + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); + + _kernel.set_params(idx++, _weights->info()->strides_in_bytes()[3]); // weights_stride_w + _kernel.set_params(idx++, _weights->info()->dimension(2)); // weights_depth + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +template +void GCDirectConvolutionLayerKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + _kernel.use(); + + // Get initial windows + Window slice = window.first_slice_window_3D(); + Window win_in = window; + + win_in.adjust(Window::DimX, -_conv_pad_x, true); + win_in.adjust(Window::DimY, -_conv_pad_y, true); + win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x); + win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y); + + Window slice_in = win_in.first_slice_window_3D(); + + unsigned int idx1 = 2 * num_arguments_per_3D_tensor(); + add_3D_tensor_argument(idx1, _weights, BufferParam(3, 2), slice); + + if(_bias != nullptr) + { + Window slice_bias; + slice_bias.use_tensor_dimensions(_bias->info()->tensor_shape()); + add_1D_tensor_argument(idx1, _bias, BufferParam(4, 2), slice_bias); + } + + do + { + unsigned int idx = 0; + + switch(_input->info()->data_type()) + { + case DataType::F16: + switch(kernel_size) + { + case 1: + add_3D_tensor_argument(idx, _input, BufferParam(1, 4), slice_in); + add_3D_tensor_argument(idx, _output, BufferParam(2, 4), slice); + break; + + case 3: + add_3D_tensor_argument(idx, _input, BufferParam(1, 3), slice_in); + add_3D_tensor_argument(idx, _output, BufferParam(2, 3), slice); + break; + + case 5: + add_3D_tensor_argument(idx, _input, BufferParam(1, 3), slice_in); + add_3D_tensor_argument(idx, _output, BufferParam(2, 3), slice); + break; + + default: + ARM_COMPUTE_ERROR("Current kernel size %d is not supported", kernel_size); + break; + } + break; + + case DataType::F32: + switch(kernel_size) + { + case 1: + case 5: + add_3D_tensor_argument(idx, _input, BufferParam(1, 2), slice_in); + add_3D_tensor_argument(idx, _output, BufferParam(2, 2), slice); + break; + + case 3: + add_3D_tensor_argument(idx, _input, BufferParam(1, 4), slice_in); + add_3D_tensor_argument(idx, _output, BufferParam(2, 4), slice); + break; + + default: + ARM_COMPUTE_ERROR("Current kernel size %d is not supported", kernel_size); + break; + } + break; + + default: + ARM_COMPUTE_ERROR("Current data type is not supported"); + break; + } + + _kernel.update_shader_params(); + enqueue(*this, slice, _lws); + } + while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in)); +} + +template class arm_compute::GCDirectConvolutionLayerKernel<1>; +template class arm_compute::GCDirectConvolutionLayerKernel<3>; +template class arm_compute::GCDirectConvolutionLayerKernel<5>; diff --git a/src/core/GLES_COMPUTE/kernels/GCDropoutKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCDropoutKernel.cpp new file mode 100644 index 0000000000..6244fbef80 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCDropoutKernel.cpp @@ -0,0 +1,110 @@ +/* + * 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/GLES_COMPUTE/kernels/GCDropoutKernel.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "support/ToolchainSupport.h" + +#include +#include +#include + +using namespace arm_compute; + +GCDropoutKernel::GCDropoutKernel() + : _input(nullptr), _mask(nullptr), _output(nullptr), _num_elems_processed_per_iteration(0) +{ +} + +void GCDropoutKernel::configure(const IGCTensor *input, IGCTensor *mask, IGCTensor *output, float ratio, bool forward) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, mask, output); + + _input = input; + _mask = mask; + _output = output; + _kernel.clear_params(); + + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + std::string fporbp = forward ? "FORWARD" : "BACKWARD"; + std::random_device rd; + std::mt19937 mt(rd()); + std::uniform_real_distribution dist(0.f, 1.f); + + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.emplace("#define RATIO " + support::cpp11::to_string(ratio)); + build_opts.emplace("#define SCALE " + support::cpp11::to_string(1. / (1. - ratio))); + build_opts.emplace("#define SEED " + support::cpp11::to_string(dist(mt))); + build_opts.emplace("#define " + dt_name); + build_opts.emplace("#define " + fporbp); + + _num_elems_processed_per_iteration = 4 / input->info()->element_size(); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("dropout", build_opts)); + + // Configure kernel window + Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration)); + + output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + IGCKernel::configure(win); +} + +void GCDropoutKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_3D(); + + do + { + unsigned int idx = 0; + + add_3D_tensor_argument(idx, _input, BufferParam(1, 2), slice); + add_3D_tensor_argument(idx, _mask, BufferParam(2, 2), slice); + add_3D_tensor_argument(idx, _output, BufferParam(3, 2), slice); + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCFillBorderKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCFillBorderKernel.cpp new file mode 100644 index 0000000000..36742ef81e --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCFillBorderKernel.cpp @@ -0,0 +1,169 @@ +/* + * 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/GLES_COMPUTE/kernels/GCFillBorderKernel.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.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" + +#include +#include +#include + +using namespace arm_compute; + +GCFillBorderKernel::GCFillBorderKernel() + : IGCKernel(), _tensor(nullptr) +{ +} + +bool GCFillBorderKernel::is_parallelisable() const +{ + return false; +} + +template +void GCFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value) +{ + T value; + constant_border_value.get(value); + _kernel.set_params(idx, static_cast(value)); +} + +void GCFillBorderKernel::configure(const IGCTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value) +{ + ARM_COMPUTE_ERROR_ON(tensor == nullptr); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(tensor, 1, DataType::F32, DataType::F16); + ARM_COMPUTE_ERROR_ON(tensor->info()->num_channels() != 1); + + border_size.limit(tensor->info()->padding()); + + // If there is no border: early exit + if(border_size.empty() || border_mode == BorderMode::UNDEFINED) + { + return; + } + + // Select appropriate kernel + std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode)); + + // Define build options + std::set build_opts; + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.emplace("#define BORDER_SIZE_TOP " + support::cpp11::to_string(border_size.top)); + build_opts.emplace("#define BORDER_SIZE_BOTTOM " + support::cpp11::to_string(border_size.bottom)); + build_opts.emplace("#define BORDER_SIZE_LEFT " + support::cpp11::to_string(border_size.left)); + build_opts.emplace("#define BORDER_SIZE_RIGHT " + support::cpp11::to_string(border_size.right)); + + if(border_mode == BorderMode::REPLICATE) + { + build_opts.emplace("#define FILL_IMAGE_BORDERS_REPLICATE\n"); + } + else + { + build_opts.emplace("#define FILL_IMAGE_BORDERS_CONSTANT\n"); + } + + switch(tensor->info()->data_type()) + { + case DataType::F16: + build_opts.emplace("#define DATA_TYPE_FP16"); + break; + + case DataType::F32: + build_opts.emplace("#define DATA_TYPE_FP32"); + break; + + default: + ARM_COMPUTE_ERROR("Current data type is not supported"); + break; + } + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel(kernel_name, build_opts)); + _tensor = tensor; + + _kernel.clear_params(); + + // Create static kernel arguments + const unsigned int valid_width = tensor->info()->valid_region().shape[0]; + const unsigned int valid_height = tensor->info()->valid_region().shape[1]; + const unsigned int total_valid_width = border_size.left + valid_width + border_size.right; + + // Set static kernel arguments + unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters + _kernel.set_params(idx++, valid_width); + _kernel.set_params(idx++, valid_height); + _kernel.set_params(idx++, tensor->info()->valid_region().anchor[0]); + _kernel.set_params(idx++, tensor->info()->valid_region().anchor[1]); + + if(BorderMode::CONSTANT == border_mode) + { + set_constant_border(idx++, constant_border_value); + } + + // Configure kernel window + Window win; + win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height)); + win.set(Window::DimY, Window::Dimension(0, 1, 1)); + win.use_tensor_dimensions(tensor->info()->tensor_shape(), Window::DimZ); + + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCFillBorderKernel::run(const Window &window) +{ + // Border mode undefined or border width == 0 + if(_kernel.get_program() == 0) + { + return; + } + + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); + + _kernel.use(); + Window slice = window.first_slice_window_3D(); + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _tensor, 1, slice); + + _kernel.update_shader_params(); + + enqueue(*this, slice); + } + while(window.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.cpp b/src/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.cpp new file mode 100644 index 0000000000..5e3788af99 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.cpp @@ -0,0 +1,129 @@ +/* + * 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/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +using namespace arm_compute; + +GCGEMMInterleave4x4Kernel::GCGEMMInterleave4x4Kernel() + : _input(nullptr), _output(nullptr) +{ +} + +void GCGEMMInterleave4x4Kernel::configure(const IGCTensor *input, IGCTensor *output) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + + TensorShape output_shape = input->info()->tensor_shape(); + output_shape.set(0, input->info()->dimension(0) * 4); + output_shape.set(1, std::ceil(input->info()->dimension(1) / 4.0f)); + + // Output auto inizialitation 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_DIMENSIONS(output->info()->tensor_shape(), output_shape); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + + _input = input; + _output = output; + + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace(("#define " + dt_name)); + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + + // Create kernel + build_opts.emplace("#define GEMM_INTERLEAVE4x4"); + _kernel = static_cast(GCKernelLibrary::get().create_kernel("gemm_interleave4x4", build_opts)); + + // Configure kernel window + const unsigned int num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(input->info()->data_type()); + constexpr unsigned int num_elems_processed_per_iteration_y = 4; + const unsigned int num_elems_written_per_iteration = num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y; + + Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); + + AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); + AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, 1, 4.f, 0.25f); + + update_window_and_padding(win, input_access, output_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + + _kernel.clear_params(); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCGEMMInterleave4x4Kernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + /* + * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values) + * |a00 a01 a02 a03| + * |a10 a11 a12 a13| + * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 | + * |a30 a31 a32 a33| + * + * After this operation, the output matrix will have the following shape: [ height * 4, width / 4 ] + */ + Window in_slice = window.first_slice_window_2D(); + Window out_slice = window.first_slice_window_2D(); + + // Change x and y steps for the slide of output tensor + out_slice.scale(Window::DimX, 4.f); + out_slice.scale(Window::DimY, 0.25f); + + do + { + unsigned int idx = 0; + add_2D_tensor_argument(idx, _input, 1, in_slice); + add_2D_tensor_argument(idx, _output, 2, out_slice); + + _kernel.update_shader_params(); + + enqueue(*this, in_slice); + } + while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixAccumulateBiasesKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixAccumulateBiasesKernel.cpp new file mode 100644 index 0000000000..434070a46c --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixAccumulateBiasesKernel.cpp @@ -0,0 +1,123 @@ +/* + * 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/GLES_COMPUTE/kernels/GCGEMMMatrixAccumulateBiasesKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" + +using namespace arm_compute; + +GCGEMMMatrixAccumulateBiasesKernel::GCGEMMMatrixAccumulateBiasesKernel() + : _accum(nullptr), _biases(nullptr) +{ +} + +void GCGEMMMatrixAccumulateBiasesKernel::configure(IGCTensor *accum, const IGCTensor *biases) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum); + ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() != 1); + + _biases = biases; + _accum = accum; + + std::set build_opts; + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + + // Create kernel + build_opts.emplace("#define GEMM_ACCUMULATE_BIASES"); + std::string dt_name = (accum->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace(("#define " + dt_name)); + _kernel = GCKernelLibrary::get().create_kernel("gemm_accumulate_biases", build_opts); + + // Configure kernel window + unsigned int num_elems_processed_per_iteration = 1; + + if(_accum->info()->data_type() == DataType::F32) + { + num_elems_processed_per_iteration = 16; + } + else if(_accum->info()->data_type() == DataType::F16) + { + num_elems_processed_per_iteration = 4; + } + + Window win = calculate_max_window(*_accum->info(), Steps(num_elems_processed_per_iteration)); + + AccessWindowStatic biases_access(biases->info(), 0, 0, ceil_to_multiple(biases->info()->dimension(0), num_elems_processed_per_iteration), biases->info()->dimension(1)); + AccessWindowHorizontal accum_access(_accum->info(), 0, num_elems_processed_per_iteration); + + update_window_and_padding(win, biases_access, accum_access); + + _kernel.clear_params(); + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCGEMMMatrixAccumulateBiasesKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); + + _kernel.use(); + + Window accum_slice = window.first_slice_window_2D(); + + Window biases_slice(accum_slice); + biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1)); + + // Run kernel + do + { + // Set arguments + unsigned int idx = 0; + if(_accum->info()->data_type() == DataType::F32) + { + add_2D_tensor_argument(idx, _accum, 1, accum_slice); + add_1D_tensor_argument(idx, _biases, 2, biases_slice); + } + else if(_accum->info()->data_type() == DataType::F16) + { + add_2D_tensor_argument(idx, _accum, BufferParam(1, 3), accum_slice); + add_1D_tensor_argument(idx, _biases, BufferParam(2, 3), biases_slice); + } + + _kernel.update_shader_params(); + + enqueue(*this, accum_slice); + } + while(window.slide_window_slice_2D(accum_slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.cpp new file mode 100644 index 0000000000..fa0415249a --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.cpp @@ -0,0 +1,104 @@ +/* + * 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/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +using namespace arm_compute; + +GCGEMMMatrixAdditionKernel::GCGEMMMatrixAdditionKernel() + : _input(nullptr), _output(nullptr) +{ +} + +void GCGEMMMatrixAdditionKernel::configure(const IGCTensor *input, IGCTensor *output, float beta) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != output->info()->dimension(0)); + ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != output->info()->dimension(1)); + + _input = input; + _output = output; + const unsigned int num_elems_processed_per_iteration = max_gc_vector_width / data_size_from_type(input->info()->data_type()); + + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace(("#define " + dt_name)); + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.emplace("#define BETA " + float_to_string_with_full_precision(beta)); + + // Create kernel + build_opts.emplace("#define GEMM_MATRIXADDITION"); + std::string data_type_name = lower_string(string_from_data_type(input->info()->data_type())); + _kernel = GCKernelLibrary::get().create_kernel(("gemm_ma"), 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); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + + update_window_and_padding(win, input_access, output_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + + _kernel.clear_params(); + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCGEMMMatrixAdditionKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_2D(); + + do + { + unsigned int idx = 0; + add_2D_tensor_argument(idx, _input, 1, slice); + add_2D_tensor_argument(idx, _output, 2, slice); + + _kernel.update_shader_params(); + + enqueue(*this, slice); + } + while(window.slide_window_slice_2D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.cpp new file mode 100644 index 0000000000..ea9b3874b2 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.cpp @@ -0,0 +1,210 @@ +/* + * 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/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/AccessWindowTranspose.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +#include +#include + +using namespace arm_compute; + +GCGEMMMatrixMultiplyKernel::GCGEMMMatrixMultiplyKernel() + : _input0(nullptr), _input1(nullptr), _output(nullptr) +{ +} + +void GCGEMMMatrixMultiplyKernel::configure(const IGCTensor *input0, const IGCTensor *input1, IGCTensor *output, float alpha, bool is_interleaved_transposed) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::F32, DataType::F16); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1, output); + + if(!is_interleaved_transposed) + { + ARM_COMPUTE_ERROR_ON(input0->info()->dimension(0) != input1->info()->dimension(1)); + } + + _input0 = input0; + _input1 = input1; + _output = output; + + std::set build_opts; + Window win; + + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.emplace("#define COLS_A " + support::cpp11::to_string(input0->info()->dimension(0))); + build_opts.emplace("#define COLS_B " + support::cpp11::to_string(input1->info()->dimension(0))); + build_opts.emplace("#define ALPHA " + float_to_string_with_full_precision(alpha)); + + // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication + if(is_interleaved_transposed) + { + switch(input0->info()->data_type()) + { + case DataType::F16: + build_opts.emplace("#define DATA_TYPE_FP16"); + break; + + case DataType::F32: + build_opts.emplace("#define DATA_TYPE_FP32"); + break; + + default: + ARM_COMPUTE_ERROR("Current data type is not supported"); + break; + } + + build_opts.emplace("#define GEMM_MM_INTERLEAVED_TRANSPOSED"); + + // Create kernel + _kernel = GCKernelLibrary::get().create_kernel(("gemm_mm_interleaved_transposed"), build_opts); + + // Configure window kernel + const unsigned int num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(input0->info()->data_type()); + constexpr unsigned int num_elems_processed_per_iteration_y = 4; + + win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); + + AccessWindowRectangle input0_access(input0->info(), 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f); + AccessWindowTranspose input1_access(input1->info(), 0, 0, num_elems_processed_per_iteration_x, 1, 0.f, 0.25f); + AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); + + update_window_and_padding(win, input0_access, input1_access, output_access); + + output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape())); + } + else + { + ARM_COMPUTE_ERROR_ON(input0->info()->dimension(0) != input1->info()->dimension(1)); + + // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor + unsigned int num_elems_processed_per_iteration_x; + unsigned int num_elems_processed_per_iteration_y; + + switch(input0->info()->data_type()) + { + case DataType::F16: + num_elems_processed_per_iteration_x = 4; + num_elems_processed_per_iteration_y = 1; + build_opts.emplace("#define DATA_TYPE_FP16"); + break; + + case DataType::F32: + num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(input0->info()->data_type()); + num_elems_processed_per_iteration_y = std::min(static_cast(output->info()->dimension(1)), 4); + build_opts.emplace("#define DATA_TYPE_FP32"); + break; + + default: + ARM_COMPUTE_ERROR("Current data type is not supported"); + break; + } + + build_opts.emplace("#define GEMM_MM_FLOATING_POINT"); + build_opts.emplace("#define NUM_ELEMS_PROCESSED_PER_THREAD_X " + support::cpp11::to_string(num_elems_processed_per_iteration_x)); + build_opts.emplace("#define NUM_ELEMS_PROCESSED_PER_THREAD_Y " + support::cpp11::to_string(num_elems_processed_per_iteration_y)); + + // Create kernel + _kernel = GCKernelLibrary::get().create_kernel("gemm_mm_floating_point", build_opts); + + win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); + + AccessWindowStatic input0_access(input0->info(), 0, 0, ceil_to_multiple(input0->info()->dimension(0), num_elems_processed_per_iteration_x), ceil_to_multiple(input0->info()->dimension(1), + num_elems_processed_per_iteration_y)); + AccessWindowStatic input1_access(input1->info(), 0, 0, ceil_to_multiple(input1->info()->dimension(0), num_elems_processed_per_iteration_x), input1->info()->dimension(1)); + AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); + + update_window_and_padding(win, input0_access, input1_access, output_access); + + Coordinates coord; + coord.set_num_dimensions(output->info()->num_dimensions()); + output_access.set_valid_region(win, ValidRegion(coord, output->info()->tensor_shape())); + } + + _kernel.clear_params(); + _kernel.set_shader_params_binding_point(0); + IGCKernel::configure(win); +} + +void GCGEMMMatrixMultiplyKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_2D(); + Window slice_matrix_b = slice; + + slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1)); + slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1)); + + do + { + Window slice_b = slice; + // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2 + // This scenario can happen when the the matrix multiplication is used to perform a convolution operation + if(_input1->info()->num_dimensions() < 3) + { + slice_b = slice_matrix_b; + } + + unsigned int idx = 0; + switch(_input0->info()->data_type()) + { + case DataType::F16: + add_2D_tensor_argument(idx, _input0, BufferParam(1, 2), slice); + add_2D_tensor_argument(idx, _input1, BufferParam(2, 3), slice_b); + add_2D_tensor_argument(idx, _output, BufferParam(3, 3), slice); + break; + + case DataType::F32: + add_2D_tensor_argument(idx, _input0, BufferParam(1, 2), slice); + add_2D_tensor_argument(idx, _input1, BufferParam(2, 2), slice_b); + add_2D_tensor_argument(idx, _output, BufferParam(3, 2), slice); + break; + + default: + ARM_COMPUTE_ERROR("Current data type is not supported"); + break; + } + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window.slide_window_slice_2D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.cpp new file mode 100644 index 0000000000..a1270b4c3d --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.cpp @@ -0,0 +1,128 @@ +/* + * 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/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.h" + +#include "arm_compute/core/AccessWindowTranspose.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +#include + +using namespace arm_compute; + +void GCGEMMTranspose1xWKernel::configure(const IGCTensor *input, IGCTensor *output) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + + TensorShape output_shape{ input->info()->tensor_shape() }; + const size_t transpose_w = 16 / input->info()->element_size(); + output_shape.set(0, input->info()->dimension(1) * transpose_w); + output_shape.set(1, static_cast(std::ceil((input->info()->dimension(0) / static_cast(transpose_w))))); + + // Output tensor auto inizialitation 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); + + const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size(); + const int scale_x = num_elems_processed_per_iteration; + + _input = input; + _output = output; + + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace(("#define " + dt_name)); + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + /* + * Following an example of how the transposition1xW works when the input data type is F32 + * + * |a00 a01 a02 a03| + * |a10 a11 a12 a13| + * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 | + * |a30 a31 a32 a33| + * + * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor) + */ + // Create kernel + build_opts.emplace("#define GEMM_TRANSPOSE1xW"); + _kernel = GCKernelLibrary::get().create_kernel("gemm_transpose1x4", build_opts); + + // Configure window + Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); + + ARM_COMPUTE_ERROR_ON_MSG((win.x().end() / scale_x) == 0, "Transposed shape would be 0 in the second dimension"); + + AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); + AccessWindowTranspose output_access(output->info(), 0, 0, num_elems_processed_per_iteration, 1, scale_x, 1.f / scale_x); + + update_window_and_padding(win, input_access, output_access); + + output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), input->info()->tensor_shape())); + + _kernel.clear_params(); + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCGEMMTranspose1xWKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + // Output is transposed + Window out_window(window); + out_window.set(Window::DimX, window.y()); + out_window.set(Window::DimY, window.x()); + + Window in_slice = window.first_slice_window_2D(); + Window out_slice = out_window.first_slice_window_2D(); + + do + { + unsigned int idx = 0; + add_2D_tensor_argument(idx, _input, 1, in_slice); + add_2D_tensor_argument(idx, _output, 2, out_slice); + + _kernel.update_shader_params(); + + enqueue(*this, in_slice); + } + while(window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCIm2ColKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCIm2ColKernel.cpp new file mode 100644 index 0000000000..935d8420ff --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCIm2ColKernel.cpp @@ -0,0 +1,230 @@ +/* + * 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/GLES_COMPUTE/kernels/GCIm2ColKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "support/ToolchainSupport.h" + +#include +#include + +using namespace arm_compute; + +GCIm2ColKernel::GCIm2ColKernel() + : _input(nullptr), _output(nullptr), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr) +{ +} + +void GCIm2ColKernel::configure(const IGCTensor *input, IGCTensor *output, std::pair kernel_dims, const PadStrideInfo &conv_info, bool has_bias) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_UNUSED(kernel_dims); + + _input = input; + _output = output; + _kernel.clear_params(); + + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.insert("#define " + dt_name); + + if(has_bias) + { + build_opts.emplace("#define HAS_BIAS"); + } + + int pad_x = 0; + int pad_y = 0; + int stride_x = 0; + int stride_y = 0; + std::tie(pad_x, pad_y) = conv_info.pad(); + std::tie(stride_x, stride_y) = conv_info.stride(); + + const bool run_img2col_reduced = (output->info()->dimension(0) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))) && (TensorShape::num_max_dimensions >= 4) + && (std::equal(input->info()->tensor_shape().cbegin() + 3, + input->info()->tensor_shape().cend(), + output->info()->tensor_shape().cbegin() + 1)) + && ((stride_x == 1) && (stride_y == 1) && (pad_x == 0) && (pad_y == 0)); + + if(!run_img2col_reduced) + { + // this path is currently not used and not validated + build_opts.insert("#define IM2COL_GENERIC"); + _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), + kernel_dims.first, kernel_dims.second, + conv_info); + _num_elems_processed_per_iteration = output->info()->dimension(0); + + build_opts.emplace("#define KERNEL_WIDTH " + support::cpp11::to_string(kernel_dims.first)); + build_opts.emplace("#define KERNEL_HEIGHT " + support::cpp11::to_string(kernel_dims.second)); + build_opts.emplace("#define KERNEL_DEPTH " + support::cpp11::to_string(input->info()->dimension(2))); + build_opts.emplace("#define CONVOLVED_WIDTH " + support::cpp11::to_string(_convolved_dims.first)); + build_opts.emplace("#define CONVOLVED_HEIGHT " + support::cpp11::to_string(_convolved_dims.second)); + build_opts.emplace("#define STRIDE_X " + support::cpp11::to_string(conv_info.stride().first)); + build_opts.emplace("#define STRIDE_Y " + support::cpp11::to_string(conv_info.stride().second)); + build_opts.emplace("#define PAD_X " + support::cpp11::to_string(conv_info.pad().first)); + build_opts.emplace("#define PAD_Y " + support::cpp11::to_string(conv_info.pad().second)); + build_opts.emplace("#define SRC_WIDTH " + support::cpp11::to_string(input->info()->dimension(0))); + build_opts.emplace("#define SRC_HEIGHT " + support::cpp11::to_string(input->info()->dimension(1))); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("im2col_generic", build_opts)); + + _run_func = &GCIm2ColKernel::run_generic; + } + else + { + build_opts.insert("#define IM2COL_REDUCED"); + _num_elems_processed_per_iteration = 4 / input->info()->element_size(); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("im2col_reduced", build_opts)); + + _run_func = &GCIm2ColKernel::run_reduced; + } + + // Configure kernel window + Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration)); + + if(input->info()->data_type() == DataType::F16) + { + // Calculate input right and bottom border + AccessWindowHorizontal input_access(input->info(), 0, _num_elems_processed_per_iteration); + + // Calculate output right and bottom border + const int output_width = output->info()->dimension(0); + const int output_height = output->info()->dimension(1); + const int output_padding_right = ceil_to_multiple(output_width, _num_elems_processed_per_iteration) - output_width; + AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height); + + update_window_and_padding(win, input_access, output_access); + } + + output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); + + if(!run_img2col_reduced) + { + // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension + win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start()); + } + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + IGCKernel::configure(win); +} + +void GCIm2ColKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON(_run_func == nullptr); + (this->*_run_func)(window); +} + +void GCIm2ColKernel::run_generic(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); + + // Get initial windows + Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ); + // Change the Z dimension's step back to 1 + window_collapsed.set_dimension_step(Window::DimZ, 1); + + Window slice = window_collapsed.first_slice_window_3D(); + Window slice_in = window_collapsed.first_slice_window_3D(); + Window slice_out = window_collapsed.first_slice_window_3D(); + + // Setup slice + slice.set(Window::DimX, Window::Dimension(0, static_cast(_convolved_dims.first), 1)); + slice.set(Window::DimY, Window::Dimension(0, static_cast(_convolved_dims.second), 1)); + + // Setup input slice + // The first three dimensions of the input are increased by the inner loops + slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); + slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); + slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); + + // Setup output slice + slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _num_elems_processed_per_iteration)); + slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1)); + slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1)); + + _kernel.use(); + + do + { + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, 1, slice_in); + add_2D_tensor_argument(idx, _output, 2, slice_out); + + _kernel.set_params(idx++, static_cast(_input->info()->dimension(2))); + _kernel.set_params(idx++, static_cast(_input->info()->strides_in_bytes()[3])); + _kernel.set_params(idx++, static_cast(_output->info()->strides_in_bytes()[3])); + _kernel.update_shader_params(); + + enqueue(*this, slice); + } + while(window_collapsed.slide_window_slice_3D(slice) && window_collapsed.slide_window_slice_3D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in)); +} + +void GCIm2ColKernel::run_reduced(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); + + Window out_window; + out_window.use_tensor_dimensions(_output->info()->tensor_shape()); + + Window out_slice = out_window.first_slice_window_1D(); + Window in_slice = window.first_slice_window_3D(); + + _kernel.use(); + + // Run kernel + do + { + // Set arguments + unsigned int idx = 0; + + add_3D_tensor_argument(idx, _input, 1, in_slice); + add_1D_tensor_argument(idx, _output, 2, out_slice); + _kernel.set_params(idx++, _input->info()->dimension(0)); + _kernel.set_params(idx++, _input->info()->dimension(1)); + _kernel.update_shader_params(); + + enqueue(*this, in_slice); + } + while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCNormalizationLayerKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCNormalizationLayerKernel.cpp new file mode 100644 index 0000000000..65e54f538c --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCNormalizationLayerKernel.cpp @@ -0,0 +1,124 @@ +/* + * 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/GLES_COMPUTE/kernels/GCNormalizationLayerKernel.h" + +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/Helpers.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" + +#include + +using namespace arm_compute; + +GCNormalizationLayerKernel::GCNormalizationLayerKernel() + : _input(nullptr), _squared_input(nullptr), _output(nullptr), _border_size(0) +{ +} + +BorderSize GCNormalizationLayerKernel::border_size() const +{ + return _border_size; +} + +void GCNormalizationLayerKernel::configure(const IGCTensor *input, const IGCTensor *squared_input, IGCTensor *output, NormalizationLayerInfo norm_info) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd"); + ARM_COMPUTE_ERROR_ON_MSG(norm_info.type() == NormType::IN_MAP_2D, "2D In-Map Normalization not implemented"); + + // Set build options + std::set build_opts; + + _input = input; + _squared_input = squared_input; + _output = output; + + const bool is_in_map = (norm_info.type() == NormType::IN_MAP_1D); + const unsigned int border_width = is_in_map ? std::min(norm_info.norm_size() / 2, 3U) : 0; + _border_size = BorderSize(0, border_width); + + // Set kernel static arguments + std::string func_name = ((norm_info.type() == NormType::IN_MAP_1D) ? "IN_MAP_1D" : "CROSS_MAP"); + build_opts.emplace(("#define " + func_name)); + build_opts.emplace(("#define COEFF " + float_to_string_with_full_precision(norm_info.scale_coeff()))); + build_opts.emplace(("#define BETA " + float_to_string_with_full_precision(norm_info.beta()))); + build_opts.emplace(("#define KAPPA " + float_to_string_with_full_precision(norm_info.kappa()))); + build_opts.emplace(("#define RADIUS " + support::cpp11::to_string(norm_info.norm_size() / 2))); + build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1))); + build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1))); + build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1))); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("normalization_layer", build_opts)); + + // Configure kernel window + const unsigned int num_elems_processed_per_iteration = 1; + const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2); + + Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); + + AccessWindowHorizontal input_access(input->info(), -_border_size.left, num_elems_read_per_iteration); + AccessWindowHorizontal squared_input_access(squared_input->info(), -_border_size.left, num_elems_read_per_iteration); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + + update_window_and_padding(win, input_access, squared_input_access, output_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + + _kernel.clear_params(); + + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCNormalizationLayerKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_3D(); + + do + { + unsigned int idx = 0; + unsigned int binding = 1; + add_3D_tensor_argument(idx, _input, binding++, slice); + add_3D_tensor_argument(idx, _squared_input, binding++, slice); + add_3D_tensor_argument(idx, _output, binding++, slice); + + _kernel.update_shader_params(); + + enqueue(*this, slice); + } + while(window.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCPixelWiseMultiplicationKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCPixelWiseMultiplicationKernel.cpp new file mode 100644 index 0000000000..2b5cee455c --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCPixelWiseMultiplicationKernel.cpp @@ -0,0 +1,127 @@ +/* + * 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/GLES_COMPUTE/kernels/GCPixelWiseMultiplicationKernel.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/Window.h" + +#include +#include +#include +#include +using namespace arm_compute; + +GCPixelWiseMultiplicationKernel::GCPixelWiseMultiplicationKernel() + : _input1(nullptr), _input2(nullptr), _output(nullptr) +{ +} + +void GCPixelWiseMultiplicationKernel::configure(const IGCTensor *input1, const IGCTensor *input2, IGCTensor *output, float scale) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2); + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + ARM_COMPUTE_ERROR_ON_MSG(scale < 0, "Scale cannot be negative. "); + + // Auto initialize output if not initialized + { + set_shape_if_empty(*output->info(), input1->info()->tensor_shape()); + set_format_if_unknown(*output->info(), Format::F32); + } + + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input1, input2, output); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2, output); + ARM_COMPUTE_ERROR_ON_MSG(scale < 0, "Scale cannot be negative. "); + + _input1 = input1; + _input2 = input2; + _output = output; + + std::string data_type; + std::string compute_type; + + // Set kernel build options + std::set build_opts; + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + + build_opts.emplace("#define SCALE " + support::cpp11::to_string(scale)); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("pixelwise_mul_float", build_opts)); + + _kernel.clear_params(); + + // Configure kernel window + constexpr unsigned int num_elems_processed_per_iteration = 1; + + Window win = calculate_max_window(*input1->info(), Steps(num_elems_processed_per_iteration)); + + AccessWindowHorizontal input1_access(input1->info(), 0, num_elems_processed_per_iteration); + AccessWindowHorizontal input2_access(input2->info(), 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + + update_window_and_padding(win, input1_access, input2_access, output_access); + + ValidRegion valid_region = intersect_valid_regions(input1->info()->valid_region(), + input2->info()->valid_region()); + output_access.set_valid_region(win, valid_region); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCPixelWiseMultiplicationKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_3D(); + + do + { + unsigned int idx = 0; + unsigned int binding = 1; + add_3D_tensor_argument(idx, _input1, binding++, slice); + add_3D_tensor_argument(idx, _input2, binding++, slice); + add_3D_tensor_argument(idx, _output, binding++, slice); + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCPoolingLayerKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCPoolingLayerKernel.cpp new file mode 100644 index 0000000000..c877da3783 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCPoolingLayerKernel.cpp @@ -0,0 +1,254 @@ +/* + * 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/GLES_COMPUTE/kernels/GCPoolingLayerKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.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" + +#include +#include +#include + +using namespace arm_compute; + +GCPoolingLayerKernel::GCPoolingLayerKernel() + : _input(nullptr), _output(nullptr), _pool_info(), _border_size(0), _num_elems_processed_per_iteration(1) +{ +} + +BorderSize GCPoolingLayerKernel::border_size() const +{ + return _border_size; +} + +void GCPoolingLayerKernel::configure(const IGCTensor *input, IGCTensor *output, const PoolingLayerInfo &pool_info) +{ + int pool_pad_x = 0; + int pool_pad_y = 0; + int pool_stride_x = 0; + int pool_stride_y = 0; + unsigned int pooled_w = 0; + unsigned int pooled_h = 0; + const PoolingType pool_type = pool_info.pool_type(); + const int pool_size = pool_info.pool_size(); + const PadStrideInfo pad_stride_info = pool_info.pad_stride_info(); + std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad(); + std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); + + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + ARM_COMPUTE_ERROR_ON(pool_pad_x >= pool_size || pool_pad_y >= pool_size); + ARM_COMPUTE_ERROR_ON(pool_size > 7 && is_data_type_fixed_point(input->info()->data_type())); + + // Check output dimensions + std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0), + input->info()->dimension(1), + pool_size, + pool_size, + pool_info.pad_stride_info()); + + // Output auto initialization if not yet initialized + { + TensorShape output_shape{ input->info()->tensor_shape() }; + output_shape.set(0, pooled_w); + output_shape.set(1, pooled_h); + + 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((output->info()->dimension(0) != pooled_w) || (output->info()->dimension(1) != pooled_h)); + + const int input_width = input->info()->dimension(0); + const int input_height = input->info()->dimension(1); + + // Set instance variables + _input = input; + _output = output; + _pool_info = pool_info; + _border_size = BorderSize(pool_pad_y, pool_pad_x); + + // Set build options + std::set build_opts; + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + if(input->info()->data_type() == DataType::F32) + { + build_opts.insert("#define DATA_TYPE_FP32"); + } + else + { + build_opts.insert("#define DATA_TYPE_FP16"); + } + build_opts.emplace(("#define POOL_" + string_from_pooling_type(pool_type))); + build_opts.emplace(("#define STRIDE_X " + support::cpp11::to_string(pool_stride_x))); + build_opts.emplace(("#define MAX_WIDTH " + support::cpp11::to_string(input->info()->dimension(0) + pool_pad_x))); + build_opts.emplace(("#define MAX_HEIGHT " + support::cpp11::to_string(input->info()->dimension(1) + pool_pad_y))); + build_opts.emplace(("#define STRIDE_Y " + support::cpp11::to_string(pool_stride_y))); + build_opts.emplace(("#define PAD_X " + support::cpp11::to_string(pool_pad_x))); + build_opts.emplace(("#define PAD_Y " + support::cpp11::to_string(pool_pad_y))); + + // Create kernel + if((pool_size == 2) || (pool_size == 3) || (pool_size == 7)) + { + // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenGLES kernel where + // each thread computes 4 output elements + const bool is_pool3x3_stride_le3 = (pool_size == 3) && (pool_stride_x <= 3) && !is_data_type_fixed_point(input->info()->data_type()); + + int num_elements_read_per_iteration = (pool_size == 7) ? 8 : pool_size; + + if(input->info()->data_type() == DataType::F32) + { + if(is_pool3x3_stride_le3) + { + // Change the number of elements processed and number of elements read per iteration for pooling 3x3 with stride less equal than 3 + _num_elems_processed_per_iteration = 4; + num_elements_read_per_iteration = pool_size * (pool_stride_x + 1); + } + } + else + { + num_elements_read_per_iteration = pool_size; + if(is_pool3x3_stride_le3) + { + _num_elems_processed_per_iteration = 4; + } + else + { + _num_elems_processed_per_iteration = 2; + } + } + + const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elements_read_per_iteration) - input_width; + const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height; + + _border_size.right = std::max(upper_bound_w, pool_pad_x); + _border_size.bottom = std::max(upper_bound_h, pool_pad_y); + + std::string kernel_name = "pooling_layer_" + support::cpp11::to_string(pool_size); + if(is_pool3x3_stride_le3) + { + build_opts.insert("#define POOLING_LAYER_3_OPTIMIZED"); + _kernel = static_cast(GCKernelLibrary::get().create_kernel(kernel_name + "_optimized", build_opts)); + } + else + { + build_opts.insert("#define POOLING_LAYER_" + support::cpp11::to_string(pool_size)); + _kernel = static_cast(GCKernelLibrary::get().create_kernel(kernel_name, build_opts)); + } + } + else // Run general case + { + if(input->info()->data_type() == DataType::F32) + { + _num_elems_processed_per_iteration = 1; + } + else + { + _num_elems_processed_per_iteration = 2; + } + const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + pool_size) - input_width; + const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height; + + _border_size.right = std::max(upper_bound_w, pool_pad_x); + _border_size.bottom = std::max(upper_bound_h, pool_pad_y); + + build_opts.emplace(("#define POOL_SIZE " + support::cpp11::to_string(pool_size))); + + build_opts.insert("#define POOLING_LAYER_N"); + _kernel = static_cast(GCKernelLibrary::get().create_kernel("pooling_layer_n", build_opts)); + } + + Window win = calculate_max_window(*output->info(), Steps(_num_elems_processed_per_iteration)); + + if(input->info()->data_type() == DataType::F32) + { + AccessWindowStatic input_access(input->info(), -pool_pad_x, -pool_pad_y, input_width + _border_size.right, input_height + _border_size.bottom); + AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration); + update_window_and_padding(win, input_access, output_access); + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); + } + else + { + // Calculate output right and bottom border + const int output_width = output->info()->dimension(0); + const int output_height = output->info()->dimension(1); + const int output_padding_right = ceil_to_multiple(output_width, _num_elems_processed_per_iteration) - output_width; + const int output_padding_bottom = ceil_to_multiple(output_height, 1) - output_height; + const int input_padding_right = ceil_to_multiple(input_width + 2 * _border_size.right, _num_elems_processed_per_iteration) - (input_width + 2 * _border_size.right); + const int input_padding_bottom = ceil_to_multiple(input_height + 2 * _border_size.bottom, 1) - (input_height + 2 * _border_size.bottom); + + // Configure kernel window + AccessWindowStatic input_access(input->info(), -pool_pad_x, -pool_pad_y, input_width + _border_size.right + input_padding_right, input_height + _border_size.bottom + input_padding_bottom); + AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height + output_padding_bottom); + update_window_and_padding(win, input_access, output_access); + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); + } + + _kernel.clear_params(); + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCPoolingLayerKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0; + std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad(); + std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); + + _kernel.use(); + + Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ); + Window slice = window_collapsed.first_slice_window_3D(); + + do + { + // Upsample input by pool size + Window in_slice(slice); + in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - pool_pad_x, in_slice.x().end() * pool_stride_x, pool_stride_x * _num_elems_processed_per_iteration)); + in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y)); + + // Set inputs + unsigned int idx = 0; + add_3D_tensor_argument(idx, _input, 1, in_slice); + add_3D_tensor_argument(idx, _output, 2, slice); + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window_collapsed.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.cpp new file mode 100644 index 0000000000..09a0f79ab2 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.cpp @@ -0,0 +1,353 @@ +/* + * 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/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.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" + +#include +#include + +using namespace arm_compute; + +void GCLogits1DMaxKernel::configure(const IGCTensor *input, IGCTensor *output) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + + // Softmax across the x dimension + TensorShape output_shape{ input->info()->tensor_shape() }; + output_shape.set(0, 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; + + // Set build options + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.insert("#define " + dt_name); + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.insert("#define SOFTMAX_LAYER_MAX"); + + // Tell the kernel that the width is not a multiple of 4 + if((input->info()->dimension(0) % 4) != 0) + { + build_opts.insert("#define NON_MULTIPLE_OF_4"); + } + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("softmax_layer_max", build_opts)); + + _kernel.clear_params(); + + // Set fixed arguments + unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters + _kernel.set_params(idx++, input->info()->dimension(0)); + + // Configure kernel window + // The kernel loops over all elements in steps of 4 + const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 4); + unsigned int num_elems_written_per_iteration = 1; + if(input->info()->data_type() == DataType::F16) + { + num_elems_written_per_iteration = 2; + } + + 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_written_per_iteration); + + update_window_and_padding(win, input_access, output_access); + + output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCLogits1DMaxKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + Window slice = window.first_slice_window_3D(); + + _kernel.use(); + + do + { + unsigned int idx1 = 0; + switch(_input->info()->data_type()) + { + case DataType::F16: + add_3D_tensor_argument(idx1, _input, BufferParam(1, 2), slice); + add_3D_tensor_argument(idx1, _output, BufferParam(2, 2), slice); + break; + + case DataType::F32: + add_3D_tensor_argument(idx1, _input, BufferParam(1, 2), slice); + add_3D_tensor_argument(idx1, _output, BufferParam(2, 2), slice); + break; + + default: + ARM_COMPUTE_ERROR("Current data type is mot supported"); + break; + } + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window.slide_window_slice_3D(slice)); +} + +GCLogits1DShiftExpSumKernel::GCLogits1DShiftExpSumKernel() + : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) +{ +} + +void GCLogits1DShiftExpSumKernel::configure(const IGCTensor *input, const IGCTensor *max, IGCTensor *output, IGCTensor *sum) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output); + + // Output auto initialization if not yet initialized + auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); + auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); + + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum); + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum); + + _input = input; + _max = max; + _output = output; + _sum = sum; + + // Set build options + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.insert("#define " + dt_name); + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.insert("#define SOFTMAX_LAYER_SHIFT_EXP_SUM"); + + // Tell the kernel that the width is not a multiple of 4 + if((input->info()->dimension(0) % 4) != 0) + { + build_opts.insert("#define NON_MULTIPLE_OF_4"); + } + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts)); + + _kernel.clear_params(); + + // Set fixed arguments + unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters + _kernel.set_params(idx++, input->info()->dimension(0)); + + // Configure window + // The kernel loops over all elements in steps of 4 + const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 4); + unsigned int num_elems_written_per_iteration = 1; + if(input->info()->data_type() == DataType::F16) + { + num_elems_written_per_iteration = 2; + } + + 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 max_access(max->info(), 0, num_elems_written_per_iteration); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + AccessWindowHorizontal sum_access(sum->info(), 0, num_elems_written_per_iteration); + + update_window_and_padding(win, input_access, max_access, output_access, sum_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape())); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCLogits1DShiftExpSumKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ); + Window slice = window_collapsed.first_slice_window_3D(); + + _kernel.use(); + + do + { + unsigned int idx = 0; + switch(_input->info()->data_type()) + { + case DataType::F16: + add_3D_tensor_argument(idx, _input, BufferParam(1, 2), slice); + add_3D_tensor_argument(idx, _max, BufferParam(2, 2), slice); + add_3D_tensor_argument(idx, _output, BufferParam(3, 2), slice); + add_3D_tensor_argument(idx, _sum, BufferParam(4, 2), slice); + break; + + case DataType::F32: + add_3D_tensor_argument(idx, _input, BufferParam(1, 2), slice); + add_3D_tensor_argument(idx, _max, BufferParam(2, 2), slice); + add_3D_tensor_argument(idx, _output, BufferParam(3, 2), slice); + add_3D_tensor_argument(idx, _sum, BufferParam(4, 2), slice); + break; + + default: + ARM_COMPUTE_ERROR("Current data type is mot supported"); + break; + } + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window_collapsed.slide_window_slice_3D(slice)); +} + +GCLogits1DNormKernel::GCLogits1DNormKernel() + : _input(nullptr), _sum(nullptr), _output(nullptr) +{ +} + +void GCLogits1DNormKernel::configure(const IGCTensor *input, const IGCTensor *sum, IGCTensor *output) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(sum, output); + + // Output auto initialization if not yet initialized + auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); + + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum, output); + ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum, output); + ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); + + _input = input; + _sum = sum; + _output = output; + + // Set build options + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.insert("#define " + dt_name); + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + build_opts.insert("#define SOFTMAX_LAYER_NORM"); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("softmax_layer_norm", build_opts)); + + // Configure window + constexpr unsigned int num_elems_processed_per_iteration = 4; + unsigned int num_elems_written_per_iteration = 1; + if(input->info()->data_type() == DataType::F16) + { + num_elems_written_per_iteration = 2; + } + + 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 sum_access(sum->info(), 0, 0, num_elems_written_per_iteration, sum->info()->dimension(1)); + AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); + + update_window_and_padding(win, input_access, sum_access, output_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + + _kernel.clear_params(); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCLogits1DNormKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ); + Window slice = window_collapsed.first_slice_window_3D(); + + _kernel.use(); + + do + { + Window sum_slice = slice; + sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1)); + + unsigned int idx1 = 0; + switch(_input->info()->data_type()) + { + case DataType::F16: + add_3D_tensor_argument(idx1, _input, BufferParam(1, 2), slice); + add_3D_tensor_argument(idx1, _sum, BufferParam(2, 2), slice); + add_3D_tensor_argument(idx1, _output, BufferParam(3, 2), slice); + break; + + case DataType::F32: + add_3D_tensor_argument(idx1, _input, BufferParam(1, 2), slice); + add_3D_tensor_argument(idx1, _sum, BufferParam(2, 2), slice); + add_3D_tensor_argument(idx1, _output, BufferParam(3, 2), slice); + break; + + default: + ARM_COMPUTE_ERROR("Current data type is mot supported"); + break; + } + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window_collapsed.slide_window_slice_3D(slice)); +} diff --git a/src/core/GLES_COMPUTE/kernels/GCTransposeKernel.cpp b/src/core/GLES_COMPUTE/kernels/GCTransposeKernel.cpp new file mode 100644 index 0000000000..b891b42ef8 --- /dev/null +++ b/src/core/GLES_COMPUTE/kernels/GCTransposeKernel.cpp @@ -0,0 +1,116 @@ +/* + * 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/GLES_COMPUTE/kernels/GCTransposeKernel.h" + +#include "arm_compute/core/AccessWindowTranspose.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" +#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" +#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Types.h" + +#include +#include + +using namespace arm_compute; + +void GCTransposeKernel::configure(const IGCTensor *input, IGCTensor *output) +{ + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_ERROR_ON_NULLPTR(output); + + TensorShape output_shape{ input->info()->tensor_shape() }; + const size_t w_out = input->info()->dimension(1); + const size_t h_out = input->info()->dimension(0); + output_shape.set(0, w_out); + output_shape.set(1, h_out); + + // Output tensor auto inizialitation 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_DIMENSIONS(output->info()->tensor_shape(), output_shape); + ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + + _input = input; + _output = output; + + std::set build_opts; + std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; + build_opts.emplace(("#define " + dt_name)); + build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); + build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); + + // Create kernel + _kernel = static_cast(GCKernelLibrary::get().create_kernel("transpose", build_opts)); + + _kernel.clear_params(); + + // Configure kernel window + const unsigned int num_elems_processed_per_iteration = 4; + + Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration, num_elems_processed_per_iteration)); + + AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration, num_elems_processed_per_iteration); + AccessWindowTranspose output_access(output->info(), 0, 0, num_elems_processed_per_iteration, num_elems_processed_per_iteration); + update_window_and_padding(win, input_access, output_access); + + output_access.set_valid_region(win, input->info()->valid_region()); + + // set shader params binding point + _kernel.set_shader_params_binding_point(0); + + IGCKernel::configure(win); +} + +void GCTransposeKernel::run(const Window &window) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); + + _kernel.use(); + + Window slice = window.first_slice_window_2D(); + + do + { + unsigned int idx = 0; + if(_input->info()->data_type() == DataType::F32) + { + add_2D_tensor_argument(idx, _input, 1, slice); + add_2D_tensor_argument(idx, _output, 2, slice); + } + else if(_input->info()->data_type() == DataType::F16) + { + add_2D_tensor_argument(idx, _input, BufferParam(1, 3), slice); + add_2D_tensor_argument(idx, _output, BufferParam(2, 3), slice); + } + + _kernel.update_shader_params(); + enqueue(*this, slice); + } + while(window.slide_window_slice_2D(slice)); +} -- cgit v1.2.1