Download code/scripts/dev_bench_polar.py from changh95/METEOR-2-p150: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/changh95/METEOR-2-p150/resolve/main/code/scripts/dev_bench_polar.py
- Command line
-
hf download hf://changh95/METEOR-2-p150/code/scripts/dev_bench_polar.py
-
curl -L -o dev_bench_polar.py https://huggingface.co/changh95/METEOR-2-p150/resolve/main/code/scripts/dev_bench_polar.py
3.41 kB
| #!/usr/bin/env python3 | |
| # SPDX-License-Identifier: Apache-2.0 | |
| """Polar batched-matmul program configs on random data of the real shape (bin/devrun).""" | |
| import sys, time | |
| import numpy as np | |
| def main(): | |
| import torch | |
| import ttnn | |
| sys.path.insert(0, "/work") | |
| from tt_meteor2.ttaw.device import close_device, open_device | |
| from tt_meteor2.ttaw.precision import compute_kernel_config | |
| dev = open_device(0, dispatch="eth", l1_small_size=32768, trace_region_size=64 << 20) | |
| try: | |
| B, K, M = 2304, 162, 64 | |
| a = torch.rand(B, M, K) / 8 | |
| b = torch.rand(B, K, M) | |
| ref = torch.bmm(a.double(), b.double()) | |
| A = ttnn.from_torch(a, dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev) | |
| Bt = ttnn.from_torch(b, dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev) | |
| ckc = compute_kernel_config("HiFi4", fp32_acc=True, packer_l1_acc=True) | |
| ckc2 = compute_kernel_config("HiFi2", fp32_acc=True, packer_l1_acc=False) | |
| def bench(name, fn, n=5): | |
| try: | |
| r = fn() | |
| ttnn.synchronize_device(dev) | |
| tid = ttnn.begin_trace_capture(dev, cq_id=0) | |
| r = fn() | |
| ttnn.end_trace_capture(dev, tid, cq_id=0) | |
| ts = [] | |
| for _ in range(n): | |
| t = time.perf_counter() | |
| ttnn.execute_trace(dev, tid, cq_id=0, blocking=False) | |
| ttnn.synchronize_device(dev) | |
| ts.append((time.perf_counter() - t) * 1e3) | |
| ttnn.release_trace(dev, tid) | |
| got = ttnn.to_torch(r).double() | |
| err = float((got - ref).abs().max()) | |
| print(f"{name:56s} {np.median(ts):7.2f} ms max_err {err:.3e}", flush=True) | |
| except Exception as e: # noqa: BLE001 | |
| print(f"{name:56s} FAILED {type(e).__name__}: {str(e)[:160]}", flush=True) | |
| bench("default", lambda: ttnn.matmul(A, Bt, dtype=ttnn.bfloat16, compute_kernel_config=ckc)) | |
| bench("core_grid 12x10", lambda: ttnn.matmul(A, Bt, dtype=ttnn.bfloat16, compute_kernel_config=ckc, | |
| core_grid=ttnn.CoreGrid(y=10, x=12))) | |
| for pm in (48, 40, 24): | |
| cfg = ttnn.MatmulMultiCoreReuseProgramConfig(compute_with_storage_grid_size=(12, 10), in0_block_w=6, | |
| out_subblock_h=2, out_subblock_w=2, per_core_M=pm, | |
| per_core_N=2) | |
| bench(f"reuse per_core_M={pm}", lambda: ttnn.matmul(A, Bt, dtype=ttnn.bfloat16, compute_kernel_config=ckc, | |
| program_config=cfg)) | |
| bench(f"reuse per_core_M={pm} HiFi2", lambda: ttnn.matmul(A, Bt, dtype=ttnn.bfloat16, | |
| compute_kernel_config=ckc2, program_config=cfg)) | |
| A1 = ttnn.to_memory_config(A, ttnn.L1_MEMORY_CONFIG) if False else A | |
| # one matmul per camera-sized batch chunk (8 x 288) | |
| A4 = ttnn.reshape(A, (8, 288, M, K)) | |
| B4 = ttnn.reshape(Bt, (8, 288, K, M)) | |
| bench("4-D default", lambda: ttnn.matmul(A4, B4, dtype=ttnn.bfloat16, compute_kernel_config=ckc)) | |
| finally: | |
| close_device(dev) | |
| if __name__ == "__main__": | |
| main() | |