test_autotune_keep.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """CPU check for adapter/autotune_keep.py: per-rank caches with disjoint shape entries digest alike
  2. when every rank's sidecar matches its launch, and differently (-> dropped) when one does not."""
  3. import contextlib
  4. import json
  5. import os
  6. import tempfile
  7. import types
  8. from pathlib import Path
  9. os.environ["DSV41_AUTOTUNE_KEEP"] = "1"
  10. import autotune_keep as ak # noqa: E402
  11. def fake_module(paths, contents):
  12. mod = types.SimpleNamespace()
  13. mod._autotune_cache_digest = lambda p, env: "stock"
  14. seen = {}
  15. @contextlib.contextmanager
  16. def ctx(model_runner, **kw):
  17. seen[model_runner] = paths[model_runner].is_file() # what tuning started from
  18. yield
  19. paths[model_runner].write_text(contents[model_runner]) # tuning saves its cache
  20. mod.seen = seen
  21. mod.flashinfer_autotune_context = ctx
  22. mod.flashinfer_autotune_cache_path = lambda mr: paths[mr]
  23. return mod
  24. def scenario(tp4):
  25. if tp4:
  26. os.environ["DSV41_LAUNCHER"] = "tp4"
  27. else:
  28. os.environ.pop("DSV41_LAUNCHER", None)
  29. for key in ("SGLANG_RUN_ID", "DSV41_REPLICATED_SPLIT", "DSV41_WO_A_W8_DRAFT",
  30. "DSV41_VERIFY_CAP", "SGLANG_SOMETHING_SHAPED"):
  31. os.environ.pop(key, None)
  32. d = Path(tempfile.mkdtemp())
  33. meta = {"flashinfer": "0.6.18", "arch": "sm121"}
  34. paths = {}
  35. contents = {}
  36. for rank, shapes in ((0, ["(192, 2304, 320)"]), (1, ["(64, 2304, 320)"])):
  37. p = d / f"rank_tp{rank}_pp0_dp0.json"
  38. contents[rank] = json.dumps({"_metadata": meta, **{s: [16, 36] for s in shapes}})
  39. p.write_text(contents[rank])
  40. paths[rank] = p
  41. mod = fake_module(paths, contents)
  42. ak.install(mod)
  43. env = {"cuda": "13.0"}
  44. assert mod._autotune_cache_digest(paths[0], env) == "", "no sidecar yet: must not be kept"
  45. if tp4:
  46. assert not paths[0].exists(), "tp4 digest deletes a cache without a matching sidecar"
  47. else:
  48. assert paths[0].exists(), "start.sh digest does not delete; the context does"
  49. for rank in (0, 1): # a tuning boot writes the caches and sidecars
  50. with mod.flashinfer_autotune_context(rank):
  51. pass
  52. assert mod.seen == {0: False, 1: False}, "a cache without this launch's sidecar must not be loaded"
  53. for rank in (0, 1): # identical relaunch: kept and loaded
  54. with mod.flashinfer_autotune_context(rank):
  55. pass
  56. assert mod.seen == {0: True, 1: True}, "a cache with a matching sidecar must be kept"
  57. d0, d1 = mod._autotune_cache_digest(paths[0], env), mod._autotune_cache_digest(paths[1], env)
  58. assert d0 and d0 == d1, "disjoint EP shape sets with matching launches must digest alike"
  59. ak.sidecar(paths[1]).write_text("other-launch\n")
  60. assert mod._autotune_cache_digest(paths[1], env) == "", "a changed launch must drop the cache"
  61. if tp4:
  62. assert not paths[1].exists(), "and delete it, so no rank can load it"
  63. else:
  64. assert paths[1].exists(), "start.sh leaves the file for the context to drop"
  65. fp_before = ak.launch_fingerprint()
  66. os.environ["SGLANG_RUN_ID"] = "sglang-run-1.0-1" # per boot, set by the engine
  67. assert ak.launch_fingerprint() == fp_before
  68. os.environ["SGLANG_RUN_ID"] = "sglang-run-2.0-2"
  69. assert ak.launch_fingerprint() == fp_before
  70. os.environ["DSV41_REPLICATED_SPLIT"] = "wqkv_a"
  71. if tp4:
  72. assert ak.launch_fingerprint() == fp_before, "decode-side switch is volatile on the TP4 launcher"
  73. else:
  74. assert ak.launch_fingerprint() != fp_before, "start.sh still fingerprints this switch"
  75. os.environ.pop("DSV41_REPLICATED_SPLIT")
  76. fp_before = ak.launch_fingerprint()
  77. os.environ["DSV41_WO_A_W8_DRAFT"] = "1" # draft einsum route: volatile on both paths
  78. assert ak.launch_fingerprint() == fp_before
  79. os.environ["DSV41_VERIFY_CAP"] = "conf:0.1" # A/B-neutral switch: same fingerprint
  80. assert ak.launch_fingerprint() == ak.launch_fingerprint()
  81. fp_a = ak.launch_fingerprint()
  82. os.environ["DSV41_VERIFY_CAP"] = "3"
  83. assert ak.launch_fingerprint() == fp_a
  84. os.environ["SGLANG_SOMETHING_SHAPED"] = "1"
  85. assert ak.launch_fingerprint() != fp_a
  86. def main():
  87. scenario(False)
  88. scenario(True)
  89. print("test_autotune_keep: ok")
  90. if __name__ == "__main__":
  91. main()