make round-to-grid with refinement the default, add --learned for learned rounding

On Qwen2.5-0.5B, learned rounding matched round-to-grid in perplexity and KL
(22.38 vs 22.37, 0.0920 vs 0.0919) at about six times the quantize time.
--simple is still accepted as a no-op. Docs and man pages updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
zefieandClaude Sonnet 5.5 committed 2026-11-05 19:34:15 -05:00
1 parent 164fa6b4a4
commit 1e50e4e045
16 files changed
+76 -39

No files matched your search

+1 -1
View File
@@ -30,4 +30,4 @@ The same directory can be quantized to one ComfyUI NVFP4 safetensors file:
Conversions between formats without quantization is available in the custom commands with `--no-quant`.
Currently the only supported modes are "Quantize to NVFP4" or "Copy without quantizing" (`--no-quant`). I may add other quantization options in the future.
Quantization uses round-to-grid with block-scale and code refinement by default. On Qwen2.5-0.5B this matched convert_to_quant's learned rounding in perplexity and KL at about one sixth of the time. Pass `--learned` to use learned rounding anyway (`--simple` is accepted and is the default). `--no-quant` copies without quantizing.
+2 -1
View File
@@ -20,6 +20,7 @@ import numpy as np
from nvfp4gguf import (
CTQ_NUM_ITER,
add_rounding_arguments,
DEFAULT_CONVERT_TO_QUANT,
DEFAULT_LLAMA_CPP,
DEFAULT_EARLY_STOP_FLOOR_STALL,
@@ -578,7 +579,7 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
help="convert_to_quant checkout, if it is not installed in the environment")
parser.add_argument("--model-name", default=None, help="Override the model name stored in the GGUF")
parser.add_argument("--device", default="auto", help="auto, cpu, cuda, or cuda:N")
parser.add_argument("--simple", action="store_true", help="Round each value onto the E2M1 grid without learned rounding")
add_rounding_arguments(parser)
parser.add_argument("--optimizer", default="auto", choices=("auto", "prodigy", "adamw", "radam", "original"),
help="Learned-rounding optimizer. auto uses prodigy when it is installed, otherwise AdamW")
parser.add_argument("--num-iter", type=int, default=CTQ_NUM_ITER, help="Learned-rounding iterations per matrix (convert_to_quant default: 4000)")
+1 -1
View File
@@ -9,7 +9,7 @@ Optionally (--format modelopt / comfy), outputs raw weight tensors (.weight),
weight_scale, weight_scale_2, and .comfy_quant for ModelOpt and ComfyUI.
If the source GGUF contains float or non-NVFP4 weights, weight matrices are quantized
to NVFP4 using convert_to_quant's learned rounding (or simple round-to-grid with --simple).
to NVFP4 using round-to-grid with scale and code refinement (or convert_to_quant's learned rounding with --learned).
If the source GGUF is already NVFP4, weights are repacked into safetensors without loss
of precision.
+2 -1
View File
@@ -20,6 +20,7 @@ import numpy as np
from nvfp4gguf import (
CTQ_NUM_ITER,
add_rounding_arguments,
DEFAULT_CONVERT_TO_QUANT,
DEFAULT_LLAMA_CPP,
DEFAULT_EARLY_STOP_FLOOR_STALL,
@@ -1046,7 +1047,7 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
help="convert_to_quant checkout, if it is not installed in the environment")
parser.add_argument("--model-name", default=None, help="Override the model name stored in the GGUF")
parser.add_argument("--device", default="auto", help="auto, cpu, cuda, or cuda:N")
parser.add_argument("--simple", action="store_true", help="Round each value onto the E2M1 grid without learned rounding")
add_rounding_arguments(parser)
parser.add_argument("--optimizer", default="auto", choices=("auto", "prodigy", "adamw", "radam", "original"),
help="Learned-rounding optimizer. auto uses prodigy when it is installed, otherwise AdamW")
parser.add_argument("--num-iter", type=int, default=CTQ_NUM_ITER, help="Learned-rounding iterations per matrix (convert_to_quant default: 4000)")
+2 -2
View File
@@ -9,8 +9,8 @@ and llama.cpp.
Optionally (--format modelopt / comfy), outputs raw weight tensors (.weight),
weight_scale, weight_scale_2, and .comfy_quant for ModelOpt and ComfyUI.
Weight matrices are quantized using convert_to_quant's learned rounding (or simple
round-to-grid with --simple). Biases are adjusted using calibration data unless
Weight matrices are quantized using round-to-grid with scale and code refinement (or convert_to_quant's learned
rounding with --learned). Biases are adjusted using calibration data unless
--no-bias-correction is specified. Embeddings, layer norms, and excluded layers
stay in their float format.
"""
+2 -1
View File
@@ -25,6 +25,7 @@ import torch
from nvfp4gguf import (
CTQ_NUM_ITER,
add_rounding_arguments,
DEFAULT_CONVERT_TO_QUANT,
DEFAULT_EARLY_STOP_FLOOR_STALL,
DEFAULT_EARLY_STOP_STALL,
@@ -399,7 +400,7 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
parser.add_argument("--convert-to-quant", type=Path, default=None,
help="convert_to_quant checkout, if it is not installed")
parser.add_argument("--device", default="auto", help="auto, cpu, cuda, or cuda:N")
parser.add_argument("--simple", action="store_true", help="Round onto the E2M1 grid without learned rounding")
add_rounding_arguments(parser)
parser.add_argument("--optimizer", default="auto", choices=("auto", "prodigy", "adamw", "radam", "original"),
help="auto uses prodigy when it is installed, otherwise AdamW")
parser.add_argument("--num-iter", type=int, default=CTQ_NUM_ITER,
+9 -6
View File
@@ -14,7 +14,7 @@ gguf_to_nvfp4_gguf \- quantize or repack an existing GGUF model to NVFP4 GGUF
[\fB\-\-convert\-to\-quant\fR \fIPATH\fR]
[\fB\-\-model\-name\fR \fINAME\fR]
[\fB\-\-device\fR \fIDEVICE\fR]
[\fB\-\-simple\fR]
[\fB\-\-learned\fR]
[\fB\-\-optimizer\fR \fIOPT\fR]
[\fB\-\-num\-iter\fR \fIN\fR]
[\fB\-\-lr\fR \fILR\fR]
@@ -51,8 +51,8 @@ reads an existing GGUF model (such as an F16, BF16, or existing quantized checkp
and converts it to NVIDIA FP4 (NVFP4) format, or repacks an existing NVFP4 GGUF file.
.PP
Vocabulary, tokenizer settings, architecture metadata, and tensor names are copied directly
from the source GGUF header. When quantizing unquantized (float) tensors, learned rounding
or simple grid rounding is applied using the same optimization flags as \fBhf_to_nvfp4_gguf\fR.
from the source GGUF header. When quantizing unquantized (float) tensors, round-to-grid refinement (default)
or learned rounding (\fB\-\-learned\fR) is applied using the same optimization flags as \fBhf_to_nvfp4_gguf\fR.
.PP
An accompanying Ollama \fBModelfile\fR is written alongside the output GGUF, configured with
the source model's parameters and templates.
@@ -91,8 +91,11 @@ Skip learned rounding. Writes a float GGUF, or copies/repacks an existing NVFP4
Target data type when learned rounding is skipped. \fBnvfp4\fR copies an already NVFP4 GGUF without re-optimizing;
\fBauto\fR leaves tensors as stored in the source file (default: \fBauto\fR).
.TP
.B \-\-learned
Use \fBconvert_to_quant\fR's learned rounding instead of the default round-to-grid with block-scale and code refinement. On Qwen2.5-0.5B it matched the default in perplexity and KL and took about six times as long.
.TP
.B \-\-simple
Round each value directly onto the E2M1 grid without learned rounding (fast, non-iterative).
Round onto the E2M1 grid without learned rounding (default; accepted for compatibility).
.TP
.B \-\-quantize\-output
Also quantize the output (\fIlm_head\fR) matrix. By default, output projection layers are left unquantized.
@@ -188,10 +191,10 @@ gguf_to_nvfp4_gguf -o ./llama3-nvfp4 ./llama-3-8b-f16.gguf
.fi
.RE
.TP
.B Fast conversion with simple grid rounding:
.B Default conversion (grid rounding with scale and code refinement):
.RS
.nf
gguf_to_nvfp4_gguf --simple -o ./qwen-nvfp4 ./qwen2.5-7b-bf16.gguf
gguf_to_nvfp4_gguf -o ./qwen-nvfp4 ./qwen2.5-7b-bf16.gguf
.fi
.RE
.TP
+6 -3
View File
@@ -23,7 +23,7 @@ gguf_to_nvfp4_hf \- convert and quantize GGUF models to NVFP4 Hugging Face forma
[\fB\-\-llama\-cpp\fR \fIPATH\fR]
[\fB\-\-convert\-to\-quant\fR \fIPATH\fR]
[\fB\-\-device\fR \fIDEVICE\fR]
[\fB\-\-simple\fR]
[\fB\-\-learned\fR]
[\fB\-\-optimizer\fR \fIOPT\fR]
[\fB\-\-num\-iter\fR \fIN\fR]
[\fB\-\-lr\fR \fILR\fR]
@@ -57,7 +57,7 @@ GGUF tensor names are reverse-mapped to canonical Hugging Face conventions
and RoPE permutations for Q and K are inverted.
.PP
If the source GGUF contains float or non-NVFP4 weights, 2D weight matrices are quantized
using convert_to_quant's learned rounding (or simple round-to-grid with \fB\-\-simple\fR).
using round-to-grid with block-scale and code refinement (or convert_to_quant's learned rounding with \fB\-\-learned\fR).
If the source GGUF already contains NVFP4 super-blocks, they are unpacked and repacked
into ModelOpt-compatible safetensors without loss of precision.
.PP
@@ -107,8 +107,11 @@ Data type for tensors when \fB\-\-no\-quant\fR is used (default: \fBauto\fR).
.BI \-\-output\-dtype " " {bfloat16,float16}
Data type used for unquantized 2D weights, layer norms, and embeddings (default: \fBbfloat16\fR).
.TP
.B \-\-learned
Use \fBconvert_to_quant\fR's learned rounding instead of the default round-to-grid with block-scale and code refinement. On Qwen2.5-0.5B it matched the default in perplexity and KL and took about six times as long.
.TP
.B \-\-simple
Round weights directly onto the E2M1 grid without iterative learned rounding.
Round onto the E2M1 grid without learned rounding (default; accepted for compatibility).
.SS Optimization & Learning Rate
.TP
.BI \-\-optimizer " " {auto,prodigy,adamw,radam,original}
+11 -8
View File
@@ -14,7 +14,7 @@ hf_to_nvfp4_gguf \- quantize Hugging Face models to NVFP4 GGUF for llama.cpp and
[\fB\-\-convert\-to\-quant\fR \fIPATH\fR]
[\fB\-\-model\-name\fR \fINAME\fR]
[\fB\-\-device\fR \fIDEVICE\fR]
[\fB\-\-simple\fR]
[\fB\-\-learned\fR]
[\fB\-\-optimizer\fR \fIOPT\fR]
[\fB\-\-num\-iter\fR \fIN\fR]
[\fB\-\-lr\fR \fILR\fR]
@@ -53,8 +53,8 @@ and \fBOllama\fR.
.PP
The model is loaded through \fBllama.cpp\fR's architecture conversion logic so that
all vocabulary, tensor naming conventions, tokenization metadata, and architectural
parameters match native GGUF standards. Weight matrices are quantized using learned
rounding (via \fBconvert_to_quant\fR) or simple nearest-grid projection onto the E2M1
parameters match native GGUF standards. Weight matrices are quantized using nearest-grid projection
with block-scale and code refinement (default) or learned rounding (\fB\-\-learned\fR, via \fBconvert_to_quant\fR), onto the E2M1
floating point grid with two-level scaling (per-block E4M3/E8M0 scales and per-tensor scales).
.PP
Along with the output GGUF, an Ollama \fBModelfile\fR is generated automatically,
@@ -96,8 +96,11 @@ Skip learned rounding. Writes a float GGUF, or repacks an already NVFP4 checkpoi
Data type for non-quantized weights when learned rounding is skipped. \fBnvfp4\fR repacks a
checkpoint that already has packed 4-bit weights. \fBauto\fR preserves the source float types (default: \fBauto\fR).
.TP
.B \-\-learned
Use \fBconvert_to_quant\fR's learned rounding instead of the default round-to-grid with block-scale and code refinement. On Qwen2.5-0.5B it matched the default in perplexity and KL and took about six times as long.
.TP
.B \-\-simple
Round weights directly onto the E2M1 grid without iterative learned rounding (fast, non-iterative).
Round onto the E2M1 grid without learned rounding (default; accepted for compatibility).
.TP
.B \-\-quantize\-output
Also quantize the final output projection (\fIlm_head\fR) matrix. By default, output layers are
@@ -191,17 +194,17 @@ Enable detailed verbose logging.
Run internal verification of NVFP4 packing against llama.cpp's reference dequantizer and exit immediately.
.SH EXAMPLES
.TP
.B Full learned-rounding conversion to GGUF:
.B Learned-rounding conversion to GGUF (slow, no measured quality gain):
.RS
.nf
hf_to_nvfp4_gguf -o ./llama3-8b-NVFP4 ./meta-llama-3-8b
hf_to_nvfp4_gguf --learned -o ./llama3-8b-NVFP4 ./meta-llama-3-8b
.fi
.RE
.TP
.B Fast quantization with simple grid rounding:
.B Default quantization (grid rounding with scale and code refinement):
.RS
.nf
hf_to_nvfp4_gguf --simple -o ./qwen-nvfp4 ./Qwen2.5-7B-Instruct
hf_to_nvfp4_gguf -o ./qwen-nvfp4 ./Qwen2.5-7B-Instruct
.fi
.RE
.TP
+8 -5
View File
@@ -22,7 +22,7 @@ hf_to_nvfp4_hf \- quantize Hugging Face models to NVFP4 Hugging Face format
[\fB\-\-llama\-cpp\fR \fIPATH\fR]
[\fB\-\-convert\-to\-quant\fR \fIPATH\fR]
[\fB\-\-device\fR \fIDEVICE\fR]
[\fB\-\-simple\fR]
[\fB\-\-learned\fR]
[\fB\-\-optimizer\fR \fIOPT\fR]
[\fB\-\-num\-iter\fR \fIN\fR]
[\fB\-\-lr\fR \fILR\fR]
@@ -56,8 +56,8 @@ vocabulary, chat templates, and generation configs. For each quantized weight ma
the script writes ModelOpt-compatible packed 4-bit weights alongside per-block E4M3 scale
factors (\fI<name>.weight_scale\fR) and per-tensor scale factors (\fI<name>.weight_scale_2\fR).
.PP
Weights are quantized using learned rounding (via \fBconvert_to_quant\fR) or simple nearest-grid
projection onto the E2M1 floating point grid with two-level scaling. Biases are adjusted
Weights are quantized using nearest-grid projection with block-scale and code refinement (default)
or learned rounding (\fB\-\-learned\fR, via \fBconvert_to_quant\fR), onto the E2M1 floating point grid with two-level scaling. Biases are adjusted
using simulated calibration data unless disabled. Checkpoint-based resumption is supported
per tensor so interrupted runs can resume without recomputing.
.SH POSITIONAL ARGUMENTS
@@ -97,8 +97,11 @@ Data type for non-quantized weights when learned rounding is skipped (default: \
.BI \-\-output\-dtype " " {bfloat16,float16}
Data type used for unquantized 2D weights, layer norms, and embeddings (default: \fBbfloat16\fR).
.TP
.B \-\-learned
Use \fBconvert_to_quant\fR's learned rounding instead of the default round-to-grid with block-scale and code refinement. On Qwen2.5-0.5B it matched the default in perplexity and KL and took about six times as long.
.TP
.B \-\-simple
Round weights directly onto the E2M1 grid without iterative learned rounding.
Round onto the E2M1 grid without learned rounding (default; accepted for compatibility).
.SS Optimization & Learning Rate
.TP
.BI \-\-optimizer " " {auto,prodigy,adamw,radam,original}
@@ -220,7 +223,7 @@ Quantize with fast simple grid rounding without learned iterations:
.PP
.nf
.RS
hf_to_nvfp4_hf --simple -o ./my-model-nvfp4 ./my-model
hf_to_nvfp4_hf -o ./my-model-nvfp4 ./my-model
.RE
.fi
.SH SEE ALSO
+5 -2
View File
@@ -10,7 +10,7 @@ hf_to_nvfp4_safetensors \- quantize a Hugging Face directory to a ComfyUI NVFP4
[\fB\-\-heur\fR]
[\fB\-\-exclude\-layers\fR \fIREGEX\fR]
[\fB\-\-convert\-to\-quant\fR \fIPATH\fR]
[\fB\-\-simple\fR]
[\fB\-\-learned\fR]
[\fB\-\-optimizer\fR \fIOPT\fR]
[\fB\-\-num\-iter\fR \fIN\fR]
[\fB\-\-lr\fR \fILR\fR]
@@ -79,8 +79,11 @@ Skip layers that convert_to_quant's built-in size heuristic rejects.
Regular expression matching layer or tensor names to leave unquantized in float format.
.SS Quantization & Optimization
.TP
.B \-\-learned
Use \fBconvert_to_quant\fR's learned rounding instead of the default round-to-grid with block-scale and code refinement. On Qwen2.5-0.5B it matched the default in perplexity and KL and took about six times as long.
.TP
.B \-\-simple
Round onto the E2M1 grid directly without iterative learned rounding.
Round onto the E2M1 grid without learned rounding (default; accepted for compatibility).
.TP
.BI \-\-optimizer " " {auto,prodigy,adamw,radam,original}
Learned-rounding optimizer. \fBauto\fR uses Prodigy when installed, otherwise AdamW (default: \fBauto\fR).
+1 -1
View File
@@ -114,7 +114,7 @@ Override maximum context length stored in the GGUF.
.TP
.I quant_opts
All standard learned-rounding and execution options supported by \fBgguf_to_nvfp4_gguf\fR
are supported: \fB\-\-simple\fR, \fB\-\-optimizer\fR, \fB\-\-lr\fR, \fB\-\-num\-iter\fR,
are supported: \fB\-\-learned\fR, \fB\-\-optimizer\fR, \fB\-\-lr\fR, \fB\-\-num\-iter\fR,
\fB\-\-scale\-optimization\fR, \fB\-\-device\fR, \fB\-\-checkpoint\-dir\fR, etc.
.SS \-\-info
.B ollama_nvfp4
+7 -4
View File
@@ -19,7 +19,7 @@ safetensors_to_nvfp4_gguf \- quantize a safetensors file to an NVFP4 GGUF weight
[\fB\-\-verbose\fR]
[\fB\-\-self\-test\fR]
[\fB\-\-device\fR \fIDEVICE\fR]
[\fB\-\-simple\fR]
[\fB\-\-learned\fR]
[\fB\-\-optimizer\fR \fIOPT\fR]
[\fB\-\-num\-iter\fR \fIN\fR]
[\fB\-\-lr\fR \fILR\fR]
@@ -93,8 +93,11 @@ Path to a vision projector GGUF to copy next to output. May be specified multipl
Maximum context length to store in the GGUF metadata.
.SS Quantization & Optimization
.TP
.B \-\-learned
Use \fBconvert_to_quant\fR's learned rounding instead of the default round-to-grid with block-scale and code refinement. On Qwen2.5-0.5B it matched the default in perplexity and KL and took about six times as long.
.TP
.B \-\-simple
Round each value directly onto the E2M1 grid without learned rounding.
Round onto the E2M1 grid without learned rounding (default; accepted for compatibility).
.TP
.BI \-\-optimizer " " {auto,prodigy,adamw,radam,original}
Learned-rounding optimizer (default: \fBauto\fR).
@@ -199,10 +202,10 @@ safetensors_to_nvfp4_gguf --no-quant --outtype nvfp4 -o repacked.gguf comfy-nvfp
.fi
.RE
.TP
.B Quantize every eligible weight matrix with simple grid rounding:
.B Quantize every eligible weight matrix (default grid rounding):
.RS
.nf
safetensors_to_nvfp4_gguf --simple --all-weights -o fast-nvfp4.gguf model.safetensors
safetensors_to_nvfp4_gguf --all-weights -o fast-nvfp4.gguf model.safetensors
.fi
.RE
.SH EXIT STATUS
+2
View File
@@ -20,6 +20,7 @@ from .quant import (
ShapeView,
TensorQuantizer,
add_quant_arguments,
add_rounding_arguments,
bind_exclude,
ctq_skips_key,
eligible_weight,
@@ -75,6 +76,7 @@ __all__ = [
"ShapeView",
"TensorQuantizer",
"add_quant_arguments",
"add_rounding_arguments",
"bind_exclude",
"ctq_skips_key",
"eligible_weight",
+15 -2
View File
@@ -1116,7 +1116,7 @@ def quant_settings(args) -> dict:
"""Flags that change the bytes in a checkpoint. A mismatch forces --fresh."""
return {
"version": QUANT_VERSION,
"simple": bool(getattr(args, "simple", False)),
"simple": bool(getattr(args, "simple", True)),
"optimizer": args.optimizer,
"num_iter": args.num_iter,
"lr": args.lr,
@@ -1436,10 +1436,23 @@ def eligible_weight(name: str, shape: tuple[int, ...], args, gguf_module) -> boo
return True
def add_rounding_arguments(parser: argparse.ArgumentParser) -> None:
"""--learned opts into learned rounding. Round-to-grid plus scale/code refinement is the default.
On Qwen2.5-0.5B, learned rounding matched round-to-grid in perplexity and KL
(22.38 vs 22.37, 0.0920 vs 0.0919) and took about six times as long.
"""
parser.add_argument("--learned", dest="simple", action="store_false",
help="Use convert_to_quant's learned rounding (much slower, no measured quality gain)")
parser.add_argument("--simple", dest="simple", action="store_true",
help="Round onto the E2M1 grid without learned rounding (default)")
parser.set_defaults(simple=True)
def add_quant_arguments(parser: argparse.ArgumentParser) -> None:
"""Flags shared with convert_to_quant's learned NVFP4 defaults."""
parser.add_argument("--device", default="auto", help="auto, cpu, cuda, or cuda:N")
parser.add_argument("--simple", action="store_true", help="Round onto the E2M1 grid without learned rounding")
add_rounding_arguments(parser)
parser.add_argument("--optimizer", default="auto", choices=("auto", "prodigy", "adamw", "radam", "original"),
help="auto uses prodigy when it is installed, otherwise AdamW")
parser.add_argument("--num-iter", type=int, default=CTQ_NUM_ITER,
+2 -1
View File
@@ -1715,6 +1715,7 @@ def _nvfp4_name(name: str) -> str:
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
from nvfp4gguf import (
add_rounding_arguments,
DEFAULT_EARLY_STOP_FLOOR_STALL,
DEFAULT_TARGET_LOSS,
DEFAULT_EARLY_STOP_STALL,
@@ -1768,7 +1769,7 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
help="llama.cpp checkout used for the GGUF reader and writer (default: ~/llama.cpp)")
quant.add_argument("--convert-to-quant", type=Path, default=None)
quant.add_argument("--device", default="auto")
quant.add_argument("--simple", action="store_true", help="Round onto the E2M1 grid without learned rounding")
add_rounding_arguments(quant)
quant.add_argument("--optimizer", default="auto", choices=("auto", "prodigy", "adamw", "radam", "original"))
quant.add_argument("--num-iter", type=int, default=4000)
quant.add_argument("--lr", type=float, default=1.0, help="Initial learning rate")