forked from tinygrad/tinygrad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
symbolic codegen and exec (tinygrad#1552)
* symbolic codegen and exec * fix and add test * no sketchy * merge_dicts type * dtypes._arg_int32
- Loading branch information
Showing
16 changed files
with
202 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import unittest | ||
from tinygrad.shape.symbolic import Variable | ||
from tinygrad.helpers import getenv, CI | ||
from tinygrad.tensor import Tensor, Device | ||
import numpy as np | ||
|
||
@unittest.skipIf(getenv("ARM64"), "ARM64 is not supported") | ||
@unittest.skipUnless(Device.DEFAULT in ["GPU", "METAL", "CLANG"], f"{Device.DEFAULT} is not supported") | ||
class TestSymbolicOps(unittest.TestCase): | ||
def test_plus1(self): | ||
def f(a): return (a+1).realize() | ||
vi = Variable("i", 1, 10) | ||
for i in range(1, 5): | ||
a = Tensor.rand(3, i) | ||
symbolic = f(a.reshape(3, vi)).reshape(3, i).cpu().numpy() | ||
expected = f(a).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) | ||
|
||
def test_add(self): | ||
def f(a, b): return (a+b).realize() | ||
vi = Variable("i", 1, 10) | ||
for i in range(1, 5): | ||
a = Tensor.rand(3, i) | ||
b = Tensor.rand(3, i) | ||
symbolic = f(a.reshape(3, vi), b.reshape(3, vi)).reshape(3, i).cpu().numpy() | ||
expected = f(a, b).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) | ||
|
||
def test_matmul(self): | ||
def f(a, b): return (a@b).realize() | ||
vi = Variable("i", 1, 10) | ||
for i in range(1, 5): | ||
a = Tensor.rand(3, i) | ||
b = Tensor.rand(i, 5) | ||
symbolic = f(a.reshape(3, vi), b.reshape(vi, 5)).cpu().numpy() | ||
expected = f(a, b).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) | ||
|
||
def test_matmul_same_var_different_val(self): | ||
def f(a, b): return (a@b).realize() | ||
vi = Variable("i", 1, 10) | ||
a = Tensor.rand(3, 4) | ||
b = Tensor.rand(7, 5) | ||
with self.assertRaises(AssertionError): | ||
f(a.reshape(3, vi), b.reshape(vi, 5)).cpu().numpy() | ||
|
||
@unittest.skipIf(Device.DEFAULT == "CLANG" and CI, "broken on CLANG CI") | ||
def test_attention(self): | ||
def f(q, k, v): return Tensor.scaled_dot_product_attention(q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2)).realize() | ||
vi = Variable("i", 1, 10) | ||
for i in range(1, 5): | ||
q = Tensor.rand(2, 1, 4, 8) | ||
k = Tensor.rand(2, i, 4, 8) | ||
v = Tensor.rand(2, i, 4, 8) | ||
symbolic = f(q, k.reshape(2, vi, 4, 8), v.reshape(2, vi, 4, 8)).reshape(2, 4, 1, 8).cpu().numpy() | ||
expected = f(q, k, v).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) | ||
|
||
def test_cat_dim0(self): | ||
def f(a, b): return a.cat(b, dim=0).realize() | ||
vi = Variable("i", 1, 10) | ||
for i in range(1, 5): | ||
a = Tensor.rand(i, 3) | ||
b = Tensor.rand(2, 3) | ||
symbolic = f(a.reshape(vi, 3), b).reshape(i+2, 3).cpu().numpy() | ||
expected = f(a, b).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) | ||
|
||
def test_cat_dim1(self): | ||
def f(a, b): return a.cat(b, dim=1).realize() | ||
vi = Variable("i", 1, 10) | ||
for i in range(1, 5): | ||
a = Tensor.rand(3, i) | ||
b = Tensor.rand(3, 2) | ||
symbolic = f(a.reshape(3, vi), b).reshape(3, i+2).cpu().numpy() | ||
expected = f(a, b).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) | ||
|
||
def test_cat_dim0_two_vars(self): | ||
def f(a, b): return a.cat(b, dim=0).realize() | ||
vi = Variable("i", 1, 10) | ||
vj = Variable("j", 1, 10) | ||
for i in range(1, 5): | ||
for j in range(1, 5): | ||
a = Tensor.rand(i, 3) | ||
b = Tensor.rand(j, 3) | ||
symbolic = f(a.reshape(vi, 3), b.reshape(vj, 3)).reshape(i+j, 3).cpu().numpy() | ||
expected = f(a, b).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) | ||
|
||
def test_cat_dim1_two_vars(self): | ||
def f(a, b): return a.cat(b, dim=1).realize() | ||
vi = Variable("i", 1, 10) | ||
vj = Variable("j", 1, 10) | ||
for i in range(1, 5): | ||
for j in range(1, 5): | ||
a = Tensor.rand(3, i) | ||
b = Tensor.rand(3, j) | ||
symbolic = f(a.reshape(3, vi), b.reshape(3, vj)).reshape(3, i+j).cpu().numpy() | ||
expected = f(a, b).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) | ||
|
||
def test_two_vars_plus1(self): | ||
def f(a, b): return (a@b+1).realize() | ||
vi = Variable("i", 1, 10) | ||
vj = Variable("j", 1, 10) | ||
for i in range(1, 5): | ||
for j in range(1, 5): | ||
a = Tensor.rand(i, 3) | ||
b = Tensor.rand(3, j) | ||
symbolic = f(a.reshape(vi, 3), b.reshape(3, vj)).reshape(i, j).cpu().numpy() | ||
expected = f(a, b).cpu().numpy() | ||
np.testing.assert_allclose(symbolic, expected, atol=1e-6, rtol=1e-6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.