Skip to content

Commit 7973b8c

Browse files
author
Awni Hannun
authored
allow mxfp8 and nvfp4 (ml-explore#709)
1 parent 7096618 commit 7973b8c

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

mlx_lm/convert.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,23 @@ def configure_parser() -> argparse.ArgumentParser:
187187
"-q", "--quantize", help="Generate a quantized model.", action="store_true"
188188
)
189189
parser.add_argument(
190-
"--q-group-size", help="Group size for quantization.", type=int, default=64
190+
"--q-group-size",
191+
help="Group size for quantization.",
192+
type=int,
193+
default=None,
191194
)
192195
parser.add_argument(
193-
"--q-bits", help="Bits per weight for quantization.", type=int, default=4
196+
"--q-bits",
197+
help="Bits per weight for quantization.",
198+
type=int,
199+
default=None,
194200
)
195201
parser.add_argument(
196202
"--q-mode",
197203
help="The quantization mode.",
198204
type=str,
199205
default="affine",
200-
choices=["affine", "mxfp4"],
206+
choices=["affine", "mxfp4", "nvfp4", "mxfp8"],
201207
)
202208
parser.add_argument(
203209
"--quant-predicate",

mlx_lm/utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,8 @@ def save_model(
611611
def quantize_model(
612612
model: nn.Module,
613613
config: dict,
614-
group_size: int,
615-
bits: int,
614+
group_size: Optional[int],
615+
bits: Optional[int],
616616
mode: str = "affine",
617617
quant_predicate: Optional[Callable[[str, nn.Module], Union[bool, dict]]] = None,
618618
) -> Tuple[nn.Module, dict]:
@@ -622,8 +622,8 @@ def quantize_model(
622622
Args:
623623
model (nn.Module): The model to be quantized.
624624
config (dict): Model configuration.
625-
group_size (int): Group size for quantization.
626-
bits (int): Bits per weight for quantization.
625+
group_size (Optional[int]): Group size for quantization.
626+
bits (Optional[int]): Bits per weight for quantization.
627627
mode (str): The quantization mode.
628628
quant_predicate (Callable): A callable that decides how to quantize
629629
each layer based on the path. Accepts the layer `path` and the
@@ -633,9 +633,21 @@ def quantize_model(
633633
Returns:
634634
Tuple: Tuple containing quantized model and config.
635635
"""
636+
637+
def defaults_for_mode(mode, group_size, bits):
638+
mode_defaults = {
639+
"affine": (64, 4),
640+
"mxfp4": (32, 4),
641+
"nvfp4": (16, 4),
642+
"mxfp8": (32, 8),
643+
}
644+
default_group_size, default_bits = mode_defaults[mode]
645+
return group_size or default_group_size, bits or default_bits
646+
636647
quantized_config = copy.deepcopy(config)
637648

638649
quant_predicate = quant_predicate or getattr(model, "quant_predicate", None)
650+
group_size, bits = defaults_for_mode(mode, group_size, bits)
639651
quant_params = {"group_size": group_size, "bits": bits, "mode": mode}
640652
if "quantization" in quantized_config:
641653
# If the model is already partially quantized, return params so that

0 commit comments

Comments
 (0)