allow per model quant config (#349)

This commit is contained in:
Awni Hannun
2025-08-05 06:08:56 -07:00
committed by GitHub
parent fc800f1a0b
commit cbfba0a973
3 changed files with 14 additions and 3 deletions
-1
View File
@@ -49,7 +49,6 @@ def mixed_quant_predicate_builder(
def mixed_quant_predicate(
path: str,
module: nn.Module,
config: dict,
) -> Union[bool, dict]:
"""Implements mixed quantization predicates with similar choices to, for example, llama.cpp's Q4_K_M.
Ref: https://github.com/ggerganov/llama.cpp/blob/917786f43d0f29b7c77a0c56767c0fa4df68b1c5/src/llama.cpp#L5265
+9
View File
@@ -235,6 +235,15 @@ class Model(nn.Module):
weights[f"{prefix}.mlp.switch_mlp.{n}.weight"] = mx.stack(to_join)
return weights
@property
def quant_predicate(self):
def predicate(path, _):
if path.endswith("mlp.gate"):
return {"group_size": 64, "bits": 8}
return True
return predicate
@property
def layers(self):
return self.model.layers
+5 -2
View File
@@ -476,6 +476,8 @@ def quantize_model(
quantized_config = copy.deepcopy(config)
quantized_config["quantization"] = {"group_size": q_group_size, "bits": q_bits}
quant_predicate = quant_predicate or getattr(model, "quant_predicate", None)
def base_predicate(path, module):
if not hasattr(module, "to_quantized"):
return False
@@ -487,8 +489,9 @@ def quantize_model(
def wrapped_predicate(p, m):
bool_or_params = base_predicate(p, m)
if bool_or_params:
bool_or_params = quant_predicate(p, m, config)
quantized_config["quantization"][p] = bool_or_params
bool_or_params = quant_predicate(p, m)
if isinstance(bool_or_params, dict):
quantized_config["quantization"][p] = bool_or_params
return bool_or_params
nn.quantize(