Skip to content

Fix finalized quant remainder dtypes#629

Merged
EHxuban11 merged 2 commits into
kernels/tier1-4from
kernels/tier2-fused-gemm
Jul 19, 2026
Merged

Fix finalized quant remainder dtypes#629
EHxuban11 merged 2 commits into
kernels/tier1-4from
kernels/tier2-fused-gemm

Conversation

@EHxuban11

@EHxuban11 EHxuban11 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Found during the tier-2 kernel campaign: two dtype bugs in the finalized-checkpoint path.

  • A finalized manifest with remainder="fp16" rebuilt fp32 destination parameters, so load_state_dict silently promoted the saved fp16 tensors (and the W4A16 kernel path fell back to reference).
  • Export cast _q_w_gscale to fp16 despite the frozen checkpoint schema requiring fp32 group scales.

The loader now casts only the non-quantized remainder before load, quant buffers keep their contract dtypes, export preserves _q_w_gscale as fp32, and re-preparing restores ordinary parameters to fp32. Adds a focused CPU round-trip test. Full suite: 56 passed, 1 skipped.

Note: stacked on kernels/tier1-4 (#623); the deferred fused-GEMM kernel itself is intentionally NOT in this PR (evidence and scratch kernel live outside the repo).

Code provenance

All changes are original, written for this PR against the existing LibreYOLO codebase. No third-party code was ported, adapted, or derived. The frozen W4 packing contract is unchanged.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

@greptile-apps

greptile-apps Bot commented Jul 19, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes dtype handling for finalized quantized checkpoints. The main changes are:

  • Preserves quant buffer dtypes during fp16 remainder export.
  • Casts only non-quantized remainder tensors when loading finalized checkpoints.
  • Restores ordinary parameters to fp32 when re-preparing or dequantizing.
  • Adds focused round-trip tests for W4A16, NVFP4, and MXFP4 checkpoint dtypes.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
libreyolo/quant/api.py Updates finalized checkpoint export, load, reprepare, and dequantize dtype handling for quantized models.
tests/unit/test_quantize.py Adds CPU round-trip coverage for finalized quant checkpoint dtype preservation.

Reviews (2): Last reviewed commit: "Protect all quant-module buffers from th..." | Re-trigger Greptile

Comment thread libreyolo/quant/api.py Outdated
The export path guarded quant buffer dtypes with a hardcoded name list,
which silently excluded buffers it did not know about (NVFP4's
weight_block_scale, MXFP4's weight_block_exp, and anything a future
recipe registers). Those two are stored as float8_e4m3fn and int8 today,
so the fp32-only cast did not corrupt them in practice, but the design
invited exactly that bug. The keep-exact set is now collected from the
finalized quant modules' own registered buffers, so the invariant is
structural: quant buffers keep their contract dtypes, only the
non-quantized remainder is cast. Adds nvfp4/mxfp4 fp16-remainder
round-trip tests asserting the scale and exponent dtypes survive
export and reload.

Claude-Session: https://claude.ai/code/session_018Bkjq2cuWtXkEB6xHcLoRH
@EHxuban11
EHxuban11 merged commit 1d50039 into kernels/tier1-4 Jul 19, 2026
2 checks passed
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