Download code/scripts/dev_bench_upload.py from changh95/METEOR-2-p150: direct link, hf CLI and curl.
- Browser
- Download file 2.95 kB
-
https://huggingface.co/changh95/METEOR-2-p150/resolve/main/code/scripts/dev_bench_upload.py
- Command line
-
hf download hf://changh95/METEOR-2-p150/code/scripts/dev_bench_upload.py
-
curl -L -o dev_bench_upload.py https://huggingface.co/changh95/METEOR-2-p150/resolve/main/code/scripts/dev_bench_upload.py
2.95 kB
| #!/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() | |