avgpool and test refactor

This commit is contained in:
George Hotz
2020-10-25 18:40:01 -07:00
parent 4c42676cb6
commit b27bcbe4b4
3 changed files with 63 additions and 45 deletions

View File

@@ -64,44 +64,6 @@ class TestTinygrad(unittest.TestCase):
# coarse approx. since a "big" eps and the non-linearities of the model
self.assertFalse(gradcheck(tiny_func, tiny_x, eps = 0.1))
class TestOps(unittest.TestCase):
def test_conv2d(self):
for cin in [1,2,3]:
for H in [2,3,5]:
for W in [2,3,5]:
x = torch.randn((5,cin,10,7), requires_grad=True)
w = torch.randn((4,cin,H,W), requires_grad=True)
xt = Tensor(x.detach().numpy())
wt = Tensor(w.detach().numpy())
out = torch.nn.functional.conv2d(x,w)
ret = Tensor.conv2d(xt, wt)
# TODO: why so inaccurate?
np.testing.assert_allclose(ret.data, out.detach().numpy(), atol=1e-5)
out.relu().mean().backward()
ret.relu().mean().backward()
np.testing.assert_allclose(w.grad, wt.grad, atol=1e-7)
np.testing.assert_allclose(x.grad, xt.grad, atol=1e-7)
def test_maxpool2x2(self):
x = torch.randn((5,2,10,8), requires_grad=True)
xt = Tensor(x.detach().numpy())
# in tinygrad
ret = xt.max_pool2d()
assert ret.shape == (5,2,10//2,8//2)
ret.mean().backward()
# in torch
out = torch.nn.MaxPool2d((2,2))(x)
out.mean().backward()
# forward and backward the same
np.testing.assert_allclose(ret.data, out.detach().numpy(), atol=1e-5)
np.testing.assert_allclose(x.grad, xt.grad, atol=1e-5)
if __name__ == '__main__':
unittest.main()