Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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/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 Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame^] | 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 38 | |
| 39 | #include <set> |
| 40 | #include <string> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 44 | GCActivationLayerKernel::GCActivationLayerKernel(GCCoreRuntimeContext *ctx) |
| 45 | : _input(nullptr), _output(nullptr), _ctx(ctx) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | void 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 Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 60 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type()); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 61 | |
| 62 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 63 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 82 | _kernel = create_opengl_kernel(_ctx, "activation_layer", build_opts); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 | { |
| 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 Iodice | 1e6c915 | 2019-08-20 11:27:20 +0100 | [diff] [blame] | 95 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 96 | } |
| 97 | else |
| 98 | { |
| 99 | update_window_and_padding(win, |
| 100 | AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration)); |
| 101 | } |
| 102 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 103 | IGCKernel::configure(win); |
| 104 | } |
| 105 | |
| 106 | void 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 Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 113 | _output->set_needs_shifting(true); |
| 114 | |
Georgios Pinitas | f92cb23 | 2018-03-29 11:51:54 +0100 | [diff] [blame] | 115 | 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 Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 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 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 125 | |
| 126 | do |
| 127 | { |
| 128 | unsigned int idx = 0; |
| 129 | unsigned int binding = 1; |
Georgios Pinitas | f92cb23 | 2018-03-29 11:51:54 +0100 | [diff] [blame] | 130 | add_3D_tensor_argument(idx, _input, binding++, slice); |
| 131 | add_3D_tensor_argument(idx, _output, binding++, slice_in); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 132 | _kernel.update_shader_params(); |
| 133 | enqueue(*this, slice); |
| 134 | } |
Georgios Pinitas | f92cb23 | 2018-03-29 11:51:54 +0100 | [diff] [blame] | 135 | while(collapsed.slide_window_slice_3D(slice) && collapsed.slide_window_slice_3D(slice_in)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 136 | } |