MCPcopy
hub / github.com/tinygrad/tinygrad / colored

Function colored

tinygrad/helpers.py:33–36  ·  view source on GitHub ↗
(st, color:str|None, background=False)

Source from the content-addressed store, hash-verified

31def all_same(items:Sequence): return all(x == items[0] for x in items) # works for empty input
32def all_int(t: Sequence[Any]) -> TypeGuard[tuple[int, ...]]: return all(isinstance(s, int) for s in t)
33def colored(st, color:str|None, background=False): # replace the termcolor library
34 if NO_COLOR: return st
35 colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
36 return f"\u001b[{10*background+60*(color.upper() == color)+30+colors.index(color.lower())}m{st}\u001b[0m" if color is not None else st
37def colorize_float(x: float): return colored(f"{x:7.2f}x", 'green' if x < 0.75 else 'red' if x > 1.15 else 'yellow')
38def time_to_str(t:float, w=8) -> str: return next((f"{t * d:{w}.2f}{pr}" for d,pr in [(1, "s "),(1e3, "ms")] if t > 10/d), f"{t * 1e6:{w}.2f}us")
39def size_to_str(s:int) -> str: return next((f"{s / d:.2f} {pr}" for d,pr in [(1<<30, "GB"),(1<<20, "MB"),(1<<10, "KB")] if s >= d), f"{s} B")

Callers 15

enumerate_devices_strFunction · 0.90
run_modelMethod · 0.90
format_packetFunction · 0.90
get_call_nameFunction · 0.90
track_statsFunction · 0.90
range_strFunction · 0.90
__call__Method · 0.90
pretty_ptxFunction · 0.90
get_optimized_astMethod · 0.90
colored_shapeMethod · 0.90
beam_searchFunction · 0.90
render_rangesFunction · 0.90

Calls 1

indexMethod · 0.45

Tested by 4

test_colored_labelMethod · 0.72
test_wrap_coloredMethod · 0.72
fmtFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…