blob: 46424a59f5949952aebcdbfc6520bd7f25c01492 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Gian Marco1aede362018-02-05 15:07:36 +00002 * 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/runtime/GLES_COMPUTE/functions/GCGEMM.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
29#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.h"
30#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.h"
31#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h"
32#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.h"
33#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
38#include "arm_compute/runtime/ITensorAllocator.h"
39
40using namespace arm_compute;
Joel Liang1c5ffd62017-12-28 10:09:51 +080041using namespace arm_compute::gles_compute;
Anthony Barbier7068f992017-10-26 15:23:08 +010042
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000043GCGEMM::GCGEMM(std::shared_ptr<IMemoryManager> memory_manager)
44 : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _ma_kernel(), _tmp_a(), _tmp_b(), _is_interleaved_transposed(false), _run_addition(false)
Anthony Barbier7068f992017-10-26 15:23:08 +010045{
46}
47
Gian Marco1aede362018-02-05 15:07:36 +000048void GCGEMM::configure(const IGCTensor *a, const IGCTensor *b, const IGCTensor *c, IGCTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier7068f992017-10-26 15:23:08 +010049{
50 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32);
51 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output);
Gian Marco1aede362018-02-05 15:07:36 +000052 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
53 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
54 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.reshape_b_only_on_first_run(), "Reshape matrix B only on first run is not supported");
55 ARM_COMPUTE_UNUSED(gemm_info);
Anthony Barbier7068f992017-10-26 15:23:08 +010056
57 if(c != nullptr)
58 {
59 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c);
60 ARM_COMPUTE_ERROR_ON_MSG(a->info()->dimension(1) != c->info()->dimension(1), "The C matrix must have the same number of rows as the matrix A");
61 ARM_COMPUTE_ERROR_ON_MSG(b->info()->dimension(0) != c->info()->dimension(0), "The C matrix must have the same number of columns as the matrix C");
62 ARM_COMPUTE_ERROR_ON_MSG(c->info()->dimension(0) != output->info()->dimension(0), "The C matrix must have the same number of rows as the output matrix");
63 ARM_COMPUTE_ERROR_ON_MSG(c->info()->dimension(1) != output->info()->dimension(1), "The C matrix must have the same number of columns as the output matrix");
64 }
65
66 ARM_COMPUTE_ERROR_ON_MSG(a->info()->dimension(0) != b->info()->dimension(1), "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
67
68 // If the input tensor has less than 16 rows, we run a special version of GEMM without reshaping the input tensors
69 _is_interleaved_transposed = a->info()->dimension(1) > 16;
70
71 const IGCTensor *matrix_a = a;
72 const IGCTensor *matrix_b = b;
73
74 if(_is_interleaved_transposed)
75 {
76 matrix_a = &_tmp_a;
77 matrix_b = &_tmp_b;
78
79 TensorShape shape_tmp_a = a->info()->tensor_shape();
80 TensorShape shape_tmp_b = b->info()->tensor_shape();
81
82 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
83 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
84
85 const unsigned int transpose_w = max_gc_vector_width / data_size_from_type(b->info()->data_type());
86 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
87 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
88
89 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type(), a->info()->fixed_point_position());
90 _tmp_a.allocator()->init(info_a);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000091 _memory_group.manage(&_tmp_a);
Anthony Barbier7068f992017-10-26 15:23:08 +010092
93 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type(), b->info()->fixed_point_position());
94 _tmp_b.allocator()->init(info_b);
95
96 // Configure interleave kernel
97 _interleave_kernel.configure(a, &_tmp_a);
98
99 // Configure transpose kernel
100 _transpose_kernel.configure(b, &_tmp_b);
101 }
102
103 _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed);
104
105 if(_is_interleaved_transposed)
106 {
107 // Allocate intermediate tensors
108 _tmp_a.allocator()->allocate();
109 _tmp_b.allocator()->allocate();
110 }
111
112 // Configure matrix addition kernel
113 if(beta != 0 && c != nullptr)
114 {
115 _ma_kernel.configure(c, output, beta);
116 _run_addition = true;
117 }
118}
119
120void GCGEMM::run()
121{
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000122 _memory_group.acquire();
Anthony Barbier7068f992017-10-26 15:23:08 +0100123 if(_is_interleaved_transposed)
124 {
125 // Run interleave kernel
Joel Liangeb8f71e2017-12-27 13:16:00 +0800126 GCScheduler::get().dispatch(_interleave_kernel, false);
Anthony Barbier7068f992017-10-26 15:23:08 +0100127
128 // Run transpose kernel
Joel Liangeb8f71e2017-12-27 13:16:00 +0800129 GCScheduler::get().dispatch(_transpose_kernel, false);
130 GCScheduler::get().memory_barrier();
Anthony Barbier7068f992017-10-26 15:23:08 +0100131 }
132
133 // Run matrix multiply kernel
Joel Liangeb8f71e2017-12-27 13:16:00 +0800134 GCScheduler::get().dispatch(_mm_kernel, !_run_addition);
Anthony Barbier7068f992017-10-26 15:23:08 +0100135
136 // Run matrix addition kernel
137 if(_run_addition)
138 {
Joel Liangeb8f71e2017-12-27 13:16:00 +0800139 GCScheduler::get().memory_barrier();
140 GCScheduler::get().dispatch(_ma_kernel);
Anthony Barbier7068f992017-10-26 15:23:08 +0100141 }
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000142 _memory_group.release();
Anthony Barbier7068f992017-10-26 15:23:08 +0100143}