patch_4p67.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. """Teach the glm5next model classes their fused projection names.
  3. local-inference-lab/GLM-5.3-Flash-NVFP4-4p67 is the first checkpoint of this
  4. family that QUANTIZES the individual attention projections (per-shard
  5. MXFP8 entries `self_attn.q_proj` … in hf_quant_config). vLLM fuses those
  6. shards into single modules (`in_proj_qkvbfg_a`, `fused_qkv_a_proj`,
  7. `wk_weights_proj`), and the ModelOpt MIXED_PRECISION config resolves a
  8. fused module's quant_algo through its shards ONLY via the model class's
  9. `packed_modules_mapping` (handed to the quant config by
  10. model_executor/model_loader/utils.py). Without it every fused linear comes
  11. up UnquantizedLinearMethod and weight loading dies with
  12. KeyError: 'layers.0.self_attn.in_proj_qkvbfg_a.weight_scale'
  13. Anchors self-assert; idempotent.
  14. """
  15. import py_compile
  16. import sys
  17. B = "/usr/local/lib/python3.12/dist-packages/vllm/models/glm5next/nvidia"
  18. MAPPING = '''
  19. # NVFP4-4p67: quantized per-shard attention/shared-expert projections
  20. # are fused by vLLM; expose the shard names so the ModelOpt
  21. # MIXED_PRECISION config can resolve the fused module's quant_algo
  22. # (see model_loader/utils.py packed_modules_mapping pass-through).
  23. packed_modules_mapping = {
  24. "gate_up_proj": ["gate_proj", "up_proj"],
  25. "in_proj_qkvbfg_a": ["q_proj", "k_proj", "v_proj",
  26. "b_proj", "f_a_proj", "g_a_proj"],
  27. "fused_qkv_a_proj": ["q_a_proj", "kv_a_proj_with_mqa"],
  28. "wk_weights_proj": ["wk", "weights_proj"],
  29. }
  30. '''
  31. TARGETS = [
  32. (
  33. f"{B}/model.py",
  34. "class Glm5NextForCausalLM(\n"
  35. " nn.Module, HasInnerState, SupportsPP, MixtureOfExperts, IsHybrid\n"
  36. "):\n",
  37. ),
  38. (
  39. f"{B}/mtp.py",
  40. "class Glm5NextMTP(nn.Module, DeepseekV2MixtureOfExperts):\n",
  41. ),
  42. ]
  43. for path, anchor in TARGETS:
  44. src = open(path).read()
  45. if "packed_modules_mapping" in src:
  46. print(f"already patched: {path}")
  47. continue
  48. n = src.count(anchor)
  49. assert n == 1, f"anchor {n}x in {path} — day-0 image drifted, review me"
  50. open(path, "w").write(src.replace(anchor, anchor + MAPPING, 1))
  51. py_compile.compile(path, doraise=True)
  52. print(f"patched: {path}")
  53. print("patch_4p67 OK")