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