|
24 | 24 | create_tiny_llama_dir, |
25 | 25 | get_tiny_gpt_oss, |
26 | 26 | get_tiny_llama, |
| 27 | + get_tiny_qwen3_5, |
27 | 28 | get_tiny_qwen3_moe, |
28 | 29 | tf_modelopt_state_and_output_tester, |
29 | 30 | ) |
@@ -243,3 +244,61 @@ def test_hf_decoder_discoverer_registration_path(): |
243 | 244 | assert LayerActivationCollector.get_decoder_layers(model) is get_homogeneous_hf_decoder_layers( |
244 | 245 | model |
245 | 246 | ) |
| 247 | + |
| 248 | + |
| 249 | +@pytest.mark.parametrize( |
| 250 | + "quant_config", |
| 251 | + [mtq.FP8_DEFAULT_CFG, mtq.INT4_AWQ_CFG], |
| 252 | + ids=["fp8", "int4_awq"], |
| 253 | +) |
| 254 | +def test_qwen3_5_hybrid_attention_quantize(quant_config): |
| 255 | + """Verify FP8 and AWQ quantization works for Qwen3.5 hybrid (GatedDeltaNet + Attention).""" |
| 256 | + import copy |
| 257 | + |
| 258 | + model = get_tiny_qwen3_5() |
| 259 | + |
| 260 | + quant_cfg = copy.deepcopy(quant_config) |
| 261 | + if quant_config is mtq.INT4_AWQ_CFG: |
| 262 | + for entry in quant_cfg["quant_cfg"]: |
| 263 | + if entry["quantizer_name"] == "*weight_quantizer": |
| 264 | + entry.setdefault("cfg", {})["block_sizes"] = {-1: 16} |
| 265 | + break |
| 266 | + |
| 267 | + # Disable narrow GatedDeltaNet projections (same as example_utils does for qwen3_5) |
| 268 | + quant_cfg["quant_cfg"].append({"quantizer_name": "*in_proj_b*", "enable": False}) |
| 269 | + quant_cfg["quant_cfg"].append({"quantizer_name": "*in_proj_a*", "enable": False}) |
| 270 | + |
| 271 | + def calib_fn(model): |
| 272 | + x = model.dummy_inputs["input_ids"] |
| 273 | + for _ in range(2): |
| 274 | + model(x) |
| 275 | + |
| 276 | + mtq.quantize(model, quant_cfg, calib_fn) |
| 277 | + |
| 278 | + # Verify the model still produces output |
| 279 | + with torch.no_grad(): |
| 280 | + out = model(model.dummy_inputs["input_ids"]) |
| 281 | + assert out.logits is not None |
| 282 | + |
| 283 | + # Verify both GatedDeltaNet and Attention linear layers got quantized |
| 284 | + has_gdn_quantized = False |
| 285 | + has_attn_quantized = False |
| 286 | + for name, module in model.named_modules(): |
| 287 | + if hasattr(module, "weight_quantizer") and hasattr(module, "weight"): |
| 288 | + if "linear_attn.in_proj_qkv" in name: |
| 289 | + has_gdn_quantized = True |
| 290 | + if "self_attn.q_proj" in name: |
| 291 | + has_attn_quantized = True |
| 292 | + assert has_gdn_quantized, "GatedDeltaNet linear layers should be quantized" |
| 293 | + assert has_attn_quantized, "Attention linear layers should be quantized" |
| 294 | + |
| 295 | + # Verify narrow projections are NOT quantized |
| 296 | + for name, module in model.named_modules(): |
| 297 | + if "in_proj_b" in name and hasattr(module, "weight_quantizer"): |
| 298 | + assert not module.weight_quantizer.is_enabled, ( |
| 299 | + f"in_proj_b should have quantization disabled: {name}" |
| 300 | + ) |
| 301 | + if "in_proj_a" in name and hasattr(module, "weight_quantizer"): |
| 302 | + assert not module.weight_quantizer.is_enabled, ( |
| 303 | + f"in_proj_a should have quantization disabled: {name}" |
| 304 | + ) |
0 commit comments