Skip to content

fix(gguf): add C++ backend bias before output rounding - #3739

Merged
Qubitium merged 1 commit into
mainfrom
codex/fix-gguf-cpp-bias-epilogue
Nov 9, 2026
Merged

Qubitium merged 1 commit into
mainfrom
codex/fix-gguf-cpp-bias-epilogue

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Problem

GGUFCppKernel.forward and GGUFCudaKernel.forward cast a GGML product and the stored bias to the activation dtype before addition. A Q8_0 dot product of 100000 canceled by FP32 bias -100000 can become NaN instead of zero. The CPU Torch fallback also added bias after output rounding.

Fix

Add bias to FP32 GGML outputs before casting to the activation dtype. When GGML returns an already rounded FP16/BF16 product, recompute only the biased case from a temporary FP32 decoded weight. Pass bias into the existing CPU Torch fused/dense epilogues. CPU autotuning now times the full biased paths.

flowchart TD
  A["GGUF C++ CPU or CUDA forward"] --> B["Run GGML matmul"]
  B --> C{"GGML output low precision and bias exists?"}
  C -- Yes --> D["Temporary FP32 decode and matmul"]
  D --> E["Add original bias in FP32"]
  C -- No --> E
  E --> F["Round once to activation dtype"]
  G["CPU Torch fallback"] --> H["Pass bias into fused or dense matmul"]
  H --> F
  I["Old product-before-bias cast"] --> X["Overflow or cancellation error"]
  classDef failure fill:#fdd,stroke:#c33
  class X failure
Loading
Path Before After
GGML FP32 output Product and bias narrowed before addition Bias added in FP32 before one output cast
GGML FP16/BF16 output Already rounded product was biased Biased case recomputed with temporary FP32 weight
CPU Torch fallback Bias added after output cast Bias passed into existing wide epilogue

Committed flow page, linked from README.

Validation

  • tests/kernels/test_gguf_cpp_bias_epilogue.py tests/kernels/test_gguf_cpp.py tests/test_cpu_bias_cancellation.py: 12 passed, 18 skipped on CPU. The new tests compare packed Q8_0 output with an independent FP64 decode and matmul, including repeated calls, both GGML output dtypes, and the Torch fallback.
  • Ruff on changed Python files and git diff --check: passed.
  • Native llama.cpp CUDA execution is unavailable on this host. The branch adds a CUDA-gated packed oracle test; CUDA output and latency require target hardware.
  • Packed checkpoint storage is unchanged. A temporary FP32 dense weight is allocated only when a biased native output is already low precision.

No backward compatibility is retained except for hf_ APIs.

@Qubitium
Qubitium merged commit 167f608 into main Nov 9, 2026
6 checks passed
@Qubitium
Qubitium deleted the codex/fix-gguf-cpp-bias-epilogue branch November 9, 2026 19:22

Copy link
Copy Markdown
Collaborator Author

Clean review found CUDA GGML could overflow before FP32 bias cancellation. Fixed in #3789; the packed Q8_0 GPU oracle and CI pass.

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