Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mlx_lm/models/granite.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,19 @@ def sanitize(self, weights):
if self.args.tie_word_embeddings:
weights.pop("lm_head.weight", None)
return weights

@property
def quant_predicate(self):
def predicate(path, _):
# o_proj, down_proj, and lm_head run small enough in block absmax
# that low-bit fp quant modes (e.g. nvfp4) underflow their scale
# encoding, corrupting dequantization. Keep them safe at 8 bits.
if (
path.endswith("self_attn.o_proj")
or path.endswith("mlp.down_proj")
or path == "lm_head"
):
return {"group_size": 64, "bits": 8}
return True

return predicate
7 changes: 6 additions & 1 deletion mlx_lm/models/granitemoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,12 @@ def sanitize(self, weights):
@property
def quant_predicate(self):
def predicate(path, _):
if path.endswith("block_sparse_moe.router.layer"):
if (
path.endswith("block_sparse_moe.router.layer")
or path.endswith("self_attn.o_proj")
or path.endswith("mlp.down_proj")
or path == "lm_head"
):
return {"group_size": 64, "bits": 8}
return True

Expand Down
47 changes: 47 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,53 @@ def test_gemma4_moe_router_quantizes_to_8bit(self):
)
self.assertEqual(config["quantization"]["bits"], 4)

def test_granite_sensitive_projections_quantize_to_8bit(self):
from mlx_lm.models import granite
from mlx_lm.utils import quantize_model

args = granite.ModelArgs(
model_type="granite",
hidden_size=64,
num_hidden_layers=1,
intermediate_size=128,
num_attention_heads=4,
rms_norm_eps=1e-5,
vocab_size=64,
logits_scaling=1.0,
attention_multiplier=0.015625,
embedding_multiplier=1.0,
residual_multiplier=1.0,
max_position_embeddings=2048,
num_key_value_heads=2,
attention_bias=False,
mlp_bias=False,
rope_theta=10000.0,
tie_word_embeddings=False,
)
model = granite.Model(args)
model, config = quantize_model(
model,
{"model_type": "granite"},
group_size=16,
bits=4,
mode="nvfp4",
)

layer = model.model.layers[0]
for sensitive in (layer.self_attn.o_proj, layer.mlp.down_proj, model.lm_head):
self.assertIsInstance(sensitive, nn.QuantizedLinear)
self.assertEqual(sensitive.bits, 8)
self.assertIsInstance(layer.self_attn.q_proj, nn.QuantizedLinear)
self.assertEqual(layer.self_attn.q_proj.bits, 4)
self.assertEqual(
config["quantization"]["model.layers.0.self_attn.o_proj"]["bits"], 8
)
self.assertEqual(
config["quantization"]["model.layers.0.mlp.down_proj"]["bits"], 8
)
self.assertEqual(config["quantization"]["lm_head"]["bits"], 8)
self.assertEqual(config["quantization"]["bits"], 4)

def test_qwen2_moe(self):
from mlx_lm.models import qwen2_moe

Expand Down
Loading