blob: 11ef86e8c38b95154d0d9fa7e2bd269b7124425c [file] [log] [blame]
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2019-2020 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 */
24#include "arm_compute/core/CL/kernels/CLMeanStdDevNormalizationKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010037
38namespace arm_compute
39{
40namespace
41{
42Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
43{
44 ARM_COMPUTE_UNUSED(epsilon);
45 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
47 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 2, "Input tensor cannot have more than 2 dimensions");
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
49
50 // Checks performed when output is configured
51 if((output != nullptr) && (output->total_size() != 0))
52 {
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
55 }
56 return Status{};
57}
58
59std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
60{
61 if(output != nullptr)
62 {
63 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
64 // Output auto inizialitation if not yet initialized
65 auto_init_if_empty(*output, *input);
66 }
67
68 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
69
70 // This kernel doesn't need padding
71 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
72 if(output != nullptr)
73 {
74 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
75 }
76
77 return std::make_pair(Status{}, win);
78}
79} // namespace
80
81CLMeanStdDevNormalizationKernel::CLMeanStdDevNormalizationKernel()
82 : _input(nullptr), _output(nullptr), _run_in_place(false)
83{
84}
85
86void CLMeanStdDevNormalizationKernel::configure(ICLTensor *input, ICLTensor *output, float epsilon)
87{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010088 configure(CLKernelLibrary::get().get_compile_context(), input, output, epsilon);
89}
90
91void CLMeanStdDevNormalizationKernel::configure(CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, float epsilon)
92{
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +010093 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
94
95 _run_in_place = (output == nullptr) || (output == input);
96
97 ARM_COMPUTE_ERROR_THROW_ON(CLMeanStdDevNormalizationKernel::validate(input->info(), (output != nullptr) ? output->info() : nullptr, epsilon));
98
99 _input = input;
100 _output = output;
101
102 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
103
104 // Set build options
105 CLBuildOptions build_opts;
106 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
107 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
108 build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(epsilon));
109 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
110 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
111
112 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100113 _kernel = create_kernel(compile_context, "mean_stddev_normalization", build_opts.options());
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +0100114
115 // Configure kernel window
116 auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info());
117 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
118 ICLKernel::configure_internal(win_config.second);
119
120 // Set config_id for enabling LWS tuning
121 _config_id = "mean_stddev_normalization_layer_";
122 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
123 _config_id += "_";
124 _config_id += support::cpp11::to_string(input->info()->dimension(0));
125 _config_id += "_";
126 _config_id += support::cpp11::to_string(input->info()->dimension(1));
127}
128
129Status CLMeanStdDevNormalizationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, float epsilon)
130{
131 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, epsilon));
132 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first);
133 return Status{};
134}
135
136void CLMeanStdDevNormalizationKernel::run(const Window &window, cl::CommandQueue &queue)
137{
138 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
139 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
140
141 Window slice = window.first_slice_window_2D();
142 // Set slice step equal to width to force gws[0] to 1, as each thread normalizes across all rows
143 slice.set_dimension_step(Window::DimX, _input->info()->dimension(0));
144
145 do
146 {
147 unsigned int idx = 0;
148 add_2D_tensor_argument(idx, _input, slice);
Michalis Spyroue1651a52019-07-11 15:00:49 +0100149 add_2D_tensor_argument_if((!_run_in_place), idx, _output, slice);
150
Michele Di Giorgio5b48ad72019-06-04 18:43:35 +0100151 enqueue(queue, *this, slice, lws_hint());
152 }
153 while(window.slide_window_slice_2D(slice));
154}
155} // namespace arm_compute