Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2023 Arm Limited. |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLMeanStdDevNormalizationKernel.h" |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
| 33 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "src/core/CL/CLValidate.h" |
| 36 | #include "src/core/helpers/AutoConfiguration.h" |
| 37 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 38 | #include "support/StringSupport.h" |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float epsilon) |
| 45 | { |
| 46 | ARM_COMPUTE_UNUSED(epsilon); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 2, "Input tensor cannot have more than 2 dimensions"); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 51 | |
| 52 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 53 | if ((output != nullptr) && (output->total_size() != 0)) |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 54 | { |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 57 | } |
| 58 | return Status{}; |
| 59 | } |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 60 | } // namespace |
| 61 | |
| 62 | CLMeanStdDevNormalizationKernel::CLMeanStdDevNormalizationKernel() |
| 63 | : _input(nullptr), _output(nullptr), _run_in_place(false) |
| 64 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 65 | _type = CLKernelType::ELEMENTWISE; |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void CLMeanStdDevNormalizationKernel::configure(ICLTensor *input, ICLTensor *output, float epsilon) |
| 69 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 70 | configure(CLKernelLibrary::get().get_compile_context(), input, output, epsilon); |
| 71 | } |
| 72 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | void CLMeanStdDevNormalizationKernel::configure(const CLCompileContext &compile_context, |
| 74 | ICLTensor *input, |
| 75 | ICLTensor *output, |
| 76 | float epsilon) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 77 | { |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 79 | |
| 80 | _run_in_place = (output == nullptr) || (output == input); |
| 81 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | ARM_COMPUTE_ERROR_THROW_ON(CLMeanStdDevNormalizationKernel::validate( |
| 83 | input->info(), (output != nullptr) ? output->info() : nullptr, epsilon)); |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 84 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | if (output != nullptr) |
Giorgio Arena | ed4b8a0 | 2021-05-12 12:28:58 +0100 | [diff] [blame] | 86 | { |
| 87 | auto_init_if_empty(*output->info(), *input->info()); |
| 88 | } |
| 89 | |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 90 | _input = input; |
| 91 | _output = output; |
| 92 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 93 | const unsigned int num_elems_processed_per_iteration = |
| 94 | adjust_vec_size(16 / input->info()->element_size(), input->info()->dimension(0)); |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 95 | |
| 96 | // Set build options |
| 97 | CLBuildOptions build_opts; |
| 98 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 99 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
| 100 | build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon)); |
| 101 | build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); |
Murray Kornelsen | 552fe4c | 2022-07-22 18:04:59 -0400 | [diff] [blame] | 102 | build_opts.add_option_if(input->info()->data_type() == DataType::F16, "-DMEANSTDNORM_HALF"); |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 103 | build_opts.add_option_if(_run_in_place, "-DIN_PLACE"); |
| 104 | |
| 105 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 106 | _kernel = create_kernel(compile_context, "mean_stddev_normalization", build_opts.options()); |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 107 | |
| 108 | // Configure kernel window |
Giorgio Arena | ed4b8a0 | 2021-05-12 12:28:58 +0100 | [diff] [blame] | 109 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 110 | ICLKernel::configure_internal(win); |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 111 | |
| 112 | // Set config_id for enabling LWS tuning |
| 113 | _config_id = "mean_stddev_normalization_layer_"; |
| 114 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 115 | _config_id += "_"; |
| 116 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 117 | _config_id += "_"; |
| 118 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 119 | } |
| 120 | |
| 121 | Status CLMeanStdDevNormalizationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, float epsilon) |
| 122 | { |
| 123 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, epsilon)); |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 124 | return Status{}; |
| 125 | } |
| 126 | |
| 127 | void CLMeanStdDevNormalizationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 128 | { |
| 129 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 130 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 131 | |
| 132 | Window slice = window.first_slice_window_2D(); |
| 133 | // Set slice step equal to width to force gws[0] to 1, as each thread normalizes across all rows |
| 134 | slice.set_dimension_step(Window::DimX, _input->info()->dimension(0)); |
| 135 | |
| 136 | do |
| 137 | { |
| 138 | unsigned int idx = 0; |
| 139 | add_2D_tensor_argument(idx, _input, slice); |
Michalis Spyrou | e1651a5 | 2019-07-11 15:00:49 +0100 | [diff] [blame] | 140 | add_2D_tensor_argument_if((!_run_in_place), idx, _output, slice); |
| 141 | |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 142 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | } while (window.slide_window_slice_2D(slice)); |
Michele Di Giorgio | 5b48ad7 | 2019-06-04 18:43:35 +0100 | [diff] [blame] | 144 | } |
| 145 | } // namespace arm_compute |