test_zero_copy.py 910 B

123456789101112131415161718192021222324252627
  1. import unittest
  2. from tinygrad import Tensor, Device
  3. import time
  4. def time_tensor_numpy(out:Tensor):
  5. times = []
  6. for _ in range(5):
  7. st = time.perf_counter()
  8. out.lazydata.base.realized.as_buffer(allow_zero_copy=True)
  9. et = time.perf_counter() - st
  10. times.append(et)
  11. return min(times)
  12. N = 4096
  13. class TestZeroCopy(unittest.TestCase):
  14. @unittest.skipIf(Device.DEFAULT not in {"CLANG", "LLVM", "CPU", "METAL"}, "device isn't zero copy")
  15. def test_zero_copy_from_default_to_cpu(self):
  16. demo = Tensor.rand(1).realize()
  17. t1 = time_tensor_numpy(demo)
  18. out = Tensor.rand(N, N).realize()
  19. t2 = time_tensor_numpy(out)
  20. gbps = out.nbytes()*1e-9/max(t2-t1, 1e-10)
  21. print(f"time(base): {t1*1e3:.2f} ms, time(copy): {t2*1e3:.2f} ms : copy speed {gbps:.2f} GB/s")
  22. self.assertGreater(gbps, 600) # more than 600 GB/s = no copy
  23. if __name__ == '__main__':
  24. unittest.main(verbosity=2)