Download code/tt_autoe2e/tests/dev_t8_sweep.py from changh95/auto_e2e-p150: direct link, hf CLI and curl.
- Browser
- Download file 5.47 kB
-
https://huggingface.co/changh95/auto_e2e-p150/resolve/main/code/tt_autoe2e/tests/dev_t8_sweep.py
- Command line
-
hf download hf://changh95/auto_e2e-p150/code/tt_autoe2e/tests/dev_t8_sweep.py
-
curl -L -o dev_t8_sweep.py https://huggingface.co/changh95/auto_e2e-p150/resolve/main/code/tt_autoe2e/tests/dev_t8_sweep.py
5.47 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """T8's convs under different ttnn conv configs (bin/devrun): block 1's 3 x 3 512 -> 512 conv and block 0's fused | |
| 2048 -> 1024 conv on random 300 x 200 BEV maps, timed (median, eager, synchronized) and compared with the default | |
| config's output (max abs, identical). | |
| bin/devrun python -m tt_autoe2e.tests.dev_t8_sweep [--out /logs/x.json] | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import time | |
| import numpy as np | |
| CKPT = "/ae2e/881e6a8367746da1eaae044a68077d0e7ce6fc1b/models/kitscenes-epoch-5/checkpoint.pt" | |
| def main(argv=None) -> int: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--out", default="/logs/t8_sweep.json") | |
| ap.add_argument("--reps", type=int, default=3) | |
| args = ap.parse_args(argv) | |
| import torch | |
| import ttnn | |
| from ..reference.port_form import BEV_H, BEV_W, Q | |
| from ..reference.tree import load_tree | |
| from ..tt.image import state_dict_of | |
| from ..tt.t8 import TtT8 | |
| from ..ttaw.device import DeviceConfig, close_device | |
| from ..ttaw.ops.conv import Conv2d, FeatureMap | |
| tree = load_tree(CKPT) | |
| state = state_dict_of(tree) | |
| dev = DeviceConfig(trace_region_size=32 << 20).open() | |
| res = {} | |
| try: | |
| t8 = TtT8(state) | |
| rng = np.random.default_rng(0) | |
| up = lambda c: ttnn.from_torch(torch.from_numpy(rng.standard_normal((1, 1, Q, c)).astype(np.float32)), | |
| dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev) | |
| x512 = FeatureMap(up(512), 1, BEV_H, BEV_W, 512) | |
| x2048 = FeatureMap(up(2048), 1, BEV_H, BEV_W, 2048) | |
| c1 = t8.blocks[1][0] | |
| f0 = t8.fused0 | |
| HS, BS, WS = (ttnn.TensorMemoryLayout.HEIGHT_SHARDED, ttnn.TensorMemoryLayout.BLOCK_SHARDED, | |
| ttnn.TensorMemoryLayout.WIDTH_SHARDED) | |
| def variant(base, slicing="auto", **cc): | |
| return Conv2d(base.weight, base.bias, padding=1, activation=base.activation, precision=base.precision, | |
| slicing=slicing, conv_config=cc, name=base.name + "_v") | |
| cases = { | |
| "c512": (c1, x512, [ | |
| ("default", {}), | |
| ("dram_bs", dict(shard_layout=BS)), | |
| ("dram_hs", dict(shard_layout=HS)), | |
| ("dram_bs_dbuf", dict(shard_layout=BS, enable_act_double_buffer=True, | |
| enable_weights_double_buffer=True)), | |
| ("dram_hs_dbuf", dict(shard_layout=HS, enable_act_double_buffer=True, | |
| enable_weights_double_buffer=True)), | |
| ("dram_bs_abh64", dict(shard_layout=BS, act_block_h_override=64)), | |
| ("dram_bs_abh128", dict(shard_layout=BS, act_block_h_override=128)), | |
| ("dram_hs_abh64", dict(shard_layout=HS, act_block_h_override=64)), | |
| ("dram_h8", dict(slicing=("height", 8))), | |
| ("dram_h4_bs", dict(slicing=("height", 4), shard_layout=BS)), | |
| ("l1_bs", dict(slicing="l1", shard_layout=BS)), | |
| ("l1_hs", dict(slicing="l1", shard_layout=HS)), | |
| ("l1_bs_dbuf", dict(slicing="l1", shard_layout=BS, enable_act_double_buffer=True, | |
| enable_weights_double_buffer=True)), | |
| ]), | |
| "f2048": (f0, x2048, [ | |
| ("default", {}), | |
| ("dram_bs", dict(shard_layout=BS)), | |
| ("dram_bs_dbuf", dict(shard_layout=BS, enable_act_double_buffer=True, | |
| enable_weights_double_buffer=True)), | |
| ("dram_hs", dict(shard_layout=HS)), | |
| ("dram_ws", dict(shard_layout=WS)), | |
| ("dram_bs_abh64", dict(shard_layout=BS, act_block_h_override=64)), | |
| ("dram_h8_bs", dict(slicing=("height", 8), shard_layout=BS)), | |
| ("dram_w4_bs", dict(slicing=("width", 4), shard_layout=BS)), | |
| ]), | |
| } | |
| for cname, (base, x, vs) in cases.items(): | |
| ref = None | |
| for vname, kw in vs: | |
| kw = dict(kw) | |
| slicing = kw.pop("slicing", "auto") | |
| conv = base if vname == "default" else variant(base, slicing, **kw) | |
| key = f"{cname}.{vname}" | |
| try: | |
| o = conv(x) | |
| ttnn.synchronize_device(dev) | |
| a = ttnn.to_torch(o.tensor).float().numpy().reshape(-1) | |
| o.deallocate() | |
| ts = [] | |
| for _ in range(args.reps): | |
| t = time.perf_counter() | |
| o = conv(x) | |
| ttnn.synchronize_device(dev) | |
| ts.append(time.perf_counter() - t) | |
| o.deallocate() | |
| ms = round(float(np.median(ts)) * 1e3, 2) | |
| if ref is None: | |
| ref = a | |
| res[key] = {"ms": ms, "identical": bool(np.array_equal(a, ref)), | |
| "max_abs": float(np.abs(a - ref).max())} | |
| except Exception as e: # noqa: BLE001 - invalid configs are part of the sweep | |
| res[key] = {"error": str(e).splitlines()[0][:160]} | |
| print(key, res[key], flush=True) | |
| finally: | |
| close_device(dev) | |
| with open(args.out, "w") as f: | |
| json.dump(res, f, indent=1) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |