Compute Library
 21.02
GCActivationLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 
32 #include "arm_compute/core/Utils.h"
37 #include "support/StringSupport.h"
38 
39 #include <set>
40 #include <string>
41 
42 using namespace arm_compute;
43 
45  : _input(nullptr), _output(nullptr), _ctx(ctx)
46 {
47 }
48 
50 {
52 
53  // Make sure _kernel is initialized before calling the parent's configure
54  _input = input;
55  _output = input;
56 
57  if(output != nullptr)
58  {
59  // Output auto inizialitation if not yet initialized
60  auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
61 
64 
65  _output = output;
66  }
67 
68  unsigned int num_elems_processed_per_iteration = 4 / input->info()->element_size();
69 
70  // Set build options
71  std::set<std::string> build_opts;
72  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
73  build_opts.emplace(("#define " + string_from_activation_func(act_info.activation())));
74  build_opts.emplace(("#define " + dt_name));
75  build_opts.emplace(("#define A_VAL " + float_to_string_with_full_precision(act_info.a())));
76  build_opts.emplace(("#define B_VAL " + float_to_string_with_full_precision(act_info.b())));
77  build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)));
78  build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)));
79  build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)));
80 
81  // Create kernel
82  _kernel = create_opengl_kernel(_ctx, "activation_layer", build_opts);
83 
84  // Configure kernel window
85  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
86 
87  if(output != nullptr)
88  {
90 
93  output_access);
94 
95  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
96  }
97  else
98  {
101  }
102 
103  IGCKernel::configure(win);
104 }
105 
107 {
110 
111  _kernel.use();
112 
113  _output->set_needs_shifting(true);
114 
116  Window slice = collapsed.first_slice_window_3D();
117  Window slice_in = collapsed.first_slice_window_3D();
118 
119  slice.shift(Window::DimX, -(_output->info()->padding()).left);
120 
121  if(_input == _output)
122  {
123  slice_in.shift(Window::DimX, -(_input->info()->padding()).left);
124  }
125 
126  do
127  {
128  unsigned int idx = 0;
129  unsigned int binding = 1;
130  add_3D_tensor_argument(idx, _input, binding++, slice);
131  add_3D_tensor_argument(idx, _output, binding++, slice_in);
132  _kernel.update_shader_params();
133  enqueue(*this, slice);
134  }
135  while(collapsed.slide_window_slice_3D(slice) && collapsed.slide_window_slice_3D(slice_in));
136 }
GCKernel create_opengl_kernel(GCCoreRuntimeContext *ctx, const std::string &kernel_name, const std::set< std::string > &build_opts)
Creates an GLES kernel.
Definition: GCHelpers.cpp:37
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
float a() const
Get the alpha value.
Definition: Types.h:1590
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
const std::string & string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
Translates a given activation function to a string.
Definition: Utils.cpp:163
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
Activation Layer Information class.
Definition: Types.h:1550
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
1 channel, 1 F16 per channel
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
GCActivationLayerKernel(GCCoreRuntimeContext *ctx=nullptr)
Default constructor.
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
Definition: Utils.h:1262
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
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
Coordinates of an item.
Definition: Coordinates.h:37
Implementation of a row access pattern.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:441
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
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
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
void configure(IGCTensor *input, IGCTensor *output, ActivationLayerInfo act_info)
Set the input and output tensor.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void run(const Window &window) override
Enqueue the OpenGL ES shader to process the given window.
unsigned int num_elems_processed_per_iteration
Core runtime context for OpenGL ES.
ActivationFunction activation() const
Get the type of activation function.
Definition: Types.h:1585
float b() const
Get the beta value.
Definition: Types.h:1595
Container for valid region of a window.
Definition: Types.h:188
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
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)