2021-06-19 12:24:15 -06:00
|
|
|
import unittest
|
|
|
|
import time
|
2022-08-18 08:41:00 -06:00
|
|
|
import tinygrad.nn.optim as optim
|
2021-06-19 12:24:15 -06:00
|
|
|
import numpy as np
|
2022-07-17 13:11:53 -06:00
|
|
|
from tinygrad.tensor import Device
|
2023-01-31 16:09:09 -07:00
|
|
|
from tinygrad.helpers import getenv
|
2021-06-19 12:24:15 -06:00
|
|
|
from extra.training import train
|
|
|
|
from extra.utils import get_parameters
|
|
|
|
from models.efficientnet import EfficientNet
|
2021-11-30 14:14:54 -07:00
|
|
|
from models.transformer import Transformer
|
|
|
|
from models.vit import ViT
|
2021-10-30 17:48:39 -06:00
|
|
|
from models.resnet import ResNet18
|
2021-06-19 12:24:15 -06:00
|
|
|
|
2023-01-31 16:09:09 -07:00
|
|
|
BS = getenv("BS", 2)
|
2021-06-19 12:24:15 -06:00
|
|
|
|
|
|
|
def train_one_step(model,X,Y):
|
|
|
|
params = get_parameters(model)
|
|
|
|
pcount = 0
|
|
|
|
for p in params:
|
|
|
|
pcount += np.prod(p.shape)
|
2023-02-06 07:55:41 -07:00
|
|
|
optimizer = optim.SGD(params, lr=0.001)
|
2021-06-19 12:24:15 -06:00
|
|
|
print("stepping %r with %.1fM params bs %d" % (type(model), pcount/1e6, BS))
|
|
|
|
st = time.time()
|
|
|
|
train(model, X, Y, optimizer, steps=1, BS=BS)
|
|
|
|
et = time.time()-st
|
|
|
|
print("done in %.2f ms" % (et*1000.))
|
|
|
|
|
|
|
|
class TestTrain(unittest.TestCase):
|
|
|
|
def test_efficientnet(self):
|
|
|
|
model = EfficientNet(0)
|
|
|
|
X = np.zeros((BS,3,224,224), dtype=np.float32)
|
|
|
|
Y = np.zeros((BS), dtype=np.int32)
|
|
|
|
train_one_step(model,X,Y)
|
|
|
|
|
2021-11-29 22:23:06 -07:00
|
|
|
def test_vit(self):
|
|
|
|
model = ViT()
|
|
|
|
X = np.zeros((BS,3,224,224), dtype=np.float32)
|
|
|
|
Y = np.zeros((BS,), dtype=np.int32)
|
|
|
|
train_one_step(model,X,Y)
|
|
|
|
|
2021-06-19 12:24:15 -06:00
|
|
|
def test_transformer(self):
|
|
|
|
# this should be small GPT-2, but the param count is wrong
|
2021-11-29 11:05:59 -07:00
|
|
|
# (real ff_dim is 768*4)
|
|
|
|
model = Transformer(syms=10, maxlen=6, layers=12, embed_dim=768, num_heads=12, ff_dim=768//4)
|
2021-06-19 12:24:15 -06:00
|
|
|
X = np.zeros((BS,6), dtype=np.float32)
|
|
|
|
Y = np.zeros((BS,6), dtype=np.int32)
|
|
|
|
train_one_step(model,X,Y)
|
|
|
|
|
2023-01-10 20:16:02 -07:00
|
|
|
if Device.DEFAULT == "GPU":
|
|
|
|
from extra.introspection import print_objects
|
|
|
|
assert print_objects() == 0
|
|
|
|
|
2021-06-19 12:24:15 -06:00
|
|
|
def test_resnet(self):
|
2021-06-21 10:37:24 -06:00
|
|
|
X = np.zeros((BS, 3, 224, 224), dtype=np.float32)
|
|
|
|
Y = np.zeros((BS), dtype=np.int32)
|
2021-10-30 17:48:39 -06:00
|
|
|
for resnet_v in [ResNet18]:
|
2021-11-30 14:14:54 -07:00
|
|
|
model = resnet_v()
|
|
|
|
model.load_from_pretrained()
|
2021-06-21 10:37:24 -06:00
|
|
|
train_one_step(model, X, Y)
|
2021-06-19 12:24:15 -06:00
|
|
|
|
|
|
|
def test_bert(self):
|
|
|
|
# TODO: write this
|
|
|
|
pass
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|