blob: ac33468ad8e068eb57529a282226df4307667c9b [file] [log] [blame]
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2019-2023 Arm Limited.
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +01003 *
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 Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLMeanStdDevNormalizationKernel.h"
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
33#include "arm_compute/core/utils/StringUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/CL/CLValidate.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010038
39namespace arm_compute
40{
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
44{
45 ARM_COMPUTE_UNUSED(epsilon);
46 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
48 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 2, "Input tensor cannot have more than 2 dimensions");
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
50
51 // Checks performed when output is configured
52 if((output != nullptr) && (output->total_size() != 0))
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
56 }
57 return Status{};
58}
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010059} // namespace
60
61CLMeanStdDevNormalizationKernel::CLMeanStdDevNormalizationKernel()
62 : _input(nullptr), _output(nullptr), _run_in_place(false)
63{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010064 _type = CLKernelType::ELEMENTWISE;
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010065}
66
67void CLMeanStdDevNormalizationKernel::configure(ICLTensor *input, ICLTensor *output, float epsilon)
68{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010069 configure(CLKernelLibrary::get().get_compile_context(), input, output, epsilon);
70}
71
Manuel Bottini679fc962020-04-21 16:08:53 +010072void CLMeanStdDevNormalizationKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, float epsilon)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010073{
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010074 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
75
76 _run_in_place = (output == nullptr) || (output == input);
77
78 ARM_COMPUTE_ERROR_THROW_ON(CLMeanStdDevNormalizationKernel::validate(input->info(), (output != nullptr) ? output->info() : nullptr, epsilon));
79
Giorgio Arenaed4b8a02021-05-12 12:28:58 +010080 if(output != nullptr)
81 {
82 auto_init_if_empty(*output->info(), *input->info());
83 }
84
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010085 _input = input;
86 _output = output;
87
Giorgio Arenaed4b8a02021-05-12 12:28:58 +010088 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / input->info()->element_size(), input->info()->dimension(0));
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010089
90 // Set build options
91 CLBuildOptions build_opts;
92 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
93 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
94 build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon));
95 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
Murray Kornelsen552fe4c2022-07-22 18:04:59 -040096 build_opts.add_option_if(input->info()->data_type() == DataType::F16, "-DMEANSTDNORM_HALF");
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010097 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
98
99 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100100 _kernel = create_kernel(compile_context, "mean_stddev_normalization", build_opts.options());
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +0100101
102 // Configure kernel window
Giorgio Arenaed4b8a02021-05-12 12:28:58 +0100103 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
104 ICLKernel::configure_internal(win);
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +0100105
106 // Set config_id for enabling LWS tuning
107 _config_id = "mean_stddev_normalization_layer_";
108 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
109 _config_id += "_";
110 _config_id += support::cpp11::to_string(input->info()->dimension(0));
111 _config_id += "_";
112 _config_id += support::cpp11::to_string(input->info()->dimension(1));
113}
114
115Status CLMeanStdDevNormalizationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
116{
117 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, epsilon));
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +0100118 return Status{};
119}
120
121void CLMeanStdDevNormalizationKernel::run(const Window &window, cl::CommandQueue &queue)
122{
123 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
124 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
125
126 Window slice = window.first_slice_window_2D();
127 // Set slice step equal to width to force gws[0] to 1, as each thread normalizes across all rows
128 slice.set_dimension_step(Window::DimX, _input->info()->dimension(0));
129
130 do
131 {
132 unsigned int idx = 0;
133 add_2D_tensor_argument(idx, _input, slice);
Michalis Spyroue1651a52019-07-11 15:00:49 +0100134 add_2D_tensor_argument_if((!_run_in_place), idx, _output, slice);
135
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +0100136 enqueue(queue, *this, slice, lws_hint());
137 }
138 while(window.slide_window_slice_2D(slice));
139}
140} // namespace arm_compute