blob: 9de44fcb7345a1e235d6e9add20de23d6d9d03ac [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
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000049 const GemmArgs _args;
Pablo Telloeb82fd22018-02-23 13:43:50 +000050
Anthony Barbier5f707732018-07-03 16:22:02 +010051 const unsigned int _buffer_per_multi;
52
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000053 unsigned int k_block=0;
Anthony Barbier5f707732018-07-03 16:22:02 +010054 unsigned int n_block=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000055
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000056 const Toi *_B_pretransposed = nullptr;
Pablo Telloeb82fd22018-02-23 13:43:50 +000057
58public:
59 GemvPretransposed(GemvPretransposed &) = delete;
Anthony Barbier5f707732018-07-03 16:22:02 +010060 GemvPretransposed & operator= (GemvPretransposed &) = delete;
Pablo Telloeb82fd22018-02-23 13:43:50 +000061
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010062 GemvPretransposed(const GemmArgs &args)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000063 : _args(args),
64 _buffer_per_multi(args._Ksize * roundup(args._Nsize, strategy::out_width())) {
Pablo Telloeb82fd22018-02-23 13:43:50 +000065 /* For now don't do any blocking. TODO: figure out if we should. */
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000066 if (strategy::supports_accumulate() && args._cfg && args._cfg->inner_block_size) {
67 k_block = args._cfg->inner_block_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000068 } else {
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000069 k_block = args._Ksize;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000070 }
71
72 if (args._cfg && args._cfg->outer_block_size) {
73 n_block = args._cfg->outer_block_size;
74 } else {
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000075 n_block = args._Nsize;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000076 }
Pablo Telloeb82fd22018-02-23 13:43:50 +000077 }
78
Michalis Spyroue7e96e02018-04-13 13:44:10 +010079 // Window is number of out_width blocks, times number of multis.
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000080 ndrange_t get_window_size() const override {
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000081 return { iceildiv(_args._Nsize, strategy::out_width()) * _args._nmulti };
Pablo Telloeb82fd22018-02-23 13:43:50 +000082 }
83
84 // Actually execute the GEMV.
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010085 void execute(const ndcoord_t &work_range, const ndcoord_t &, int) override {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010086#ifdef CYCLE_PROFILING
Pablo Telloeb82fd22018-02-23 13:43:50 +000087 profiler prof;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010088#endif
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000089 strategy strat(_args._ci);
Pablo Telloeb82fd22018-02-23 13:43:50 +000090
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010091 const auto start = work_range.get_position(0);
92 const auto end = work_range.get_position_end(0);
93
Michalis Spyroue7e96e02018-04-13 13:44:10 +010094 /* Break the window values down into multis of interest... */
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000095 const unsigned int window_per_multi = iceildiv(_args._Nsize, strategy::out_width());
Anthony Barbier5f707732018-07-03 16:22:02 +010096 const unsigned int multi_0 = start / window_per_multi;
97 const unsigned int multi_end = end / window_per_multi;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010098
99 /* ... and figure out where we start and end in the first and last multi. */
Georgios Pinitas1d480652019-01-23 11:24:50 +0000100 const unsigned int n_0 = (start - (multi_0 * window_per_multi)) * strategy::out_width();
101 const unsigned int n_max = (end - (multi_end * window_per_multi)) * strategy::out_width();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000102
103 static_assert(std::is_same<Tr, Tri>::value, "GemvPretransposed: Result types must be the same.");
104
Anthony Barbier5f707732018-07-03 16:22:02 +0100105 for (unsigned int multi=multi_0; multi<=multi_end; multi++) {
106 const unsigned int n_start = (multi==multi_0) ? n_0 : 0;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000107 const unsigned int n_end = (multi==multi_end) ? n_max : _args._Nsize;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000108
Anthony Barbier5f707732018-07-03 16:22:02 +0100109 if (n_end <= n_start)
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100110 continue;
111
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000112 for (unsigned int k0=0; k0<_args._Ksize; k0+=k_block) {
113 unsigned int kmax = std::min(k0 + k_block, _args._Ksize);
Anthony Barbier5f707732018-07-03 16:22:02 +0100114
115 for (unsigned int n=n_start; n<n_end; n+=n_block) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100116 unsigned int nmax = std::min(n + n_block, n_end);
117#ifdef CYCLE_PROFILING
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000118 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (kmax-k0) * (nmax-n));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100119#endif
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000120 strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + k0,
121 _B_pretransposed + (multi * _buffer_per_multi) + (n * roundup(_args._Ksize, strategy::k_unroll())) + (k0 * strategy::out_width()),
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100122 this->_Cptr + (multi * this->_C_multi_stride) + n,
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000123 (nmax - n), (kmax-k0),
124 this->_bias ? this->_bias + (multi * this->_bias_multi_stride) + n : nullptr,
125 _args._act, (k0 != 0));
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100126 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000127 }
128 }
129 }
130
131 /* Pretransposed interface implementation */
Anthony Barbier5f707732018-07-03 16:22:02 +0100132 bool B_is_pretransposed() const override {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000133 return true;
134 }
135
Anthony Barbier5f707732018-07-03 16:22:02 +0100136 bool B_pretranspose_required() const override {
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000137 /* Transpose is required if _B_pretransposed is still nullptr */
138 return (_B_pretransposed == nullptr);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000139 }
140
Anthony Barbier5f707732018-07-03 16:22:02 +0100141 size_t get_B_pretransposed_array_size() const override {
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000142 return _buffer_per_multi * _args._nmulti * sizeof(To);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000143 }
144
Anthony Barbier5f707732018-07-03 16:22:02 +0100145 void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride) override {
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000146 Toi *B_buffer = reinterpret_cast<Toi *>(buffer);
147 strategy strat(_args._ci);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000148
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000149 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
150 strat.transforms.PrepareB(B_buffer + (multi * _buffer_per_multi), B + (multi * B_multi_stride), ldb, 0, _args._Nsize, 0, _args._Ksize);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000151 }
152
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000153 _B_pretransposed = B_buffer;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000154 }
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100155
Anthony Barbier5f707732018-07-03 16:22:02 +0100156 void set_pretransposed_B_data(void *buffer) override {
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000157 _B_pretransposed = reinterpret_cast<Toi *>(buffer);
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100158 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000159};
160
161} // namespace arm_gemm