blob: c6a3bc0edbaa6c03d4e3921aa1956f0ffe22ee6f [file] [log] [blame]
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01001/*
2 * Copyright (c) 2021 Arm Limited.
3 *
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 "utils.hpp"
25
26#include "bfloat.hpp"
27
Kevin Lo7195f712022-01-07 15:46:02 +080028#if !defined(__OpenBSD__)
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010029#include <alloca.h>
Kevin Lo7195f712022-01-07 15:46:02 +080030#endif /* !defined(__OpenBSD__) */
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010031
32namespace arm_gemm {
33
34/*
35 * Generic transform.
36 *
37 * Assuming the untransposed case, this works by first reading <BlockBy>
38 * consecutive values from the first input row. This same number of values
39 * are then read from the next <IntBy-1> rows. Now return to the first
40 * input row and repeat.
41 *
42 * Need to cope with the work requested in either dimension not actually
43 * being a multiple of the block sizes.
44 */
45template <unsigned int tIntBy, unsigned int BlockBy, bool Transposed, size_t TOutSize, size_t TInSize, VLType vlt>
46struct TransformImpl {
47 template <typename TOut, typename TIn>
48 static void Transform(TOut* out, const TIn* const in, const int stride,
49 const int y0, const int ymax, const int x0, const int xmax) {
50 // NOTE: This code is disabled to avoid the call to get_vector_length(), so templated transforms will not be
51 // correct for SVE. This is not an issue as we have specializations for all SVE cases.
52 // For SVE cases we multiply the interleave factor by the vector length.
53 // const unsigned int IntBy = tIntBy * (vlt == VLType::SVE ? get_vector_length<TOut>() / BlockBy : 1);
54 const unsigned int IntBy = tIntBy;
55
56 const int n_whole_y_blocks = (ymax - y0) / IntBy;
57 const int y_remainders = (ymax - y0) % IntBy;
58 const int n_y_blocks = n_whole_y_blocks + (y_remainders ? 1 : 0);
59
60 const int n_whole_x_blocks = (xmax - x0) / BlockBy;
61 const int x_remainders = (xmax - x0) % BlockBy;
62 const int n_x_blocks = n_whole_x_blocks + (x_remainders ? 1 : 0);
63
64 // "Y" loop: advance down the rows of the source IntBy rows at a time.
65 // Set up fill_rows to show the number rows to copy from, and blank_rows
66 // for the number of blank rows to add.
67 for (int y_block=0 ; y_block < n_y_blocks; y_block++) {
68 int fill_rows = (y_block < n_whole_y_blocks) ? IntBy : y_remainders;
69 int blank_rows = IntBy - fill_rows;
70
71 int y_base = y0 + (y_block * IntBy);
72
73 // So now advance along this block of rows, BlockBy columns at a time.
74 for (int x_block=0 ; x_block < n_x_blocks; x_block++) {
75 int fill_cols = (x_block < n_whole_x_blocks) ? BlockBy : x_remainders;
76 int blank_cols = BlockBy - fill_cols;
77
78 int x_base = x0 + (x_block * BlockBy);
79
80 for (int row = 0; row < fill_rows; row++) {
81 for (int col = 0; col < fill_cols; col++) {
82 // In-range copy. If it's transposed, we reverse the sense of rows and columns here.
83 if (Transposed) {
84 *out++ = static_cast<TOut>(in[(x_base + col) * stride + y_base + row]);
85 } else {
86 *out++ = static_cast<TOut>(in[(y_base + row) * stride + x_base + col]);
87 }
88 }
89 // "col" tail - row is in range but column is out of range.
90 for (int col=0; col < blank_cols; col++) {
91 *out++ = static_cast<TOut>(0);
92 }
93 }
94 // "row" tail - row is out of range so fill with zeros always.
95 TOut zeroval = static_cast<TOut>(0);
96 int pads = blank_rows * (fill_cols + blank_cols);
97
98 for (int i=0; i<pads; i++) {
99 out[i] = zeroval;
100 }
101
102 out += pads;
103 }
104 }
105 }
106
107 template <typename T>
108 static void Transform(T* out, const T* const in, const int stride,
109 const int k0, const int kmax, const int x0, const int xmax) {
110 Transform<T, T>(out, in, stride, k0, kmax, x0, xmax);
111 }
112};
113
114/*****************************************************************************/
115template <unsigned int IntBy, unsigned int BlockBy, bool Transposed, VLType vlt=VLType::None, typename TOut, typename TIn>
116void Transform(
117 TOut* out, const TIn* const in, const int stride,
118 const int k0, const int kmax, const int x0, const int xmax
119) {
120 // Redirect to a specialised implementation predicated on argument size.
121 TransformImpl<IntBy, BlockBy, Transposed, sizeof(TOut), sizeof(TIn), vlt>::Transform(
122 out, in, stride, k0, kmax, x0, xmax
123 );
124}
125/*****************************************************************************/
126
127#include "transforms/list.hpp"
128
129// We don't have assembler transforms for AArch32, generate templated ones here.
130#ifdef __arm__
131template void Transform<8, 1, true, VLType::None>(float *, const float *, int, int, int, int, int);
132#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
133template void Transform<8, 1, true, VLType::None>(float *, const __fp16 *, int, int, int, int, int);
134#endif // defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
135template void Transform<8, 1, true, VLType::None>(float *, const bfloat16 *, int, int, int, int, int);
136#endif // AArch32
137
138} // namespace arm_gemm