blob: 62a203c97bd4b6516e3df415c8eeefa38a3eeef0 [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/CLBatchNormalizationLayerKernel.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"
Michalis Spyrou172e5702017-06-26 14:18:47 +010029#include "arm_compute/core/FixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
Michalis Spyrou172e5702017-06-26 14:18:47 +010036#include "support/ToolchainSupport.h"
37
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038using namespace arm_compute;
39
Giorgio Arena70623822017-11-27 15:50:10 +000040namespace
41{
42Error validate_arguments(const ITensorInfo *input, const ITensorInfo *output,
43 const ITensorInfo *mean, const ITensorInfo *var,
44 const ITensorInfo *beta, const ITensorInfo *gamma,
45 float epsilon)
46{
47 ARM_COMPUTE_UNUSED(epsilon);
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, var, beta, gamma);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var, beta, gamma);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, mean, var, beta, gamma);
52 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != mean->dimension(0));
53
54 if(output != nullptr && output->total_size() != 0)
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
59 }
60
61 return Error{};
62}
63
64std::pair<Error, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
65{
Giorgio Arenaf6a43c52017-12-01 12:16:25 +000066 if(output != nullptr)
67 {
68 // Output tensor auto initialization if not yet initialized
69 auto_init_if_empty(*output, *input->clone());
70 }
71
Giorgio Arena70623822017-11-27 15:50:10 +000072 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
73
74 // Configure kernel window
75 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
76 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
77
Giorgio Arena2995f5b2017-11-29 17:33:59 +000078 bool window_changed = false;
Giorgio Arena70623822017-11-27 15:50:10 +000079 if(output != nullptr)
80 {
81 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
82 window_changed = update_window_and_padding(win, input_access, output_access);
83 output_access.set_valid_region(win, input->valid_region());
84 }
85 else
86 {
87 window_changed = update_window_and_padding(win, input_access);
88 }
89
90 Error err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Error{};
91 return std::make_pair(err, win);
92}
93} // namespace
94
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095CLBatchNormalizationLayerKernel::CLBatchNormalizationLayerKernel()
96 : _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _beta(nullptr), _gamma(nullptr), _epsilon(0)
97{
98}
99
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100100void CLBatchNormalizationLayerKernel::configure(ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *var, const ICLTensor *beta, const ICLTensor *gamma,
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 float epsilon)
102{
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000103 ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var, beta, gamma);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 _input = input;
106 _output = output;
107 _mean = mean;
108 _var = var;
109 _beta = beta;
110 _gamma = gamma;
111 _epsilon = epsilon;
112
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100113 if(output != nullptr)
114 {
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000115 ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), output->info());
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100116 // Output tensor auto initialization if not yet initialized
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000117 auto_init_if_empty(*output->info(), *input->info()->clone());
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100118 }
119
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000120 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr,
121 mean->info(), var->info(), beta->info(), gamma->info(), epsilon));
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100122
Michalis Spyrou172e5702017-06-26 14:18:47 +0100123 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
124
125 // Set build options
126 std::set<std::string> build_opts;
127 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
128 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100129 build_opts.emplace(output == nullptr ? "-DIN_PLACE" : "");
Michalis Spyrou172e5702017-06-26 14:18:47 +0100130 if(is_data_type_fixed_point(input->info()->data_type()))
131 {
132 build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
133 }
134
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 // Create kernel
Michalis Spyrou172e5702017-06-26 14:18:47 +0100136 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("batchnormalization_layer", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
138 // Set kernel static arguments
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000139 unsigned int include_output = (output != nullptr) ? 1 : 0;
140 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor() + 4 * num_arguments_per_1D_tensor(); // Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 _kernel.setArg<cl_float>(idx++, _epsilon);
142
143 // Configure kernel window
Giorgio Arena70623822017-11-27 15:50:10 +0000144 auto win_config = validate_and_configure_window(input->info(), (output == nullptr) ? nullptr : output->info());
145 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
146 ICLKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147}
148
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000149Error CLBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
150 const ITensorInfo *mean, const ITensorInfo *var,
151 const ITensorInfo *beta, const ITensorInfo *gamma,
152 float epsilon)
153{
Giorgio Arena70623822017-11-27 15:50:10 +0000154 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon));
155 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output == nullptr) ? nullptr : output->clone().get()).first);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000156
157 return Error{};
158}
159
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160void CLBatchNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
161{
162 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
163 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
164
165 Window slice = window.first_slice_window_3D();
166
167 Window vector_slice = window.first_slice_window_1D();
168 vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
169
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000170 unsigned int include_output = (_output != nullptr) ? 1 : 0;
171 unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 add_1D_tensor_argument(idx, _mean, vector_slice);
173 add_1D_tensor_argument(idx, _var, vector_slice);
174 add_1D_tensor_argument(idx, _beta, vector_slice);
175 add_1D_tensor_argument(idx, _gamma, vector_slice);
176
177 do
178 {
179 idx = 0;
180 add_3D_tensor_argument(idx, _input, slice);
Georgios Pinitas409ee0a2017-08-18 10:16:09 +0100181 if(_output != nullptr)
182 {
183 add_3D_tensor_argument(idx, _output, slice);
184 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 enqueue(queue, *this, slice);
186 }
187 while(window.slide_window_slice_3D(slice));
188}