Skip to content

Commit 7acb122

Browse files
authored
Merge pull request #5373 from Mousius/bgemm-optimized
Add optimized BGEMM kernel for NEOVERSEV1 target
2 parents e927373 + 740efd7 commit 7acb122

File tree

13 files changed

+816
-99
lines changed

13 files changed

+816
-99
lines changed

benchmark/Makefile

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
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+
129
TOPDIR = ..
230
include $(TOPDIR)/Makefile.system
331

@@ -56,7 +84,7 @@ GOTO_LAPACK_TARGETS=
5684
endif
5785

5886
ifeq ($(BUILD_BFLOAT16),1)
59-
GOTO_BFLOAT_TARGETS=sbgemm.goto
87+
GOTO_BFLOAT_TARGETS=bgemm.goto sbgemm.goto
6088
else
6189
GOTO_BFLOAT_TARGETS=
6290
endif
@@ -635,6 +663,8 @@ zcholesky.essl : zcholesky.$(SUFFIX)
635663

636664
##################################### Sgemm ####################################################
637665
ifeq ($(BUILD_BFLOAT16),1)
666+
bgemm.goto : bgemm.$(SUFFIX) ../$(LIBNAME)
667+
$(CC) $(CFLAGS) -o $(@F) $^ $(CEXTRALIB) $(EXTRALIB) $(FEXTRALIB) -lm
638668
sbgemm.goto : sbgemm.$(SUFFIX) ../$(LIBNAME)
639669
$(CC) $(CFLAGS) -o $(@F) $^ $(CEXTRALIB) $(EXTRALIB) $(FEXTRALIB) -lm
640670
endif
@@ -2970,6 +3000,8 @@ zcholesky.$(SUFFIX) : cholesky.c
29703000
$(CC) $(CFLAGS) -c -DCOMPLEX -DDOUBLE -o $(@F) $^
29713001

29723002
ifeq ($(BUILD_BFLOAT16),1)
3003+
bgemm.$(SUFFIX) : gemm.c
3004+
$(CC) $(CFLAGS) -c -DBFLOAT16 -DBGEMM -UCOMPLEX -UDOUBLE -o $(@F) $^
29733005
sbgemm.$(SUFFIX) : gemm.c
29743006
$(CC) $(CFLAGS) -c -DBFLOAT16 -UCOMPLEX -UDOUBLE -o $(@F) $^
29753007
endif

benchmark/gemm.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333

3434
#ifdef DOUBLE
3535
#define GEMM BLASFUNC(dgemm)
36+
#elif defined(BFLOAT16) && defined(BGEMM)
37+
#define GEMM BLASFUNC(bgemm)
3638
#elif defined(BFLOAT16)
3739
#define GEMM BLASFUNC(sbgemm)
3840
#undef IFLOAT
@@ -60,8 +62,18 @@ int main(int argc, char *argv[]){
6062

6163
IFLOAT *a, *b;
6264
FLOAT *c;
65+
#ifdef BGEMM
66+
blasint one=1;
67+
blasint two=2;
68+
float alpha_in[] = {1.0, 0.0};
69+
float beta_in[] = {0.0, 0.0};
70+
FLOAT alpha[2], beta[2];
71+
sbstobf16_(&two, alpha_in, &one, alpha, &one);
72+
sbstobf16_(&two, beta_in, &one, beta, &one);
73+
#else
6374
FLOAT alpha[] = {1.0, 0.0};
6475
FLOAT beta [] = {0.0, 0.0};
76+
#endif
6577
char transa = 'N';
6678
char transb = 'N';
6779
blasint m, n, k, i, j, lda, ldb, ldc;

common_b.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@
3030
#define COMMON_B_H
3131

3232
#ifndef DYNAMIC_ARCH
33-
#define BGEMM_ONCOPY bgemm_oncopy
34-
#define BGEMM_OTCOPY bgemm_otcopy
35-
#define BGEMM_INCOPY bgemm_incopy
36-
#define BGEMM_ITCOPY bgemm_itcopy
33+
#define BGEMM_ONCOPY bgemm_oncopy
34+
#define BGEMM_OTCOPY bgemm_otcopy
35+
36+
#if BGEMM_DEFAULT_UNROLL_M == BGEMM_DEFAULT_UNROLL_N
37+
#define BGEMM_INCOPY bgemm_oncopy
38+
#define BGEMM_ITCOPY bgemm_otcopy
39+
#else
40+
#define BGEMM_INCOPY bgemm_incopy
41+
#define BGEMM_ITCOPY bgemm_itcopy
42+
#endif
3743

3844
#define BGEMM_BETA bgemm_beta
3945
#define BGEMM_KERNEL bgemm_kernel

interface/Makefile

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
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+
129
TOPDIR = ..
230
include $(TOPDIR)/Makefile.system
331

@@ -526,7 +554,7 @@ ifneq ($(BUILD_COMPLEX16),1)
526554
ZBLASOBJS=
527555
endif
528556

529-
FUNCOBJS = $(SBEXTOBJS) $(CXERBLAOBJS) $(SBBLASOBJS) $(SBLASOBJS) $(DBLASOBJS) $(CBLASOBJS) $(ZBLASOBJS) $(SHBLASOBJS)
557+
FUNCOBJS = $(SBEXTOBJS) $(CXERBLAOBJS) $(BBLASOBJS) $(SBBLASOBJS) $(SBLASOBJS) $(DBLASOBJS) $(CBLASOBJS) $(ZBLASOBJS) $(SHBLASOBJS)
530558

531559
ifeq ($(EXPRECISION), 1)
532560
FUNCOBJS += $(QBLASOBJS) $(XBLASOBJS)

kernel/Makefile.L3

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,10 @@ ZBLASOBJS += \
674674
endif
675675

676676
ifeq ($(BUILD_BFLOAT16), 1)
677+
BGEMMINCOPYOBJ_P = $(BGEMMINCOPYOBJ:.$(SUFFIX)=.$(PSUFFIX))
678+
BGEMMITCOPYOBJ_P = $(BGEMMITCOPYOBJ:.$(SUFFIX)=.$(PSUFFIX))
679+
BGEMMONCOPYOBJ_P = $(BGEMMONCOPYOBJ:.$(SUFFIX)=.$(PSUFFIX))
680+
BGEMMOTCOPYOBJ_P = $(BGEMMOTCOPYOBJ:.$(SUFFIX)=.$(PSUFFIX))
677681
SBGEMMINCOPYOBJ_P = $(SBGEMMINCOPYOBJ:.$(SUFFIX)=.$(PSUFFIX))
678682
SBGEMMITCOPYOBJ_P = $(SBGEMMITCOPYOBJ:.$(SUFFIX)=.$(PSUFFIX))
679683
SBGEMMONCOPYOBJ_P = $(SBGEMMONCOPYOBJ:.$(SUFFIX)=.$(PSUFFIX))
@@ -2998,6 +3002,20 @@ $(KDIR)xgemm_beta$(TSUFFIX).$(PSUFFIX) : $(KERNELDIR)/$(XGEMM_BETA)
29983002

29993003

30003004
ifeq ($(BUILD_BFLOAT16), 1)
3005+
$(BGEMMONCOPYOBJ_P) : $(KERNELDIR)/$(BGEMMONCOPY)
3006+
$(CC) $(PFLAGS) -c -DBFLOAT16 -DBGEMM -UDOUBLE -UCOMPLEX $< -o $@
3007+
3008+
$(BGEMMOTCOPYOBJ_P) : $(KERNELDIR)/$(BGEMMOTCOPY)
3009+
$(CC) $(PFLAGS) -c -DBFLOAT16 -DBGEMM -UDOUBLE -UCOMPLEX $< -o $@
3010+
3011+
ifneq ($(BGEMM_UNROLL_M), $(BGEMM_UNROLL_N))
3012+
$(BGEMMINCOPYOBJ_P) : $(KERNELDIR)/$(BGEMMINCOPY)
3013+
$(CC) $(PFLAGS) -c -DBFLOAT16 -DBGEMM -UDOUBLE -UCOMPLEX $< -o $@
3014+
3015+
$(BGEMMITCOPYOBJ_P) : $(KERNELDIR)/$(BGEMMITCOPY)
3016+
$(CC) $(PFLAGS) -c -DBFLOAT16 -DBGEMM -UDOUBLE -UCOMPLEX $< -o $@
3017+
endif
3018+
30013019
$(SBGEMMONCOPYOBJ_P) : $(KERNELDIR)/$(SBGEMMONCOPY)
30023020
$(CC) $(PFLAGS) -c -DBFLOAT16 -UDOUBLE -UCOMPLEX $< -o $@
30033021

@@ -3010,7 +3028,6 @@ $(SBGEMMINCOPYOBJ_P) : $(KERNELDIR)/$(SBGEMMINCOPY)
30103028

30113029
$(SBGEMMITCOPYOBJ_P) : $(KERNELDIR)/$(SBGEMMITCOPY)
30123030
$(CC) $(PFLAGS) -c -DBFLOAT16 -UDOUBLE -UCOMPLEX $< -o $@
3013-
30143031
endif
30153032
endif
30163033

@@ -3137,6 +3154,8 @@ endif
31373154

31383155

31393156
ifeq ($(BUILD_BFLOAT16), 1)
3157+
$(KDIR)bgemm_kernel$(TSUFFIX).$(PSUFFIX) : $(KERNELDIR)/$(BGEMMKERNEL) $(BGEMMDEPEND)
3158+
$(CC) $(PFLAGS) -c -DBFLOAT16 -DBGEMM -UDOUBLE -UCOMPLEX $< -o $@
31403159
$(KDIR)sbgemm_kernel$(TSUFFIX).$(PSUFFIX) : $(KERNELDIR)/$(SBGEMMKERNEL) $(SBGEMMDEPEND)
31413160
$(CC) $(PFLAGS) -c -DBFLOAT16 -UDOUBLE -UCOMPLEX $< -o $@
31423161
endif

kernel/arm64/KERNEL.NEOVERSEV1

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
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+
129
include $(KERNELDIR)/KERNEL.ARMV8SVE
230

331
SGEMVNKERNEL = gemv_n_sve_v1x3.c
432
DGEMVNKERNEL = gemv_n_sve_v1x3.c
533
SGEMVTKERNEL = gemv_t_sve_v1x3.c
634
DGEMVTKERNEL = gemv_t_sve_v1x3.c
735
ifeq ($(BUILD_BFLOAT16), 1)
36+
BGEMM_BETA = bgemm_beta_neon.c
37+
BGEMMKERNEL = bgemm_kernel_$(BGEMM_UNROLL_M)x$(BGEMM_UNROLL_N)_neoversev1.c
38+
ifneq ($(BGEMM_UNROLL_M), $(BGEMM_UNROLL_N))
39+
BGEMMINCOPY = sbgemm_ncopy_$(SBGEMM_UNROLL_M)_neoversev1.c
40+
BGEMMITCOPY = sbgemm_tcopy_$(SBGEMM_UNROLL_M)_neoversev1.c
41+
BGEMMINCOPYOBJ = bgemm_incopy$(TSUFFIX).$(SUFFIX)
42+
BGEMMITCOPYOBJ = bgemm_itcopy$(TSUFFIX).$(SUFFIX)
43+
endif
44+
BGEMMONCOPY = sbgemm_ncopy_$(BGEMM_UNROLL_N)_neoversev1.c
45+
BGEMMOTCOPY = sbgemm_tcopy_$(BGEMM_UNROLL_N)_neoversev1.c
46+
BGEMMONCOPYOBJ = bgemm_oncopy$(TSUFFIX).$(SUFFIX)
47+
BGEMMOTCOPYOBJ = bgemm_otcopy$(TSUFFIX).$(SUFFIX)
48+
849
SBGEMM_BETA = sbgemm_beta_neoversev1.c
950
SBGEMMKERNEL = sbgemm_kernel_$(SBGEMM_UNROLL_M)x$(SBGEMM_UNROLL_N)_neoversev1.c
1051
ifneq ($(SBGEMM_UNROLL_M), $(SBGEMM_UNROLL_N))
@@ -21,4 +62,4 @@ SBGEMMOTCOPYOBJ = sbgemm_otcopy$(TSUFFIX).$(SUFFIX)
2162
SBGEMVNKERNEL = sbgemv_n_neon.c
2263
SBGEMVTKERNEL = sbgemv_t_bfdot.c
2364

24-
endif
65+
endif

kernel/arm64/bgemm_beta_neon.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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_in, 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;
45+
bfloat16_t z;
46+
47+
bfloat16_t zero_bf16 = vcvth_bf16_f32(0.0f);
48+
bfloat16x8_t zeros = vdupq_n_bf16(zero_bf16);
49+
50+
bfloat16_t beta_bf16;
51+
memcpy(&beta_bf16, &beta_in, sizeof(bfloat16_t));
52+
float beta = vcvtah_f32_bf16(beta_bf16);
53+
float32x4_t beta_neon = vdupq_n_f32(beta);
54+
55+
ptr_c = (bfloat16_t *)c;
56+
57+
chunk = m >> 3;
58+
remain = m & 7;
59+
60+
if (beta == 0.0f){
61+
for (j = 0; j < n; j ++){
62+
ptr_c0 = ptr_c;
63+
ptr_c += ldc;
64+
65+
for (i = 0; i < chunk; i ++){
66+
vst1q_bf16(ptr_c0, zeros);
67+
ptr_c0 += 8;
68+
}
69+
70+
for (i = 0; i < remain; i ++){
71+
ptr_c0[0] = zero_bf16;
72+
ptr_c0 ++;
73+
}
74+
}
75+
} else {
76+
for (j = 0; j < n; j ++){
77+
ptr_c0 = ptr_c;
78+
ptr_c += ldc;
79+
80+
for (i = 0; i < chunk; i ++){
81+
x0 = vld1q_bf16(ptr_c0);
82+
83+
y0 = vcvtq_low_f32_bf16(x0);
84+
y1 = vcvtq_high_f32_bf16(x0);
85+
86+
y0 = vmulq_f32(y0, beta_neon);
87+
y1 = vmulq_f32(y1, beta_neon);
88+
89+
z0 = vcvtq_low_bf16_f32(y0);
90+
z0 = vcvtq_high_bf16_f32(z0, y1);
91+
92+
vst1q_bf16(ptr_c0, z0);
93+
94+
ptr_c0 += 8;
95+
}
96+
97+
for (i = 0; i < remain; i ++){
98+
x = vcvtah_f32_bf16(ptr_c0[0]);
99+
z = vcvth_bf16_f32(x * beta);
100+
101+
ptr_c0[0] = z;
102+
ptr_c0 ++;
103+
}
104+
}
105+
}
106+
return 0;
107+
};

0 commit comments

Comments
 (0)