blob: 30dee7fe215d86d818d07ff19714cadd805f52fb [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/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"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Error.h"
33#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Utils.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38using namespace arm_compute;
39
Georgios Pinitas358ca202017-12-07 16:47:52 +000040namespace
41{
42Status validate_arguments(const ITensorInfo *accum, const ITensorInfo *biases)
43{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010044 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(accum);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::F16, DataType::F32);
Georgios Pinitas358ca202017-12-07 16:47:52 +000046 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum);
Georgios Pinitas358ca202017-12-07 16:47:52 +000047 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() != 1);
48
49 return Status{};
50}
51
52std::pair<Status, Window> validate_and_configure_window(ITensorInfo *accum, ITensorInfo *biases, GPUTarget gpu_target,
53 unsigned int &num_elems_processed_per_iteration)
54{
55 // Select the vector size to use (8 for Bifrost; 16 for Midgard).
Georgios Pinitasa34286e2018-09-04 12:18:50 +010056 bool is_gpu_bifrost = gpu_target_is_in(gpu_target,
57 GPUTarget::G71, GPUTarget::G72, GPUTarget::G76,
58 GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT,
59 GPUTarget::G52, GPUTarget::G52LIT);
60 num_elems_processed_per_iteration = is_gpu_bifrost ? 8 : 16;
Georgios Pinitas358ca202017-12-07 16:47:52 +000061
62 // Configure kernel window
63 Window win = calculate_max_window(*accum, Steps(num_elems_processed_per_iteration));
64
65 AccessWindowStatic biases_access(biases, 0, 0, ceil_to_multiple(biases->dimension(0), num_elems_processed_per_iteration), biases->dimension(1));
66 AccessWindowHorizontal accum_access(accum, 0, num_elems_processed_per_iteration);
67
68 bool window_changed = update_window_and_padding(win, biases_access, accum_access);
69
70 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
71 return std::make_pair(err, win);
72}
73} // namespace
74
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075CLGEMMMatrixAccumulateBiasesKernel::CLGEMMMatrixAccumulateBiasesKernel()
76 : _accum(nullptr), _biases(nullptr)
77{
78}
79
80void CLGEMMMatrixAccumulateBiasesKernel::configure(ICLTensor *accum, const ICLTensor *biases)
81{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010082 configure(CLKernelLibrary::get().get_compile_context(), accum, biases);
83}
84
Manuel Bottini679fc962020-04-21 16:08:53 +010085void CLGEMMMatrixAccumulateBiasesKernel::configure(const CLCompileContext &compile_context, ICLTensor *accum, const ICLTensor *biases)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010086{
Georgios Pinitas358ca202017-12-07 16:47:52 +000087 // Perform validate step
88 ARM_COMPUTE_ERROR_ON_NULLPTR(accum, biases);
89 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(accum->info(), biases->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 _biases = biases;
92 _accum = accum;
93
Michalis Spyroua9676112018-02-22 18:07:43 +000094 // Get the target gpu
95 GPUTarget gpu_target = get_target();
Georgios Pinitas358ca202017-12-07 16:47:52 +000096 unsigned int vector_size = 0;
97
98 // Configure kernel window
Michalis Spyroua9676112018-02-22 18:07:43 +000099 auto win_config = validate_and_configure_window(accum->info(), biases->info(), gpu_target, vector_size);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000100 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100101 ICLKernel::configure_internal(win_config.second);
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000102
103 // Add build options
104 CLBuildOptions build_opts;
105 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(accum->info()->data_type()));
106 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100107
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100109 _kernel = create_kernel(compile_context, "gemm_accumulate_biases", build_opts.options());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000110}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
Georgios Pinitas358ca202017-12-07 16:47:52 +0000112Status CLGEMMMatrixAccumulateBiasesKernel::validate(const ITensorInfo *accum, const ITensorInfo *biases, GPUTarget gpu_target)
113{
114 unsigned int num_elems_processed_per_iteration = 0;
115 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(accum, biases));
116 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(accum->clone().get(), biases->clone().get(), gpu_target, num_elems_processed_per_iteration).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117
Georgios Pinitas358ca202017-12-07 16:47:52 +0000118 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119}
120
121void CLGEMMMatrixAccumulateBiasesKernel::run(const Window &window, cl::CommandQueue &queue)
122{
123 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
124 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
125
126 Window accum_slice = window.first_slice_window_2D();
127
128 Window biases_slice(accum_slice);
129 biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
130
131 // Run kernel
132 do
133 {
134 // Set arguments
135 unsigned int idx = 0;
136 add_2D_tensor_argument(idx, _accum, accum_slice);
137 add_1D_tensor_argument(idx, _biases, biases_slice);
138
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100139 enqueue(queue, *this, accum_slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 }
141 while(window.slide_window_slice_2D(accum_slice));
142}