Skip to content

gemm_batch/sbgemv: fix validation, trans, 64-bit stride, and BFLOAT16 - #6279

Open
M-Colley wants to merge 1 commit into
OpenMathLib:developfrom
M-Colley:fix/gemm-batch-validation-sbgemm
Open

M-Colley wants to merge 1 commit into
OpenMathLib:developfrom
M-Colley:fix/gemm-batch-validation-sbgemm

Conversation

@M-Colley

Copy link
Copy Markdown
Contributor

Summary

gemm_batch, gemm_batch_strided, and sbgemv contain parameter validation defects, missing BFLOAT16 batch driver wiring, missing lowercase and 'R' Fortran transpose handling, and 32-bit batch stride integer overflow. This pull request resolves each defect and adds unit tests in utest/test_extensions.

Root Cause and Changes

  1. interface/gemm_batch.c sums group_size[i] without validating order, group_count < 0 (info = 14), or group_size[i] < 0 (info = 15). Negative group_size entries can cancel positive entries (group_size = {2, -2}), causing malloc(0) followed by an out-of-bounds heap write when populating args_array. Validating order, group_count, and every group_size[i] up front prevents heap corruption and avoids malloc(0).
  2. interface/gemm_batch_strided.c allocates count * sizeof(blas_arg_t) before validating arguments, reports invalid ldb as parameter 10 (b) and invalid ldc as parameter 13 (beta) due to unshifted parameter indices from gemm_batch.c, omits count < 0 (info = 17) validation, and computes i * stridea * COMPSIZE in 32-bit blasint arithmetic. Hoisting validation before allocation, reporting ldb (11), ldc (15), and count (17), and casting i to BLASLONG resolves all four issues.
  3. Both interface/gemm_batch.c and interface/gemm_batch_strided.c reject lowercase Fortran transpose characters ('n', 't', 'c', 'r') and uppercase 'R', omit #elif defined(BFLOAT16) definitions for SBGEMM_BATCH / sbgemm_batch_thread (falling through to sgemm_batch_thread), and skip purely imaginary alpha in complex small-matrix dispatch. driver/level3/CMakeLists.txt also omits sbgemm_batch_thread when BUILD_BFLOAT16 is enabled. Adding TOUPPER + 'R' handling, wiring sbgemm_batch_thread in both interfaces and CMake, and checking both real and imaginary parts of alpha brings batched GEMM into parity with interface/gemm.c.
  4. interface/sbgemv.c uses an unconditional else branch for CblasRowMajor, executing invalid CBLAS_ORDER values as row-major instead of reporting info = 0 via xerbla, and multiplies 1L * m * n using 32-bit long on 64-bit Windows. Checking else if (order == CblasRowMajor) and casting m to BLASLONG matches interface/gemv.c.

Verification

  • Added batch_validation_and_lowercase_trans in utest/test_extensions/test_cgemm.c and utest/test_extensions/test_zgemm.c, plus sbgemm_batch_and_sbgemv_order under BUILD_BFLOAT16 in utest/test_extensions/test_cgemm.c.
  • Built with -DBUILD_BFLOAT16=ON and verified all cgemm (96/96) and zgemm (95/95) extension tests pass.

Validate CBLAS order (info = 0), group_count < 0 (info = 14), and
group_size[i] < 0 (info = 15) in gemm_batch.c prior to allocation to
prevent heap buffer overflows on negative group sizes and malloc(0)
calls.

Hoist parameter validation in gemm_batch_strided.c before allocation,
report ldb as parameter 11, ldc as parameter 15, and negative count as
parameter 17, and widen batch stride arithmetic to BLASLONG.

Accept lowercase Fortran transpose specifiers and OpenBLAS 'R'/'r'
conjugate-no-transpose in gemm_batch.c and gemm_batch_strided.c, and
wire sbgemm_batch_thread in CMake and both BFLOAT16 batch interfaces.

Check order == CblasRowMajor explicitly in interface/sbgemv.c so
invalid CBLAS order invokes xerbla with info = 0, and widen m * n to
BLASLONG.

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