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