blob: df01eab2403ed86e7f3b97bb28e83de8adc91fa7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/CLNormalizationLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/Window.h"
34
35using namespace arm_compute;
36
Giorgio Arenac23b6332017-11-28 15:23:51 +000037namespace
38{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000039Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, NormalizationLayerInfo norm_info)
Giorgio Arenac23b6332017-11-28 15:23:51 +000040{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010041 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010042 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Giorgio Arenac23b6332017-11-28 15:23:51 +000043 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
44
45 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
Giorgio Arenac23b6332017-11-28 15:23:51 +000046
Giorgio Arenac23b6332017-11-28 15:23:51 +000047 // Checks performed when output is configured
48 if(output->total_size() != 0)
49 {
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Giorgio Arenac23b6332017-11-28 15:23:51 +000052 }
53
Georgios Pinitas631c41a2017-12-06 11:53:03 +000054 return Status{};
Giorgio Arenac23b6332017-11-28 15:23:51 +000055}
56
Georgios Pinitas631c41a2017-12-06 11:53:03 +000057std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, NormalizationLayerInfo norm_info)
Giorgio Arenac23b6332017-11-28 15:23:51 +000058{
Giorgio Arenaf6a43c52017-12-01 12:16:25 +000059 // Output tensor auto initialization if not yet initialized
60 auto_init_if_empty(*output, *input->clone());
61
Georgios Pinitas01624362017-11-30 10:53:31 +000062 const unsigned int norm_size = norm_info.norm_size();
63 bool is_in_map = norm_info.is_in_map();
64
Giorgio Arenac23b6332017-11-28 15:23:51 +000065 const unsigned int border_width = is_in_map ? std::min(norm_size / 2, 3U) : 0;
66 const BorderSize border_size = BorderSize(0, border_width);
67
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010068 const unsigned int num_elems_processed_per_iteration = 4;
Giorgio Arenac23b6332017-11-28 15:23:51 +000069 const unsigned int num_elems_read_per_iteration = is_in_map ? (num_elems_processed_per_iteration + 2 * (norm_size / 2)) : num_elems_processed_per_iteration;
70
71 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
72
Georgios Pinitas01624362017-11-30 10:53:31 +000073 // We do not use a Rectangle window for IN_MAP_2D as we clamp the top and bottom accesses inside the kernel, avoiding padding
Giorgio Arenac23b6332017-11-28 15:23:51 +000074 AccessWindowHorizontal input_access(input, -border_size.left, num_elems_read_per_iteration);
75 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
76
77 bool window_changed = update_window_and_padding(win, input_access, output_access);
78
79 output_access.set_valid_region(win, input->valid_region());
80
Georgios Pinitas631c41a2017-12-06 11:53:03 +000081 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenac23b6332017-11-28 15:23:51 +000082 return std::make_pair(err, win);
83}
84} // namespace
85
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086CLNormalizationLayerKernel::CLNormalizationLayerKernel()
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010087 : _input(nullptr), _output(nullptr), _border_size(0), _is_in_map(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088{
89}
90
91BorderSize CLNormalizationLayerKernel::border_size() const
92{
93 return _border_size;
94}
95
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +010096void CLNormalizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, NormalizationLayerInfo norm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097{
Georgios Pinitas30902ed2017-11-14 15:32:57 +000098 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas09004ca2017-07-03 17:30:14 +010099
100 // Output tensor auto initialization if not yet initialized
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000101 auto_init_if_empty(*output->info(), *input->info()->clone());
Georgios Pinitas09004ca2017-07-03 17:30:14 +0100102
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000103 // Perform validation step
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000104 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), norm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
Gian Marco Iodiced60a6b92017-08-10 10:43:40 +0100106 _input = input;
107 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Georgios Pinitas41caa622017-11-16 14:37:08 +0000109 _is_in_map = norm_info.is_in_map();
steniu01f70256b2017-07-13 14:03:35 +0100110 const unsigned int border_width = _is_in_map ? std::min(norm_info.norm_size() / 2, 3U) : 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 _border_size = BorderSize(0, border_width);
112
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100113 const unsigned int num_elems_processed_per_iteration = 4;
Georgios Pinitas01624362017-11-30 10:53:31 +0000114 const bool is_in_map_2D = (norm_info.type() == NormType::IN_MAP_2D);
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100115
116 // Set build options
Georgios Pinitas01624362017-11-30 10:53:31 +0000117 CLBuildOptions build_opts;
118 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
Georgios Pinitas01624362017-11-30 10:53:31 +0000119 build_opts.add_option(("-DCOEFF=" + float_to_string_with_full_precision(norm_info.scale_coeff())));
120 build_opts.add_option(("-DBETA=" + float_to_string_with_full_precision(norm_info.beta())));
121 build_opts.add_option(("-DKAPPA=" + float_to_string_with_full_precision(norm_info.kappa())));
122 build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
123 build_opts.add_option(("-DRADIUS=" + support::cpp11::to_string(norm_info.norm_size() / 2)));
124 build_opts.add_option(("-DNUM_SLICES=" + support::cpp11::to_string(input->info()->dimension(2))));
125 build_opts.add_option_if(is_in_map_2D, "-DIN_MAP_2D");
Michele Di Giorgio6c928342017-06-22 16:55:57 +0100126
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 // Create kernel
Georgios Pinitas01624362017-11-30 10:53:31 +0000128 std::string kernel_name = _is_in_map ? "normalization_layer_in_map" : "normalization_layer_cross_map";
129 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 // Configure kernel window
Georgios Pinitas01624362017-11-30 10:53:31 +0000132 auto win_config = validate_and_configure_window(input->info(), output->info(), norm_info);
Giorgio Arenac23b6332017-11-28 15:23:51 +0000133 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
134 ICLKernel::configure(win_config.second);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000135
136 // Set config_id for enabling LWS tuning
137 _config_id = "normalization_layer_";
138 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
139 _config_id += "_";
140 _config_id += support::cpp11::to_string(static_cast<std::underlying_type<NormType>::type>(norm_info.type()));
141 _config_id += "_";
142 _config_id += support::cpp11::to_string(norm_info.norm_size());
143 _config_id += "_";
144 _config_id += support::cpp11::to_string(input->info()->dimension(0));
145 _config_id += "_";
146 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147}
148
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000149Status CLNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, NormalizationLayerInfo norm_info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000150{
Giorgio Arenac23b6332017-11-28 15:23:51 +0000151 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, norm_info));
Georgios Pinitas01624362017-11-30 10:53:31 +0000152 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), norm_info).first);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000153
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000154 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000155}
156
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157void CLNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
158{
159 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
160 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
161
steniu01f70256b2017-07-13 14:03:35 +0100162 const int collapsed_dimension = _is_in_map ? Window::DimZ : 4;
163 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), collapsed_dimension);
164 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
166 do
167 {
168 unsigned int idx = 0;
169 add_3D_tensor_argument(idx, _input, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 add_3D_tensor_argument(idx, _output, slice);
171 enqueue(queue, *this, slice);
172 }
steniu01f70256b2017-07-13 14:03:35 +0100173 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174}