blob: f4c84f3d661a135da54162a975cf1e25895c8301 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2017-2020 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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/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 Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010037
38using namespace arm_compute;
39
40GCGEMMMatrixAccumulateBiasesKernel::GCGEMMMatrixAccumulateBiasesKernel()
Frank Leib9d38ee2017-12-05 10:43:33 +080041 : _accum(nullptr), _biases(nullptr), _lws(gles::NDRange(1U, 1U, 1U))
Anthony Barbier7068f992017-10-26 15:23:08 +010042{
43}
44
45void 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 Leib9d38ee2017-12-05 10:43:33 +080055 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 Barbier7068f992017-10-26 15:23:08 +010058
59 // Create kernel
60 build_opts.emplace("#define GEMM_ACCUMULATE_BIASES");
Frank Leib9d38ee2017-12-05 10:43:33 +080061
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 Barbier7068f992017-10-26 15:23:08 +010069 std::string dt_name = (accum->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
70 build_opts.emplace(("#define " + dt_name));
Frank Leib9d38ee2017-12-05 10:43:33 +080071
Anthony Barbier7068f992017-10-26 15:23:08 +010072 _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 Leib9d38ee2017-12-05 10:43:33 +080083#if defined(ACCUM_PROCESS_4X)
Anthony Barbier7068f992017-10-26 15:23:08 +010084 num_elems_processed_per_iteration = 4;
Frank Leib9d38ee2017-12-05 10:43:33 +080085#elif defined(ACCUM_PROCESS_8X) /* ACCUM_PROCESS_4X */
86 num_elems_processed_per_iteration = 8;
87#endif /* ACCUM_PROCESS_4X */
Anthony Barbier7068f992017-10-26 15:23:08 +010088 }
89
Frank Leib9d38ee2017-12-05 10:43:33 +080090 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 Barbier7068f992017-10-26 15:23:08 +010093
Frank Leib9d38ee2017-12-05 10:43:33 +080094 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 Barbier7068f992017-10-26 15:23:08 +010098
99 update_window_and_padding(win, biases_access, accum_access);
100
Anthony Barbier7068f992017-10-26 15:23:08 +0100101 IGCKernel::configure(win);
102}
103
104void 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 Barbier7068f992017-10-26 15:23:08 +0100121
Joel Liangabd03cf2018-01-08 15:20:48 +0800122 add_2D_tensor_argument(idx, _accum, 1, accum_slice);
123 add_1D_tensor_argument(idx, _biases, 2, biases_slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100124 _kernel.update_shader_params();
125
Frank Leib9d38ee2017-12-05 10:43:33 +0800126 enqueue(*this, accum_slice, _lws);
Anthony Barbier7068f992017-10-26 15:23:08 +0100127 }
128 while(window.slide_window_slice_2D(accum_slice));
129}