Integrate oneDNN matmul threadpool backend - #970
Open
romirdes wants to merge 4 commits into
Open
Conversation
oneDNN backend that uses the high-level dnnl::matmul primitive with the THREADPOOL CPU runtime, coexisting with but mutually exclusive from the merged BRGeMM (SEQ ukernel) backend. oneDNN picks the kernel and calls back into gemma.cpp's hwy::ThreadPool through a threadpool_iface adapter for all parallelism. - ops/onednn_matmul.h / -inl.h: engine singleton, HwyThreadPoolAdapter bridge, reordered-weights cache, thread_local user-managed scratchpad, and DoMatMul_OneDnn that falls back to HWY on failure. - ops/matmul-inl.h: dispatch hook (BF16xBF16, M>1) with fallback to the stock path; MMPerKey bookkeeping field; zones caller label; bench fair-timing for the first JIT+reorder call. - Mutual exclusion enforced at three layers: CMake FATAL_ERROR, Bazel ambiguous-select(), and a C++ #error guard. - CMake + Bazel wiring to fetch/build oneDNN v3.11 (THREADPOOL runtime).
Move the shared body into bazel/onednn.bzl as onednn_targets(cpu_runtime),
which derives the runtime-dependent config lines and the one conditional
copt from its argument. Both BUILD files reduce to a single call:
onednn_targets(cpu_runtime = "SEQ") # bazel/onednn.BUILD
onednn_targets(cpu_runtime = "THREADPOOL") # bazel/onednn_threadpool.BUILD
…escriptor check. Other minor, non-performance edits.
…e/onednn-matmul-threadpool
romirdes
marked this pull request as ready for review
July 29, 2026 21:08
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.
Summary
Adds an optional oneDNN matmul-primitive backend for
MatMul, built against oneDNN's THREADPOOL CPU runtime so oneDNN parallelizes by calling back into gemma.cpp's existing thread pool instead of spawning its own threads.The backend is opt-in and off by default:
bazel build --define gemma_onednn_matmul=1 ...cmake -DGEMMA_ONEDNN_MATMUL=ON ...It is mutually exclusive with the existing BRGeMM backend (
GEMMA_ONEDNN_BRGEMM), because oneDNN's CPU runtime (SEQ vs THREADPOOL) is a whole-library compile-time choice. Enabling both fails fast: a CMakeFATAL_ERROR, an ambiguousselect()in Bazel, and an#errorguard inops/onednn_matmul.h.The path engages only for BF16×BF16 with
M > 1(M == 1 measured slower than stock gemma for some shapes) and falls through to the standard MatMul whenever oneDNN declines or fails, so behavior is unchanged when the flag is off.Changes
ops/onednn_matmul.h#error, and the threadpool adapter that lets oneDNN dispatch work onto gemma.cpp's thread pool.ops/onednn_matmul-inl.hDoMatMul_OneDnn()implementation: builds/caches the matmul primitive and reordered weights, applies scale andadd, returns false to decline unsupported cases.ops/matmul-inl.hM > 1, guarded byGEMMA_ONEDNN_MATMUL, with fall-through to the existing MatMul.ops/matmul.hops/onednn_matmul.h; addMMPerKey::onednn_builtto record that the oneDNN path handled a shape.ops/bench_matmul.ccutil/zones.hCallers::kOneDnnMatMulprofiler zone.util/zones.cc"OneDnnMatMul"name.BUILD.bazelgemma_onednn_matmulconfig setting,GEMMA_ONEDNN_MATMUL=1define, new headers, and the@onednn_tpdependency.MODULE.bazel@onednn_tparchive — same oneDNN v3.11 tarball/sha256 as@onednn(shared download cache), differentbuild_filefor the THREADPOOL runtime.bazel/onednn.bzlonednn_targets(cpu_runtime)derives the runtime-dependent config lines and conditional copt from its argument.bazel/onednn.BUILDonednn_targets(cpu_runtime = "SEQ")call.bazel/onednn_threadpool.BUILDonednn_targets(cpu_runtime = "THREADPOOL")call.CMakeLists.txtGEMMA_ONEDNN_MATMULoption, the mutual-exclusionFATAL_ERROR, and aFetchContentoneDNN v3.11 build configured withDNNL_CPU_RUNTIME=THREADPOOL.Testing
Built and benchmarked with
ops/bench_matmulunder both Bazel and CMake, against the stock MatMul and the BRGeMM backend. Default (flag-off) builds are unaffected.