Fix: Small matrix gemm_batch with DYNAMIC_ARCH - #5980
Merged
martin-frbg merged 1 commit intoAug 16, 2026
Conversation
… with DYNAMIC_ARCH activated Co-authored-by: Claude Code <noreply@anthropic.com> Co-authored-by: glm-5.3 <service@zhipuai.cn>
ajz34
force-pushed
the
fix/dynarch_small_matrix_batch
branch
from
August 15, 2026 15:05
95e7822 to
0b663a1
Compare
Contributor
Author
|
This comment presents the reproduction of MWE. The C code to reproduce the MWE./*
* MWE: grouped batched GEMM segfaults on small matrices in DYNAMIC_ARCH builds.
*
* Build: gcc bug2_dynarch_small_matrix_batch.c -o bug2 \
* -I<openblas>/build/generated -I<openblas>/build \
* -Wl,-rpath,<openblas>/build/lib -L<openblas>/build/lib -lopenblas
*
* Pass criterion: the call RETURNS (no SIGSEGV). Printed values are
* informational only.
*
* Co-authored-by: Claude Code <noreply@anthropic.com>
* Co-authored-by: glm-5.3 <service@zhipuai.cn>
*/
#include <stdio.h>
#include <cblas.h>
/* m*n*k <= 100^3 routes the batch through the SMALL_MATRIX_OPT kernels,
* which is where the bug lives; sizes are otherwise arbitrary. */
#define M 8
#define N 12
#define K 16
#define COUNT 3 /* matrices, all in one group */
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0); /* survive being killed mid-print */
printf("openblas: %s\n", openblas_get_config());
/* one col-major matrix per batch entry, fully in bounds */
double a[COUNT][M * K], b[COUNT][K * N], c[COUNT][M * N];
const double *ap[COUNT], *bp[COUNT];
double *cp[COUNT];
for (int t = 0; t < COUNT; t++) {
for (int i = 0; i < M * K; i++)
a[t][i] = i % 7 + 1;
for (int i = 0; i < K * N; i++)
b[t][i] = i % 5 + 1;
for (int i = 0; i < M * N; i++)
c[t][i] = 0.0;
ap[t] = a[t];
bp[t] = b[t];
cp[t] = c[t];
}
/* single group of COUNT identical-shaped problems */
CBLAS_TRANSPOSE ta[1] = { CblasNoTrans }, tb[1] = { CblasNoTrans };
blasint m[1] = { M }, n[1] = { N }, k[1] = { K };
blasint lda[1] = { M }, ldb[1] = { K }, ldc[1] = { M };
blasint group_count = 1, group_size[1] = { COUNT };
double alpha[1] = { 1.0 }, beta[1] = { 0.0 };
cblas_dgemm_batch(CblasColMajor, ta, tb, m, n, k, alpha, ap, lda, bp,
ldb, beta, cp, ldc, group_count, group_size);
for (int t = 0; t < COUNT; t++)
printf("matrix %d: c[0]=%.1f\n", t, c[t][0]);
printf("returned normally (no segfault)\n");
return 0;
}The code before PR will give Segmentation fault (exit=139). The correct behavior should be something like |
Collaborator
|
Indeed, yes, thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi devs!
This PR will fix segfault, when batched gemm on small matrices with DYNAMIC_ARCH compiled OpenBLAS.
DYNAMIC_ARCH seems to require
SMALL_KERNEL_ADDRto resolve dispatched small kernel.For non-DYNAMIC_ARCH, since there is only one table and not required to be resolved, so the current code works.
This was found when I'm developing rust's numpy-like toolkit RSTSR, when trying to use batched gemm for broadcasted matrix-multiplication implementation with OpenBLAS backend.
This bug was originally found in AI code agent session (Claude Code with model GLM-5.3).