Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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/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" |
| 35 | #include "support/ToolchainSupport.h" |
| 36 | |
| 37 | #include <set> |
| 38 | #include <string> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | GCActivationLayerKernel::GCActivationLayerKernel() |
| 43 | : _input(nullptr), _output(nullptr) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void GCActivationLayerKernel::configure(IGCTensor *input, IGCTensor *output, ActivationLayerInfo act_info) |
| 48 | { |
| 49 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 50 | |
| 51 | // Make sure _kernel is initialized before calling the parent's configure |
| 52 | _input = input; |
| 53 | _output = input; |
| 54 | |
| 55 | if(output != nullptr) |
| 56 | { |
| 57 | // Output auto inizialitation if not yet initialized |
| 58 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 59 | |
| 60 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 61 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 62 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 63 | |
| 64 | _output = output; |
| 65 | } |
| 66 | |
| 67 | unsigned int num_elems_processed_per_iteration = 4 / input->info()->element_size(); |
| 68 | |
| 69 | // Set build options |
| 70 | std::set<std::string> build_opts; |
| 71 | std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; |
| 72 | build_opts.emplace(("#define " + string_from_activation_func(act_info.activation()))); |
| 73 | build_opts.emplace(("#define " + dt_name)); |
| 74 | build_opts.emplace(("#define A_VAL " + float_to_string_with_full_precision(act_info.a()))); |
| 75 | build_opts.emplace(("#define B_VAL " + float_to_string_with_full_precision(act_info.b()))); |
| 76 | build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1))); |
| 77 | build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1))); |
| 78 | build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1))); |
| 79 | |
| 80 | // Create kernel |
| 81 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("activation_layer", build_opts)); |
| 82 | |
| 83 | // Configure kernel window |
| 84 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 85 | |
| 86 | if(output != nullptr) |
| 87 | { |
| 88 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 89 | |
| 90 | update_window_and_padding(win, |
| 91 | AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration), |
| 92 | output_access); |
| 93 | |
| 94 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | update_window_and_padding(win, |
| 99 | AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration)); |
| 100 | } |
| 101 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 102 | IGCKernel::configure(win); |
| 103 | } |
| 104 | |
| 105 | void GCActivationLayerKernel::run(const Window &window) |
| 106 | { |
| 107 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 108 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); |
| 109 | |
| 110 | _kernel.use(); |
| 111 | |
| 112 | Window slice = window.first_slice_window_3D(); |
| 113 | |
| 114 | do |
| 115 | { |
| 116 | unsigned int idx = 0; |
| 117 | unsigned int binding = 1; |
| 118 | add_3D_tensor_argument(idx, _input, binding++, slice); |
| 119 | add_3D_tensor_argument(idx, _output, binding++, slice); |
| 120 | _kernel.update_shader_params(); |
| 121 | enqueue(*this, slice); |
| 122 | } |
| 123 | while(window.slide_window_slice_3D(slice)); |
| 124 | } |