tensor variable (#4362)

* tensor variable support

* consttype without variable?

* __setitem__

* symbolic mean works

* arange test

* more tests

* a few more tests
This commit is contained in:
George Hotz
2024-05-01 06:08:57 +09:00
committed by GitHub
parent d2f89615b2
commit 27ee49bf30
7 changed files with 84 additions and 12 deletions

View File

@@ -0,0 +1,61 @@
import unittest
import numpy as np
from tinygrad import Tensor, Variable
class TestTensorVariable(unittest.TestCase):
def test_add_tvar(self):
vv = Variable("a", 0, 10)
vv.bind(1)
ret = (Tensor(vv) + 3).item()
assert ret == 4
def test_inner_tvar_node(self):
vv = Variable("w", 0, 10)
vv.bind(2)
ret = Tensor.from_node(vv * 4).item()
assert ret == 8
def test_inner_tvar_mul(self):
vv = Variable("w", 0, 10)
vv.bind(2)
assert (Tensor(3) * vv).item() == 6
def test_inner_tvar_mul_node(self):
vv = Variable("w", 0, 10)
vv.bind(2)
assert (Tensor(3) * (vv * 4)).item() == 24
def test_symbolic_mean(self):
vv = Variable("a", 1, 10)
vv.bind(2)
t = Tensor.ones(2, 2).contiguous().reshape(2, vv)
ret = t.mean().item()
assert ret == 1
def test_symbolic_mean_2d(self):
vv = Variable("a", 1, 10)
vv.bind(2)
vv2 = Variable("b", 1, 10)
vv2.bind(2)
t = Tensor.ones(2, 2).contiguous().reshape(vv2, vv)
ret = t.mean().item()
assert ret == 1
def test_symbolic_mean_2d_axis_1(self):
vv = Variable("a", 1, 10)
vv.bind(2)
vv2 = Variable("b", 1, 10)
vv2.bind(2)
t = Tensor.ones(2, 2).contiguous().reshape(vv2, vv)
ret = t.mean(axis=1).reshape(2, 1).numpy()
assert np.all(ret == 1)
@unittest.skip("symbolic arange isn't supported")
def test_symbolic_arange(self):
vv = Variable("a", 1, 10)
vv.bind(2)
ret = Tensor.arange(0, vv)
ret.realize()
if __name__ == '__main__':
unittest.main()

View File

@@ -143,7 +143,7 @@ class TestRoundUp(unittest.TestCase):
class TestFetch(unittest.TestCase):
def test_fetch_bad_http(self):
self.assertRaises(Exception, fetch, 'http://www.google.com/404')
self.assertRaises(Exception, fetch, 'http://www.google.com/404', allow_caching=False)
@unittest.skipIf(not CI, "pre commit tests should run offline")
def test_fetch_small(self):