blob: fb01a731b860d66916fc8134a0643eae56f44aa1 [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
Joseph Dobson6f8b17d2020-02-11 19:32:11 +00002 * Copyright (c) 2017-2020 Arm Limited.
Pablo Telloeb82fd22018-02-23 13:43:50 +00003 *
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#pragma once
25
26#include <stdio.h>
27
28#include "arm_gemm.hpp"
29
Vincent ABRIOU267e65d2020-05-27 16:26:46 +020030#include "arm_compute/core/NEON/kernels/arm_gemm/ndrange.hpp"
Pablo Telloeb82fd22018-02-23 13:43:50 +000031
Michalis Spyroue7e96e02018-04-13 13:44:10 +010032#ifdef CYCLE_PROFILING
33#include "profiler.hpp"
34#endif
35
Anthony Barbier5f707732018-07-03 16:22:02 +010036namespace arm_gemm {
37
Pablo Telloeb82fd22018-02-23 13:43:50 +000038// Implementation of the GemmCommon abstract class.
39//
40// This is implementation is for native GEMM with no transposition.
41//
42// By default the source data is used in-place, but if type conversion is
43// needed we need to allocate working space (CURRENTLY NOT IMPLEMENTED).
44
Anthony Barbier5f707732018-07-03 16:22:02 +010045template<typename strategy, typename To, typename Tr>
46class GemmNative : public GemmCommon<To, Tr> {
Pablo Telloeb82fd22018-02-23 13:43:50 +000047 typedef typename strategy::operand_type Toi;
Anthony Barbier5f707732018-07-03 16:22:02 +010048 typedef typename strategy::result_type Tri;
Pablo Telloeb82fd22018-02-23 13:43:50 +000049
50 const unsigned int _Msize;
51 const unsigned int _Nsize;
52 const unsigned int _Ksize;
53
Michalis Spyroue7e96e02018-04-13 13:44:10 +010054 const unsigned int _nbatches;
55 const unsigned int _nmultis;
56
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010057 const Activation _act;
Pablo Telloeb82fd22018-02-23 13:43:50 +000058
Anthony Barbier5f707732018-07-03 16:22:02 +010059 const CPUInfo * const _ci;
Pablo Telloeb82fd22018-02-23 13:43:50 +000060
Georgios Pinitas1d480652019-01-23 11:24:50 +000061 const unsigned int _k_block;
62 const unsigned int _n_block;
Pablo Telloeb82fd22018-02-23 13:43:50 +000063
Georgios Pinitas1d480652019-01-23 11:24:50 +000064 const NDRange<4> _window_range;
65
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010066 static unsigned int compute_k_block(const GemmArgs &args) {
Georgios Pinitas1d480652019-01-23 11:24:50 +000067 return args._Ksize;
David Manselle39334c2018-07-06 17:53:35 +010068 }
69
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010070 static unsigned int compute_n_block(const GemmArgs &args) {
Georgios Pinitas1d480652019-01-23 11:24:50 +000071 if ((args._cfg != nullptr) && args._cfg->outer_block_size > 0) {
72 return args._cfg->outer_block_size;
73 } else {
74 return args._Nsize;
75 }
David Manselle39334c2018-07-06 17:53:35 +010076 }
77
Pablo Telloeb82fd22018-02-23 13:43:50 +000078public:
79 GemmNative(GemmNative &) = delete;
Anthony Barbier5f707732018-07-03 16:22:02 +010080 GemmNative & operator= (GemmNative &) = delete;
Pablo Telloeb82fd22018-02-23 13:43:50 +000081
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010082 GemmNative(const GemmArgs &args)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010083 : _Msize(args._Msize), _Nsize(args._Nsize), _Ksize(args._Ksize),
84 _nbatches(args._nbatches), _nmultis(args._nmulti),
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010085 _act(args._act), _ci(args._ci),
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010086 _k_block(compute_k_block(args)), _n_block(compute_n_block(args)),
87 _window_range(iceildiv(_Msize, strategy::out_height()), _nbatches, iceildiv(_Nsize, _n_block), _nmultis) { }
Pablo Telloeb82fd22018-02-23 13:43:50 +000088
David Manselle39334c2018-07-06 17:53:35 +010089 // Window is amount per multi multiplied by total number of multis.
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000090 ndrange_t get_window_size() const override {
91 return { _window_range.total_size(), 1u, 1u, 1u, 1u, 1u };
Georgios Pinitas1d480652019-01-23 11:24:50 +000092 }
93
94 // Native GEMMs can always be dynamically scheduled (whether requested or not)
95 bool supports_dynamic_scheduling() const override {
96 return true;
Pablo Telloeb82fd22018-02-23 13:43:50 +000097 }
98
99 // Actually execute the GEMM.
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000100 void execute_1d(unsigned int start, unsigned int end, int) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100101#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +0000102 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100103#endif
Anthony Barbier5f707732018-07-03 16:22:02 +0100104 strategy strat(_ci);
105
Pablo Telloeb82fd22018-02-23 13:43:50 +0000106 static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same.");
107 static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same.");
108
Georgios Pinitas1d480652019-01-23 11:24:50 +0000109 auto p = _window_range.iterator(start, end);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000110
Georgios Pinitas1d480652019-01-23 11:24:50 +0000111 if (p.done()) {
112 return;
113 }
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100114
Georgios Pinitas1d480652019-01-23 11:24:50 +0000115 do {
116 unsigned int y0 = p.dim(0) * strategy::out_height();
117 unsigned int ymax = std::min(p.dim0_max() * strategy::out_height(), _Msize);
118 unsigned int batch = p.dim(1);
119 unsigned int n0 = p.dim(2) * _n_block;
120 unsigned int nmax = std::min(n0 + _n_block, _Nsize);
121 unsigned int multi = p.dim(3);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000122
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100123#ifdef CYCLE_PROFILING
Georgios Pinitas1d480652019-01-23 11:24:50 +0000124 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (ymax-y0) * (nmax - n0) * _Ksize);
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100125#endif
126
David Manselle39334c2018-07-06 17:53:35 +0100127 strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (y0 * this->_lda), this->_lda,
Georgios Pinitas1d480652019-01-23 11:24:50 +0000128 this->_Bptr + (multi * this->_B_multi_stride) + n0, this->_ldb,
129 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (y0 * this->_ldc) + n0, this->_ldc,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100130 (ymax-y0), (nmax-n0), _Ksize,
131 (strategy::supports_bias() && this->_bias) ? this->_bias + (multi * this->_bias_multi_stride) + n0 : nullptr,
132 _act, false);
133
134 // Add bias externally if needed
135 if (!strategy::supports_bias() && this->_bias) {
136 bias_adder(this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (y0 * this->_ldc) + n0, this->_ldc,
137 this->_bias + (multi * this->_bias_multi_stride) + n0,
138 (ymax - y0), (nmax - n0));
139 }
Georgios Pinitas1d480652019-01-23 11:24:50 +0000140 } while (p.next_dim1());
Pablo Telloeb82fd22018-02-23 13:43:50 +0000141 }
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000142
143 //Execute
144 void execute(const ndcoord_t& work_range, const ndcoord_t& thread_locator, int threadid) override {
145 UNUSED(thread_locator);
146
147 const auto start = work_range.get_position(0);
148 const auto stop = work_range.get_position_end(0);
149
150 execute_1d(start, stop, threadid);
151 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000152};
153
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100154} // namespace arm_gemm