Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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/CL/kernels/CLGEMMMatrixAccumulateBiasesKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 31 | #include "arm_compute/core/Error.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" |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 39 | namespace |
| 40 | { |
| 41 | Status validate_arguments(const ITensorInfo *accum, const ITensorInfo *biases) |
| 42 | { |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum); |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(biases, accum); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() != 1); |
| 47 | |
| 48 | return Status{}; |
| 49 | } |
| 50 | |
| 51 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *accum, ITensorInfo *biases, GPUTarget gpu_target, |
| 52 | unsigned int &num_elems_processed_per_iteration) |
| 53 | { |
| 54 | // Select the vector size to use (8 for Bifrost; 16 for Midgard). |
Sam Laynton | 56e8e86 | 2018-04-05 13:26:08 +0100 | [diff] [blame^] | 55 | num_elems_processed_per_iteration = gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) ? 8 : 16; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 56 | |
| 57 | // Configure kernel window |
| 58 | Window win = calculate_max_window(*accum, Steps(num_elems_processed_per_iteration)); |
| 59 | |
| 60 | AccessWindowStatic biases_access(biases, 0, 0, ceil_to_multiple(biases->dimension(0), num_elems_processed_per_iteration), biases->dimension(1)); |
| 61 | AccessWindowHorizontal accum_access(accum, 0, num_elems_processed_per_iteration); |
| 62 | |
| 63 | bool window_changed = update_window_and_padding(win, biases_access, accum_access); |
| 64 | |
| 65 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 66 | return std::make_pair(err, win); |
| 67 | } |
| 68 | } // namespace |
| 69 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | CLGEMMMatrixAccumulateBiasesKernel::CLGEMMMatrixAccumulateBiasesKernel() |
| 71 | : _accum(nullptr), _biases(nullptr) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | void CLGEMMMatrixAccumulateBiasesKernel::configure(ICLTensor *accum, const ICLTensor *biases) |
| 76 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 77 | // Perform validate step |
| 78 | ARM_COMPUTE_ERROR_ON_NULLPTR(accum, biases); |
| 79 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(accum->info(), biases->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | |
| 81 | _biases = biases; |
| 82 | _accum = accum; |
| 83 | |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 84 | // Get the target gpu |
| 85 | GPUTarget gpu_target = get_target(); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 86 | unsigned int vector_size = 0; |
| 87 | |
| 88 | // Configure kernel window |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 89 | auto win_config = validate_and_configure_window(accum->info(), biases->info(), gpu_target, vector_size); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 90 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 91 | ICLKernel::configure(win_config.second); |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 92 | |
| 93 | // Add build options |
| 94 | CLBuildOptions build_opts; |
| 95 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(accum->info()->data_type())); |
| 96 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
| 97 | build_opts.add_option_if(is_data_type_fixed_point(accum->info()->data_type()), |
| 98 | "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(accum->info()->fixed_point_position())); |
Gian Marco Iodice | 578ab61 | 2017-06-23 09:34:33 +0100 | [diff] [blame] | 99 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | // Create kernel |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 101 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_accumulate_biases", build_opts.options())); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 102 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 104 | Status CLGEMMMatrixAccumulateBiasesKernel::validate(const ITensorInfo *accum, const ITensorInfo *biases, GPUTarget gpu_target) |
| 105 | { |
| 106 | unsigned int num_elems_processed_per_iteration = 0; |
| 107 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(accum, biases)); |
| 108 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(accum->clone().get(), biases->clone().get(), gpu_target, num_elems_processed_per_iteration).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 110 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void CLGEMMMatrixAccumulateBiasesKernel::run(const Window &window, cl::CommandQueue &queue) |
| 114 | { |
| 115 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 116 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 117 | |
| 118 | Window accum_slice = window.first_slice_window_2D(); |
| 119 | |
| 120 | Window biases_slice(accum_slice); |
| 121 | biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 122 | |
| 123 | // Run kernel |
| 124 | do |
| 125 | { |
| 126 | // Set arguments |
| 127 | unsigned int idx = 0; |
| 128 | add_2D_tensor_argument(idx, _accum, accum_slice); |
| 129 | add_1D_tensor_argument(idx, _biases, biases_slice); |
| 130 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 131 | enqueue(queue, *this, accum_slice, _lws_hint); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | } |
| 133 | while(window.slide_window_slice_2D(accum_slice)); |
| 134 | } |