blob: 7f52ac5a1427be7be59cbf701c0960799dcd8e4a [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"
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010029#include "bias_adder.hpp"
Pablo Telloeb82fd22018-02-23 13:43:50 +000030#include "mergeresults.hpp"
Pablo Telloeb82fd22018-02-23 13:43:50 +000031#include "transform.hpp"
32
Michalis Spyroue7e96e02018-04-13 13:44:10 +010033#ifdef CYCLE_PROFILING
34#include "profiler.hpp"
35#endif
36
Anthony Barbier5f707732018-07-03 16:22:02 +010037namespace arm_gemm {
38
Pablo Telloeb82fd22018-02-23 13:43:50 +000039// Implementation of the GemmCommon abstract class.
40//
Michalis Spyroue7e96e02018-04-13 13:44:10 +010041// This is implementation is for GEMV with pretransposition.
Anthony Barbier5f707732018-07-03 16:22:02 +010042//
Michalis Spyroue7e96e02018-04-13 13:44:10 +010043// batches are not supported as a batched GEMV makes no sense (can be converted to a GEMM).
Anthony Barbier5f707732018-07-03 16:22:02 +010044template<typename strategy, typename To, typename Tr>
45class GemvPretransposed : public GemmCommon<To, Tr> {
Pablo Telloeb82fd22018-02-23 13:43:50 +000046 typedef typename strategy::operand_type Toi;
Anthony Barbier5f707732018-07-03 16:22:02 +010047 typedef typename strategy::result_type Tri;
Pablo Telloeb82fd22018-02-23 13:43:50 +000048
49 const unsigned int _Nsize;
50 const unsigned int _Ksize;
Anthony Barbier5f707732018-07-03 16:22:02 +010051
Michalis Spyroue7e96e02018-04-13 13:44:10 +010052 const unsigned int _nmultis;
Pablo Telloeb82fd22018-02-23 13:43:50 +000053
54 const bool _trB;
55
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010056 const Activation _act;
Pablo Telloeb82fd22018-02-23 13:43:50 +000057
Anthony Barbier5f707732018-07-03 16:22:02 +010058 const CPUInfo * const _ci;
Pablo Telloeb82fd22018-02-23 13:43:50 +000059
Anthony Barbier5f707732018-07-03 16:22:02 +010060 const unsigned int _buffer_per_multi;
61
62 unsigned int m_block=0;
63 unsigned int n_block=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000064
65 const Toi *_A_pretransposed = nullptr;
66
67public:
68 GemvPretransposed(GemvPretransposed &) = delete;
Anthony Barbier5f707732018-07-03 16:22:02 +010069 GemvPretransposed & operator= (GemvPretransposed &) = delete;
Pablo Telloeb82fd22018-02-23 13:43:50 +000070
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010071 GemvPretransposed(const GemmArgs &args)
72 : _Nsize(args._Nsize), _Ksize(args._Ksize), _nmultis(args._nmulti), _trB(args._trB), _act(args._act), _ci(args._ci),
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010073 _buffer_per_multi(_Ksize * iceildiv(_Nsize, strategy::A_interleave()) * strategy::A_interleave()) {
Pablo Telloeb82fd22018-02-23 13:43:50 +000074 /* For now don't do any blocking. TODO: figure out if we should. */
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000075 if (args._cfg && args._cfg->inner_block_size) {
76 m_block = args._cfg->inner_block_size;
77 } else {
78 m_block = _Ksize;
79 }
80
81 if (args._cfg && args._cfg->outer_block_size) {
82 n_block = args._cfg->outer_block_size;
83 } else {
84 n_block = _Nsize;
85 }
Pablo Telloeb82fd22018-02-23 13:43:50 +000086 }
87
Michalis Spyroue7e96e02018-04-13 13:44:10 +010088 // Window is number of out_width blocks, times number of multis.
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000089 ndrange_t get_window_size() const override {
90 return { iceildiv(_Nsize, strategy::out_width()) * _nmultis, 1u, 1u, 1u, 1u, 1u };
Pablo Telloeb82fd22018-02-23 13:43:50 +000091 }
92
93 // Actually execute the GEMV.
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000094 void execute_1d(unsigned int start, unsigned int end, int) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010095#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +000096 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010097#endif
Pablo Telloeb82fd22018-02-23 13:43:50 +000098 strategy strat(_ci);
99
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100100 /* Break the window values down into multis of interest... */
Georgios Pinitas1d480652019-01-23 11:24:50 +0000101 const unsigned int window_per_multi = iceildiv(_Nsize, strategy::out_width());
Anthony Barbier5f707732018-07-03 16:22:02 +0100102 const unsigned int multi_0 = start / window_per_multi;
103 const unsigned int multi_end = end / window_per_multi;
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100104
105 /* ... and figure out where we start and end in the first and last multi. */
Georgios Pinitas1d480652019-01-23 11:24:50 +0000106 const unsigned int n_0 = (start - (multi_0 * window_per_multi)) * strategy::out_width();
107 const unsigned int n_max = (end - (multi_end * window_per_multi)) * strategy::out_width();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000108
109 static_assert(std::is_same<Tr, Tri>::value, "GemvPretransposed: Result types must be the same.");
110
Anthony Barbier5f707732018-07-03 16:22:02 +0100111 for (unsigned int multi=multi_0; multi<=multi_end; multi++) {
112 const unsigned int n_start = (multi==multi_0) ? n_0 : 0;
113 const unsigned int n_end = (multi==multi_end) ? n_max : _Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000114
Anthony Barbier5f707732018-07-03 16:22:02 +0100115 if (n_end <= n_start)
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100116 continue;
117
Anthony Barbier5f707732018-07-03 16:22:02 +0100118 for (unsigned int m0=0; m0<_Ksize; m0+=m_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100119 unsigned int mmax = std::min(m0 + m_block, _Ksize);
Anthony Barbier5f707732018-07-03 16:22:02 +0100120
121 for (unsigned int n=n_start; n<n_end; n+=n_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100122 unsigned int nmax = std::min(n + n_block, n_end);
123#ifdef CYCLE_PROFILING
Anthony Barbier5f707732018-07-03 16:22:02 +0100124 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (mmax-m0) * (nmax-n));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100125#endif
Pablo Telloeb82fd22018-02-23 13:43:50 +0000126 /* This assumes that the underlying call was a GEMM with M=1; for the N=1 case we would have to pick up this->_Bptr below instead */
Georgios Pinitas1d480652019-01-23 11:24:50 +0000127 strat.kernel(_A_pretransposed + (multi * _buffer_per_multi) + (n * _Ksize) + (m0 * strategy::A_interleave()),
128 (_Ksize * strategy::A_interleave()),
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100129 this->_Aptr + (multi * this->_A_multi_stride) + m0,
130 this->_Cptr + (multi * this->_C_multi_stride) + n,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100131 static_cast<Tr>(0), (mmax-m0), (nmax-n));
132
133 // Handle activation separately for now
134 if (this->_bias) {
135 activator<true>(this->_Cptr + (multi * this->_C_multi_stride) + n, 0,
136 this->_bias + (multi * this->_bias_multi_stride) + n,
137 _act, 1, (nmax-n));
138 } else {
139 activator<false>(this->_Cptr + (multi * this->_C_multi_stride) + n, 0,
140 static_cast<const Tr *>(nullptr),
141 _act, 1, (nmax-n));
142 }
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100143 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000144 }
145 }
146 }
147
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000148 // Execute
149 void execute(const ndcoord_t& work_range, const ndcoord_t& thread_locator, int threadid) override {
150 UNUSED(thread_locator);
151
152 const auto start = work_range.get_position(0);
153 const auto size = work_range.get_size(0);
154 const auto stop = start + size;
155
156 execute_1d(start, stop, threadid);
157 }
158
Pablo Telloeb82fd22018-02-23 13:43:50 +0000159 /* Pretransposed interface implementation */
Anthony Barbier5f707732018-07-03 16:22:02 +0100160 bool B_is_pretransposed() const override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000161 return true;
162 }
163
Anthony Barbier5f707732018-07-03 16:22:02 +0100164 bool B_pretranspose_required() const override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000165 /* Transpose is required if _A_pretransposed is still nullptr */
166 return (_A_pretransposed == nullptr);
167 }
168
Anthony Barbier5f707732018-07-03 16:22:02 +0100169 size_t get_B_pretransposed_array_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100170 return _buffer_per_multi * _nmultis * sizeof(To);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000171 }
172
Anthony Barbier5f707732018-07-03 16:22:02 +0100173 void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride) override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000174 Toi *A_buffer = reinterpret_cast<Toi *>(buffer);
175
Anthony Barbier5f707732018-07-03 16:22:02 +0100176 for (unsigned int multi=0; multi<_nmultis; multi++) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100177 /* Reverse sense here as we are dealing with B rather than A. So if
178 * strategy::A_transpose is false and _trB is false, we still
179 * transpose. */
Georgios Pinitas1d480652019-01-23 11:24:50 +0000180 if (_trB ^ strategy::A_transpose()) {
181 Transform<strategy::A_interleave(), strategy::A_block(), false>(A_buffer + (multi * _buffer_per_multi), B + (multi * B_multi_stride), ldb, 0, _Nsize, 0, _Ksize);
Anthony Barbier5f707732018-07-03 16:22:02 +0100182 } else {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000183 Transform<strategy::A_interleave(), strategy::A_block(), true>(A_buffer + (multi * _buffer_per_multi), B + (multi * B_multi_stride), ldb, 0, _Nsize, 0, _Ksize);
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100184 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000185 }
186
187 _A_pretransposed = A_buffer;
188 }
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100189
Anthony Barbier5f707732018-07-03 16:22:02 +0100190 void set_pretransposed_B_data(void *buffer) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100191 _A_pretransposed = reinterpret_cast<Toi *>(buffer);
192 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000193};
194
195} // namespace arm_gemm