2023-12-21 06:33:21 +08:00
|
|
|
import unittest
|
2023-12-21 13:06:27 +08:00
|
|
|
import time
|
2023-12-21 06:33:21 +08:00
|
|
|
import numpy as np
|
2024-08-17 03:09:00 +08:00
|
|
|
from test.helpers import TestUOps
|
2024-05-11 11:09:22 +08:00
|
|
|
from tinygrad import Tensor, dtypes
|
2024-03-27 12:02:46 +08:00
|
|
|
from tinygrad.engine.schedule import create_schedule
|
2024-05-11 11:09:22 +08:00
|
|
|
from tinygrad.engine.realize import lower_schedule_item, run_schedule
|
2023-12-21 06:33:21 +08:00
|
|
|
|
2024-08-17 03:09:00 +08:00
|
|
|
class TestFusionOp(TestUOps):
|
2023-12-21 06:33:21 +08:00
|
|
|
def test_contiguous_add(self):
|
|
|
|
def test(contig=False):
|
|
|
|
bt = Tensor(np.arange(16), dtype=dtypes.float32).reshape(4,4)
|
|
|
|
x = bt.permute(1,0)
|
|
|
|
if contig: x = x.contiguous()
|
|
|
|
return (x.permute(1,0) + bt).data()
|
|
|
|
assert test() == test(True)
|
|
|
|
|
|
|
|
def test_expand_fuse(self):
|
|
|
|
bt = Tensor(np.ones((10, 1)), dtype=dtypes.float32)
|
|
|
|
out = (bt*2).expand(10,10).sum(1)
|
|
|
|
sched = create_schedule([out.lazydata], None)
|
|
|
|
run_schedule(sched)
|
2024-04-18 11:43:10 +08:00
|
|
|
outd = out.tolist()
|
2023-12-21 06:33:21 +08:00
|
|
|
assert all(x == 20.0 for x in outd)
|
|
|
|
|
2023-12-21 13:06:27 +08:00
|
|
|
def test_recursive_add(self):
|
|
|
|
st = time.perf_counter()
|
|
|
|
a = Tensor([1,2,3,4])
|
2023-12-21 15:32:27 +08:00
|
|
|
for _ in range(24): a = a + a
|
2023-12-21 13:06:27 +08:00
|
|
|
sched = create_schedule([a.lazydata], None)
|
2024-05-11 11:09:22 +08:00
|
|
|
ei = lower_schedule_item(sched[-1])
|
2024-08-15 01:52:02 +08:00
|
|
|
self.assertLess(time.perf_counter()-st, 2.0)
|
2024-05-11 11:09:22 +08:00
|
|
|
assert len(ei.prg.p.src.splitlines()) < 250
|
2023-12-21 13:06:27 +08:00
|
|
|
|
2023-12-21 15:32:27 +08:00
|
|
|
def test_recursive_add_cmp(self):
|
|
|
|
st = time.perf_counter()
|
|
|
|
a = Tensor([1,2,3,4])
|
|
|
|
for _ in range(24): a = a + a
|
|
|
|
sched1 = create_schedule([a.lazydata], None)
|
|
|
|
b = Tensor([1,2,3,4])
|
|
|
|
for _ in range(24): b = b + b
|
|
|
|
sched2 = create_schedule([b.lazydata], None)
|
|
|
|
c = Tensor([1,2,3,4])
|
|
|
|
for _ in range(23): c = c + c
|
|
|
|
sched3 = create_schedule([c.lazydata], None)
|
2024-08-17 03:09:00 +08:00
|
|
|
self.assert_equiv_uops(sched1[-1].ast, sched2[-1].ast)
|
|
|
|
with self.assertRaises(AssertionError): self.assert_equiv_uops(sched1[-1].ast, sched3[-1].ast)
|
2024-08-15 01:52:02 +08:00
|
|
|
self.assertLess(time.perf_counter()-st, 2.0)
|
2023-12-21 15:32:27 +08:00
|
|
|
|
2023-12-21 06:33:21 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=2)
|