Skip to content

Commit 9d4ef3f

Browse files
authored
Merge pull request #6274 from M-Colley/fix/bfloat16-sbgemmt-bscal
bfloat16: fix sbgemmt row-major uplo, k=0/alpha=0 beta, and SMP bscal
2 parents 3380f92 + 3f33c6c commit 9d4ef3f

4 files changed

Lines changed: 134 additions & 2 deletions

File tree

‎interface/sbgemmt.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ void CNAME(enum CBLAS_ORDER order, enum CBLAS_UPLO Uplo,
230230
lda = LDB;
231231
ldb = LDA;
232232

233-
if (Uplo == CblasUpper) uplo = 0;
234-
if (Uplo == CblasLower) uplo = 1;
233+
if (Uplo == CblasUpper) uplo = 1;
234+
if (Uplo == CblasLower) uplo = 0;
235235

236236
if (TransB == CblasNoTrans)
237237
transa = 0;
@@ -315,6 +315,21 @@ void CNAME(enum CBLAS_ORDER order, enum CBLAS_UPLO Uplo,
315315

316316
IDEBUG_START;
317317

318+
if (k == 0 || alpha == ZERO) {
319+
if (beta != ONE) {
320+
if (uplo == 1) {
321+
for (i = 0; i < m; i++) {
322+
SCAL_K(m - i, 0, 0, beta, c + i * ldc + i, 1, NULL, 0, NULL, 0);
323+
}
324+
} else {
325+
for (i = 0; i < m; i++) {
326+
SCAL_K(i + 1, 0, 0, beta, c + i * ldc, 1, NULL, 0, NULL, 0);
327+
}
328+
}
329+
}
330+
return;
331+
}
332+
318333
const blasint incb = ((transb & 1) == 0) ? 1 : ldb;
319334

320335
if (uplo == 1) {

‎interface/scal.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ void CNAME(blasint n, FLOAT alpha, FLOAT *x, blasint incx){
102102

103103
#ifdef DOUBLE
104104
mode = BLAS_DOUBLE | BLAS_REAL;
105+
#elif defined(BGEMM)
106+
mode = BLAS_BFLOAT16 | BLAS_REAL;
105107
#else
106108
mode = BLAS_SINGLE | BLAS_REAL;
107109
#endif

‎utest/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ ${DIR_EXT}/test_ztrsv.c
9393
${DIR_EXT}/test_ctrsv.c
9494
${DIR_EXT}/test_zgemm.c
9595
${DIR_EXT}/test_cgemm.c
96+
${DIR_EXT}/test_bgemm.c
9697
)
9798

9899
# crashing on travis cl with an error code suggesting resource not found

‎utest/test_extensions/test_bgemm.c‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,118 @@ CTEST(bgemm, nn_alpha1p5_beta2_M50_N50_K50)
369369
ASSERT_DBL_NEAR_TOL(0.0f, rdiff, BGEMM_TOL);
370370
}
371371

372+
#ifndef NO_CBLAS
373+
CTEST(bgemm, sbgemmt_rowmajor_uplo)
374+
{
375+
blasint m = 4, k = 3, lda = 3, ldb = 4, ldc = 4;
376+
blasint i, j;
377+
bfloat16 a[12], b[12];
378+
float c_upper[16], c_lower[16];
379+
380+
for (i = 0; i < 12; i++) {
381+
a[i] = float_to_bf16(1.0f);
382+
b[i] = float_to_bf16(2.0f);
383+
}
384+
for (i = 0; i < 16; i++) {
385+
c_upper[i] = -99.0f;
386+
c_lower[i] = -99.0f;
387+
}
388+
389+
cblas_sbgemmt(CblasRowMajor, CblasUpper, CblasNoTrans, CblasNoTrans,
390+
m, k, 1.0f, a, lda, b, ldb, 0.0f, c_upper, ldc);
391+
for (i = 0; i < m; i++) {
392+
for (j = 0; j < m; j++) {
393+
float expected = (j >= i) ? 6.0f : -99.0f;
394+
ASSERT_DBL_NEAR_TOL((double)expected, (double)c_upper[i * ldc + j], SINGLE_EPS);
395+
}
396+
}
397+
398+
cblas_sbgemmt(CblasRowMajor, CblasLower, CblasNoTrans, CblasNoTrans,
399+
m, k, 1.0f, a, lda, b, ldb, 0.0f, c_lower, ldc);
400+
for (i = 0; i < m; i++) {
401+
for (j = 0; j < m; j++) {
402+
float expected = (j <= i) ? 6.0f : -99.0f;
403+
ASSERT_DBL_NEAR_TOL((double)expected, (double)c_lower[i * ldc + j], SINGLE_EPS);
404+
}
405+
}
406+
407+
for (i = 0; i < 16; i++) c_upper[i] = -99.0f;
408+
cblas_sbgemmt(CblasColMajor, CblasUpper, CblasNoTrans, CblasNoTrans,
409+
m, k, 1.0f, b, ldb, a, lda, 0.0f, c_upper, ldc);
410+
for (j = 0; j < m; j++) {
411+
for (i = 0; i < m; i++) {
412+
float expected = (i <= j) ? 6.0f : -99.0f;
413+
ASSERT_DBL_NEAR_TOL((double)expected, (double)c_upper[j * ldc + i], SINGLE_EPS);
414+
}
415+
}
416+
}
417+
#endif
418+
419+
CTEST(bgemm, sbgemmt_k0_and_alpha0_beta_scaling)
420+
{
421+
char uplo_u = 'U', uplo_l = 'L', trans = 'N';
422+
blasint m = 4, k0 = 0, k3 = 3, lda = 4, ldb = 4, ldc = 4;
423+
blasint i, j;
424+
float alpha1 = 1.0f, alpha0 = 0.0f, beta2 = 2.0f, beta0 = 0.0f;
425+
bfloat16 a[16], b[16];
426+
float c[16];
427+
428+
for (i = 0; i < 16; i++) {
429+
a[i] = (bfloat16)0x7fc0u; /* quiet NaN */
430+
b[i] = float_to_bf16(1.0f);
431+
c[i] = 3.0f;
432+
}
433+
434+
/* k == 0 with beta == 2 must scale only the upper triangle (column-major) */
435+
BLASFUNC(sbgemmt)(&uplo_u, &trans, &trans, &m, &k0, &alpha1,
436+
a, &lda, b, &ldb, &beta2, c, &ldc);
437+
for (j = 0; j < m; j++) {
438+
for (i = 0; i < m; i++) {
439+
float expected = (i <= j) ? 6.0f : 3.0f;
440+
ASSERT_DBL_NEAR_TOL((double)expected, (double)c[j * ldc + i], SINGLE_EPS);
441+
}
442+
}
443+
444+
/* alpha == 0 with beta == 0 must zero the lower triangle without reading NaN in A */
445+
for (i = 0; i < 16; i++) c[i] = (i == 0) ? (float)NAN : -99.0f;
446+
BLASFUNC(sbgemmt)(&uplo_l, &trans, &trans, &m, &k3, &alpha0,
447+
a, &lda, b, &ldb, &beta0, c, &ldc);
448+
for (j = 0; j < m; j++) {
449+
for (i = 0; i < m; i++) {
450+
float expected = (i >= j) ? 0.0f : -99.0f;
451+
ASSERT_DBL_NEAR_TOL((double)expected, (double)c[j * ldc + i], SINGLE_EPS);
452+
}
453+
}
454+
}
455+
456+
CTEST(bgemm, bscal_multithreaded)
457+
{
458+
blasint n = 1048600, guard = 64, incx = 1;
459+
blasint i;
460+
bfloat16 alpha = float_to_bf16(2.0f);
461+
bfloat16 init_val = float_to_bf16(1.5f);
462+
bfloat16 canary = float_to_bf16(7.0f);
463+
bfloat16 *x = (bfloat16 *)calloc((size_t)(n * 2 + guard), sizeof(bfloat16));
464+
465+
ASSERT_NOT_NULL(x);
466+
for (i = 0; i < n; i++) x[i] = init_val;
467+
for (i = n; i < n + guard; i++) x[i] = canary;
468+
469+
BLASFUNC(bscal)(&n, &alpha, x, &incx);
470+
471+
for (i = 0; i < n; i++) {
472+
if (bf16_to_float(x[i]) != 3.0f) {
473+
free(x);
474+
ASSERT_FAIL();
475+
}
476+
}
477+
for (i = n; i < n + guard; i++) {
478+
if (bf16_to_float(x[i]) != 7.0f) {
479+
free(x);
480+
ASSERT_FAIL();
481+
}
482+
}
483+
free(x);
484+
}
485+
372486
#endif /* BUILD_BFLOAT16 */

0 commit comments

Comments
 (0)