patch_dspark_draft_page.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. """Patch the r37 DFlash draft attention (model_executor/models/qwen3_dflash.py)
  3. so the DRAFT model's SlidingWindowSpec does not inherit the target's
  4. sparse-MLA shared-page padding.
  5. Why: the target's B12X sparse-MLA path (GlmMoeDsa, full GLM-5.3) writes
  6. ``cache_config.skip_page_size_padded`` during backend resolution
  7. (platforms/interface.py). The draft model's DFlash sliding-window layers
  8. (qwen3_dflash.py DFlashAttention.get_kv_cache_spec) then inherit that value
  9. into their SlidingWindowSpec. The draft's bf16 page (head 64, block 256) is
  10. larger than the target's padded MLA page, so KV layout resolution trips
  11. assert page_size_padded >= unpadded_page_size_bytes
  12. (vllm/v1/kv_cache_interface.py).
  13. The draft KV group is independent of the target's; it must use its own
  14. (unpadded) page size — page_size_padded=None does exactly that.
  15. Usage (in-container recipe mod, idempotent, refuses on drift):
  16. python3 patch_dspark_draft_page.py [--check]
  17. """
  18. import os
  19. import sys
  20. import vllm
  21. FILE = os.path.join(vllm.__path__[0], "model_executor/models/qwen3_dflash.py")
  22. OLDER = (
  23. "page_size_padded=getattr(\n"
  24. ' vllm_config.cache_config, "skip_page_size_padded", None\n'
  25. " ),"
  26. )
  27. NEWER = (
  28. "page_size_padded=None, # sm121: draft KV group must not inherit the\n"
  29. " # target sparse-MLA shared-page padding (see\n"
  30. " # patches/patch_dspark_draft_page.py header)\n"
  31. " "
  32. )
  33. def _consistent(src, old, new):
  34. co, cn = src.count(old), src.count(new)
  35. if cn >= 1:
  36. return cn == 1 and co <= new.count(old)
  37. return co == 1
  38. def main():
  39. check = "--check" in sys.argv
  40. src = open(FILE).read()
  41. if check:
  42. if not _consistent(src, OLDER, NEWER):
  43. raise SystemExit(f"CHECK FAIL: {FILE} is neither pristine nor patched")
  44. print(f"check OK: {FILE} state is consistent")
  45. return
  46. if src.count(OLDER) != 1:
  47. raise SystemExit(
  48. f"refusing: {FILE} old anchor matched {src.count(OLDER)}x; "
  49. f"re-sync patch"
  50. )
  51. out = src.replace(OLDER, NEWER)
  52. assert out.count(NEWER) == 1
  53. import py_compile
  54. open(FILE, "w").write(out)
  55. py_compile.compile(FILE, doraise=True)
  56. print(f"patched {FILE} (draft SlidingWindowSpec: no shared-page inheritance)")
  57. if __name__ == "__main__":
  58. main()