blob: 3d30350c59fc845b1a17875f8257bebb4f997be3 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
John Richardson62385bc2018-04-20 13:11:36 +01002 * Copyright (c) 2017-2018 ARM Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/core/CL/kernels/CLL2NormalizeLayerKernel.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010025
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/FixedPoint.h"
30#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
36#include "support/ToolchainSupport.h"
37
38using namespace arm_compute;
39
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000040CLL2NormalizeLayerKernel::CLL2NormalizeLayerKernel()
Michalis Spyrou04f089c2017-08-08 17:42:38 +010041 : _input(nullptr), _sum(nullptr), _output(nullptr), _axis(0), _epsilon(1e-12)
42{
43}
44
John Richardson62385bc2018-04-20 13:11:36 +010045namespace
46{
47Status validate_arguments(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon)
48{
49 ARM_COMPUTE_UNUSED(epsilon);
50
51 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, sum, output);
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
54 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW);
55 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 0, "Unsupported reduction axis, Supported axis is 0");
56 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
57
58 // Reduce shape on axis
59 TensorShape sum_shape = input->tensor_shape();
60 sum_shape.set(axis, 1);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(sum->tensor_shape(), sum_shape);
62
63 if(output->total_size() != 0)
64 {
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
68 ARM_COMPUTE_RETURN_ERROR_ON(output->data_layout() != DataLayout::NCHW);
69 }
70
71 return Status{};
72}
73
74std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
75{
76 const unsigned int num_elems_processed_per_iteration = 16;
77
78 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
79
80 // Output tensor auto initialization if not yet initialized
81 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type(), input->fixed_point_position());
82
83 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
84 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
85
86 bool window_changed = update_window_and_padding(win, input_access, output_access);
87 output_access.set_valid_region(win, input->valid_region());
88
89 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
90
91 return std::make_tuple(err, win);
92}
93} // namespace
94
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000095void CLL2NormalizeLayerKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, unsigned int axis, float epsilon)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010096{
John Richardson62385bc2018-04-20 13:11:36 +010097 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
98 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), sum->info(), output->info(), axis, epsilon));
Michalis Spyrou04f089c2017-08-08 17:42:38 +010099
100 _input = input;
101 _sum = sum;
102 _output = output;
103 _axis = axis;
104 _epsilon = epsilon;
105
106 const unsigned int num_elems_processed_per_iteration = 16;
107
108 // Set build options
109 std::set<std::string> build_opts;
110 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
111 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
112
113 // Create kernel
114 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("l2_normalize", build_opts));
115
116 // Set epsilon argument
117 unsigned int idx = num_arguments_per_1D_tensor() * 3;
118 _kernel.setArg<cl_uint>(idx, _epsilon);
119
120 // Configure kernel window
John Richardson62385bc2018-04-20 13:11:36 +0100121 auto win_config = validate_and_configure_window(_input->info(), _output->info());
122 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100123
John Richardson62385bc2018-04-20 13:11:36 +0100124 ICLKernel::configure(std::get<1>(win_config));
125}
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100126
John Richardson62385bc2018-04-20 13:11:36 +0100127Status CLL2NormalizeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, unsigned int axis, float epsilon)
128{
129 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, sum, output, axis, epsilon));
130 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100131
John Richardson62385bc2018-04-20 13:11:36 +0100132 return Status{};
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100133}
134
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000135void CLL2NormalizeLayerKernel::run(const Window &window, cl::CommandQueue &queue)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100136{
137 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
138 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
139
140 Window window_sum(window);
141 window_sum.set(Window::DimX, Window::Dimension(0, 0, 0));
142
143 Window in_slice = window.first_slice_window_1D();
144 Window sum_slice = window_sum.first_slice_window_1D();
145
146 do
147 {
148 unsigned int idx = 0;
149 add_1D_tensor_argument(idx, _input, in_slice);
150 add_1D_tensor_argument(idx, _sum, sum_slice);
151 add_1D_tensor_argument(idx, _output, in_slice);
152 enqueue(queue, *this, in_slice);
153 }
154 while(window.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(sum_slice));
155}