Skip to content

fix(gguf): preserve CPU cancellation under BF16 autocast - #3743

Merged
Qubitium merged 2 commits into
mainfrom
codex/fix-gguf-cpu-autocast-cancellation
Nov 9, 2026
Merged

Qubitium merged 2 commits into
mainfrom
codex/fix-gguf-cpu-autocast-cancellation

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fix the remaining public GGUFTorchLinear.forward CPU cancellation error under BF16 autocast. On FP16 Q8_0 input, a 100000 dot plus -100000 FP32 bias returned -160; the independent FP64/FP32 oracle returns 0. The dense CPU path now disables autocast only while its FP32 torch.mm runs. Bias addition and final activation-dtype rounding remain in the existing path. The forced fused path is unchanged and tested as a control.

Flow

flowchart TD
    A["GGUFTorchLinear.forward"] --> B{"Dense CPU low precision with bias?"}
    B -- "Yes" --> C["_forward_dequant_matmul reconstructs temporary FP32 weight"]
    C --> D{"CPU autocast active?"}
    D -- "Yes" --> E["Disable CPU autocast around torch.mm"]
    D -- "No" --> F["Run torch.mm directly"]
    E --> G["Add FP32 bias and round once to activation dtype"]
    F --> G
    B -- "No" --> H["Existing fused or other reduction path"]
    A -. "Old outer BF16 autocast" .-> X["FP32 dot rounded to BF16 before bias; finite cancellation lost"]
    classDef failure fill:#fce8e6,stroke:#a33,color:#111
    class X failure
Loading
Mechanism Before After
Dense CPU reduction (gptqmodel/nn_modules/qlinear/gguf.py:1715-1716) Outer BF16 autocast narrowed the nominal FP32 torch.mm result before bias addition Temporarily disable CPU autocast for torch.mm when it is active
Forced fused CPU path (_forward_fused_k) Already returned 0 in this case Unchanged, covered as a control
Inference storage Packed Q8_0 weight and FP32 bias persisted Same persistent tensors; no expanded weight cache

Reference diagram, before/after details, and benchmark: GGUF dense fallback precision.

Tests and measurements

  • The new public-forward test uses independently decoded Q8_0 values and FP64/FP32 Torch oracles. It covers FP16/BF16 input, dense/fused CPU dispatch, normal/BF16-autocast execution, repeated forwards, output dtype, packed state, and absence of retained expanded tensors.
  • pytest tests/test_gguf_cpu_autocast_cancellation.py tests/test_gguf_bias_range.py tests/test_cpu_bias_cancellation.py tests/test_gguf_dynamic_format.py tests/kernels/test_gguf_torch_cpu.py tests/kernels/test_gguf_dense_bf16_precision.py tests/kernels/test_gguf_simple_fp16_range.py tests/kernels/test_gguf_other_fp16_range.py tests/kernels/test_gguf_inference_scale_cache.py tests/kernels/test_gguf_cpp.py -q: 63 passed, 110 skipped on Torch 2.16.0.dev20261102+cu130, CPU runtime.
  • PYTHON_GIL=0 pytest tests/test_gguf_cpu_autocast_cancellation.py -q: 8 passed. The new CPU autocast API probe passed in three separate subprocesses, each with 8 threads × 4 iterations.
  • Changed-file Ruff, git diff --check, and three Mermaid fences: passed.
  • Additional persistent tensor bytes: 0. For the 1×32 case, primary temporary payloads are a 128-byte FP32 weight, 128-byte promoted input, and 4-byte FP32 dot.
Outer BF16 autocast Before After Output before → after
Disabled 29.84 µs 30.59 µs 0 → 0
Enabled 32.25 µs 35.47 µs -160 → 0

Timing used one CPU thread, 100 warmups per path, and medians of five alternating groups of 500 public forwards; it includes dequantization and Python overhead.

Scope and environment

This branch starts from main containing #3727, #3728, and #3739. No public API or compatibility shim changes. The separate llama.cpp backend was not modified. Seven native Q8_0 checks were skipped because llama-cpp-python/the gguf oracle are unavailable. Three new #3739 C++ tests fail at GGUFCppKernel construction on this host because llama_cpp is missing; they do not enter the changed Torch reduction. CUDA/ROCm paths were not run.

Do not merge this PR.

@Qubitium
Qubitium merged commit ac1f3c3 into main Nov 9, 2026
6 checks passed
@Qubitium
Qubitium deleted the codex/fix-gguf-cpu-autocast-cancellation branch November 9, 2026 19:33
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.

1 participant