Download code/scripts/micro/hgemv_prof.py from changh95/hamer-p150: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/changh95/hamer-p150/resolve/main/code/scripts/micro/hgemv_prof.py
- Command line
-
hf download hf://changh95/hamer-p150/code/scripts/micro/hgemv_prof.py
-
curl -L -o hgemv_prof.py https://huggingface.co/changh95/hamer-p150/resolve/main/code/scripts/micro/hgemv_prof.py
2.08 kB
| """device-profiler zones of hgemv (HG_PROF=1 TT_METAL_DEVICE_PROFILER=1): usage hgemv_prof.py [ln=1] [N=1024]""" | |
| import sys, os, csv | |
| from pathlib import Path | |
| from collections import defaultdict | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[2])) | |
| from common import * | |
| from hamer import tt_kernels as tk | |
| a = dict(s.split("=") for s in sys.argv[1:]) | |
| K, N, ln = 1024, int(a.get("N", 1024)), a.get("ln", "1") == "1" | |
| dev = open_dev() | |
| try: | |
| rowf = lambda v: v.reshape(1, 1, 1, -1).expand(1, 1, 32, v.numel()).contiguous() | |
| x = ttnn.from_torch(rowf(torch.randn(K)), dtype=ttnn.float32, layout=ttnn.TILE_LAYOUT, device=dev, memory_config=ttnn.L1_MEMORY_CONFIG) | |
| b = ttnn.from_torch(rowf(torch.randn(N)).to(torch.bfloat16), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev) | |
| w = tk.CmmWeight(torch.randn(K, N) * 0.03, dev, N // 32, 1, K // 32, ttnn.bfloat16) | |
| for _ in range(4): | |
| o = tk.hgemv(x, w, b, ln_eps=1e-5 if ln else None); ttnn.synchronize_device(dev); ttnn.deallocate(o) | |
| ttnn.ReadDeviceProfiler(dev) | |
| finally: | |
| from open_device_12x10 import close_device | |
| close_device(dev) | |
| rows = list(csv.reader(open(os.environ["TT_METAL_PROFILER_DIR"] + "/.logs/profile_log_device.csv")))[2:] | |
| ev = defaultdict(list) | |
| for r in rows: | |
| try: | |
| ev[(r[7].strip(), int(r[1]), int(r[2]), r[3].strip())].append((int(r[5]), r[10].strip(), r[11].strip())) | |
| except Exception: | |
| pass | |
| last = sorted({k[0] for k in ev}, key=lambda v: int(v) if v.isdigit() else 0)[-1] | |
| t0 = min(c for k, v in ev.items() if k[0] == last for c, _, _ in v) | |
| dur = defaultdict(list) | |
| for k, v in ev.items(): | |
| if k[0] != last: | |
| continue | |
| st = {} | |
| for c, n, t in sorted(v): | |
| if "START" in t: st[n] = c | |
| elif "END" in t and n in st: | |
| dur[(k[3], n)].append(((st[n] - t0) / 1.35e3, (c - st.pop(n)) / 1.35e3)) | |
| for k in sorted(dur): | |
| v = dur[k]; s = sorted(d for _, d in v); b0 = sorted(b for b, _ in v) | |
| print(f"{k[0]:8s} {k[1]:20s} n={len(v):3d} start med {b0[len(b0)//2]:6.2f} dur med {s[len(s)//2]:6.2f} max {s[-1]:6.2f} us") | |