File size: 3,410 Bytes
0f131e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #!/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()
|