2023-09-29 19:40:35 +08:00
|
|
|
import unittest
|
2024-01-29 01:21:26 +08:00
|
|
|
from tinygrad import Tensor, GlobalCounters
|
2024-01-31 01:34:28 +08:00
|
|
|
from tinygrad.helpers import Timing, CI, Profiling, WINO
|
2023-09-29 19:40:35 +08:00
|
|
|
from tinygrad.ops import LoadOps
|
|
|
|
from tinygrad.codegen.linearizer import Linearizer
|
|
|
|
|
|
|
|
class TestWinograd(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2024-01-18 09:21:26 +08:00
|
|
|
self.old = WINO.value
|
|
|
|
WINO.value = 1
|
|
|
|
def tearDown(self):
|
|
|
|
WINO.value = self.old
|
2023-09-29 19:40:35 +08:00
|
|
|
|
|
|
|
def test_speed(self):
|
|
|
|
x = Tensor.empty(1,4,9,9)
|
|
|
|
w = Tensor.empty(4,4,3,3)
|
|
|
|
|
|
|
|
with Timing("running conv: "):
|
|
|
|
out = Tensor.conv2d(x, w)
|
|
|
|
|
|
|
|
with Timing("scheduling: "):
|
|
|
|
sched = out.lazydata.schedule()
|
|
|
|
|
|
|
|
for i,s in enumerate(sched):
|
2023-10-07 23:59:25 +08:00
|
|
|
if s.ast.op in LoadOps: continue
|
2023-12-21 10:04:49 +08:00
|
|
|
ops = s.ast.lazyops
|
2023-09-29 20:41:29 +08:00
|
|
|
with Timing(f"linearize {i} with {len(ops):4d} ops: "):
|
2023-10-07 23:59:25 +08:00
|
|
|
l = Linearizer(s.ast)
|
2023-09-29 19:40:35 +08:00
|
|
|
l.hand_coded_optimizations()
|
|
|
|
l.linearize()
|
|
|
|
|
2023-10-07 19:39:21 +08:00
|
|
|
def test_profile(self):
|
|
|
|
x,w = Tensor.rand(1,4,9,9).realize(), Tensor.rand(4,4,3,3).realize()
|
2023-11-17 06:15:56 +08:00
|
|
|
with Profiling(enabled=not CI, sort='time'):
|
|
|
|
out = Tensor.conv2d(x,w).realize()
|
2023-10-07 19:39:21 +08:00
|
|
|
out.numpy()
|
|
|
|
|
2024-01-29 01:21:26 +08:00
|
|
|
def test_four_kernels(self):
|
|
|
|
x,w = Tensor.rand(1,4,9,9).realize(), Tensor.rand(4,4,3,3).realize()
|
|
|
|
GlobalCounters.reset()
|
|
|
|
out = Tensor.conv2d(x,w).realize()
|
|
|
|
assert GlobalCounters.kernel_count == 4
|
|
|
|
out.numpy()
|
|
|
|
|
2023-09-29 19:40:35 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=2)
|