2022-06-25 13:33:53 +08:00
#!/usr/bin/env python3
2023-02-11 02:09:37 +08:00
import gc
2022-06-25 13:33:53 +08:00
import time
from tqdm import trange
from models . efficientnet import EfficientNet
2023-08-22 22:36:24 +08:00
from tinygrad . nn . state import get_parameters
2023-02-11 02:09:37 +08:00
from tinygrad . nn import optim
2022-06-25 13:33:53 +08:00
from tinygrad . tensor import Tensor
2023-01-30 09:15:00 +08:00
from tinygrad . ops import GlobalCounters
2023-02-01 07:09:09 +08:00
from tinygrad . helpers import getenv
2023-08-29 10:59:55 +08:00
from tinygrad . jit import CacheCollector
2022-06-25 13:33:53 +08:00
2022-07-05 03:19:00 +08:00
def tensors_allocated ( ) :
2023-02-11 02:09:37 +08:00
return sum ( isinstance ( x , Tensor ) for x in gc . get_objects ( ) )
2022-06-25 13:33:53 +08:00
2023-02-01 07:09:09 +08:00
NUM = getenv ( " NUM " , 2 )
BS = getenv ( " BS " , 8 )
CNT = getenv ( " CNT " , 10 )
BACKWARD = getenv ( " BACKWARD " , 0 )
TRAINING = getenv ( " TRAINING " , 1 )
ADAM = getenv ( " ADAM " , 0 )
CLCACHE = getenv ( " CLCACHE " , 0 )
2022-06-25 13:33:53 +08:00
if __name__ == " __main__ " :
print ( f " NUM: { NUM } BS: { BS } CNT: { CNT } " )
2022-07-03 05:38:45 +08:00
model = EfficientNet ( NUM , classes = 1000 , has_se = False , track_running_stats = False )
2023-07-03 06:07:30 +08:00
parameters = get_parameters ( model )
2022-07-16 22:47:43 +08:00
for p in parameters : p . realize ( )
2022-07-20 00:33:07 +08:00
if ADAM : optimizer = optim . Adam ( parameters , lr = 0.001 )
else : optimizer = optim . SGD ( parameters , lr = 0.001 )
2022-06-25 13:33:53 +08:00
2022-07-05 04:28:03 +08:00
Tensor . training = TRAINING
2022-07-11 11:54:57 +08:00
Tensor . no_grad = not BACKWARD
2022-06-25 13:33:53 +08:00
for i in trange ( CNT ) :
2023-02-09 06:46:09 +08:00
GlobalCounters . reset ( )
2022-07-05 04:09:15 +08:00
cpy = time . monotonic ( )
2022-07-05 00:50:56 +08:00
x_train = Tensor . randn ( BS , 3 , 224 , 224 , requires_grad = False ) . realize ( )
y_train = Tensor . randn ( BS , 1000 , requires_grad = False ) . realize ( )
2022-06-25 13:33:53 +08:00
2023-02-12 04:00:14 +08:00
# TODO: replace with TinyJit
2023-01-30 09:15:00 +08:00
if i < 3 or not CLCACHE :
st = time . monotonic ( )
out = model . forward ( x_train )
2023-02-25 02:11:24 +08:00
loss = out . log_softmax ( ) . mul ( y_train ) . mean ( )
2023-08-29 10:59:55 +08:00
if i == 2 and CLCACHE : CacheCollector . start ( )
2023-01-30 09:15:00 +08:00
if BACKWARD :
optimizer . zero_grad ( )
loss . backward ( )
optimizer . step ( )
mt = time . monotonic ( )
loss . realize ( )
for p in parameters :
p . realize ( )
et = time . monotonic ( )
else :
st = mt = time . monotonic ( )
2023-02-09 06:46:09 +08:00
for prg , args in cl_cache : prg ( * args )
2023-01-30 09:15:00 +08:00
et = time . monotonic ( )
if i == 2 and CLCACHE :
2023-08-29 10:59:55 +08:00
cl_cache = CacheCollector . finish ( )
2023-01-30 09:15:00 +08:00
2023-02-11 13:13:29 +08:00
mem_used = GlobalCounters . mem_used
2023-08-14 05:33:05 +08:00
loss_cpu = loss . detach ( ) . numpy ( )
2022-07-05 00:50:56 +08:00
cl = time . monotonic ( )
2023-02-09 06:46:09 +08:00
print ( f " { ( st - cpy ) * 1000.0 : 7.2f } ms cpy, { ( cl - st ) * 1000.0 : 7.2f } ms run, { ( mt - st ) * 1000.0 : 7.2f } ms build, { ( et - mt ) * 1000.0 : 7.2f } ms realize, { ( cl - et ) * 1000.0 : 7.2f } ms CL, { loss_cpu : 7.2f } loss, { tensors_allocated ( ) : 4d } tensors, { mem_used / 1e9 : .2f } GB used, { GlobalCounters . global_ops * 1e-9 / ( cl - st ) : 9.2f } GFLOPS " )