blob: 5fa1987bf17b501e69a0c67ff0402c7df0c27099 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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/GLES_COMPUTE/kernels/GCNormalizationLayerKernel.h"
25
26#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
27#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
28#include "arm_compute/core/GLES_COMPUTE/IGCTensor.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"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000034#include "support/StringSupport.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010035
36#include <string>
37
38using namespace arm_compute;
39
40GCNormalizationLayerKernel::GCNormalizationLayerKernel()
41 : _input(nullptr), _squared_input(nullptr), _output(nullptr), _border_size(0)
42{
43}
44
45BorderSize GCNormalizationLayerKernel::border_size() const
46{
47 return _border_size;
48}
49
50void GCNormalizationLayerKernel::configure(const IGCTensor *input, const IGCTensor *squared_input, IGCTensor *output, NormalizationLayerInfo norm_info)
51{
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
53 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
54 ARM_COMPUTE_ERROR_ON_MSG(!(norm_info.norm_size() % 2), "Normalization size should be odd");
55 ARM_COMPUTE_ERROR_ON_MSG(norm_info.type() == NormType::IN_MAP_2D, "2D In-Map Normalization not implemented");
56
57 // Set build options
58 std::set<std::string> build_opts;
59
60 _input = input;
61 _squared_input = squared_input;
62 _output = output;
63
Georgios Pinitas41caa622017-11-16 14:37:08 +000064 const bool is_in_map = norm_info.is_in_map();
Anthony Barbier7068f992017-10-26 15:23:08 +010065 const unsigned int border_width = is_in_map ? std::min(norm_info.norm_size() / 2, 3U) : 0;
66 _border_size = BorderSize(0, border_width);
67
68 // Set kernel static arguments
69 std::string func_name = ((norm_info.type() == NormType::IN_MAP_1D) ? "IN_MAP_1D" : "CROSS_MAP");
70 build_opts.emplace(("#define " + func_name));
71 build_opts.emplace(("#define COEFF " + float_to_string_with_full_precision(norm_info.scale_coeff())));
72 build_opts.emplace(("#define BETA " + float_to_string_with_full_precision(norm_info.beta())));
73 build_opts.emplace(("#define KAPPA " + float_to_string_with_full_precision(norm_info.kappa())));
74 build_opts.emplace(("#define RADIUS " + support::cpp11::to_string(norm_info.norm_size() / 2)));
75 build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
76 build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
77 build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
78
79 // Create kernel
80 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("normalization_layer", build_opts));
81
82 // Configure kernel window
83 const unsigned int num_elems_processed_per_iteration = 1;
84 const unsigned int num_elems_read_per_iteration = num_elems_processed_per_iteration + 2 * (norm_info.norm_size() / 2);
85
86 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
87
88 AccessWindowHorizontal input_access(input->info(), -_border_size.left, num_elems_read_per_iteration);
89 AccessWindowHorizontal squared_input_access(squared_input->info(), -_border_size.left, num_elems_read_per_iteration);
90 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
91
92 update_window_and_padding(win, input_access, squared_input_access, output_access);
93
94 output_access.set_valid_region(win, input->info()->valid_region());
95
Anthony Barbier7068f992017-10-26 15:23:08 +010096 IGCKernel::configure(win);
97}
98
99void GCNormalizationLayerKernel::run(const Window &window)
100{
101 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
102 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
103
104 _kernel.use();
105
106 Window slice = window.first_slice_window_3D();
107
108 do
109 {
110 unsigned int idx = 0;
111 unsigned int binding = 1;
112 add_3D_tensor_argument(idx, _input, binding++, slice);
113 add_3D_tensor_argument(idx, _squared_input, binding++, slice);
114 add_3D_tensor_argument(idx, _output, binding++, slice);
115
116 _kernel.update_shader_params();
117
118 enqueue(*this, slice);
119 }
120 while(window.slide_window_slice_3D(slice));
121}