Skip to content

fix(cpu): serialize BF16x3 capability probing with the precision owner (#4486) - #4517

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4486-bf16x3-precision-owner
Nov 17, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4486-bf16x3-precision-owner

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4486

Root cause

has_cpu_bf16x3_precision() probes oneDNN by mutating the process-global
torch.backends.mkldnn.matmul.fp32_precision (snapshot -> set "bf16" ->
restore snapshot) but did not take _CPU_BF16X3_LOCK, the lock that owns
that setting for cpu_bf16x3_scope(). functools.lru_cache permits concurrent
cold misses, so:

  1. Probe A reads ieee in _cpu_bf16x3_precision() and pauses.
  2. Forward B enters cpu_bf16x3_scope(): depth 0 -> sets bf16, depth 1.
  3. A resumes and restores its stale ieee snapshot 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:

  • Move _CPU_BF16X3_LOCK, _CPU_BF16X3_DEPTH, _CPU_BF16X3_RESTORE above the probe.
  • has_cpu_bf16x3_precision() holds _CPU_BF16X3_LOCK for the whole probe, so a
    scope entry/exit cannot interleave with the snapshot/restore.
  • If _CPU_BF16X3_DEPTH > 0, an active scope has already proven oneDNN accepts
    BF16x3; return True without 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):

{'scope_entry': 'bf16', 'probe_supported': True,
 'scope_after_probe': 'ieee', 'active_depth': 1, 'final': 'ieee'}
AssertionError: scope_after_probe == 'ieee' expected bf16

After (this branch):

{'probe_supported': True, 'scope_entry': 'bf16',
 'scope_after_probe': 'bf16', 'active_depth': 1, 'final': 'ieee'}

Regression test: tests/test_concurrency_guards.py::test_cpu_bf16x3_capability_probe_respects_an_active_scope

  • before fix: 1 failed — assert 'ieee' == 'bf16' (precision_in_scope)
  • after fix: 1 passed

Tests run

CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_concurrency_guards.py tests/test_cpu_policy_4396.py tests/test_exllamav3_cpu.py -q
-> 80 passed

ROCm venv (torch 2.16.0.dev20261025+rocm10.1), HIP_VISIBLE_DEVICES=2, thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest \
  tests/test_concurrency_guards.py tests/test_cpu_policy_4396.py -q
-> 17 passed

Lint:

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

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-01 with avx512_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

  • The regression test uses a bounded (1 s) scheduling window: pre-fix the
    forward thread enters the scope while the probe is paused; post-fix it blocks
    on the owner lock, which is the intended serialization.
  • No numerical-drift or performance claim is made. The change only serializes
    the capability probe; tests/test_exllamav3_cpu.py (independent oracle suite)
    passes unchanged.

Comment thread tests/test_concurrency_guards.py Fixed
Comment thread tests/test_concurrency_guards.py Fixed
@Qubitium
Qubitium merged commit 4403a2c into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4486-bf16x3-precision-owner branch November 17, 2026 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] BF16x3 capability probing bypasses the process-global precision owner

1 participant