File size: 2,953 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 | #!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""Camera upload layouts: H2D time of the s2d uint8 input as 12-byte pages vs whole s2d rows, and the device
reshape / typecast to the stem's [N*H*W, 12] bf16 rows (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.tt.model import S2D_H, S2D_W
dev = open_device(0, dispatch="eth", l1_small_size=32768, trace_region_size=64 << 20)
try:
n = 8 * S2D_H * S2D_W
a = np.random.randint(0, 256, (n, 12), np.uint8)
ref = torch.from_numpy(a.astype(np.float32)).to(torch.bfloat16)
for name, shape in (("12 B pages", (1, 1, n, 12)), ("s2d rows", (1, 1, 8 * S2D_H, S2D_W * 12)),
("4 rows", (1, 1, 8 * S2D_H // 4, 4 * S2D_W * 12))):
h = ttnn.from_torch(torch.from_numpy(a.reshape(shape)), dtype=ttnn.uint8, layout=ttnn.ROW_MAJOR_LAYOUT)
d = ttnn.allocate_tensor_on_device(h.shape, ttnn.uint8, ttnn.ROW_MAJOR_LAYOUT, dev, ttnn.DRAM_MEMORY_CONFIG)
ts = []
for _ in range(5):
ttnn.synchronize_device(dev)
t = time.perf_counter()
ttnn.copy_host_to_device_tensor(h, d)
ttnn.synchronize_device(dev)
ts.append((time.perf_counter() - t) * 1e3)
print(f"upload {name:12s} {shape}: {np.median(ts):6.2f} ms", flush=True)
for variant in ("reshape_u8_then_cast", "cast_then_reshape"):
def fn():
if variant == "reshape_u8_then_cast":
return ttnn.typecast(ttnn.reshape(d, (1, 1, n, 12)), ttnn.bfloat16)
return ttnn.reshape(ttnn.typecast(d, ttnn.bfloat16), (1, 1, n, 12))
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(5):
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)
ok = torch.equal(ttnn.to_torch(r).reshape(n, 12), ref)
ttnn.release_trace(dev, tid)
print(f" {variant:22s} {np.median(ts):6.2f} ms exact={ok}", flush=True)
except Exception as e: # noqa: BLE001
print(f" {variant:22s} FAILED {type(e).__name__}: {str(e)[:160]}", flush=True)
ttnn.deallocate(d)
finally:
close_device(dev)
if __name__ == "__main__":
main()
|