Repository navigation
fix(cpu): serialize BF16x3 capability probing with the precision owner (#4486) - #4517
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.
Fixes #4486
Root cause
has_cpu_bf16x3_precision()probes oneDNN by mutating the process-globaltorch.backends.mkldnn.matmul.fp32_precision(snapshot -> set"bf16"->restore snapshot) but did not take
_CPU_BF16X3_LOCK, the lock that ownsthat setting for
cpu_bf16x3_scope().functools.lru_cachepermits concurrentcold misses, so:
ieeein_cpu_bf16x3_precision()and pauses.cpu_bf16x3_scope(): depth 0 -> setsbf16, depth 1.ieeesnapshot while depth is still 1.Result: an active BF16x3 scope runs with IEEE precision, violating the
"library-owned scope retains bf16 until its last participant exits" contract.
Fix
Probe and scope now share one process-global owner:
_CPU_BF16X3_LOCK,_CPU_BF16X3_DEPTH,_CPU_BF16X3_RESTOREabove the probe.has_cpu_bf16x3_precision()holds_CPU_BF16X3_LOCKfor the whole probe, so ascope entry/exit cannot interleave with the snapshot/restore.
_CPU_BF16X3_DEPTH > 0, an active scope has already proven oneDNN acceptsBF16x3; return
Truewithout writing, so the probe never clobbers the owner.No environment-variable switches and no caller-visible API change.
Before / after
Issue repro, adapted only so the probe's scheduling gate is bounded (the fix
makes the forward block on the owner lock, so the literal script's gate now
times out by design rather than observing the race):
Before (
origin/main@ba6ed5672):After (this branch):
Regression test:
tests/test_concurrency_guards.py::test_cpu_bf16x3_capability_probe_respects_an_active_scope1 failed—assert 'ieee' == 'bf16'(precision_in_scope)1 passedTests run
CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:
ROCm venv (torch 2.16.0.dev20261025+rocm10.1),
HIP_VISIBLE_DEVICES=2, thread caps = 4:Lint:
Branch merged
origin/main(461b42023) before the final test/lint run.Hardware
CPU-only fix; no GPU kernel involved. Host CPU: AMD Eng Sample
100-000001458-01withavx512_bf16 avx512bw avx512dq avx512f avx512vbmi avx512vl.The regression test uses the public probe/scope APIs with a fake precision
state, so it does not depend on BF16x3 hardware.
Limitations
forward thread enters the scope while the probe is paused; post-fix it blocks
on the owner lock, which is the intended serialization.
the capability probe;
tests/test_exllamav3_cpu.py(independent oracle suite)passes unchanged.