test_draft_head_fp8_tp4.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """CPU check for adapter/draft_head_fp8_tp4.py and its selection in sitecustomize.
  2. - the TP4 variant builds the same fp8 copy as adapter/draft_head_fp8.py (bitwise) and refuses a
  3. drifted draft class;
  4. - sitecustomize installs draft_head_fp8_tp4 only when start-tp4.sh set DSV41_LAUNCHER=tp4
  5. and DSV41_DRAFT_HEAD_FP8_IMPL=tp4. ./start.sh always installs draft_head_fp8.py.
  6. Nothing is installed when DSV41_DRAFT_HEAD_FP8 is off. The Triton kernels are checked on the Spark.
  7. """
  8. import importlib.util
  9. import os
  10. import sys
  11. import types
  12. from pathlib import Path
  13. from unittest.mock import patch
  14. os.environ["DSV41_DRAFT_HEAD_FP8"] = "1"
  15. import torch # noqa: E402
  16. import draft_head_fp8 as default_impl # noqa: E402
  17. import draft_head_fp8_tp4 as tp4_impl # noqa: E402
  18. ROOT = Path(__file__).resolve().parents[1]
  19. def check_twin():
  20. w = (torch.randn(1000, 256) * 0.02).to(torch.bfloat16)
  21. q4, e4 = tp4_impl.make_twin(w)
  22. q, e = default_impl.make_twin(w)
  23. assert torch.equal(q4.view(torch.uint8), q.view(torch.uint8)) and torch.equal(e4, e), "fp8 copies differ"
  24. try:
  25. tp4_impl.install(types.SimpleNamespace(DeepseekV4ForCausalLMDSpark=type("X", (), {})))
  26. except RuntimeError:
  27. return
  28. raise AssertionError("drifted class accepted")
  29. def check_selection():
  30. spec = importlib.util.spec_from_file_location("dsv41_sc_test", ROOT / "adapter/sitecustomize.py")
  31. sc = importlib.util.module_from_spec(spec)
  32. with patch.dict(os.environ, {"DSV41_SOURCE": ""}):
  33. spec.loader.exec_module(sc)
  34. class Loader:
  35. def create_module(self, spec):
  36. return None
  37. def exec_module(self, module):
  38. pass
  39. for env, expected in (({"DSV41_DRAFT_HEAD_FP8": "1"}, "default"),
  40. ({"DSV41_DRAFT_HEAD_FP8": "1", "DSV41_DRAFT_HEAD_FP8_IMPL": "tp4"}, "default"),
  41. ({"DSV41_LAUNCHER": "tp4", "DSV41_DRAFT_HEAD_FP8": "1", "DSV41_DRAFT_HEAD_FP8_IMPL": "tp4"}, "tp4"),
  42. ({"DSV41_DRAFT_HEAD_FP8": "1", "DSV41_DRAFT_HEAD_FP8_IMPL": ""}, "default"),
  43. ({"DSV41_DRAFT_HEAD_FP8": "0", "DSV41_DRAFT_HEAD_FP8_IMPL": "tp4"}, None)):
  44. calls = []
  45. fakes = {name: types.SimpleNamespace(install=lambda m, tag=tag: calls.append(tag))
  46. for name, tag in (("draft_head_fp8", "default"), ("draft_head_fp8_tp4", "tp4"))}
  47. clean = {k: v for k, v in os.environ.items() if not k.startswith("DSV41_")}
  48. with patch.dict(os.environ, {**clean, **env}, clear=True), patch.dict(sys.modules, fakes):
  49. sc.EngramLoader(Loader()).exec_module(
  50. types.SimpleNamespace(__name__="sglang.srt.models.deepseek_v4_dspark"))
  51. assert calls == ([expected] if expected else []), (env, calls)
  52. if __name__ == "__main__":
  53. check_twin()
  54. check_selection()
  55. print("test_draft_head_fp8_tp4: ok")