[BugFix] Fix ModelOpt mixed-precision quantization for sparse quantized_layers configs. (#47318)
Signed-off-by: Daniel Afrimi <dafrimi@nvidia.com> Signed-off-by: <dafrimi@nvidia.com>
This commit is contained in:
@@ -133,6 +133,49 @@ def test_modelopt_mixed_precision_quantizes_parallel_lm_head():
|
|||||||
assert isinstance(method, ModelOptNvFp4LinearMethod)
|
assert isinstance(method, ModelOptNvFp4LinearMethod)
|
||||||
|
|
||||||
|
|
||||||
|
def test_modelopt_mixed_precision_infers_fused_gate_up_projection():
|
||||||
|
from vllm.model_executor.layers.linear import LinearBase
|
||||||
|
|
||||||
|
config = _mixed_precision_config(
|
||||||
|
{
|
||||||
|
"model.layers.0.mlp.gate_proj": {"quant_algo": "NVFP4"},
|
||||||
|
"model.layers.0.mlp.up_proj": {"quant_algo": "NVFP4"},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
fake_layer = MagicMock(spec=LinearBase)
|
||||||
|
with patch(
|
||||||
|
"vllm.model_executor.layers.quantization.modelopt.init_nvfp4_linear_kernel"
|
||||||
|
):
|
||||||
|
method = config.get_quant_method(fake_layer, "model.layers.0.mlp.gate_up_proj")
|
||||||
|
|
||||||
|
assert isinstance(method, ModelOptNvFp4LinearMethod)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("quantized_prefix", "missing_prefix"),
|
||||||
|
[
|
||||||
|
("model.layers.0.mlp.gate_proj", "model.layers.0.mlp.down_proj"),
|
||||||
|
("model.layers.0.self_attn.o_proj", "model.layers.0.self_attn.qkv_proj"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_modelopt_mixed_precision_does_not_infer_missing_sibling_linear(
|
||||||
|
quantized_prefix, missing_prefix
|
||||||
|
):
|
||||||
|
from vllm.model_executor.layers.linear import LinearBase
|
||||||
|
|
||||||
|
config = _mixed_precision_config(
|
||||||
|
{
|
||||||
|
quantized_prefix: {"quant_algo": "NVFP4"},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
fake_layer = MagicMock(spec=LinearBase)
|
||||||
|
method = config.get_quant_method(fake_layer, missing_prefix)
|
||||||
|
|
||||||
|
assert isinstance(method, UnquantizedLinearMethod)
|
||||||
|
|
||||||
|
|
||||||
def test_vocab_parallel_embedding_weight_loader_accepts_scalar_scale():
|
def test_vocab_parallel_embedding_weight_loader_accepts_scalar_scale():
|
||||||
holder = Mock()
|
holder = Mock()
|
||||||
scale = torch.nn.Parameter(torch.empty(1))
|
scale = torch.nn.Parameter(torch.empty(1))
|
||||||
|
|||||||
@@ -2461,16 +2461,29 @@ class ModelOptMixedPrecisionConfig(ModelOptQuantConfigBase):
|
|||||||
if key.startswith(parent_dot):
|
if key.startswith(parent_dot):
|
||||||
return info["quant_algo"].upper()
|
return info["quant_algo"].upper()
|
||||||
|
|
||||||
# 4. Parent-prefix fallback for fused projections (qkv_proj, gate_up_proj).
|
# 4. Parent-prefix fallback for fused projections whose config lists
|
||||||
for candidate in self._quantized_layer_prefix_candidates(prefix):
|
# shard names instead of vLLM's packed module name.
|
||||||
parent_dot = candidate.rsplit(".", 1)[0] + "."
|
fused_projection_shards = {
|
||||||
algos = {
|
"qkv_proj": ("q_proj", "k_proj", "v_proj"),
|
||||||
info["quant_algo"].upper()
|
"gate_up_proj": ("gate_proj", "up_proj"),
|
||||||
for key, info in self.quantized_layers.items()
|
}
|
||||||
if key.startswith(parent_dot) and "." not in key[len(parent_dot) :]
|
shard_names = fused_projection_shards.get(proj_name)
|
||||||
}
|
if shard_names is not None:
|
||||||
if len(algos) == 1:
|
for candidate in self._quantized_layer_prefix_candidates(prefix):
|
||||||
return algos.pop()
|
parent_dot = candidate.rsplit(".", 1)[0] + "."
|
||||||
|
shard_algos: set[str] = set()
|
||||||
|
for shard_name in shard_names:
|
||||||
|
shard_prefix = f"{parent_dot}{shard_name}"
|
||||||
|
if shard_prefix in self.quantized_layers:
|
||||||
|
algo = self.quantized_layers[shard_prefix]["quant_algo"].upper()
|
||||||
|
shard_algos.add(algo)
|
||||||
|
if len(shard_algos) == 1:
|
||||||
|
return shard_algos.pop()
|
||||||
|
if len(shard_algos) > 1:
|
||||||
|
raise ValueError(
|
||||||
|
f"Mixed quant_algo within fused layer {prefix}: "
|
||||||
|
f"{shard_algos}. All shards must use the same quantization."
|
||||||
|
)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user