Skip to content

fix(gguf): preserve FP32 bias range - #3727

Merged
Qubitium merged 3 commits into
mainfrom
codex/fix-gguf-bias-range
Nov 9, 2026
Merged

Qubitium merged 3 commits into
mainfrom
codex/fix-gguf-bias-range

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Preserve FP32 GGUF bias from packing through BF16 checkpoint reload and the public GGUFTorchLinear.forward handoff. The dense path now moves bias to the activation device without narrowing it, and returns the activation dtype after the matmul. No dense weight cache or compatibility shim was added; no hf_ API changed.

Why

An FP32 GGUF bias survived packing but was rounded to BF16 by Accelerate during BF16 checkpoint load. Public GGUFTorchLinear.forward also cast the bias to the activation dtype before dispatch. For a Q8_0 FP16 case with a 100000 dot and -100000 FP32 bias, the old public output was -inf instead of 0. A BF16 case with a 99840 dot and -100000 bias returned 0 instead of -160.

Flow

flowchart TD
    A["ModelLoader.from_quantized collects GGUFTorchLinear bias names"] --> B["load_checkpoint_in_model_then_tie_weights"]
    B --> C{"BF16 requested?"}
    C -- "Yes" --> D["Accelerate loads checkpoint as BF16"]
    D --> E["_restore_fp32_checkpoint_buffers reads selected source biases"]
    E -- "Missing source, invalid buffer or shape" --> X["Raise RuntimeError"]
    E -- "Valid slot" --> F["Store FP32 bias on the loaded device"]
    C -- "No" --> G["Accelerate load; FP16 keep list applies when needed"]
    F --> H["GGUFTorchLinear.forward keeps original bias dtype"]
    G --> H
    H -- "Dense" --> I["Pass bias to _forward_dequant_matmul"]
    H -- "Fused" --> J["Pass bias to _forward_fused_k"]
    I --> K["Return activation dtype"]
    J --> K
    classDef failure fill:#fce8e6,stroke:#a33,color:#111
    class X failure
Loading
Mechanism Before After
BF16 load (models/loader.py:2364, utils/model.py:2749) keep_in_fp32_modules did not prevent Accelerate from narrowing GGUF bias Selectively reread FP32 bias from single or indexed safetensors or PyTorch shards after load
Public dense forward (nn_modules/qlinear/gguf.py:1915) Bias cast to activation dtype before _forward_dequant_matmul Move bias across devices without dtype change; cast only the resulting output if needed
Inference storage Packed GGUF weight persisted; dense weight was temporary Same packed storage and temporary dense weight

The selective reread now matches Accelerate’s directory file preference and errors when any requested bias is absent. Tests cover a missing index entry and a directory containing both whole-file formats.

Reference flow and numerical examples: GGUF dense fallback precision.

Validation

  • Independent FP64 packed Q8_0 oracle checks FP16 and BF16 public CPU output exactly, with repeated forwards and no retained expanded weight.
  • Save/reload checks cover both dtypes across single safetensors, indexed safetensors, and PyTorch .bin, plus BF16 safetensors key remapping.
  • After rebasing onto merged fix(gguf): add CPU bias before output rounding #3728, pytest tests/test_gguf_bias_range.py tests/test_cpu_bias_cancellation.py tests/kernels/test_gguf_torch_cpu.py -q: 38 passed.
  • Changed-file ruff check and git diff --check: passed.
  • An optional full-format trial had 54 passed, 20 failed because llama-cpp-python is absent for those quantizers. CUDA, ROCm, and native llama.cpp output were not validated on this CPU host.

Scope and #3728 follow-up

Merged #3728 widened the CPU dense reduction inside _forward_dequant_matmul. This PR leaves that reduction and CPU autocast handling unchanged. Preserving FP32 bias through public forward also removes pre-dispatch narrowing that affected #3728's public path. A distinct cancellation defect remains under CPU BF16 autocast: the FP16 Q8_0 100000 + (-100000) case should return 0 but currently returns -160. That reduction/autocast fix belongs in a separate follow-up PR.

Comment thread tests/test_gguf_bias_range.py Fixed
Comment thread tests/test_gguf_bias_range.py Fixed
Comment thread tests/test_gguf_bias_range.py Fixed
Comment thread tests/test_gguf_bias_range.py Fixed
Comment thread tests/test_gguf_bias_range.py Fixed
Comment thread tests/test_gguf_bias_range.py Fixed
Comment thread tests/test_gguf_bias_range.py Fixed
Comment thread tests/test_gguf_bias_range.py Fixed
@Qubitium
Qubitium force-pushed the codex/fix-gguf-bias-range branch 3 times, most recently from 88fcc4b to 2d798e3 Compare November 9, 2026 18:39
@Qubitium
Qubitium force-pushed the codex/fix-gguf-bias-range branch from e0de9f1 to 88b0dbc Compare November 9, 2026 19:07
@Qubitium
Qubitium merged commit 0f82347 into main Nov 9, 2026
6 checks passed
@Qubitium
Qubitium deleted the codex/fix-gguf-bias-range branch November 9, 2026 19:11
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