Skip to content

[BUG] GGUF Torch CPU picks fused (BF16x3) vs dense (IEEE) per shape by a wall-clock race: logits depend on concurrent CPU load and differ run to run #4590

Closed

Description

@Qubitium

Found on main @ 4b65923, CPU (x86-64 with AVX512-BF16, CPython 3.14t, PYTHON_GIL=0). This is a timing race, so it does not depend on the GIL.

What is wrong

GGUFTorchLinear._should_use_fused_k_forward (nn_modules/qlinear/gguf.py:1731-1747) picks between _forward_fused_k and _forward_dequant_matmul per (device, dtype, x.shape) with _autotune() (:1719-1725). _autotune() is a wall-clock race: 1 warmup and 2 timed launches per path, 5% margin. The two paths are not numerically equivalent. On CPU the fused path runs its FP32 GEMMs inside cpu_bf16x3_matmul() (oneDNN BF16x3, :1911), while the dense path is a plain IEEE FP32 matmul_fp32_reduce. So which arithmetic a module uses is decided by timing noise, and anything else running in the process changes that timing: another inference thread, a quantize running in parallel, a data loader.

The decision is cached per input shape, so every new prefill length re-runs the race. Two identical runs of the same model on the same input therefore return different logits, depending on the CPU load at the time of each first forward.

Repro

GGUF Q4_K_M checkpoint of Qwen2.5-0.5B-Instruct that I quantized with GPTQModel (GGUFConfig(bits=4, format="q_k_m")), run on CPU:

import sys, threading, torch
from gptqmodel import GPTQModel, BACKEND
from gptqmodel.nn_modules.qlinear import BaseQuantLinear
q = GPTQModel.load(sys.argv[1], backend=BACKEND.GGUF_TORCH, device="cpu", dtype=torch.float16)
mods = [m for m in q.model.modules() if isinstance(m, BaseQuantLinear)]
stop = threading.Event()
def busy():   # unrelated work in another thread of the same process
    a = torch.randn(1024, 1024)
    while not stop.is_set():
        a @ a
if sys.argv[3] == "busy":
    t = threading.Thread(target=busy); t.start()
x = torch.randint(100, 5000, (1, 7), generator=torch.Generator().manual_seed(1))
with torch.inference_mode():
    q.model(x)            # first forward: per-module fused-vs-dense autotune runs here
stop.set()
with torch.inference_mode():
    logits = q.model(x).logits.float()
fused = sum(bool(m._gguf_fused_autotune_cache.get(("cpu", None, torch.float16, (7, m.in_features)), False)) for m in mods)
torch.save(logits, sys.argv[2] + f"_{sys.argv[3]}_fused{fused}.pt")
print("RESULT", sys.argv[3], "modules_on_fused_path", fused, "of", len(mods))

Six processes, 3 idle and 3 with the busy thread, all with the same input:

RESULT idle modules_on_fused_path 168 of 168
RESULT busy modules_on_fused_path 167 of 168
RESULT idle modules_on_fused_path 168 of 168
RESULT busy modules_on_fused_path 165 of 168
RESULT idle modules_on_fused_path 168 of 168
RESULT busy modules_on_fused_path 168 of 168

logits vs the first idle run:
  idle 168/168, idle 168/168, busy 168/168 : bit-identical
  busy 167/168                              : max|dlogit| = 0.0186
  busy 165/168                              : max|dlogit| = 0.0186

Whenever every module made the same choice, the logits are bit-identical. When the other thread moved one to three modules onto the dense path, the logits changed. Without a deliberate load thread it still happens: three idle runs with module-level probes at M=1/7/33 picked 157, 158 and 166 of 168 modules for the fused path, and gave three different output hashes. A kernel-level stress test (8 threads on distinct modules, after warmup) also gives 117-147 bitwise mismatches against the serial reference out of 1512 checks.

Suggested fix

Either make the autotune choose only between numerically equivalent paths (for example, run the dense CPU path under the same cpu_bf16x3_matmul() scope as the fused one, or neither of them), or make the choice deterministic: a static shape rule like the existing fused_cpu_max_rows / fused_cpu_min_matrix_elements gates, with timing used only offline to tune those defaults. If timing-based selection stays, document that GGUF Torch outputs are not reproducible across runs, and default autotune to off for CPU.

Which test should have caught it

None today. A test that forces each branch (autotune_enabled=False vs a forced dense choice) and asserts bitwise-equal outputs would catch it, and so would a reproducibility test that runs the same input in two processes.

Severity

Low. The logit drift is small (about 0.02 here, no argmax flips in 7 positions), but GGUF Torch outputs on CPU are not reproducible and depend on what else the process is doing. That breaks eval reproducibility and serial-vs-concurrent comparisons.

🤖 Generated with Claude Code

Activity

  1. ZX-ModelCloud commented on Nov 17, 2026

    @ZX-ModelCloud
    Collaborator

    Claimed. I'm working on this issue and will submit a fix PR. Please avoid duplicate work in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions