tinygrad/test/models/test_mnist.py

117 lines
3.8 KiB
Python
Raw Normal View History

2020-10-19 01:16:01 +08:00
#!/usr/bin/env python
import unittest
2020-10-19 01:16:01 +08:00
import numpy as np
2023-08-22 22:36:24 +08:00
from tinygrad.nn.state import get_parameters
from tinygrad.tensor import Tensor
2023-02-28 02:19:54 +08:00
from tinygrad.nn import optim, BatchNorm2d
from extra.training import train, evaluate
from extra.datasets import fetch_mnist
CI < 5 minutes (#1252) * models matrix * fix typo and install gpu deps * install llvm deps if needed * fix * testops with cuda * remove pip cache since not work * cuda env * install cuda deps * maybe it will work now * i can't read * all tests in matrix * trim down more * opencl stuff in matrix * opencl pip cache * test split * change cuda test exclusion * test * fix cuda maybe * add models * add more n=auto * third thing * fix bug * cache pip more * change name * update tests * try again cause why not * balance * try again... * try apt cache for cuda * try on gpu: * try cuda again * update packages step * replace libz-dev with zlib1g-dev * only cache cuda * why error * fix gpuocelot bug * apt cache err * apt cache to slow? * opt and image in single runner * add a couple n=autos * remove test matrix * try cuda apt cache again * libz-dev -> zlib1g-dev * remove -s since not supported by xdist * the cache takes too long and doesn't work * combine webgpu and metal tests * combine imagenet to c and cpu tests * torch tests with linters * torch back by itself * small windows clang test with torch tests * fix a goofy windows bug * im dumb * bro * clang with linters * fix pylint error * linter not work on windows * try with clang again * clang and imagenet? * install deps * fix * fix quote * clang by itself (windows too slow) * env vars for imagenet * cache pip for metal and webgpu tests * try torch with metal and webgpu * doesn't work, too long * remove -v * try -n=logical * don't use logical * revert accidental thing * remove some prints unless CI * fix print unless CI * ignore speed tests for slow tests * clang windows in matrix (ubuntu being tested in imagenet->c test) * try manual pip cache * fix windows pip cache path * all manual pip cache * fix pip cache dir for macos * print_ci function in helpers * CI as variable, no print_ci * missed one * cuda tests with docker image * remove setup-python action for cuda * python->python3? * remove -s -v * try fix pip cache * maybe fix * try to fix pip cache * is this the path? * maybe cache pip * try again * create wheels dir * ? * cuda pip deps in dockerfile * disable pip cache for clang * image from ghcr instead of docker hub * why is clang like this * fast deps * try use different caches * remove the fast thing * try with lighter image * remove setup python for cuda * small docker and cuda fast deps * ignore a few more tests * cool docker thing (maybe) * oops * quotes * fix docker command * fix bug * ignore train efficientnet test * remove dockerfile (docker stuff takes too long) * remove docker stuff and normal cuda * oops * ignore the tests for cuda * does this work * ignore test_train on slow backends * add space * llvm ignore same tests as cuda * nvm * ignore lr scheduler tests * get some stats * fix ignore bug * remove extra ' * remove and * ignore test for llvm * change ignored tests and durationon all backends * fix * and -> or * ignore some more cuda tests * finally? * does this fix it * remove durations=0 * add some more tests to llvm * make last pytest more readable * fix * don't train efficientnet on cpu * try w/out pip cache * pip cache seems to be generally better * pytest file markers * try apt fast for cuda * use quick install for apt-fast * apt-fast not worth * apt-get to apt * fix typo * suppress warnings * register markers * disable debug on fuzz tests * change marker names * apt update and apt install in one command * update marker names in test.yml * webgpu pytest marker
2023-07-24 04:00:56 +08:00
import pytest
pytestmark = [pytest.mark.exclude_gpu, pytest.mark.exclude_clang]
2020-10-19 01:16:01 +08:00
# load the mnist dataset
2020-10-19 04:30:25 +08:00
X_train, Y_train, X_test, Y_test = fetch_mnist()
2020-10-19 01:16:01 +08:00
2020-10-19 05:55:20 +08:00
# create a model
2020-10-19 04:08:14 +08:00
class TinyBobNet:
def __init__(self):
self.l1 = Tensor.scaled_uniform(784, 128)
self.l2 = Tensor.scaled_uniform(128, 10)
2020-10-19 04:08:14 +08:00
2020-10-27 23:53:35 +08:00
def parameters(self):
return get_parameters(self)
2020-10-27 23:53:35 +08:00
2020-10-19 04:08:14 +08:00
def forward(self, x):
return x.dot(self.l1).relu().dot(self.l2)
2020-10-19 04:08:14 +08:00
# create a model with a conv layer
class TinyConvNet:
2023-02-28 02:19:54 +08:00
def __init__(self, has_batchnorm=False):
2020-10-26 04:48:44 +08:00
# https://keras.io/examples/vision/mnist_convnet/
conv = 3
#inter_chan, out_chan = 32, 64
inter_chan, out_chan = 8, 16 # for speed
self.c1 = Tensor.scaled_uniform(inter_chan,1,conv,conv)
self.c2 = Tensor.scaled_uniform(out_chan,inter_chan,conv,conv)
self.l1 = Tensor.scaled_uniform(out_chan*5*5, 10)
2023-02-28 02:19:54 +08:00
if has_batchnorm:
self.bn1 = BatchNorm2d(inter_chan)
self.bn2 = BatchNorm2d(out_chan)
else:
self.bn1, self.bn2 = lambda x: x, lambda x: x
2020-10-27 23:53:35 +08:00
def parameters(self):
return get_parameters(self)
2020-10-27 23:53:35 +08:00
2023-02-28 02:39:47 +08:00
def forward(self, x:Tensor):
2020-11-08 01:17:57 +08:00
x = x.reshape(shape=(-1, 1, 28, 28)) # hacks
2023-02-28 02:19:54 +08:00
x = self.bn1(x.conv2d(self.c1)).relu().max_pool2d()
x = self.bn2(x.conv2d(self.c2)).relu().max_pool2d()
x = x.reshape(shape=[x.shape[0], -1])
return x.dot(self.l1)
2020-10-23 21:11:38 +08:00
class TestMNIST(unittest.TestCase):
2022-06-06 03:40:12 +08:00
def test_sgd_onestep(self):
2022-06-06 03:13:05 +08:00
np.random.seed(1337)
2022-06-06 03:40:12 +08:00
model = TinyBobNet()
optimizer = optim.SGD(model.parameters(), lr=0.001)
train(model, X_train, Y_train, optimizer, BS=69, steps=1)
2022-07-03 06:47:10 +08:00
for p in model.parameters(): p.realize()
2022-06-07 00:25:31 +08:00
2022-06-07 00:45:37 +08:00
def test_sgd_threestep(self):
np.random.seed(1337)
model = TinyBobNet()
optimizer = optim.SGD(model.parameters(), lr=0.001)
2022-07-18 06:38:43 +08:00
train(model, X_train, Y_train, optimizer, BS=69, steps=3)
def test_sgd_sixstep(self):
np.random.seed(1337)
model = TinyBobNet()
optimizer = optim.SGD(model.parameters(), lr=0.001)
train(model, X_train, Y_train, optimizer, BS=69, steps=6, noloss=True)
2022-06-07 00:45:37 +08:00
2022-06-07 00:25:31 +08:00
def test_adam_onestep(self):
np.random.seed(1337)
model = TinyBobNet()
optimizer = optim.Adam(model.parameters(), lr=0.001)
train(model, X_train, Y_train, optimizer, BS=69, steps=1)
2022-07-03 06:47:10 +08:00
for p in model.parameters(): p.realize()
2020-11-08 01:17:57 +08:00
2022-06-07 00:38:28 +08:00
def test_adam_threestep(self):
np.random.seed(1337)
model = TinyBobNet()
optimizer = optim.Adam(model.parameters(), lr=0.001)
train(model, X_train, Y_train, optimizer, BS=69, steps=3)
2022-06-06 07:31:20 +08:00
def test_conv_onestep(self):
np.random.seed(1337)
model = TinyConvNet()
optimizer = optim.SGD(model.parameters(), lr=0.001)
2022-07-16 23:32:42 +08:00
train(model, X_train, Y_train, optimizer, BS=69, steps=1, noloss=True)
for p in model.parameters(): p.realize()
2022-06-06 07:31:20 +08:00
2020-10-23 21:33:18 +08:00
def test_conv(self):
2020-10-23 21:11:38 +08:00
np.random.seed(1337)
2020-10-23 17:53:01 +08:00
model = TinyConvNet()
2020-10-27 23:53:35 +08:00
optimizer = optim.Adam(model.parameters(), lr=0.001)
2022-07-04 06:18:00 +08:00
train(model, X_train, Y_train, optimizer, steps=100)
2023-07-24 03:57:29 +08:00
assert evaluate(model, X_test, Y_test) > 0.93 # torch gets 0.9415 sometimes
2023-02-28 02:19:54 +08:00
def test_conv_with_bn(self):
np.random.seed(1337)
model = TinyConvNet(has_batchnorm=True)
2023-03-20 14:53:56 +08:00
optimizer = optim.AdamW(model.parameters(), lr=0.003)
train(model, X_train, Y_train, optimizer, steps=200)
assert evaluate(model, X_test, Y_test) > 0.94
2023-02-28 02:19:54 +08:00
2020-10-23 21:33:18 +08:00
def test_sgd(self):
2020-10-23 21:11:38 +08:00
np.random.seed(1337)
2020-10-23 17:53:01 +08:00
model = TinyBobNet()
2020-10-27 23:53:35 +08:00
optimizer = optim.SGD(model.parameters(), lr=0.001)
2022-07-04 06:18:00 +08:00
train(model, X_train, Y_train, optimizer, steps=600)
2023-02-20 05:05:37 +08:00
assert evaluate(model, X_test, Y_test) > 0.94 # CPU gets 0.9494 sometimes
if __name__ == '__main__':
unittest.main()