import numpy as np import torch import unittest, copy, mmap, random, math, array from tinygrad import Tensor, Device, dtypes, nn from tinygrad.helpers import getenv, temp, mv_address from extra.gradcheck import numerical_jacobian, jacobian, gradcheck from hypothesis import given, settings, strategies as strat from tinygrad.dtype import DTYPES_DICT from tinygrad.uop.ops import UOp settings.register_profile("my_profile", max_examples=200, deadline=None, derandomize=getenv("DERANDOMIZE_CI", False)) settings.load_profile("my_profile") x_init = np.random.randn(1,3).astype(np.float32) U_init = np.random.randn(3,3).astype(np.float32) V_init = np.random.randn(3,3).astype(np.float32) W_init = np.random.randn(3,3).astype(np.float32) m_init = np.random.randn(1,3).astype(np.float32) gradient = np.random.randn(1,3).astype(np.float32) class TestTinygrad(unittest.TestCase): def test_zerodim_initialization(self): self.assertEqual(Tensor(55).shape, ()) self.assertEqual(Tensor(3.14).shape, ()) def test_deviceless_const_construct_device_repr(self): t = Tensor(UOp.const(dtypes.float, 2.0)) self.assertIsNone(t.uop.device) self.assertIsNone(t.device) self.assertIn(" 0.5) assert mask.shape == (3, 2, 0) c = mask.where(a, b) assert c.shape == (3, 2, 0) np.testing.assert_equal(c.numpy(), np.where(mask.numpy(), a.numpy(), b.numpy())) def test_reduce_over_non_zero(self): a = Tensor.ones(3, 2, 0).sum(axis=1) assert a.shape == (3, 0) np.testing.assert_equal(a.numpy(), np.sum(np.zeros((3, 2, 0)), axis=1)) def test_reduce_over_zero(self): a = Tensor.ones(3, 2, 0).sum(axis=2) assert a.shape == (3, 2) np.testing.assert_equal(a.numpy(), np.sum(np.zeros((3, 2, 0)), axis=2)) a = Tensor.ones(3, 2, 0).sum(axis=2, keepdim=True) assert a.shape == (3, 2, 1) np.testing.assert_equal(a.numpy(), np.sum(np.zeros((3, 2, 0)), axis=2, keepdims=True)) def test_clone(self): a = Tensor.rand(16, 16).realize() b = a.clone() np.testing.assert_allclose(a.numpy(), b.numpy()) self.assertIsNot(a.uop.base.buffer, b.uop.base.buffer) a = Tensor.rand(16, 16).mul(5.0).add(5.0).realize() b = a.clone() np.testing.assert_allclose(a.numpy(), b.numpy()) self.assertIsNot(a.uop.base.buffer, b.uop.base.buffer) def test_clone_deviceless_const(self): t = Tensor(UOp.const(dtypes.float, 2.0)).clone() np.testing.assert_equal(t.numpy(), 2.0) self.assertTrue(t.uop.has_buffer_identity()) def test_numpy_deviceless_const(self): np.testing.assert_equal(Tensor(UOp.const(dtypes.float, 2.0)).numpy(), 2.0) def test_clone_with_shrink(self): a = Tensor.rand(16, 16) b = a.shrink(((2, 10), None)).clone() b.realize() self.assertIsNot(a.uop.base.buffer, b.uop.base.buffer) def test_clone_with_shrink_realized(self): a = Tensor.rand(16, 16).realize() b = a.shrink(((2, 10), None)).clone() b.realize() self.assertIsNot(a.uop.base.buffer, b.uop.base.buffer) def test_clone_with_grad(self): a = Tensor.rand(16, 16) a.mul(5.0).add(5.0).mean().backward() b = a.clone() assert a.grad is not None assert b.grad is not None np.testing.assert_allclose(a.grad.numpy(), b.grad.numpy()) def test_clone_deviceless_const_to_cpu(self): t = Tensor(UOp.const(dtypes.float, 2.0)).clone(device="CPU") self.assertEqual(t.device, "CPU") np.testing.assert_equal(t.numpy(), 2.0) def test_reduce_default(self): np.testing.assert_equal(Tensor([]).max().numpy(), -float("inf")) np.testing.assert_equal(Tensor([]).min().numpy(), float("inf")) np.testing.assert_equal(Tensor([]).sum().numpy(), 0) np.testing.assert_equal(Tensor([]).mean().numpy(), float("nan")) class TestTensorCreationDevice(unittest.TestCase): # test auxiliary tensors are created on the same device def test_one_hot(self): y = Tensor([1, 2, 3]).to("CPU") x = y.one_hot(10) x.realize() if __name__ == '__main__': unittest.main()