|
| 1 | +/*************************************************************************** |
| 2 | +Copyright (c) 2026, 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 | +/* A panels of the SME2 kernels (sme2_gemm_packed.h) from op(A) with contiguous columns. */ |
| 29 | +/* |
| 30 | + * SME2 bf16 GEMM kernel for a 512-bit streaming vector length, the bfloat16 counterpart of |
| 31 | + * gemm_kernel_sme2.c: C += alpha * A * B on the packed panels of the level-3 driver. |
| 32 | + * |
| 33 | + * BFMOPA ZA32 with .H operands accumulates 2-wide dot products, so one instruction covers |
| 34 | + * two k steps: per k pair the kernel zips rows l and l+1 of the standard [k][m] and [k][n] |
| 35 | + * panels into the lane layout 2*i + (k & 1) that the outer product needs (a missing second |
| 36 | + * row is loaded under a false predicate, which zero-fills). C accumulates in four ZA32 |
| 37 | + * tiles of 16x16 fp32: tile rows are the wn columns of the block, tile columns the wm rows, |
| 38 | + * with the C load/store helpers of sme2_gemm_tile.h. |
| 39 | + * |
| 40 | + * A panels come in chunks of SBGEMM_UNROLL_M rows ([k][64], the last chunk narrower), the |
| 41 | + * B panels of the generic copies in power-of-two groups of SBGEMM_UNROLL_N columns. Without |
| 42 | + * SME2 at run time, or in the compiler, plain loops run on the same layout. |
| 43 | + */ |
| 44 | + |
| 45 | +#include "common.h" |
| 46 | +#include <string.h> |
| 47 | + |
| 48 | +#include "sme2_gemm_detect.h" |
| 49 | + |
| 50 | +#define S2B_UM GEMM_DEFAULT_UNROLL_M |
| 51 | +#define S2B_UN GEMM_DEFAULT_UNROLL_N |
| 52 | + |
| 53 | +#if S2B_UM != 64 || S2B_UN != 16 |
| 54 | +#error "the SME2 sbgemm kernel needs SBGEMM_UNROLL_M 64 and SBGEMM_UNROLL_N 16" |
| 55 | +#endif |
| 56 | + |
| 57 | +/* Width of the B panel group at column j0 of n: full groups, then the largest power of two left. */ |
| 58 | +static inline int s2b_wn(BLASLONG n, BLASLONG j0) { |
| 59 | + BLASLONG r = n - j0; |
| 60 | + if (r >= S2B_UN) return S2B_UN; |
| 61 | + int w = 1; |
| 62 | + while (2 * w <= r) w *= 2; |
| 63 | + return w; |
| 64 | +} |
| 65 | + |
| 66 | +static inline FLOAT s2b_cvt(IFLOAT h) { |
| 67 | + unsigned int u = (unsigned int)h << 16; |
| 68 | + FLOAT f; |
| 69 | + memcpy(&f, &u, sizeof(f)); |
| 70 | + return f; |
| 71 | +} |
| 72 | + |
| 73 | +#ifdef HAVE_SME2_GEMM |
| 74 | +#include "sme2_gemm_tile.h" |
| 75 | + |
| 76 | +#if S2_VL != 16 || S2_NR != 64 || S2_NT != 4 |
| 77 | +#error "the SME2 sbgemm kernel needs a 512-bit streaming vector length" |
| 78 | +#endif |
| 79 | + |
| 80 | +/* The file is built with the SME flags of its target (no SME2); the functions here enable SME2 themselves. */ |
| 81 | +#pragma clang attribute push(__attribute__((target("sme2"))), apply_to = function) |
| 82 | + |
| 83 | +S2_INL void s2b_op(const int SUB, int t, svbool_t p, svbfloat16_t b, svbfloat16_t a) S2_S S2_ZA { |
| 84 | + switch (t) { |
| 85 | + case 0: SUB ? svmops_za32_bf16_m(0, p, p, b, a) : svmopa_za32_bf16_m(0, p, p, b, a); break; |
| 86 | + case 1: SUB ? svmops_za32_bf16_m(1, p, p, b, a) : svmopa_za32_bf16_m(1, p, p, b, a); break; |
| 87 | + case 2: SUB ? svmops_za32_bf16_m(2, p, p, b, a) : svmopa_za32_bf16_m(2, p, p, b, a); break; |
| 88 | + default: SUB ? svmops_za32_bf16_m(3, p, p, b, a) : svmopa_za32_bf16_m(3, p, p, b, a); break; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/* ZA += or -= the A panel (wm rows) times the B panel (wn columns) over k steps of the packed layout. */ |
| 93 | +S2_NOINL void s2b_mac(const int SUB, BLASLONG k, const IFLOAT *a, BLASLONG wm, const IFLOAT *b, BLASLONG wn) S2_S |
| 94 | + S2_ZA { |
| 95 | + const svbool_t pt = svptrue_b16(); /* BFMOPA governs per 16-bit element */ |
| 96 | + const svbool_t pm0 = svwhilelt_b16_s32(0, (int32_t)wm); |
| 97 | + const svbool_t pm1 = svwhilelt_b16_s32(S2B_UN * 2, (int32_t)wm); |
| 98 | + const svbool_t pn = svwhilelt_b16_s32(0, (int32_t)wn); |
| 99 | + const svbool_t pf = svpfalse_b(); |
| 100 | + for (BLASLONG l = 0; l < k; l += 2) { |
| 101 | + const int two = l + 1 < k; |
| 102 | + const IFLOAT *ar = a + l * wm, *br = b + l * wn; |
| 103 | + const svuint16_t e0 = svld1_u16(pm0, ar), e1 = svld1_u16(pm1, ar + S2B_UN * 2); |
| 104 | + const svuint16_t o0 = svld1_u16(two ? pm0 : pf, ar + wm), o1 = svld1_u16(two ? pm1 : pf, ar + wm + S2B_UN * 2); |
| 105 | + const svbfloat16_t a0 = svreinterpret_bf16_u16(svzip1_u16(e0, o0)); |
| 106 | + const svbfloat16_t a1 = svreinterpret_bf16_u16(svzip2_u16(e0, o0)); |
| 107 | + const svbfloat16_t a2 = svreinterpret_bf16_u16(svzip1_u16(e1, o1)); |
| 108 | + const svbfloat16_t a3 = svreinterpret_bf16_u16(svzip2_u16(e1, o1)); |
| 109 | + const svbfloat16_t bv = svreinterpret_bf16_u16(svzip1_u16(svld1_u16(pn, br), svld1_u16(two ? pn : pf, br + wn))); |
| 110 | + s2b_op(SUB, 0, pt, bv, a0); |
| 111 | + s2b_op(SUB, 1, pt, bv, a1); |
| 112 | + s2b_op(SUB, 2, pt, bv, a2); |
| 113 | + s2b_op(SUB, 3, pt, bv, a3); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/* Block of wm rows and wn columns of C; sgn 1 for C += alpha AB with alpha = +-1 (the AB part |
| 118 | + added or subtracted in ZA), sgn 0 for a general alpha scaled on the way out of ZA. */ |
| 119 | +S2_NOINL static void s2b_blk(BLASLONG k, const IFLOAT *a, BLASLONG wm, const IFLOAT *b, BLASLONG wn, FLOAT *C, |
| 120 | + BLASLONG ldc, FLOAT alpha, int sgn) S2_S S2_ZA { |
| 121 | + if (sgn) { |
| 122 | + s2_c_load(1, S2_NT, C, ldc, (int)wn, (int)wm, S2_LOAD, (FLOAT)1); |
| 123 | + s2b_mac(alpha == (FLOAT)-1, k, a, wm, b, wn); |
| 124 | + s2_c_store(1, S2_NT, C, ldc, (int)wn, (int)wm); |
| 125 | + } else { |
| 126 | + svzero_za(); |
| 127 | + s2b_mac(0, k, a, wm, b, wn); |
| 128 | + s2_c_store_alpha(S2_NT, C, ldc, (int)wn, (int)wm, alpha, 1); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +__arm_new("za") __arm_locally_streaming static void s2b_drive(BLASLONG m, BLASLONG n, BLASLONG k, FLOAT alpha, |
| 133 | + const IFLOAT *sa, const IFLOAT *sb, FLOAT *C, |
| 134 | + BLASLONG ldc) { |
| 135 | + const int sgn = alpha == (FLOAT)1 || alpha == (FLOAT)-1; |
| 136 | + for (BLASLONG j0 = 0; j0 < n;) { |
| 137 | + const BLASLONG wn = n - j0 >= S2B_UN ? S2B_UN : s2b_wn(n, j0); |
| 138 | + const IFLOAT *b = sb + j0 * k; |
| 139 | + for (BLASLONG i0 = 0; i0 < m; i0 += S2B_UM) { |
| 140 | + const BLASLONG wm = m - i0 < S2B_UM ? m - i0 : S2B_UM; |
| 141 | + s2b_blk(k, sa + i0 * k, wm, b, wn, C + i0 + j0 * ldc, ldc, alpha, sgn); |
| 142 | + } |
| 143 | + j0 += wn; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +#pragma clang attribute pop |
| 148 | +#endif |
| 149 | + |
| 150 | +static void s2b_loops(BLASLONG m, BLASLONG n, BLASLONG k, FLOAT alpha, const IFLOAT *sa, const IFLOAT *sb, FLOAT *C, |
| 151 | + BLASLONG ldc) { |
| 152 | + for (BLASLONG j0 = 0; j0 < n;) { |
| 153 | + const BLASLONG wn = s2b_wn(n, j0); |
| 154 | + const IFLOAT *b = sb + j0 * k; |
| 155 | + for (BLASLONG i0 = 0; i0 < m; i0 += S2B_UM) { |
| 156 | + const BLASLONG wm = m - i0 < S2B_UM ? m - i0 : S2B_UM; |
| 157 | + const IFLOAT *a = sa + i0 * k; |
| 158 | + for (BLASLONG j = 0; j < wn; ++j) |
| 159 | + for (BLASLONG i = 0; i < wm; ++i) { |
| 160 | + FLOAT s = 0; |
| 161 | + for (BLASLONG l = 0; l < k; ++l) s += s2b_cvt(a[l * wm + i]) * s2b_cvt(b[l * wn + j]); |
| 162 | + C[i0 + i + (j0 + j) * ldc] += alpha * s; |
| 163 | + } |
| 164 | + } |
| 165 | + j0 += wn; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +int CNAME(BLASLONG m, BLASLONG n, BLASLONG k, FLOAT alpha, IFLOAT *ba, IFLOAT *bb, FLOAT *C, BLASLONG ldc) { |
| 170 | + if (m <= 0 || n <= 0 || k <= 0) return 0; |
| 171 | +#ifdef HAVE_SME2_GEMM |
| 172 | + if (s2_usable()) { |
| 173 | + s2b_drive(m, n, k, alpha, ba, bb, C, ldc); |
| 174 | + return 0; |
| 175 | + } |
| 176 | +#endif |
| 177 | + s2b_loops(m, n, k, alpha, ba, bb, C, ldc); |
| 178 | + return 0; |
| 179 | +} |
0 commit comments