blob: 0173b81cf84f681e28ef726f705de9e305405919 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
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/GCActivationLayerKernel.h"
25
26#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
27#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
28#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/IAccessWindow.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010038
39#include <set>
40#include <string>
41
42using namespace arm_compute;
43
Georgios Pinitas7ae80a92019-10-25 18:25:17 +010044GCActivationLayerKernel::GCActivationLayerKernel(GCCoreRuntimeContext *ctx)
45 : _input(nullptr), _output(nullptr), _ctx(ctx)
Anthony Barbier7068f992017-10-26 15:23:08 +010046{
47}
48
49void GCActivationLayerKernel::configure(IGCTensor *input, IGCTensor *output, ActivationLayerInfo act_info)
50{
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
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
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010060 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +010061
62 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
63 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Anthony Barbier7068f992017-10-26 15:23:08 +010064
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
Georgios Pinitas7ae80a92019-10-25 18:25:17 +010082 _kernel = create_opengl_kernel(_ctx, "activation_layer", build_opts);
Anthony Barbier7068f992017-10-26 15:23:08 +010083
84 // Configure kernel window
85 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
86
87 if(output != nullptr)
88 {
89 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
90
91 update_window_and_padding(win,
92 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration),
93 output_access);
94
Gian Marco Iodice1e6c9152019-08-20 11:27:20 +010095 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier7068f992017-10-26 15:23:08 +010096 }
97 else
98 {
99 update_window_and_padding(win,
100 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
101 }
102
Anthony Barbier7068f992017-10-26 15:23:08 +0100103 IGCKernel::configure(win);
104}
105
106void GCActivationLayerKernel::run(const Window &window)
107{
108 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
109 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window);
110
111 _kernel.use();
112
Frank Lei4406fd62018-02-01 14:47:14 +0800113 _output->set_needs_shifting(true);
114
Georgios Pinitasf92cb232018-03-29 11:51:54 +0100115 Window collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ);
116 Window slice = collapsed.first_slice_window_3D();
117 Window slice_in = collapsed.first_slice_window_3D();
Frank Lei4406fd62018-02-01 14:47:14 +0800118
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 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100125
126 do
127 {
128 unsigned int idx = 0;
129 unsigned int binding = 1;
Georgios Pinitasf92cb232018-03-29 11:51:54 +0100130 add_3D_tensor_argument(idx, _input, binding++, slice);
131 add_3D_tensor_argument(idx, _output, binding++, slice_in);
Anthony Barbier7068f992017-10-26 15:23:08 +0100132 _kernel.update_shader_params();
133 enqueue(*this, slice);
134 }
Georgios Pinitasf92cb232018-03-29 11:51:54 +0100135 while(collapsed.slide_window_slice_3D(slice) && collapsed.slide_window_slice_3D(slice_in));
Anthony Barbier7068f992017-10-26 15:23:08 +0100136}