Repository navigation
fix(gguf): preserve CPU cancellation under BF16 autocast - #3743
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.
Summary
Fix the remaining public
GGUFTorchLinear.forwardCPU cancellation error under BF16 autocast. On FP16 Q8_0 input, a100000dot plus-100000FP32 bias returned-160; the independent FP64/FP32 oracle returns0. The dense CPU path now disables autocast only while its FP32torch.mmruns. 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 failuregptqmodel/nn_modules/qlinear/gguf.py:1715-1716)torch.mmresult before bias additiontorch.mmwhen it is active_forward_fused_k)0in this caseReference diagram, before/after details, and benchmark: GGUF dense fallback precision.
Tests and measurements
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.git diff --check, and three Mermaid fences: passed.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/thegguforacle are unavailable. Three new #3739 C++ tests fail atGGUFCppKernelconstruction on this host becausellama_cppis missing; they do not enter the changed Torch reduction. CUDA/ROCm paths were not run.Do not merge this PR.