test_lazyop.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import unittest
  2. from tinygrad.tensor import Tensor
  3. from tinygrad.engine.schedule import create_schedule
  4. # stuff needed to unpack a kernel
  5. # ruff: noqa: F401
  6. from tinygrad.ops import MetaOps, LazyOp, TernaryOps, BinaryOps, UnaryOps, ReduceOps, BufferOps, MemBuffer, ConstBuffer
  7. from tinygrad.lazy import LazyBuffer
  8. from tinygrad import dtypes
  9. from tinygrad.shape.shapetracker import ShapeTracker
  10. from tinygrad.shape.view import View
  11. from tinygrad.shape.symbolic import Variable
  12. import numpy as np
  13. import time
  14. inf, nan = float('inf'), float('nan')
  15. class TestLazyOp(unittest.TestCase):
  16. def test_lazyop_str(self):
  17. t = Tensor.rand(10) + Tensor.rand(10)
  18. s = create_schedule([t.lazydata])
  19. ast = s[-1].ast
  20. ast_remade = eval(str(ast))
  21. self.assertEqual(ast, ast_remade)
  22. def test_selfreferential_speed(self):
  23. st = time.monotonic()
  24. for i in range(25):
  25. p = Tensor([1]).lazydata
  26. for _ in range(i): p = p.e(BinaryOps.ADD, p)
  27. # sanity check if caching works this should be way faster
  28. assert time.monotonic() -st < 0.5, f"{i}"
  29. if __name__ == '__main__':
  30. unittest.main()