Skip to content

bfloat16: fix sbgemmt row-major uplo, k=0/alpha=0 beta, and SMP bscal - #6274

Open
M-Colley wants to merge 1 commit into
OpenMathLib:developfrom
M-Colley:fix/bfloat16-sbgemmt-bscal
Open

M-Colley wants to merge 1 commit into
OpenMathLib:developfrom
M-Colley:fix/bfloat16-sbgemmt-bscal

Conversation

@M-Colley

Copy link
Copy Markdown
Contributor

Summary

This change resolves three defects in bfloat16 routines. First, cblas_sbgemmt with CblasRowMajor assigns uplo = 0 for CblasUpper and uplo = 1 for CblasLower in interface/sbgemmt.c, which writes the transposed triangle of C because row-major layout swaps A and B (C^T = B^T * A^T). Second, sbgemmt delegates beta scaling to sbgemv_n and sbgemv_t, which return immediately when k == 0 without scaling C and still read A and B when alpha == 0. Third, interface/scal.c sets mode = BLAS_SINGLE | BLAS_REAL in the #ifdef SMP path when compiled with BGEMM (bscal and cblas_bscal), so blas_level1_thread partitions the bfloat16 vector with 4-byte strides (overflowing the array by 2x when n > 1048576 on multiple threads) and legacy_exec reads 4 bytes from the 2-byte bfloat16 alpha scalar.

Changes

  • Swap uplo under order == CblasRowMajor (CblasUpper -> 1, CblasLower -> 0) in interface/sbgemmt.c, matching interface/gemmt.c.
  • Handle k == 0 || alpha == ZERO prior to the sbgemv loop in interface/sbgemmt.c by scaling or zeroing each column slice of the active triangle of C with SCAL_K when beta != ONE.
  • Set mode = BLAS_BFLOAT16 | BLAS_REAL under #ifdef BGEMM in interface/scal.c.
  • Add utest regression tests (sbgemmt_rowmajor_uplo, sbgemmt_k0_and_alpha0_beta_scaling, and bscal_multithreaded) in utest/test_extensions/test_bgemm.c and register test_bgemm.c in utest/CMakeLists.txt to match utest/Makefile.

Verification

  • Built with -DBUILD_BFLOAT16=ON on Windows x86_64 (HASWELL, MinGW-w64 GCC 14.3.0, 22 threads) and verified that all three new bgemm regression tests fail prior to the fix and pass (26/26) with the fix applied, alongside the full openblas_utest and openblas_utest_ext suites.

Fix three bugs in bfloat16 BLAS routines:

1. In interface/sbgemmt.c, CblasRowMajor maps A*B^T by transposing C,
   which swaps upper and lower triangles. Flip uplo for CblasRowMajor
   (CblasUpper -> 1, CblasLower -> 0) to match interface/gemmt.c.
2. In interface/sbgemmt.c, sbgemv_n and sbgemv_t return immediately
   when k == 0 and read A and B when alpha == ZERO. Handle k == 0 and
   alpha == ZERO prior to the gemv loop by scaling or zeroing the
   target triangle of C via SCAL_K when beta != ONE.
3. In interface/scal.c, multithreaded bscal and cblas_bscal set mode to
   BLAS_SINGLE | BLAS_REAL, causing blas_level1_thread to advance by
   4-byte strides across 2-byte bfloat16 arrays and legacy_exec to read
   4 bytes from a 2-byte alpha scalar. Set mode to BLAS_BFLOAT16 |
   BLAS_REAL under BGEMM.

Add regression tests for all three cases in test_bgemm.c and register
test_bgemm.c in utest/CMakeLists.txt to match utest/Makefile.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant