blob: b2bcb9225d75042c3aa64a3e21b2aaed1bfa15dd [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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 */
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"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35using namespace arm_compute;
36
37CLNormalizationLayerKernel::CLNormalizationLayerKernel()
38 : _input(nullptr), _squared_input(nullptr), _output(nullptr), _border_size(0)
39{
40}
41
42BorderSize CLNormalizationLayerKernel::border_size() const
43{
44 return _border_size;
45}
46
47void CLNormalizationLayerKernel::configure(const ICLTensor *input, const ICLTensor *squared_input, ICLTensor *output, NormalizationLayerInfo norm_info)
48{
Georgios Pinitas09004ca2017-07-03 17:30:14 +010049 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
50 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
51
52 // Output tensor auto initialization if not yet initialized
53 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
54
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, squared_input, output);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, squared_input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 ARM_COMPUTE_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
58 ARM_COMPUTE_ERROR_ON_MSG(norm_info.type() == NormType::IN_MAP_2D, "2D In-Map Normalization not implemented");
59
60 // Set build options
61 std::set<std::string> build_opts;
62 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
63
64 _input = input;
65 _squared_input = squared_input;
66 _output = output;
67
68 const bool is_in_map = (norm_info.type() == NormType::IN_MAP_1D);
69 const unsigned int border_width = is_in_map ? std::min(norm_info.norm_size() / 2, 3U) : 0;
70 _border_size = BorderSize(0, border_width);
71
72 // Create kernel
73 std::string kernel_name = (norm_info.type() == NormType::IN_MAP_1D) ? "normalization_layer_in_map_1D" : "normalization_layer_cross_map";
74 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
75
76 // Set kernel static arguments
77 unsigned int idx = 3 * num_arguments_per_3D_tensor(); // Skip the input and output parameters
78 _kernel.setArg<cl_float>(idx++, norm_info.scale_coeff());
79 _kernel.setArg<cl_float>(idx++, norm_info.beta());
80 _kernel.setArg<cl_float>(idx++, norm_info.kappa());
81 _kernel.setArg<cl_uint>(idx++, norm_info.norm_size() / 2);
82
83 // Configure kernel window
84 const unsigned int num_elems_processed_per_iteration = (is_in_map) ? 4 : 1;
85 const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2);
86
87 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
88
89 AccessWindowHorizontal input_access(input->info(), -_border_size.left, num_elems_read_per_iteration);
90 AccessWindowHorizontal squared_input_access(squared_input->info(), -_border_size.left, num_elems_read_per_iteration);
91 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
92
93 update_window_and_padding(win, input_access, squared_input_access, output_access);
94
95 output_access.set_valid_region(win, input->info()->valid_region());
96
97 ICLKernel::configure(win);
98}
99
100void CLNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
101{
102 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
103 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
104
105 Window slice = window.first_slice_window_3D();
106
107 do
108 {
109 unsigned int idx = 0;
110 add_3D_tensor_argument(idx, _input, slice);
111 add_3D_tensor_argument(idx, _squared_input, slice);
112 add_3D_tensor_argument(idx, _output, slice);
113 enqueue(queue, *this, slice);
114 }
115 while(window.slide_window_slice_3D(slice));
116}