test_draft_head_fp8.py 1.0 KB

12345678910111213141516171819202122232425262728
  1. """CPU check for adapter/draft_head_fp8.py: the fp8 copy reconstructs the head within fp8 rounding,
  2. and the patch refuses a drifted draft class. The Triton kernel itself is checked on the Spark."""
  3. import os
  4. import types
  5. os.environ["DSV41_DRAFT_HEAD_FP8"] = "1"
  6. import torch # noqa: E402
  7. import draft_head_fp8 as dh # noqa: E402
  8. def main():
  9. w = (torch.randn(1000, 256) * 0.02).to(torch.bfloat16) # 1000 rows: not a multiple of 32
  10. q, e = dh.make_twin(w)
  11. assert q.shape == (1000, 256) and e.shape == (32, 8)
  12. back = q.float().view(1000, 8, 32) * torch.exp2(e.float() - 127).repeat_interleave(32, 0)[:1000].unsqueeze(-1)
  13. rel = ((back.view(1000, 256) - w.float()).norm() / w.float().norm()).item()
  14. assert rel < 0.05, rel
  15. try:
  16. dh.install(types.SimpleNamespace(DeepseekV4ForCausalLMDSpark=type("X", (), {})))
  17. except RuntimeError:
  18. print(f"test_draft_head_fp8: ok (rel err {rel:.3f})")
  19. return
  20. raise AssertionError("drifted class accepted")
  21. if __name__ == "__main__":
  22. main()