test_folded_fence.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """CPU check for adapter/folded_result_fence.py with stand-ins that have the engine's interface."""
  2. import os
  3. import types
  4. from dataclasses import dataclass
  5. os.environ["DSV41_FOLDED_FENCE"] = "1"
  6. import torch # noqa: E402
  7. import folded_result_fence as ff # noqa: E402
  8. @dataclass
  9. class AcceptOuts:
  10. correct_len: torch.Tensor
  11. bonus: torch.Tensor
  12. cap_trim_lens: torch.Tensor
  13. commit_lens: torch.Tensor
  14. new_seq_lens: torch.Tensor
  15. out_tokens: torch.Tensor
  16. BUF = {f: torch.arange(6) for f in ff.FIELDS}
  17. class Executor:
  18. def accept_and_finalize(self, *, folded_accept, **kw):
  19. return AcceptOuts(**BUF) if folded_accept else AcceptOuts(**{f: torch.zeros(6) for f in ff.FIELDS})
  20. def main():
  21. ff.install(types.SimpleNamespace(TargetVerifyExecutor=Executor, AcceptOuts=AcceptOuts))
  22. out = Executor().accept_and_finalize(folded_accept=True)
  23. for f in ff.FIELDS:
  24. assert torch.equal(getattr(out, f), BUF[f]) and getattr(out, f).data_ptr() != BUF[f].data_ptr()
  25. BUF["out_tokens"].fill_(-1) # the next replay overwrites the persistent buffer
  26. assert out.out_tokens.tolist() == list(range(6))
  27. eager = Executor().accept_and_finalize(folded_accept=False)
  28. assert eager.out_tokens.sum() == 0
  29. try:
  30. ff.install(types.SimpleNamespace())
  31. except RuntimeError:
  32. print("test_folded_fence: ok")
  33. return
  34. raise AssertionError("drifted module accepted")
  35. if __name__ == "__main__":
  36. main()