X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=speed.py;h=e03b3b74febe038e3e7382fb155909e197c71cc1;hb=6f7f454565cabfeda22c32103cc0ca41364de14a;hp=e4add261984da7a5275ceb3401d75fa06c8413a2;hpb=31ffed8a581c04094e8d2727c88de6e1d2c07a65;p=pytorch.git diff --git a/speed.py b/speed.py index e4add26..e03b3b7 100755 --- a/speed.py +++ b/speed.py @@ -1,5 +1,10 @@ #!/usr/bin/env python +# Any copyright is dedicated to the Public Domain. +# https://creativecommons.org/publicdomain/zero/1.0/ + +# Written by Francois Fleuret + import time, torch if torch.cuda.is_available(): @@ -12,21 +17,22 @@ else: nb_runs = 10000 d1, d2, d3 = 2048, 2048, 2048 -a, b = torch.rand(d1, d2).to(device), torch.rand(d2, d3).to(device) - -sync() -start_time = time.perf_counter() -for k in range(nb_runs): - c = a @ b -sync() -duration = time.perf_counter() - start_time +for t in [ torch.float32, torch.float16 ]: + a = torch.rand(d1, d2, device = device, dtype = t) + b = torch.rand(d2, d3, device = device, dtype = t) -nb_flop = float(nb_runs * d1 * d2 * d3 * 2) # 1 multiply-and-add is 2 ops -speed = nb_flop / duration + sync() + start_time = time.perf_counter() + for k in range(nb_runs): + c = a @ b + sync() + duration = time.perf_counter() - start_time -for u in [ '', 'K', 'M', 'G', 'T', 'P' ]: - if speed < 1e3: break - speed /= 1e3 + nb_flop = float(nb_runs * d1 * d2 * d3 * 2) # 1 multiply-and-add is 2 ops + speed = nb_flop / duration -print(f'{speed:.02f} {u}flops on {device}') + for u in [ '', 'K', 'M', 'G', 'T', 'P' ]: + if speed < 1e3: break + speed /= 1e3 + print(f'{speed:.02f} {u}flops with {t} on {device}')