Skip to content

Commit 5d16517

Browse files
committed
add neoversev1 bgemm kernels
1 parent 63ce52e commit 5d16517

4 files changed

Lines changed: 563 additions & 7 deletions

File tree

kernel/arm64/KERNEL.NEOVERSEV1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,18 @@ SBGEMMOTCOPYOBJ = sbgemm_otcopy$(TSUFFIX).$(SUFFIX)
2121
SBGEMVNKERNEL = sbgemv_n_neon.c
2222
SBGEMVTKERNEL = sbgemv_t_bfdot.c
2323

24+
endif
25+
26+
ifeq ($(BUILD_BFLOAT16_ONLY), 1)
27+
BGEMM_BETA = bgemm_beta_neon.c
28+
BGEMMKERNEL = bgemm_kernel_$(BGEMM_UNROLL_M)x$(BGEMM_UNROLL_N)_neoversev1.c
29+
BGEMMINCOPY = sbgemm_ncopy_$(BGEMM_UNROLL_M)_neoversev1.c
30+
BGEMMITCOPY = sbgemm_tcopy_$(BGEMM_UNROLL_M)_neoversev1.c
31+
BGEMMINCOPYOBJ = bgemm_incopy$(TSUFFIX).$(SUFFIX)
32+
BGEMMITCOPYOBJ = bgemm_itcopy$(TSUFFIX).$(SUFFIX)
33+
BGEMMONCOPY = sbgemm_ncopy_$(BGEMM_UNROLL_N)_neoversev1.c
34+
BGEMMOTCOPY = sbgemm_tcopy_$(BGEMM_UNROLL_N)_neoversev1.c
35+
BGEMMONCOPYOBJ = bgemm_oncopy$(TSUFFIX).$(SUFFIX)
36+
BGEMMOTCOPYOBJ = bgemm_otcopy$(TSUFFIX).$(SUFFIX)
37+
2438
endif

kernel/arm64/bgemm_beta_neon.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/***************************************************************************
2+
* Copyright (c) 2025, 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
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* *****************************************************************************/
28+
29+
#include "common.h"
30+
31+
#include <arm_neon.h>
32+
33+
int CNAME(BLASLONG m, BLASLONG n, BLASLONG dummy1, float beta, IFLOAT *dummy2,
34+
BLASLONG dummy3, IFLOAT *dummy4, BLASLONG dummy5, FLOAT *c,
35+
BLASLONG ldc) {
36+
BLASLONG i, j;
37+
BLASLONG chunk, remain;
38+
39+
bfloat16_t *ptr_c, *ptr_c0;
40+
41+
bfloat16x8_t x0, z0;
42+
float32x4_t y0, y1;
43+
44+
float x, z;
45+
46+
bfloat16_t zero_bf16 = vcvth_bf16_f32(0.0f);
47+
bfloat16x8_t zeros = vdupq_n_bf16(zero_bf16);
48+
49+
float32x4_t beta_neon = vdupq_n_f32(beta);
50+
51+
ptr_c = (bfloat16_t *)c;
52+
53+
chunk = m >> 3;
54+
remain = m & 7;
55+
56+
if (beta == 0.0f){
57+
for (j = 0; j < n; j ++){
58+
ptr_c0 = ptr_c;
59+
ptr_c += ldc;
60+
61+
for (i = 0; i < chunk; i ++){
62+
vst1q_bf16(ptr_c0, zeros);
63+
ptr_c0 += 8;
64+
}
65+
66+
for (i = 0; i < remain; i ++){
67+
ptr_c0[0] = zero_bf16;
68+
ptr_c0 ++;
69+
}
70+
}
71+
} else {
72+
for (j = 0; j < n; j ++){
73+
ptr_c0 = ptr_c;
74+
ptr_c += ldc;
75+
76+
for (i = 0; i < chunk; i ++){
77+
x0 = vld1q_bf16(ptr_c0);
78+
79+
y0 = vcvtq_low_f32_bf16(x0);
80+
y1 = vcvtq_high_f32_bf16(x0);
81+
82+
y0 = vmulq_f32(y0, beta_neon);
83+
y1 = vmulq_f32(y1, beta_neon);
84+
85+
z0 = vcvtq_low_bf16_f32(y0);
86+
z0 = vcvtq_high_bf16_f32(z0, y1);
87+
88+
vst1q_bf16(ptr_c0, z0);
89+
90+
ptr_c0 += 8;
91+
}
92+
93+
for (i = 0; i < remain; i ++){
94+
x = vcvtah_f32_bf16(ptr_c0[0]);
95+
z = vcvth_bf16_f32(x * beta);
96+
97+
ptr_c0[0] = z;
98+
ptr_c0 ++;
99+
}
100+
}
101+
}
102+
return 0;
103+
};

kernel/arm64/bgemm_beta.c renamed to kernel/arm64/bgemm_kernel_4x4_neoversev1.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,21 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
* *****************************************************************************/
2828

29+
#include <arm_sve.h>
30+
2931
#include "common.h"
3032

31-
#include <arm_neon.h>
33+
#define ALPHA_ONE
34+
#include "bgemm_kernel_4x4_neoversev1_impl.c"
35+
#undef ALPHA_ONE
36+
#include "bgemm_kernel_4x4_neoversev1_impl.c"
37+
38+
int CNAME(BLASLONG m, BLASLONG n, BLASLONG k, float alpha, IFLOAT *A, IFLOAT *B,
39+
FLOAT *C, BLASLONG ldc) {
40+
if (alpha == 1.0f)
41+
return bgemm_kernel_neoversev1_alpha_one(m, n, k, alpha, A, B, C, ldc);
42+
else
43+
return bgemm_kernel_neoversev1_alpha(m, n, k, alpha, A, B, C, ldc);
44+
return 0;
45+
}
3246

33-
int CNAME(BLASLONG m, BLASLONG n, BLASLONG dummy1, FLOAT beta, IFLOAT *dummy2,
34-
BLASLONG dummy3, IFLOAT *dummy4, BLASLONG dummy5, FLOAT *c,
35-
BLASLONG ldc) {
36-
printf("running bgemm_beta...\n");
37-
return 0;
38-
};

0 commit comments

Comments
 (0)