Compute Library
 21.02
GCArithmeticAdditionKernel.cpp
Go to the documentation of this file.
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  */
25 
26 #include "arm_compute/core/Error.h"
34 #include "arm_compute/core/Utils.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 {
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);
56 
57  // Validate in case of configured output
58  if((output != nullptr) && (output->total_size() != 0))
59  {
62  }
63 
64  return Status{};
65 }
66 
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 
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 
90  : _input1(nullptr), _input2(nullptr), _output(nullptr)
91 {
92 }
93 
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());
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 
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 
135 {
138 
139  _kernel.use();
140 
141  _output->set_needs_shifting(true);
142 
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 }
bool set_format_if_unknown(ITensorInfo &info, Format format)
Set the format, data type and number of channels to the specified value if the current data type is u...
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:132
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
void shift(size_t dimension, int shift_value)
Shift the values of a given dimension by the given shift_value.
Definition: Window.inl:133
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
std::string to_string(T &&value)
Convert integer and float values to string.
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy)
Static function to check if given info will lead to a valid configuration of GCArithmeticAdditionKern...
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
GCKernel class.
Status class.
Definition: Error.h:52
const ValidRegion valid_region
Definition: Scale.cpp:221
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Manages all the GLES kernels compilation and caching, provides accessors for the GLES Context...
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
void set_needs_shifting(bool needs_shifting)
Set the flag indicating whether or not a tensor needs shifting.
Definition: IGCTensor.cpp:61
Implementation of a row access pattern.
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
Set the shape to the specified value if the current assignment is empty.
ValidRegion intersect_valid_regions(const Ts &... regions)
Intersect multiple valid regions.
Definition: WindowHelpers.h:74
void run(const Window &window) override
Enqueue the OpenGL ES shader to process the given window.
virtual PaddingSize padding() const =0
Padding of tensor.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
Wrapper to configure the Khronos EGL and OpenGL ES C header.
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Definition: Error.h:159
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
GCKernel create_kernel(const std::string &shader_name, const StringSet &build_options_set={}) const
Creates a kernel from the kernel library.
unsigned int num_elems_processed_per_iteration
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Container for valid region of a window.
Definition: Types.h:188
void configure(const IGCTensor *input1, const IGCTensor *input2, IGCTensor *output, ConvertPolicy policy)
Initialise the kernel&#39;s inputs, output and convertion policy.
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
Describe a multidimensional execution window.
Definition: Window.h:39
ConvertPolicy
Policy to handle overflow.
Definition: Types.h:385
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)