blob: 945e363839a67e2efed8216e7b906e1e6081bb67 [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +01002 * 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 {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010090 return { iceildiv(_Nsize, strategy::out_width()) * _nmultis };
Pablo Telloeb82fd22018-02-23 13:43:50 +000091 }
92
93 // Actually execute the GEMV.
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010094 void execute(const ndcoord_t &work_range, const ndcoord_t &, int) override {
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
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100100 const auto start = work_range.get_position(0);
101 const auto end = work_range.get_position_end(0);
102
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100103 /* Break the window values down into multis of interest... */
Georgios Pinitas1d480652019-01-23 11:24:50 +0000104 const unsigned int window_per_multi = iceildiv(_Nsize, strategy::out_width());
Anthony Barbier5f707732018-07-03 16:22:02 +0100105 const unsigned int multi_0 = start / window_per_multi;
106 const unsigned int multi_end = end / window_per_multi;
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100107
108 /* ... and figure out where we start and end in the first and last multi. */
Georgios Pinitas1d480652019-01-23 11:24:50 +0000109 const unsigned int n_0 = (start - (multi_0 * window_per_multi)) * strategy::out_width();
110 const unsigned int n_max = (end - (multi_end * window_per_multi)) * strategy::out_width();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000111
112 static_assert(std::is_same<Tr, Tri>::value, "GemvPretransposed: Result types must be the same.");
113
Anthony Barbier5f707732018-07-03 16:22:02 +0100114 for (unsigned int multi=multi_0; multi<=multi_end; multi++) {
115 const unsigned int n_start = (multi==multi_0) ? n_0 : 0;
116 const unsigned int n_end = (multi==multi_end) ? n_max : _Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000117
Anthony Barbier5f707732018-07-03 16:22:02 +0100118 if (n_end <= n_start)
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100119 continue;
120
Anthony Barbier5f707732018-07-03 16:22:02 +0100121 for (unsigned int m0=0; m0<_Ksize; m0+=m_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100122 unsigned int mmax = std::min(m0 + m_block, _Ksize);
Anthony Barbier5f707732018-07-03 16:22:02 +0100123
124 for (unsigned int n=n_start; n<n_end; n+=n_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100125 unsigned int nmax = std::min(n + n_block, n_end);
126#ifdef CYCLE_PROFILING
Anthony Barbier5f707732018-07-03 16:22:02 +0100127 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (mmax-m0) * (nmax-n));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100128#endif
Pablo Telloeb82fd22018-02-23 13:43:50 +0000129 /* 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 +0000130 strat.kernel(_A_pretransposed + (multi * _buffer_per_multi) + (n * _Ksize) + (m0 * strategy::A_interleave()),
131 (_Ksize * strategy::A_interleave()),
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100132 this->_Aptr + (multi * this->_A_multi_stride) + m0,
133 this->_Cptr + (multi * this->_C_multi_stride) + n,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100134 static_cast<Tr>(0), (mmax-m0), (nmax-n));
135
136 // Handle activation separately for now
137 if (this->_bias) {
138 activator<true>(this->_Cptr + (multi * this->_C_multi_stride) + n, 0,
139 this->_bias + (multi * this->_bias_multi_stride) + n,
140 _act, 1, (nmax-n));
141 } else {
142 activator<false>(this->_Cptr + (multi * this->_C_multi_stride) + n, 0,
143 static_cast<const Tr *>(nullptr),
144 _act, 1, (nmax-n));
145 }
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100146 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000147 }
148 }
149 }
150
151 /* Pretransposed interface implementation */
Anthony Barbier5f707732018-07-03 16:22:02 +0100152 bool B_is_pretransposed() const override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000153 return true;
154 }
155
Anthony Barbier5f707732018-07-03 16:22:02 +0100156 bool B_pretranspose_required() const override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000157 /* Transpose is required if _A_pretransposed is still nullptr */
158 return (_A_pretransposed == nullptr);
159 }
160
Anthony Barbier5f707732018-07-03 16:22:02 +0100161 size_t get_B_pretransposed_array_size() const override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100162 return _buffer_per_multi * _nmultis * sizeof(To);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000163 }
164
Anthony Barbier5f707732018-07-03 16:22:02 +0100165 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 +0000166 Toi *A_buffer = reinterpret_cast<Toi *>(buffer);
167
Anthony Barbier5f707732018-07-03 16:22:02 +0100168 for (unsigned int multi=0; multi<_nmultis; multi++) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100169 /* Reverse sense here as we are dealing with B rather than A. So if
170 * strategy::A_transpose is false and _trB is false, we still
171 * transpose. */
Georgios Pinitas1d480652019-01-23 11:24:50 +0000172 if (_trB ^ strategy::A_transpose()) {
173 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 +0100174 } else {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000175 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 +0100176 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000177 }
178
179 _A_pretransposed = A_buffer;
180 }
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100181
Anthony Barbier5f707732018-07-03 16:22:02 +0100182 void set_pretransposed_B_data(void *buffer) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100183 _A_pretransposed = reinterpret_cast<Toi *>(buffer);
184 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000185};
186
187} // namespace arm_gemm