mirror of https://github.com/commaai/tinygrad.git
tqdm touchup (#6113)
more precise names and don't repeat set_description
This commit is contained in:
parent
5f1554b574
commit
379d080e74
|
@ -310,8 +310,9 @@ def flat_mv(mv:memoryview): return mv if len(mv) == 0 else mv.cast("B", shape=(m
|
||||||
|
|
||||||
class tqdm:
|
class tqdm:
|
||||||
def __init__(self, iterable=None, desc:str='', disable:bool=False, unit:str='it', unit_scale=False, total:Optional[int]=None, rate:int=100):
|
def __init__(self, iterable=None, desc:str='', disable:bool=False, unit:str='it', unit_scale=False, total:Optional[int]=None, rate:int=100):
|
||||||
self.iter, self.desc, self.dis, self.unit, self.unit_scale, self.rate = iterable, f"{desc}: " if desc else "", disable, unit, unit_scale, rate
|
self.iter, self.disable, self.unit, self.unit_scale, self.rate = iterable, disable, unit, unit_scale, rate
|
||||||
self.st, self.i, self.n, self.skip, self.t = time.perf_counter(), -1, 0, 1, getattr(iterable, "__len__", lambda:0)() if total is None else total
|
self.st, self.i, self.n, self.skip, self.t = time.perf_counter(), -1, 0, 1, getattr(iterable, "__len__", lambda:0)() if total is None else total
|
||||||
|
self.set_description(desc)
|
||||||
self.update(0)
|
self.update(0)
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for item in self.iter:
|
for item in self.iter:
|
||||||
|
@ -321,18 +322,18 @@ class tqdm:
|
||||||
def set_description(self, desc:str): self.desc = f"{desc}: " if desc else ""
|
def set_description(self, desc:str): self.desc = f"{desc}: " if desc else ""
|
||||||
def update(self, n:int=0, close:bool=False):
|
def update(self, n:int=0, close:bool=False):
|
||||||
self.n, self.i = self.n+n, self.i+1
|
self.n, self.i = self.n+n, self.i+1
|
||||||
if self.dis or (not close and self.i % self.skip != 0): return
|
if self.disable or (not close and self.i % self.skip != 0): return
|
||||||
prog, dur, ncols = self.n/self.t if self.t else 0, time.perf_counter()-self.st, shutil.get_terminal_size().columns
|
prog, elapsed, ncols = self.n/self.t if self.t else 0, time.perf_counter()-self.st, shutil.get_terminal_size().columns
|
||||||
if self.i/dur > self.rate and self.i: self.skip = max(int(self.i/dur)//self.rate,1)
|
if self.i/elapsed > self.rate and self.i: self.skip = max(int(self.i/elapsed)//self.rate,1)
|
||||||
def fmt(t): return ':'.join(f'{x:02d}' if i else str(x) for i,x in enumerate([int(t)//3600,int(t)%3600//60,int(t)%60]) if i or x)
|
def HMS(t): return ':'.join(f'{x:02d}' if i else str(x) for i,x in enumerate([int(t)//3600,int(t)%3600//60,int(t)%60]) if i or x)
|
||||||
def fn(x): return (f"{x/1000**int(g:=math.log(x,1000)):.{int(3-3*math.fmod(g,1))}f}"[:4].rstrip('.')+' kMGTPEZY'[int(g)].strip()) if x else '0.00'
|
def SI(x): return (f"{x/1000**int(g:=math.log(x,1000)):.{int(3-3*math.fmod(g,1))}f}"[:4].rstrip('.')+' kMGTPEZY'[int(g)].strip()) if x else '0.00'
|
||||||
unit_text = f'{fn(self.n)}{f"/{fn(self.t)}" if self.t else self.unit}' if self.unit_scale else f'{self.n}{f"/{self.t}" if self.t else self.unit}'
|
unit_text = f'{SI(self.n)}{f"/{SI(self.t)}" if self.t else self.unit}' if self.unit_scale else f'{self.n}{f"/{self.t}" if self.t else self.unit}'
|
||||||
it_text = (fn(self.n/dur) if self.unit_scale else f"{self.n/dur:5.2f}") if self.n else "?"
|
tm_text = f'{HMS(elapsed)}<{HMS(elapsed/prog-elapsed) if self.n else "?"}' if self.t else HMS(elapsed)
|
||||||
tm = f'{fmt(dur)}<{fmt(dur/prog-dur) if self.n else "?"}' if self.t else fmt(dur)
|
it_text = (SI(self.n/elapsed) if self.unit_scale else f"{self.n/elapsed:5.2f}") if self.n else "?"
|
||||||
suf = f'{unit_text} [{tm}, {it_text}{self.unit}/s]'
|
suf = f'{unit_text} [{tm_text}, {it_text}{self.unit}/s]'
|
||||||
sz = max(ncols-len(self.desc)-5-2-len(suf), 1)
|
sz = max(ncols-len(self.desc)-3-2-2-len(suf), 1)
|
||||||
bar = '\r' + self.desc + (f'{100*prog:3.0f}%|{("█"*int(num:=sz*prog)+" ▏▎▍▌▋▊▉"[int(8*num)%8].strip()).ljust(sz," ")}| ' if self.t else '') + suf
|
bar = '\r' + self.desc + (f'{100*prog:3.0f}%|{("█"*int(num:=sz*prog)+" ▏▎▍▌▋▊▉"[int(8*num)%8].strip()).ljust(sz," ")}| ' if self.t else '') + suf
|
||||||
print(bar[:ncols+1],flush=True,end='\n'*close,file=sys.stderr)
|
print(bar[:ncols+1], flush=True, end='\n'*close, file=sys.stderr)
|
||||||
|
|
||||||
class trange(tqdm):
|
class trange(tqdm):
|
||||||
def __init__(self, n:int, **kwargs): super().__init__(iterable=range(n), total=n, **kwargs)
|
def __init__(self, n:int, **kwargs): super().__init__(iterable=range(n), total=n, **kwargs)
|
||||||
|
|
Loading…
Reference in New Issue