2024-04-03 04:45:58 +08:00
|
|
|
import unittest, math
|
2024-04-03 08:52:05 +08:00
|
|
|
from tinygrad import Tensor, Device, dtypes
|
2024-04-01 01:09:23 +08:00
|
|
|
from tinygrad.engine.schedule import create_schedule
|
2024-04-02 04:53:43 +08:00
|
|
|
from tinygrad.features.multi import MultiLazyBuffer
|
|
|
|
from tinygrad.helpers import CI
|
|
|
|
from tinygrad.ops import BufferOps
|
|
|
|
import numpy as np
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def _check_ast_count(desired_count:int, t:Tensor):
|
|
|
|
# NOTE: this has side effect because everything can be scheduled only once
|
2024-04-02 04:53:43 +08:00
|
|
|
schedule = create_schedule(t.lazydata.lbs if isinstance(t.lazydata, MultiLazyBuffer) else [t.lazydata])
|
|
|
|
asts = [s for s in schedule if s.ast[0].op is BufferOps.STORE]
|
2024-04-01 01:09:23 +08:00
|
|
|
assert len(asts) == desired_count
|
|
|
|
|
2024-04-04 02:39:28 +08:00
|
|
|
class TestUnaryOpsConstFolding(unittest.TestCase):
|
2024-04-03 08:52:05 +08:00
|
|
|
def test_all_consts_ops(self):
|
|
|
|
_check_ast_count(0, Tensor.ones(4).exp())
|
|
|
|
_check_ast_count(0, Tensor.ones(4).sqrt())
|
|
|
|
_check_ast_count(0, Tensor.ones(4) + Tensor.ones(4))
|
|
|
|
_check_ast_count(0, Tensor.ones(4) / Tensor.ones(4))
|
|
|
|
|
|
|
|
def test_cast(self):
|
|
|
|
_check_ast_count(0, Tensor.ones(4).cast(dtypes.int16))
|
|
|
|
_check_ast_count(0, Tensor.full(4, fill_value=-1).cast(dtypes.uint16))
|
|
|
|
|
2024-04-04 02:39:28 +08:00
|
|
|
class TestBinaryOpsConstFolding(unittest.TestCase):
|
2024-04-01 01:09:23 +08:00
|
|
|
def test_add_literal_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) + 0)
|
2024-04-01 01:09:23 +08:00
|
|
|
def test_add_tensor_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) + Tensor.zeros(4))
|
2024-04-02 03:09:04 +08:00
|
|
|
def test_literal_zero_add(self):
|
|
|
|
_check_ast_count(0, 0 + Tensor([1.0, 2, 3, 4]))
|
|
|
|
def test_tensor_zero_add(self):
|
|
|
|
_check_ast_count(0, Tensor.zeros(4) + Tensor([1.0, 2, 3, 4]))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_sub_literal_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) - 0)
|
2024-04-01 01:09:23 +08:00
|
|
|
def test_sub_tensor_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) - Tensor.zeros(4))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_mul_literal_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) * 0)
|
2024-04-01 01:09:23 +08:00
|
|
|
def test_mul_tensor_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) * Tensor.zeros(4))
|
2024-04-02 03:09:04 +08:00
|
|
|
def test_literal_zero_mul(self):
|
|
|
|
_check_ast_count(0, 0 * Tensor([1.0, 2, 3, 4]) * 0)
|
|
|
|
def test_tensor_zero_mul(self):
|
|
|
|
_check_ast_count(0, Tensor.zeros(4) * Tensor([1.0, 2, 3, 4]))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_mul_literal_one(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) * 1)
|
2024-04-01 01:09:23 +08:00
|
|
|
def test_mul_tensor_one(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) * Tensor.ones(4))
|
2024-04-02 03:09:04 +08:00
|
|
|
def test_literal_one_mul(self):
|
|
|
|
_check_ast_count(0, 1 * Tensor([1.0, 2, 3, 4]))
|
|
|
|
def test_tensor_one_mul(self):
|
|
|
|
_check_ast_count(0, Tensor.ones(4) * Tensor([1.0, 2, 3, 4]))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_div_literal_one(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) / 1)
|
2024-04-01 01:09:23 +08:00
|
|
|
def test_div_tensor_one(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) / Tensor.ones(4))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_pow_literal_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) ** 0)
|
2024-04-01 01:09:23 +08:00
|
|
|
def test_pow_tensor_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) ** Tensor.zeros(4))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_pow_literal_one(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) ** 1)
|
2024-04-01 01:09:23 +08:00
|
|
|
def test_pow_tensor_one(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) ** Tensor.ones(4))
|
2024-04-02 03:09:04 +08:00
|
|
|
def test_literal_one_pow(self):
|
|
|
|
_check_ast_count(0, 1 ** Tensor([1.0, 2, 3, 4]))
|
|
|
|
def test_tensor_one_pow(self):
|
|
|
|
_check_ast_count(0, Tensor.ones(4) ** Tensor([1.0, 2, 3, 4]))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
2024-04-04 22:46:28 +08:00
|
|
|
# folds advance indexing into basic indexing
|
|
|
|
class TestIndexingConstFolding(unittest.TestCase):
|
|
|
|
def test_scalar_index(self):
|
|
|
|
t = Tensor.arange(16).float().reshape(1,1,4,4).realize()
|
|
|
|
_check_ast_count(0, t[:,:,Tensor(1),:])
|
|
|
|
_check_ast_count(0, t[:,:,Tensor(1)+2,:])
|
|
|
|
_check_ast_count(0, t[:,:,Tensor(1),Tensor(0)])
|
|
|
|
|
|
|
|
@unittest.expectedFailure
|
|
|
|
def test_const_tensor_index(self):
|
|
|
|
# TODO: implement const tensor folded indexing
|
|
|
|
t = Tensor.arange(16).float().reshape(1,1,4,4).realize()
|
|
|
|
_check_ast_count(0, t[:,:,Tensor.ones(2,1),:])
|
|
|
|
_check_ast_count(0, t[:,:,Tensor.ones(1,2)+2,:])
|
|
|
|
_check_ast_count(0, t[:,:,Tensor.ones(1,1),Tensor.zeros(2,1,2)])
|
|
|
|
|
2024-04-01 01:09:23 +08:00
|
|
|
class TestMovedConstFolding(unittest.TestCase):
|
|
|
|
def test_add_shrunk_zero(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) + Tensor.zeros(6).shrink(((1, 5),)))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_add_padded_zero(self):
|
|
|
|
# TODO: it's 1 now, this might be possible to fold
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(1, Tensor([1.0, 2, 3, 4]) + Tensor.zeros(2).pad(((1, 1),)))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_mul_shrunk_one(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(0, Tensor([1.0, 2, 3, 4]) * Tensor.ones(6).shrink(((1, 5),)))
|
2024-04-01 01:09:23 +08:00
|
|
|
|
|
|
|
def test_add_padded_one(self):
|
2024-04-01 04:35:36 +08:00
|
|
|
_check_ast_count(1, Tensor([1.0, 2, 3, 4]) * Tensor.ones(2).pad(((1, 1),)))
|
2024-04-02 04:53:43 +08:00
|
|
|
|
2024-04-04 00:31:24 +08:00
|
|
|
def test_cast_padded(self):
|
2024-04-13 13:05:08 +08:00
|
|
|
_check_ast_count(1, Tensor.ones(4).pad(((1, 1),)).cast(dtypes.int16))
|
2024-04-04 00:31:24 +08:00
|
|
|
np.testing.assert_equal(Tensor.ones(4).pad(((1, 1),)).cast(dtypes.int16).numpy(), [0, 1, 1, 1, 1, 0])
|
|
|
|
_check_ast_count(1, Tensor.ones(4).pad(((1, 1),)).cast(dtypes.int64))
|
|
|
|
np.testing.assert_equal(Tensor.ones(4).pad(((1, 1),)).cast(dtypes.int64).numpy(), [0, 1, 1, 1, 1, 0])
|
2024-04-13 13:05:08 +08:00
|
|
|
_check_ast_count(1, Tensor.full(4, fill_value=-1).pad(((1, 1),)).cast(dtypes.uint16))
|
|
|
|
np.testing.assert_equal(Tensor.full(4, fill_value=-1).pad(((1, 1),)).cast(dtypes.uint16).numpy(), [0, 65535, 65535, 65535, 65535, 0])
|
2024-04-04 00:31:24 +08:00
|
|
|
|
2024-04-04 02:39:28 +08:00
|
|
|
class TestReduceOpsConstFolding(unittest.TestCase):
|
|
|
|
def test_const_sum(self):
|
|
|
|
_check_ast_count(0, Tensor.ones(4, 5, 6).sum())
|
|
|
|
np.testing.assert_equal(Tensor.ones(4, 5, 6).sum().numpy(), 4 * 5 * 6)
|
|
|
|
_check_ast_count(0, Tensor.ones(4, 5, 6).sum(axis=0))
|
|
|
|
np.testing.assert_equal(Tensor.ones(4, 5, 6).sum(axis=0).numpy(), np.full((5, 6), 4))
|
|
|
|
_check_ast_count(0, Tensor(4).sum())
|
|
|
|
np.testing.assert_equal(Tensor(4).sum().numpy(), 4)
|
|
|
|
|
|
|
|
def test_padded_const_sum(self):
|
|
|
|
_check_ast_count(1, Tensor.ones(4).pad(((1, 1),)).sum())
|
|
|
|
np.testing.assert_equal(Tensor.ones(4).pad(((1, 1),)).sum().numpy(), 4)
|
|
|
|
|
|
|
|
# NOTE: cannot just count the non-padded area because some UnaryOps f do not have f(0) = 0.
|
|
|
|
_check_ast_count(1, Tensor.ones(4).pad(((1, 1),)).exp().sum())
|
|
|
|
np.testing.assert_allclose(Tensor.ones(4).pad(((1, 1),)).exp().sum().numpy(), 4 * math.e + 2)
|
|
|
|
|
|
|
|
def test_const_max(self):
|
|
|
|
_check_ast_count(0, Tensor.ones(4, 5, 6).max())
|
|
|
|
np.testing.assert_equal(Tensor.ones(4, 5, 6).max().numpy(), 1)
|
|
|
|
_check_ast_count(0, Tensor(4).max())
|
|
|
|
np.testing.assert_equal(Tensor(4).max().numpy(), 4)
|
|
|
|
|
2024-04-02 04:53:43 +08:00
|
|
|
@unittest.skipIf(CI and Device.DEFAULT in {"GPU", "CUDA", "METAL"}, "no GPU CI")
|
|
|
|
class TestMultiConstFolding(unittest.TestCase):
|
|
|
|
def test_multi_const_folding_literal(self):
|
|
|
|
ds = tuple(f"{Device.DEFAULT}:{i}" for i in range(4))
|
|
|
|
t = Tensor.arange(16).float().realize().to(ds)
|
|
|
|
|
|
|
|
# non const folding case creates one ast on each shard
|
|
|
|
_check_ast_count(4, t + 1)
|
|
|
|
_check_ast_count(4, 1 + t)
|
|
|
|
_check_ast_count(4, t * 2)
|
|
|
|
_check_ast_count(4, 2 * t)
|
|
|
|
|
|
|
|
# const folded
|
|
|
|
_check_ast_count(0, t + 0)
|
|
|
|
_check_ast_count(0, 0 + t)
|
|
|
|
_check_ast_count(0, t * 0)
|
|
|
|
_check_ast_count(0, 0 * t)
|
|
|
|
_check_ast_count(0, t * 1)
|
|
|
|
_check_ast_count(0, 1 * t)
|
|
|
|
np.testing.assert_equal((t + 0).numpy(), np.arange(16))
|
|
|
|
np.testing.assert_equal((t * 0).numpy(), [0] * 16)
|
|
|
|
np.testing.assert_equal((t * 1).numpy(), np.arange(16))
|
|
|
|
|
2024-04-03 08:52:05 +08:00
|
|
|
_check_ast_count(0, t ** 0)
|
|
|
|
_check_ast_count(0, t ** 1)
|
|
|
|
_check_ast_count(0, 1 ** t)
|
|
|
|
|
2024-04-02 04:53:43 +08:00
|
|
|
def test_multi_const_folding_tensor(self):
|
|
|
|
ds = tuple(f"{Device.DEFAULT}:{i}" for i in range(4))
|
|
|
|
t = Tensor.arange(16).float().realize().to(ds)
|
|
|
|
zero = Tensor.zeros(16).realize().to(ds)
|
|
|
|
one = Tensor.ones(16).realize().to(ds)
|
|
|
|
|
|
|
|
# const folded
|
|
|
|
_check_ast_count(0, t + zero)
|
|
|
|
_check_ast_count(0, zero + t)
|
|
|
|
_check_ast_count(0, t * zero)
|
|
|
|
_check_ast_count(0, zero * t)
|
|
|
|
_check_ast_count(0, t * one)
|
|
|
|
_check_ast_count(0, one * t)
|
|
|
|
np.testing.assert_equal((t + zero).numpy(), np.arange(16))
|
|
|
|
np.testing.assert_equal((t * zero).numpy(), [0] * 16)
|
|
|
|
np.testing.assert_equal((t * one).numpy(), np.arange(16))
|
|
|
|
|
|
|
|
@unittest.expectedFailure
|
|
|
|
def test_multi_todo_pow(self):
|
|
|
|
ds = tuple(f"{Device.DEFAULT}:{i}" for i in range(4))
|
|
|
|
t = Tensor.arange(16).float().realize().to(ds)
|
2024-04-03 08:52:05 +08:00
|
|
|
zero = Tensor.zeros(16).realize().to(ds)
|
|
|
|
one = Tensor.ones(16).realize().to(ds)
|
2024-04-02 04:53:43 +08:00
|
|
|
|
|
|
|
# TODO: fix pow folding
|
2024-04-03 08:52:05 +08:00
|
|
|
_check_ast_count(0, t ** zero)
|
|
|
|
_check_ast_count(0, t ** one)
|
|
|
|
_check_ast_count(0, one ** t)
|
2024-04-02 07:22:14 +08:00
|
|
|
|
2024-04-03 04:45:58 +08:00
|
|
|
class TestTautologicalCompare(unittest.TestCase):
|
|
|
|
# without const folding, these would have triggered -Wtautological-compare in clang
|
|
|
|
def test_lt_false(self):
|
|
|
|
# bool < False is always false
|
|
|
|
np.testing.assert_equal((Tensor([True, False]) < False).numpy(), [False, False])
|
|
|
|
|
|
|
|
def test_true_lt(self):
|
|
|
|
# True < bool is always false
|
|
|
|
np.testing.assert_equal((True < Tensor([True, False])).numpy(), [False, False])
|
|
|
|
|
|
|
|
def test_truth_table(self):
|
|
|
|
np.testing.assert_equal((Tensor(False) < Tensor(False)).numpy(), False)
|
|
|
|
np.testing.assert_equal((Tensor(False) < Tensor(True)).numpy(), True)
|
|
|
|
np.testing.assert_equal((Tensor(True) < Tensor(False)).numpy(), False)
|
|
|
|
np.testing.assert_equal((Tensor(True) < Tensor(True)).numpy(), False)
|
|
|
|
|
|
|
|
@unittest.skip("not implemented yet")
|
|
|
|
def test_a_eq_a(self):
|
|
|
|
# self eq is always true for int or bool
|
|
|
|
a = Tensor([1, 2, 3])
|
|
|
|
np.testing.assert_equal((a == a).numpy(), [True, True, True])
|
|
|
|
|
|
|
|
# not true for nan
|
|
|
|
a = Tensor([math.nan, 1.0, 2.0])
|
|
|
|
np.testing.assert_equal((a == a).numpy(), [False, True, True])
|
|
|
|
|
|
|
|
@unittest.skip("not implemented yet")
|
|
|
|
def test_a_ne_a(self):
|
|
|
|
# self not eq is always false for int or bool
|
|
|
|
a = Tensor([1, 2, 3])
|
|
|
|
np.testing.assert_equal((a != a).numpy(), [False, False, False])
|
|
|
|
|
|
|
|
# not true for nan
|
|
|
|
a = Tensor([math.nan, 1.0, 2.0])
|
|
|
|
np.testing.assert_equal((a != a).numpy(), [True, False, False])
|
|
|
|
|
2024-04-02 07:22:14 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|