Repository navigation
fix(cpu): refcount the BF16x3 matmul guard and drop the EXL3 duplicate (#4116) - #4127
Merged
Merged
Conversation
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.
Part of #4116 (item 2)
TL;DR
The CPU oneDNN BF16x3 matmul precision was guarded with the per-entry
save/restore pattern that #4102 removed from the TF32 and BLAS guards, in two
copies. It is now one reference-counted scope,
cpu_bf16x3_scope, shared byGGUF CPU, bitsandbytes CPU and the EXL3 torch kernel. The EXL3 duplicate is
deleted, so there is one implementation of the guard.
1. The problem
file:lineatfe302047dwith cpu_bf16x3_matmul():nn_modules/qlinear/gguf.py:1917with cpu_bf16x3_matmul():nn_modules/qlinear/bitsandbytes.py:730with _cpu_bf16x3_matmul():nn_modules/exllamav3_torch.py:555torch.backends.mkldnn.matmul.fp32_precisionutils/cpu_features.py:88-104nn_modules/exllamav3_torch.py:74-91Both guards read the process-global precision, set
bf16, and restore thevalue they read on exit. Two overlapping callers therefore clobber each other:
flowchart TD A["caller A enters: reads none, sets bf16"] --> B["caller B enters: reads bf16, keeps it"] B --> C["A exits: restores none"] C --> D["B's remaining matmuls run IEEE fp32"] D --> E["B's output differs from an all-BF16x3 run"]The issue measured the ordering directly: with A in, B in, then A out, B saw
noneafter A exited, from both former call sites.2. The new flow
flowchart TD A["cpu_bf16x3_scope enter"] --> B{"_CPU_BF16X3_DEPTH == 0"} B -- yes --> C{"current precision is bf16"} C -- yes --> D["caller already asked for it: do not count"] C -- no --> E["set bf16, save _CPU_BF16X3_RESTORE"] E --> F["_CPU_BF16X3_DEPTH = 1"] B -- no --> G["_CPU_BF16X3_DEPTH += 1"] F --> H["caller's matmuls run"] G --> H D --> H H --> I["exit: _CPU_BF16X3_DEPTH -= 1"] I --> J{"depth == 0 and a snapshot exists"} J -- yes --> K["restore the saved precision"] J -- no --> L["another caller still inside"]Three properties are deliberate:
RLock, notLock. The scope is re-entrant (a nested entry, or anEXL3 forward inside a GGUF scope). The lock is only held for the counter and
the setting write, never across
yield, so it does not serialize matmuls.enteredflag. A caller that cannot apply the pin (the caller alreadyselected BF16x3, or oneDNN refuses the write) yields without touching the
counter. Otherwise it could raise the depth with no snapshot and stop the
next eligible caller from applying the pin.
cpu_bf16x3_matmul()is now a thin eligibilitywrapper over
cpu_bf16x3_scope(), and the EXL3 module imports the samescope. Its own gate (
GPTQMODEL_EXL3_CPU_BF16X3, AVX512-BF16, the shapeheuristic) is unchanged and still evaluated at the call site.
The capability probe is shared too:
has_cpu_bf16x3_precision()inutils/cpu_features.pyholds the oneDNN probe, and each caller keeps its ownenvironment gate (
has_cpu_bf16x3_matmul()for GGUF/bitsandbytes,_cpu_bf16x3_support()for EXL3).3. Failure and ambiguity contract
4. Change inventory
gptqmodel/utils/cpu_features.pyRLock+ depth + snapshot, shared capability probegptqmodel/nn_modules/exllamav3_torch.pydocs/concurrency.mdtests/test_concurrency_guards.pytests/test_exllamav3_cpu.py5. Evidence
All measurements are CPU-only on a host with AVX512-BF16 (
has_avx512_bf16()true,
has_cpu_bf16x3_matmul()true), torch2.16.0.dev20261102+cu130,Python 3.14.10. "Before" is a worktree of
origin/mainatfe302047d.5.1 Guard order, both former call sites
A enters, B enters, A exits, B reads the precision:
cpu_features.cpu_bf16x3_matmul{"b_entry": "bf16", "b_after_A_exit": "none"}exllamav3_torch._cpu_bf16x3_matmul{"b_entry": "bf16", "b_after_A_exit": "none"}cpu_features.cpu_bf16x3_matmul{"b_entry": "bf16", "b_after_A_exit": "bf16"}exllamav3_torch.cpu_bf16x3_scope{"b_entry": "bf16", "b_after_A_exit": "bf16"}After the fix the probe also reports
{"duplicate_present": false, "same_object_as_cpu_features": true}: both call sites resolve to one function.5.2 2048-wide
ExllamaV3TorchLinearCPU forward, controlled interleavingReal checkpoint
Llama-3.2-1B-Instruct-EXL3-W4-ALL,q_proj(2048 -> 2048),512-row activations. A GGUF/bitsandbytes scope exits after the forward's first
chunk; the precision is recorded at every chunk:
bf16, bf16, none, nonebf16, bf16, bf16, bf165.3 Unconstrained race, 30 trials
A short EXL3 forward starts first; a 2048-wide EXL3 forward starts 25% of the
short forward's duration later and is compared against its all-BF16x3
reference:
PYTHON_GIL=0PYTHON_GIL=1The issue reported 5/30 and 1/30 on its own host; the hit rate depends on how
much of the guarded window is matmul rather than EXL3 weight decode, which is
why 5.2 (deterministic ordering) is the regression and this is the corroborating
sample.
6. Tests
New regressions in
tests/test_concurrency_guards.py:test_cpu_bf16x3_matmul_entry_point_keeps_the_second_callertest_cpu_bf16x3_scope_is_reference_countedtest_cpu_bf16x3_scope_is_reentrant_and_restores_onceRLock) and restores oncetest_cpu_bf16x3_scope_keeps_a_caller_selected_precisiontest_cpu_bf16x3_guard_has_one_implementationcpu_featuresshare the scope; the duplicate is goneFail-before on
fe302047d: this file reports5 failed, 3 passed. Theentry-point test fails behaviourally with
AssertionError: assert 'none' == 'bf16'at the "A exited, B still inside" line; the other four fail because theshared scope does not exist there.
7. Reviewer notes
thread holds it. That is inherent to a process-global setting; removing it
needs the issue's alternative (an explicit hi/lo bf16 split matmul), which
changes arithmetic and cost and is not attempted here.
GPTQMODEL_CPU_BF16X3andGPTQMODEL_EXL3_CPU_BF16X3keep their previous meanings; only thecapability probe they share was factored out.
docs/concurrency.mdnow lists four global resources (it listed three) andthe resource-map diagram includes
cpu_bf16x3_scope.