blob: 434070a46c870bffe4d7c6d532e75f2e22f203d7 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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"
36
37using namespace arm_compute;
38
39GCGEMMMatrixAccumulateBiasesKernel::GCGEMMMatrixAccumulateBiasesKernel()
40 : _accum(nullptr), _biases(nullptr)
41{
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;
54 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
55 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
56 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
57
58 // Create kernel
59 build_opts.emplace("#define GEMM_ACCUMULATE_BIASES");
60 std::string dt_name = (accum->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
61 build_opts.emplace(("#define " + dt_name));
62 _kernel = GCKernelLibrary::get().create_kernel("gemm_accumulate_biases", build_opts);
63
64 // Configure kernel window
65 unsigned int num_elems_processed_per_iteration = 1;
66
67 if(_accum->info()->data_type() == DataType::F32)
68 {
69 num_elems_processed_per_iteration = 16;
70 }
71 else if(_accum->info()->data_type() == DataType::F16)
72 {
73 num_elems_processed_per_iteration = 4;
74 }
75
76 Window win = calculate_max_window(*_accum->info(), Steps(num_elems_processed_per_iteration));
77
78 AccessWindowStatic biases_access(biases->info(), 0, 0, ceil_to_multiple(biases->info()->dimension(0), num_elems_processed_per_iteration), biases->info()->dimension(1));
79 AccessWindowHorizontal accum_access(_accum->info(), 0, num_elems_processed_per_iteration);
80
81 update_window_and_padding(win, biases_access, accum_access);
82
83 _kernel.clear_params();
84 // set shader params binding point
85 _kernel.set_shader_params_binding_point(0);
86
87 IGCKernel::configure(win);
88}
89
90void GCGEMMMatrixAccumulateBiasesKernel::run(const Window &window)
91{
92 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
93 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
94
95 _kernel.use();
96
97 Window accum_slice = window.first_slice_window_2D();
98
99 Window biases_slice(accum_slice);
100 biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
101
102 // Run kernel
103 do
104 {
105 // Set arguments
106 unsigned int idx = 0;
107 if(_accum->info()->data_type() == DataType::F32)
108 {
109 add_2D_tensor_argument(idx, _accum, 1, accum_slice);
110 add_1D_tensor_argument(idx, _biases, 2, biases_slice);
111 }
112 else if(_accum->info()->data_type() == DataType::F16)
113 {
114 add_2D_tensor_argument(idx, _accum, BufferParam(1, 3), accum_slice);
115 add_1D_tensor_argument(idx, _biases, BufferParam(2, 3), biases_slice);
116 }
117
118 _kernel.update_shader_params();
119
120 enqueue(*this, accum_slice);
121 }
122 while(window.slide_window_slice_2D(accum_slice));
123}