Skip to content

[CPU] Fix strided half-precision GEMM dimensions in BNNS and simd_gemm - #4947

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
Yunt1:fix/4739-cpu-segmented-mm-half
Nov 24, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
Yunt1:fix/4739-cpu-segmented-mm-half

Conversation

@Yunt1

@Yunt1 Yunt1 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4739 (follow-up to #4924 with simd_gemm fixed for Linux/Windows as well).

Summary

  • mlx/backend/cpu/gemms/bnns.cpp (macOS): BNNSNDArrayDescriptor::size for BNNSDataLayoutRowMajorMatrix was set to {lda, (M * K) / lda} and {ldb, (K * N) / ldb} instead of the actual logical matrix dimensions {cols, rows}. Set iA_desc.size to {a_transposed ? M : K, a_transposed ? K : M}, iB_desc.size to {b_transposed ? K : N, b_transposed ? N : K}, and o_desc.stride to {1, ldc}.
  • mlx/backend/cpu/gemms/simd_gemm.h, simd_bf16.cpp, simd_fp16.cpp (Linux/Windows): Pass and use lda, ldb, ldc in load_block and output store instead of assuming contiguous K/N strides.
  • tests/blas_tests.cpp: Add test segmented_mm float16 and bfloat16 covering all four transpose combinations.

Testing

  • macOS arm64 (-DMLX_BUILD_METAL=OFF -DMLX_BUILD_TESTS=ON): ./build/tests/tests -sf="*blas_tests.cpp" -> 2/2 test cases passed (31/31 assertions).
  • Syntax-checked simd_bf16.cpp and simd_fp16.cpp with clang++ -fsyntax-only.

@Yunt1
Yunt1 force-pushed the fix/4739-cpu-segmented-mm-half branch from 4f4b3b1 to 8783720 Compare November 23, 2026 02:14
@Yunt1 Yunt1 changed the title Fix/4739 cpu segmented mm half [CPU] Fix strided half-precision GEMM dimensions in BNNS and simd_gemm Nov 23, 2026
@zcbenz
zcbenz marked this pull request as ready for review November 24, 2026 08:32
@zcbenz
zcbenz force-pushed the fix/4739-cpu-segmented-mm-half branch from 8783720 to 101367e Compare November 24, 2026 08:32
@zcbenz
zcbenz merged commit c3ff43b into ml-explore:main Nov 24, 2026
24 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
ml-explore#4947)

Co-authored-by: Yunt1 <Yunt1@users.noreply.github.com>
Co-authored-by: Cheng <git@zcbenz.com>

Ported: osaurus-ai/mlx PR 26 already passes lda and ldb to simd_gemm (the
fix for ml-explore/mlx issue 4739). This commit adds what PR 26 did not: ldc
for the output, and the BNNS dimensions. The Highway path
(simd_low_precision_gemm.h) passes ldc to its simd_gemm call too.
tests/blas_tests.cpp keeps PR 26's test and adds Apple's.

(cherry picked from commit c3ff43b)
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.

CPU segmented_mm gives wrong values for bfloat16 and float16

2 participants