blob: 06c34863d757c7919e4f4f525375913c86e24853 [file] [log] [blame]
zhenglin926d5e12017-12-21 15:36:50 +08001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
zhenglin926d5e12017-12-21 15:36:50 +08003 *
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/GCArithmeticAdditionKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
29#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
30#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/IAccessWindow.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
zhenglin926d5e12017-12-21 15:36:50 +080038
39#include <cstddef>
40#include <set>
41#include <string>
42
43using namespace arm_compute;
44
45namespace
46{
47Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy)
48{
49 ARM_COMPUTE_UNUSED(policy);
50 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
51 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::F16);
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::F16);
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, input2);
54
55 // Validate in case of configured output
56 if((output != nullptr) && (output->total_size() != 0))
57 {
58 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, output);
60 }
61
62 return Status{};
63}
64
65std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output)
66{
67 constexpr unsigned int num_elems_processed_per_iteration = 8;
68
69 Window win = calculate_max_window(*input1, Steps(num_elems_processed_per_iteration));
70
71 AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration);
72 AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration);
73 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
74
75 bool window_changed = update_window_and_padding(win, input1_access, input2_access, output_access);
76
77 ValidRegion valid_region = intersect_valid_regions(input1->valid_region(),
78 input2->valid_region());
79
80 output_access.set_valid_region(win, valid_region);
81
82 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
83 return std::make_pair(err, win);
84}
85} // namespace
86
87GCArithmeticAdditionKernel::GCArithmeticAdditionKernel()
88 : _input1(nullptr), _input2(nullptr), _output(nullptr)
89{
90}
91
92void GCArithmeticAdditionKernel::configure(const IGCTensor *input1, const IGCTensor *input2, IGCTensor *output, ConvertPolicy policy)
93{
94 ARM_COMPUTE_UNUSED(policy);
95 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
96
97 // Auto initialize output if not initialized
98 {
99 set_shape_if_empty(*output->info(), input1->info()->tensor_shape());
100 set_format_if_unknown(*output->info(), Format::F16);
101 }
102
103 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), policy));
104
105 _input1 = input1;
106 _input2 = input2;
107 _output = output;
108
109 // Set kernel build options
110 std::set<std::string> build_opts;
111 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
112 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
113 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
114
115 // Create kernel
116 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("arithmetic_add", build_opts));
117
118 // Configure kernel window
119 auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info());
120 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
121 IGCKernel::configure(win_config.second);
122}
123
124Status GCArithmeticAdditionKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy)
125{
126 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, policy));
127 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first);
128
129 return Status{};
130}
131
132void GCArithmeticAdditionKernel::run(const Window &window)
133{
134 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
135 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window);
136
137 _kernel.use();
138
Frank Lei4406fd62018-02-01 14:47:14 +0800139 _output->set_needs_shifting(true);
140
141 Window slice = window.first_slice_window_3D();
142 Window slice_in = window.first_slice_window_3D();
143
144 slice.shift(Window::DimX, -(_output->info()->padding()).left);
145
zhenglin926d5e12017-12-21 15:36:50 +0800146 do
147 {
148 unsigned int idx = 0;
149 unsigned int binding = 1; // SSBO binding starts from 1.
Frank Lei4406fd62018-02-01 14:47:14 +0800150 add_3D_tensor_argument(idx, _input1, binding++, slice_in);
151 add_3D_tensor_argument(idx, _input2, binding++, slice_in);
152 add_3D_tensor_argument(idx, _output, binding++, slice);
zhenglin926d5e12017-12-21 15:36:50 +0800153
154 _kernel.update_shader_params();
155
156 enqueue(*this, slice);
157 }
Frank Lei4406fd62018-02-01 14:47:14 +0800158 while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(slice_in));
zhenglin926d5e12017-12-21 15:36:50 +0800159}