mia_retarget.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. """Point MiaAI's vendored patcher at where its targets live in this base.
  3. glm53-flash_SM121.py stays verbatim (MIT, licence beside it). Two of its edits
  4. target code that moved, so this rewrites their paths and anchors in a copy
  5. before it runs:
  6. - The indexer's two top-k allocations moved from
  7. model_executor/layers/sparse_attn_indexer_kpool.py to
  8. models/glm5next/nvidia/sparse_indexer.py, byte for byte.
  9. - FlashInfer 0.7.0 moved the FA2 "FP8 kv_data_type for MLA requires an SM90"
  10. gate from mla/_core.py into mla/_batch_mla/_backends/_fa_common.py, as a
  11. function of `device` at a shallower indent.
  12. Every retarget matches exactly once, or the build fails.
  13. usage: mia_retarget.py <copy of glm53-flash_SM121.py>
  14. """
  15. import sys
  16. from pathlib import Path
  17. path = Path(sys.argv[1])
  18. text = path.read_text()
  19. RETARGETS = [
  20. (
  21. "model_executor/layers/sparse_attn_indexer_kpool.py",
  22. "models/glm5next/nvidia/sparse_indexer.py",
  23. ),
  24. ('FI / "mla/_core.py"', 'FI / "mla/_batch_mla/_backends/_fa_common.py"'),
  25. (
  26. 'old = " major, minor = get_compute_capability(self.device)\\n'
  27. ' if major != 9:\\n"',
  28. 'old = " major, minor = get_compute_capability(device)\\n'
  29. ' if major != 9:\\n"',
  30. ),
  31. (
  32. 'new = " major, minor = get_compute_capability(self.device)\\n'
  33. ' if major not in (9, 12):\\n"',
  34. 'new = " major, minor = get_compute_capability(device)\\n'
  35. ' if major not in (9, 12):\\n"',
  36. ),
  37. ]
  38. for old, new in RETARGETS:
  39. n = text.count(old)
  40. assert n == 1, f"[mia-retarget] {old[:60]!r} matched {n}x, want 1"
  41. text = text.replace(old, new)
  42. path.write_text(text)
  43. print("[mia-retarget] indexer path and FlashInfer 0.7.0 fp8 gate retargeted")