From f190ca446d31455d9e834c7e629280561011343e Mon Sep 17 00:00:00 2001 From: adamritter <58403584+adamritter@users.noreply.github.com> Date: Fri, 20 Nov 2020 03:03:42 +0000 Subject: [PATCH] Detach (#123) * Detach * Torch.detach reuses the buffer in the * Fix test * wakey wakey GitHub Actions Co-authored-by: holonomicjl <58403584+holonomicjl@users.noreply.github.com> --- test/test_ops.py | 3 +++ test/test_optim.py | 2 +- tinygrad/tensor.py | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/test/test_ops.py b/test/test_ops.py index 7c53e87aae..c2f7ee7882 100644 --- a/test/test_ops.py +++ b/test/test_ops.py @@ -89,6 +89,9 @@ class TestOps(unittest.TestCase): helper_test_op([(4,3,6,6)], lambda x: torch.reshape(x, (-1,3,6,6)), lambda x: x.reshape(shape=(-1,3,6,6)), gpu=self.gpu) helper_test_op([(4,3,6,6)], lambda x: torch.reshape(x, (-1,1,6,6)), lambda x: x.reshape(shape=(-1,1,6,6)), gpu=self.gpu) + def test_detach(self): + helper_test_op([(4,3,6,6)], lambda x: x.detach(), lambda x: x.detach(), gpu=self.gpu, forward_only=True) + def test_conv2d(self): for bs in [1,8]: for cin in [1,3]: diff --git a/test/test_optim.py b/test/test_optim.py index 927c4d85cc..2ff7580e31 100644 --- a/test/test_optim.py +++ b/test/test_optim.py @@ -55,7 +55,7 @@ class TestOptim(unittest.TestCase): def test_adam(self): for x,y in zip(step_tinygrad(Adam), step_pytorch(torch.optim.Adam)): - np.testing.assert_allclose(x, y, atol=1e-5) + np.testing.assert_allclose(x, y, atol=1e-4) def test_sgd(self): for x,y in zip(step_tinygrad(SGD, kwargs={'lr': 0.001}), diff --git a/tinygrad/tensor.py b/tinygrad/tensor.py index 6e6af278d4..24a01028fe 100644 --- a/tinygrad/tensor.py +++ b/tinygrad/tensor.py @@ -173,6 +173,9 @@ class Tensor: else: return self + def detach(self): + return Tensor(self.data, self.gpu) + # ***** put ops in these dicts ***** ops = {}