File size: 5,098 Bytes
ca1a504 | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | # SPDX-License-Identifier: Apache-2.0
"""Micro-benchmark of the SAM global-attention pieces (eager, warm cache, median of 5, synchronized).
bin/devrun -t 600 -- python -u common/dsocr/tt_dsocr/tests/bench_vision_ops.py
"""
from __future__ import annotations
import os
import sys
import time
import torch
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(os.path.dirname(HERE)))
def main():
import ttnn
from tt_dsocr.ttaw.device import open_device, close_device
from tt_dsocr.tt import sdpa_chunks as SC
dev = open_device(0, dispatch="eth", trace_region_size=64 << 20)
cc = ttnn.init_device_compute_kernel_config(dev.arch(), math_fidelity=ttnn.MathFidelity.HiFi4,
math_approx_mode=False, fp32_dest_acc_en=True, packer_l1_acc=True)
up = lambda t, d=ttnn.bfloat16: ttnn.from_torch(t, dtype=d, layout=ttnn.TILE_LAYOUT, device=dev) # noqa: E731
g = dev.compute_with_storage_grid_size()
def tm(name, fn, n=5):
fn()
ttnn.synchronize_device(dev)
ts = []
for _ in range(n):
t0 = time.time()
r = fn()
ttnn.synchronize_device(dev)
ts.append(time.time() - t0)
ttnn.deallocate(r) if hasattr(r, "shape") else None
ts.sort()
print(f"{name:60s} {ts[len(ts) // 2] * 1e3:8.3f} ms", flush=True)
try:
for B12, gq, G in ((12, 64, 64), (72, 40, 64)):
q4 = up(torch.randn(B12, gq, G, 64))
rh = up(torch.randn(B12, gq, 64, G))
rw = up(torch.randn(B12, G, 64, G))
tm(f"transpose(1,2) [{B12},{gq},{G},64]", lambda: ttnn.transpose(q4, 1, 2))
tm(f"permute(0,2,1,3) [{B12},{gq},{G},64]", lambda: ttnn.permute(q4, (0, 2, 1, 3)))
tm(f"batched matmul rel_h [{B12},{gq},{G},64]x[..,64,{G}]",
lambda: ttnn.matmul(q4, rh, compute_kernel_config=cc, dtype=ttnn.bfloat16))
q4t = ttnn.transpose(q4, 1, 2)
tm(f"batched matmul rel_w [{B12},{G},{gq},64]x[..,64,{G}]",
lambda: ttnn.matmul(q4t, rw, compute_kernel_config=cc, dtype=ttnn.bfloat16))
B = B12 // 12
N = gq * G
q = up(torch.randn(B, 12, N, 64))
a = up(torch.randn(B, 12, N, G))
one = up(torch.zeros(B, 12, N, 32))
tm(f"concat q' [{B},12,{N},64+2x{G}+32]", lambda: ttnn.concat([q, a, a, one], dim=3))
qa = ttnn.concat([q, a, a, one], dim=3)
ka = ttnn.concat([q, a, a, one], dim=3)
# plain SDPA only through tt/sdpa_chunks.plain_sdpa (even key-chunk counts: the tt-metal odd-chunk issue)
tm(f"flash_mla_prefill [{B},12,{N},{qa.shape[-1]}] v64",
lambda: SC.plain_sdpa(ttnn, qa, ka, q, grid=g, site="bench", q_chunk=128, k_chunk=128, mla=True,
scale=1.0, compute_kernel_config=cc))
for qc, kc in ((256, 256), (64, 128), (128, 256)):
tm(f" flash_mla_prefill chunks {qc}/{kc}",
lambda: SC.plain_sdpa(ttnn, qa, ka, q, grid=g, site="bench", q_chunk=qc, k_chunk=kc, mla=True,
scale=1.0, compute_kernel_config=cc))
P = up(torch.zeros(N * B, N * B), ttnn.bfloat8_b)
x = up(torch.randn(N * B, 768), ttnn.float32)
tm(f"perm matmul [{N * B}x{N * B}] @ [{N * B},768] fp32", lambda: ttnn.matmul(
P, x, compute_kernel_config=cc, dtype=ttnn.float32))
for t in (q4, rh, rw, q4t, q, a, one, qa, ka, P, x):
ttnn.deallocate(t)
# window aug pieces at 1024 (Nw 4928)
Nw = 4928
q = up(torch.randn(1, 12, Nw, 64))
wa = up(torch.randn(1, 1, 64, 480))
ma = up(torch.randn(1, 1, Nw, 480))
sa = up(torch.randn(1, 1, 480, 128))
tm("window q@Waug [1,12,4928,64]x[64,480]", lambda: ttnn.matmul(q, wa, compute_kernel_config=cc,
dtype=ttnn.bfloat16))
pq = ttnn.matmul(q, wa, compute_kernel_config=cc, dtype=ttnn.bfloat16)
tm("window *Maug (bcast heads)", lambda: ttnn.multiply(pq, ma))
tm("window @Saug [1,12,4928,480]x[480,128]", lambda: ttnn.matmul(pq, sa, compute_kernel_config=cc,
dtype=ttnn.bfloat16))
x = up(torch.randn(1, 1, Nw, 768))
w = up(torch.randn(768, 2304))
grid = ttnn.CoreGrid(y=int(g.y), x=int(g.x))
tm("linear qkv [4928,768]x[768,2304] bf16 HiFi4", lambda: ttnn.linear(x, w, compute_kernel_config=cc,
core_grid=grid))
w2 = up(torch.randn(768, 3072))
tm("linear mlp1 [4928,768]x[768,3072] bf16 HiFi4", lambda: ttnn.linear(x, w2, compute_kernel_config=cc,
core_grid=grid))
finally:
close_device(dev)
if __name__ == "__main__":
main()
|