File size: 4,526 Bytes
8eeb550 | 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 | #!/usr/bin/env python3
"""R7 probe (OPT_PLAN round-7 rank 2, the D2H of the packed readback): device-to-host copy time of a 16 MB fp32
ROW_MAJOR DRAM tensor by page size (the PACK segments use 4 KB pages: rows of 1,024 fp32), and of a TILE one.
``copy_device_to_host_tensor`` into a preallocated host tensor (the runner's read), p50 of ``--reps``.
METEOR_DEVICE_OK=1 bin/devrun -t 600 -- python -u code/scripts/d2h_probe.py
"""
from __future__ import annotations
import argparse
import sys
import time
import numpy as np
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--mb", type=int, default=16)
ap.add_argument("--rows", default="256,1024,4096,16384,65536", help="fp32 elements per page")
ap.add_argument("--reps", type=int, default=15)
ap.add_argument("--fresh", action="store_true", help="also read into a fresh host tensor viewed zero-copy")
ap.add_argument("--borrow", action="store_true", help="also read into a host tensor borrowing a torch buffer")
a = ap.parse_args()
import torch
import ttnn
from tt_meteor.ttaw.device import open_device
from tt_meteor.ttaw.tensors import to_numpy
dev = open_device(dispatch="eth")
n = a.mb * (1 << 20) // 4
x = np.arange(n, dtype=np.float32)
for spec in [("rm", int(r)) for r in a.rows.split(",")] + [("tile", 1024)]:
kind, r = spec
rows = n // r
t = ttnn.from_torch(torch.from_numpy(x.reshape(1, 1, rows, r)), dtype=ttnn.float32,
layout=ttnn.ROW_MAJOR_LAYOUT if kind == "rm" else ttnn.TILE_LAYOUT, device=dev,
memory_config=ttnn.DRAM_MEMORY_CONFIG)
host = ttnn.allocate_tensor_on_host(t.spec, dev)
ts, tn = [], []
for _ in range(a.reps):
ttnn.synchronize_device(dev)
t0 = time.perf_counter()
ttnn.copy_device_to_host_tensor(t, host, blocking=True, cq_id=0)
t1 = time.perf_counter()
arr = to_numpy(host)
t2 = time.perf_counter()
ts.append((t1 - t0) * 1e3)
tn.append((t2 - t1) * 1e3)
ok = kind != "rm" or np.array_equal(np.asarray(arr).reshape(-1), x)
p50 = float(np.median(ts))
print(f"{kind:4s} page {r * 4 // 1024:4d} KB: copy p50 {p50:7.2f} ms ({a.mb / p50:5.2f} GB/s) min "
f"{min(ts):7.2f} to_numpy p50 {float(np.median(tn)):6.2f} ms {'ok' if ok else 'MISMATCH'}",
flush=True)
if a.fresh:
# a fresh host tensor per read, viewed zero-copy (dlpack of its host buffer): the arrays stay fresh per
# frame like to_numpy's, without the second host copy
tf = []
for _ in range(a.reps):
ttnn.synchronize_device(dev)
t0 = time.perf_counter()
h2 = ttnn.allocate_tensor_on_host(t.spec, dev)
ttnn.copy_device_to_host_tensor(t, h2, blocking=True, cq_id=0)
v = np.from_dlpack(h2.host_buffer().get_shard(ttnn.MeshCoordinate(0, 0)).__array__())
v = v.view(np.float32)
tf.append((time.perf_counter() - t0) * 1e3)
same = kind != "rm" or np.array_equal(v.reshape(-1), x)
print(f" fresh + view: p50 {float(np.median(tf)):7.2f} ms (copy + to_numpy p50 "
f"{float(np.median(np.add(ts, tn))):7.2f}) {'ok' if same else 'MISMATCH'}", flush=True)
if kind == "rm" and a.borrow:
# the host tensor borrows a torch buffer (from_torch without a device: zero-copy for ROW_MAJOR fp32):
# the copy lands in numpy-visible memory, no to_torch copy afterwards
buf = torch.zeros((1, 1, rows, r), dtype=torch.float32)
bh = ttnn.from_torch(buf, dtype=ttnn.float32, layout=ttnn.ROW_MAJOR_LAYOUT)
view = buf.numpy()
tb = []
for _ in range(a.reps):
ttnn.synchronize_device(dev)
t0 = time.perf_counter()
ttnn.copy_device_to_host_tensor(t, bh, blocking=True, cq_id=0)
tb.append((time.perf_counter() - t0) * 1e3)
same = np.array_equal(view.reshape(-1), x)
print(f" borrowed: copy p50 {float(np.median(tb)):7.2f} ms numpy view {'ok' if same else 'MISMATCH'}"
f" (spec equal {bh.spec == t.spec if hasattr(bh, 'spec') else '?'})", flush=True)
ttnn.deallocate(t)
ttnn.close_device(dev)
return 0
if __name__ == "__main__":
sys.exit(main())
|