blob: 6d856e98c359757e225cb178a37eda29895c62ee [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Stephen Lie855c232018-01-04 14:13:22 +08002 * Copyright (c) 2017-2018 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"
36
37using namespace arm_compute;
38
39GCGEMMMatrixAccumulateBiasesKernel::GCGEMMMatrixAccumulateBiasesKernel()
Frank Leib9d38ee2017-12-05 10:43:33 +080040 : _accum(nullptr), _biases(nullptr), _lws(gles::NDRange(1U, 1U, 1U))
Anthony Barbier7068f992017-10-26 15:23:08 +010041{
42}
43
44void GCGEMMMatrixAccumulateBiasesKernel::configure(IGCTensor *accum, const IGCTensor *biases)
45{
46 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::F16, DataType::F32);
47 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum);
48 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() != 1);
49
50 _biases = biases;
51 _accum = accum;
52
53 std::set<std::string> build_opts;
Frank Leib9d38ee2017-12-05 10:43:33 +080054 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(_lws[0]));
55 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(_lws[1]));
56 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(_lws[2]));
Anthony Barbier7068f992017-10-26 15:23:08 +010057
58 // Create kernel
59 build_opts.emplace("#define GEMM_ACCUMULATE_BIASES");
Frank Leib9d38ee2017-12-05 10:43:33 +080060
61#define ACCUM_PROCESS_4X
62
63#if defined(ACCUM_PROCESS_4X)
64 build_opts.emplace("#define ACCUM_PROCESS_4X");
65#elif defined(ACCUM_PROCESS_8X) /* ACCUM_PROCESS_4X */
66 build_opts.emplace("#define ACCUM_PROCESS_8X");
67#endif /* ACCUM_PROCESS_4X */
Anthony Barbier7068f992017-10-26 15:23:08 +010068 std::string dt_name = (accum->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
69 build_opts.emplace(("#define " + dt_name));
Frank Leib9d38ee2017-12-05 10:43:33 +080070
Anthony Barbier7068f992017-10-26 15:23:08 +010071 _kernel = GCKernelLibrary::get().create_kernel("gemm_accumulate_biases", build_opts);
72
73 // Configure kernel window
74 unsigned int num_elems_processed_per_iteration = 1;
75
76 if(_accum->info()->data_type() == DataType::F32)
77 {
78 num_elems_processed_per_iteration = 16;
79 }
80 else if(_accum->info()->data_type() == DataType::F16)
81 {
Frank Leib9d38ee2017-12-05 10:43:33 +080082#if defined(ACCUM_PROCESS_4X)
Anthony Barbier7068f992017-10-26 15:23:08 +010083 num_elems_processed_per_iteration = 4;
Frank Leib9d38ee2017-12-05 10:43:33 +080084#elif defined(ACCUM_PROCESS_8X) /* ACCUM_PROCESS_4X */
85 num_elems_processed_per_iteration = 8;
86#endif /* ACCUM_PROCESS_4X */
Anthony Barbier7068f992017-10-26 15:23:08 +010087 }
88
Frank Leib9d38ee2017-12-05 10:43:33 +080089 const int accum_width = accum->info()->dimension(0);
90 const int accum_padding_right = ceil_to_multiple(accum_width, num_elems_processed_per_iteration * _lws[0]) - accum_width;
91 BorderSize border = BorderSize(0, accum_padding_right, 0, 0);
Anthony Barbier7068f992017-10-26 15:23:08 +010092
Frank Leib9d38ee2017-12-05 10:43:33 +080093 Window win = calculate_max_enlarged_window(*_accum->info(), Steps(num_elems_processed_per_iteration), border);
94
95 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));
96 AccessWindowStatic accum_access(_accum->info(), 0, 0, accum_width + accum_padding_right, _accum->info()->dimension(1));
Anthony Barbier7068f992017-10-26 15:23:08 +010097
98 update_window_and_padding(win, biases_access, accum_access);
99
Anthony Barbier7068f992017-10-26 15:23:08 +0100100 IGCKernel::configure(win);
101}
102
103void GCGEMMMatrixAccumulateBiasesKernel::run(const Window &window)
104{
105 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
106 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
107
108 _kernel.use();
109
110 Window accum_slice = window.first_slice_window_2D();
111
112 Window biases_slice(accum_slice);
113 biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
114
115 // Run kernel
116 do
117 {
118 // Set arguments
119 unsigned int idx = 0;
Anthony Barbier7068f992017-10-26 15:23:08 +0100120
Joel Liangabd03cf2018-01-08 15:20:48 +0800121 add_2D_tensor_argument(idx, _accum, 1, accum_slice);
122 add_1D_tensor_argument(idx, _biases, 2, biases_slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100123 _kernel.update_shader_params();
124
Frank Leib9d38ee2017-12-05 10:43:33 +0800125 enqueue(*this, accum_slice, _lws);
Anthony Barbier7068f992017-10-26 15:23:08 +0100126 }
127 while(window.slide_window_slice_2D(accum_slice));
128}