From 6554530a57a6d73ff58cdf7e601ec3cab824a4c1 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Mon, 8 Jun 2026 04:45:49 +0200 Subject: kernels: fix OpenBLAS thread contention and numerical instability --- Makefile | 4 +++- qwen_asr_kernels.c | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index ddfa2b3..97d138e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ CC = gcc -CFLAGS_BASE = -Wall -Wextra -O3 -march=native -ffast-math +# Keep IEEE math semantics by default. Some GCC/OpenBLAS Linux builds produced +# NaN embeddings with -ffast-math. +CFLAGS_BASE = -Wall -Wextra -O3 -march=native LDFLAGS = -lm -lpthread CJSON_CFLAGS ?= $(shell sh -c 'pkg-config --cflags libcjson 2>/dev/null') CJSON_LDFLAGS ?= $(shell sh -c 'pkg-config --libs libcjson 2>/dev/null') diff --git a/qwen_asr_kernels.c b/qwen_asr_kernels.c index 6b2e955..24c8162 100644 --- a/qwen_asr_kernels.c +++ b/qwen_asr_kernels.c @@ -25,6 +25,10 @@ #endif #endif +#if defined(USE_OPENBLAS) +void openblas_set_num_threads(int num_threads); +#endif + #ifndef M_PI #define M_PI 3.14159265358979323846 #endif @@ -91,6 +95,14 @@ void qwen_set_threads(int n) { if (n < 1) n = 1; if (n > QWEN_MAX_THREADS) n = QWEN_MAX_THREADS; +#if defined(USE_OPENBLAS) + /* OpenBLAS owns the heavy F32 GEMM parallelism on Linux. Keep the + * auxiliary pool serial to avoid nested thread contention. */ + int blas_threads = n; + openblas_set_num_threads(blas_threads); + n = 1; +#endif + /* Shutdown existing workers */ if (tp.n_threads > 1) { pthread_mutex_lock(&tp.mutex); @@ -104,15 +116,31 @@ void qwen_set_threads(int n) { } tp.n_threads = n; - if (n <= 1) return; + if (n <= 1) { + if (qwen_verbose >= 2) { +#if defined(USE_OPENBLAS) + fprintf(stderr, "Thread pool: %d threads, OpenBLAS: %d threads\n", + n, blas_threads); +#else + fprintf(stderr, "Thread pool: %d threads\n", n); +#endif + } + return; + } for (int i = 0; i < n - 1; i++) { tp.tids[i] = i + 1; pthread_create(&tp.threads[i], NULL, worker_loop, &tp.tids[i]); } - if (qwen_verbose >= 2) + if (qwen_verbose >= 2) { +#if defined(USE_OPENBLAS) + fprintf(stderr, "Thread pool: %d threads, OpenBLAS: %d threads\n", + n, blas_threads); +#else fprintf(stderr, "Thread pool: %d threads\n", n); +#endif + } } int qwen_get_num_cpus(void) { -- cgit v1.3.1