|
| 1 | +/*************************************************************************** |
| 2 | +Copyright (c) 2024, The OpenBLAS Project |
| 3 | +All rights reserved. |
| 4 | +Redistribution and use in source and binary forms, with or without |
| 5 | +modification, are permitted provided that the following conditions are |
| 6 | +met: |
| 7 | +1. Redistributions of source code must retain the above copyright |
| 8 | +notice, this list of conditions and the following disclaimer. |
| 9 | +2. Redistributions in binary form must reproduce the above copyright |
| 10 | +notice, this list of conditions and the following disclaimer in |
| 11 | +the documentation and/or other materials provided with the |
| 12 | +distribution. |
| 13 | +3. Neither the name of the OpenBLAS project nor the names of |
| 14 | +its contributors may be used to endorse or promote products |
| 15 | +derived from this software without specific prior written permission. |
| 16 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE |
| 20 | +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 22 | +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 23 | +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 | +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 25 | +THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +*****************************************************************************/ |
| 27 | + |
| 28 | +#include <arm_sme.h> |
| 29 | + |
| 30 | +#include "common.h" |
| 31 | + |
| 32 | +// Outer product kernel. |
| 33 | +// Computes a 2SVL x 2SVL block of C, utilizing all four FP32 tiles of ZA. |
| 34 | +// This kernel is unpredicated, and assumes a full 2SVL x 2SVL block. |
| 35 | +__attribute__((always_inline)) inline void |
| 36 | +kernel_2x2(const float *A, const float *B, float *C, float alpha, |
| 37 | + size_t shared_dim, size_t a_step, size_t b_step, size_t c_step) |
| 38 | + __arm_out("za") __arm_streaming { |
| 39 | + const size_t svl = svcntw(); |
| 40 | + |
| 41 | + // Predicate set-up |
| 42 | + svbool_t ptrue = svptrue_b32(); |
| 43 | + |
| 44 | + // Load from C into ZA |
| 45 | + for (size_t i = 0; i < (svl >> 1); i++) { |
| 46 | + svld1_ver_za32(0, i, ptrue, &C[0 * svl + i * c_step]); |
| 47 | + svld1_ver_za32(1, i, ptrue, &C[1 * svl + i * c_step]); |
| 48 | + svld1_ver_za32(2, i, ptrue, &C[0 * svl + (i + svl) * c_step]); |
| 49 | + svld1_ver_za32(3, i, ptrue, &C[1 * svl + (i + svl) * c_step]); |
| 50 | + } |
| 51 | + |
| 52 | + svfloat32_t alpha_vec = svdup_f32(alpha); |
| 53 | + |
| 54 | + // Iterate through shared dimension (K) |
| 55 | + for (size_t k = 0; k < shared_dim; k++) { |
| 56 | + // Load column of A |
| 57 | + svfloat32x2_t cols_a = svld1_x2(svptrue_c32(), &A[k * a_step]); |
| 58 | + |
| 59 | + // Load row of B |
| 60 | + svfloat32x2_t rows_b = svld1_x2(svptrue_c32(), &B[k * b_step]); |
| 61 | + |
| 62 | + // Multiply B through by alpha |
| 63 | + svfloat32_t row_b_0 = svmul_x(ptrue, alpha_vec, svget2(rows_b, 0)); |
| 64 | + svfloat32_t row_b_1 = svmul_x(ptrue, alpha_vec, svget2(rows_b, 1)); |
| 65 | + |
| 66 | + // Perform outer products |
| 67 | + svmopa_za32_m(0, ptrue, ptrue, svget2(cols_a, 0), row_b_0); |
| 68 | + svmopa_za32_m(1, ptrue, ptrue, svget2(cols_a, 1), row_b_0); |
| 69 | + svmopa_za32_m(2, ptrue, ptrue, svget2(cols_a, 0), row_b_1); |
| 70 | + svmopa_za32_m(3, ptrue, ptrue, svget2(cols_a, 1), row_b_1); |
| 71 | + } |
| 72 | + |
| 73 | + // Store out to C from ZA |
| 74 | + for (size_t i = 0; i < (svl >> 1); i++) { |
| 75 | + // Store out one row of C per tile |
| 76 | + svst1_ver_za32(0, i, ptrue, &C[0 * svl + i * c_step]); |
| 77 | + svst1_ver_za32(1, i, ptrue, &C[1 * svl + i * c_step]); |
| 78 | + svst1_ver_za32(2, i, ptrue, &C[0 * svl + (i + svl) * c_step]); |
| 79 | + svst1_ver_za32(3, i, ptrue, &C[1 * svl + (i + svl) * c_step]); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Outer product kernel. |
| 84 | +// Computes an SVL x SVL block of C, utilizing a single FP32 tile of ZA (ZA0). |
| 85 | +// This kernel is predicated, and can handle under-filled blocks. |
| 86 | +__attribute__((always_inline)) inline void |
| 87 | +kernel_1x1(const float *A, const float *B, float *C, float alpha, |
| 88 | + size_t shared_dim, size_t a_len, size_t a_step, size_t b_len, |
| 89 | + size_t b_step, size_t c_step, size_t c_rows, size_t c_cols) |
| 90 | + __arm_out("za") __arm_streaming { |
| 91 | + |
| 92 | + // Predicate set-up |
| 93 | + svbool_t pg = svptrue_b32(); |
| 94 | + svbool_t pg_a = svwhilelt_b32((size_t)0, a_len); |
| 95 | + svbool_t pg_b = svwhilelt_b32((size_t)0, b_len); |
| 96 | + svbool_t pg_c = svwhilelt_b32((size_t)0, c_rows); |
| 97 | + |
| 98 | + // Load from C into ZA |
| 99 | + for (size_t i = 0; i < c_cols; i++) { |
| 100 | + svld1_ver_za32(0, i, pg_c, &C[i * c_step]); |
| 101 | + } |
| 102 | + |
| 103 | + svfloat32_t alpha_vec = svdup_f32_z(pg_b, alpha); |
| 104 | + |
| 105 | + // Iterate through shared dimension (K) |
| 106 | + for (size_t k = 0; k < shared_dim; k++) { |
| 107 | + // Load column of A |
| 108 | + svfloat32_t col_a = svld1(pg_a, &A[k * a_step]); |
| 109 | + // Load row of B |
| 110 | + svfloat32_t row_b = svld1(pg_b, &B[k * b_step]); |
| 111 | + // Multiply B through by alpha |
| 112 | + row_b = svmul_x(pg_b, alpha_vec, row_b); |
| 113 | + // Perform outer product |
| 114 | + svmopa_za32_m(0, pg, pg, col_a, row_b); |
| 115 | + } |
| 116 | + |
| 117 | + // Store out to C from ZA |
| 118 | + for (size_t i = 0; i < c_cols; i++) { |
| 119 | + svst1_ver_za32(0, i, pg_c, &C[i * c_step]); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +__arm_new("za") __arm_locally_streaming |
| 124 | + int CNAME(BLASLONG bm, BLASLONG bn, BLASLONG bk, FLOAT alpha0, FLOAT *ba, |
| 125 | + FLOAT *bb, FLOAT *C, BLASLONG ldc) { |
| 126 | + |
| 127 | + const BLASLONG num_rows = bm; |
| 128 | + const BLASLONG num_cols = bn; |
| 129 | + |
| 130 | + const FLOAT *a_ptr = ba; |
| 131 | + const FLOAT *b_ptr = bb; |
| 132 | + FLOAT *c_ptr = C; |
| 133 | + |
| 134 | + const BLASLONG svl = svcntw(); |
| 135 | + |
| 136 | + const BLASLONG a_step = bm; |
| 137 | + const BLASLONG b_step = bn; |
| 138 | + const BLASLONG c_step = ldc; |
| 139 | + |
| 140 | + // Block over rows of C (panels of A) |
| 141 | + BLASLONG row_idx = 0; |
| 142 | + |
| 143 | + // 2x2 loop |
| 144 | + BLASLONG row_batch = 2 * svl; |
| 145 | + |
| 146 | + // Block over row dimension of C |
| 147 | + for (; row_idx + row_batch <= num_rows; row_idx += row_batch) { |
| 148 | + BLASLONG col_idx = 0; |
| 149 | + BLASLONG col_batch = 2 * svl; |
| 150 | + |
| 151 | + // Block over column dimension of C |
| 152 | + for (; col_idx + col_batch <= num_cols; col_idx += col_batch) { |
| 153 | + kernel_2x2(&a_ptr[row_idx], &b_ptr[col_idx], |
| 154 | + &c_ptr[row_idx + col_idx * c_step], alpha0, bk, a_step, b_step, |
| 155 | + c_step); |
| 156 | + } |
| 157 | + |
| 158 | + // Handle under-filled blocks w/ 2x(1x1) kernels |
| 159 | + col_batch = 1 * svl; |
| 160 | + for (; col_idx < num_cols; col_idx += col_batch) { |
| 161 | + col_batch = MIN(col_batch, num_cols - col_idx); |
| 162 | + |
| 163 | + kernel_1x1(&a_ptr[row_idx], &b_ptr[col_idx], |
| 164 | + &c_ptr[row_idx + col_idx * c_step], alpha0, bk, svl, a_step, |
| 165 | + col_batch, b_step, c_step, svl, col_batch); |
| 166 | + |
| 167 | + kernel_1x1(&a_ptr[row_idx + svl], &b_ptr[col_idx], |
| 168 | + &c_ptr[(row_idx + svl) + col_idx * c_step], alpha0, bk, svl, |
| 169 | + a_step, col_batch, b_step, c_step, svl, col_batch); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Handle under-filled blocks w/ 1x1 kernels |
| 174 | + row_batch = 1 * svl; |
| 175 | + for (; row_idx < num_rows; row_idx += row_batch) { |
| 176 | + row_batch = MIN(row_batch, num_rows - row_idx); |
| 177 | + // Block over column dimension of C |
| 178 | + BLASLONG col_batch = svl; |
| 179 | + for (BLASLONG col_idx = 0; col_idx < num_cols; col_idx += col_batch) { |
| 180 | + col_batch = MIN(col_batch, num_cols - col_idx); |
| 181 | + kernel_1x1(&a_ptr[row_idx], &b_ptr[col_idx], |
| 182 | + &c_ptr[row_idx + col_idx * c_step], alpha0, bk, row_batch, |
| 183 | + a_step, col_batch, b_step, c_step, row_batch, col_batch); |
| 184 | + } |
| 185 | + } |
| 186 | + return 0; |
| 187 | +} |
0 commit comments