Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [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/GCGEMMMatrixAccumulateBiasesKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
| 29 | #include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" |
| 30 | #include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" |
| 31 | #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Utils.h" |
| 35 | #include "arm_compute/core/Validate.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | GCGEMMMatrixAccumulateBiasesKernel::GCGEMMMatrixAccumulateBiasesKernel() |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 41 | : _accum(nullptr), _biases(nullptr), _lws(gles::NDRange(1U, 1U, 1U)) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | void GCGEMMMatrixAccumulateBiasesKernel::configure(IGCTensor *accum, const IGCTensor *biases) |
| 46 | { |
| 47 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::F16, DataType::F32); |
| 48 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum); |
| 49 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() != 1); |
| 50 | |
| 51 | _biases = biases; |
| 52 | _accum = accum; |
| 53 | |
| 54 | std::set<std::string> build_opts; |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 55 | build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(_lws[0])); |
| 56 | build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(_lws[1])); |
| 57 | build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(_lws[2])); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 58 | |
| 59 | // Create kernel |
| 60 | build_opts.emplace("#define GEMM_ACCUMULATE_BIASES"); |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 61 | |
| 62 | #define ACCUM_PROCESS_4X |
| 63 | |
| 64 | #if defined(ACCUM_PROCESS_4X) |
| 65 | build_opts.emplace("#define ACCUM_PROCESS_4X"); |
| 66 | #elif defined(ACCUM_PROCESS_8X) /* ACCUM_PROCESS_4X */ |
| 67 | build_opts.emplace("#define ACCUM_PROCESS_8X"); |
| 68 | #endif /* ACCUM_PROCESS_4X */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 69 | std::string dt_name = (accum->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; |
| 70 | build_opts.emplace(("#define " + dt_name)); |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 71 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 72 | _kernel = GCKernelLibrary::get().create_kernel("gemm_accumulate_biases", build_opts); |
| 73 | |
| 74 | // Configure kernel window |
| 75 | unsigned int num_elems_processed_per_iteration = 1; |
| 76 | |
| 77 | if(_accum->info()->data_type() == DataType::F32) |
| 78 | { |
| 79 | num_elems_processed_per_iteration = 16; |
| 80 | } |
| 81 | else if(_accum->info()->data_type() == DataType::F16) |
| 82 | { |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 83 | #if defined(ACCUM_PROCESS_4X) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 84 | num_elems_processed_per_iteration = 4; |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 85 | #elif defined(ACCUM_PROCESS_8X) /* ACCUM_PROCESS_4X */ |
| 86 | num_elems_processed_per_iteration = 8; |
| 87 | #endif /* ACCUM_PROCESS_4X */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 90 | const int accum_width = accum->info()->dimension(0); |
| 91 | const int accum_padding_right = ceil_to_multiple(accum_width, num_elems_processed_per_iteration * _lws[0]) - accum_width; |
| 92 | BorderSize border = BorderSize(0, accum_padding_right, 0, 0); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 93 | |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 94 | Window win = calculate_max_enlarged_window(*_accum->info(), Steps(num_elems_processed_per_iteration), border); |
| 95 | |
| 96 | AccessWindowStatic biases_access(biases->info(), 0, 0, ceil_to_multiple(biases->info()->dimension(0), num_elems_processed_per_iteration * _lws[0]), biases->info()->dimension(1)); |
| 97 | AccessWindowStatic accum_access(_accum->info(), 0, 0, accum_width + accum_padding_right, _accum->info()->dimension(1)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 98 | |
| 99 | update_window_and_padding(win, biases_access, accum_access); |
| 100 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 101 | IGCKernel::configure(win); |
| 102 | } |
| 103 | |
| 104 | void GCGEMMMatrixAccumulateBiasesKernel::run(const Window &window) |
| 105 | { |
| 106 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 107 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); |
| 108 | |
| 109 | _kernel.use(); |
| 110 | |
| 111 | Window accum_slice = window.first_slice_window_2D(); |
| 112 | |
| 113 | Window biases_slice(accum_slice); |
| 114 | biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 115 | |
| 116 | // Run kernel |
| 117 | do |
| 118 | { |
| 119 | // Set arguments |
| 120 | unsigned int idx = 0; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 121 | |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame] | 122 | add_2D_tensor_argument(idx, _accum, 1, accum_slice); |
| 123 | add_1D_tensor_argument(idx, _biases, 2, biases_slice); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 124 | _kernel.update_shader_params(); |
| 125 | |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 126 | enqueue(*this, accum_slice, _lws); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 127 | } |
| 128 | while(window.slide_window_slice_2D(accum_slice)); |
| 129 | } |