File size: 4,042 Bytes
7b01b36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
# SPDX-License-Identifier: Apache-2.0
"""Where T8's time goes (bin/devrun): the concat, each conv of the 3 BasicBlocks, the residual adds, the linear and the
LayerNorm on random BEVs, each synchronized (eager).

    bin/devrun python -m tt_autoe2e.tests.dev_t8_prof
"""
from __future__ import annotations

import json
import time

import numpy as np

CKPT = "/ae2e/881e6a8367746da1eaae044a68077d0e7ce6fc1b/models/kitscenes-epoch-5/checkpoint.pt"


def main() -> int:
    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 FeatureMap, residual_add

    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)
        bevs = [ttnn.from_torch(torch.from_numpy(rng.standard_normal((1, 1, Q, 256)).astype(np.float32)),
                                dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev) for _ in range(8)]
        sync = lambda: ttnn.synchronize_device(dev)
        for rep in range(2):
            tm = {}
            t = time.perf_counter()
            x = FeatureMap(ttnn.concat(list(bevs), dim=3, memory_config=ttnn.DRAM_MEMORY_CONFIG), 1, BEV_H, BEV_W, 2048)
            sync()
            tm["concat"] = time.perf_counter() - t
            for i, (c1, c2, ds) in enumerate(t8.blocks):
                t = time.perf_counter()
                h = c1(x)
                sync()
                tm[f"b{i}.conv1"] = time.perf_counter() - t
                t = time.perf_counter()
                o = c2(h)
                sync()
                tm[f"b{i}.conv2"] = time.perf_counter() - t
                h.deallocate()
                t = time.perf_counter()
                idt = ds(x) if ds is not None else x
                sync()
                tm[f"b{i}.downsample"] = time.perf_counter() - t
                t = time.perf_counter()
                y = residual_add(o, idt, "relu")
                sync()
                tm[f"b{i}.add"] = time.perf_counter() - t
                o.deallocate()
                if ds is not None:
                    idt.deallocate()
                x.deallocate()
                x = y
            if t8.fused0 is not None:
                x0 = FeatureMap(ttnn.concat(list(bevs), dim=3, memory_config=ttnn.DRAM_MEMORY_CONFIG), 1, BEV_H, BEV_W,
                                2048)
                sync()
                t = time.perf_counter()
                f = t8.fused0(x0)
                sync()
                tm["b0.fused_conv1_downsample"] = time.perf_counter() - t
                t = time.perf_counter()
                rows = int(f.tensor.shape[-2])
                h = ttnn.relu(ttnn.slice(f.tensor, [0, 0, 0, 0], [1, 1, rows, 512]))
                i2 = ttnn.slice(f.tensor, [0, 0, 0, 512], [1, 1, rows, 1024])
                sync()
                tm["b0.fused_split"] = time.perf_counter() - t
                tm["fused_shape"] = 0.0
                res_shape = tuple(int(d) for d in f.tensor.shape)
                for z_ in (h, i2):
                    ttnn.deallocate(z_)
                f.deallocate()
                x0.deallocate()
            t = time.perf_counter()
            tt = ttnn.reshape(x.tensor, (1, 1, Q, 512)) if tuple(x.tensor.shape) != (1, 1, Q, 512) else x.tensor
            z = t8.linear(tt)
            sync()
            tm["linear"] = time.perf_counter() - t
            x.deallocate()
            ttnn.deallocate(z)
            res = {k: round(v * 1e3, 2) for k, v in tm.items()}
        res.pop("fused_shape", None)
        res["total_unfused_ms"] = round(sum(v for k, v in res.items() if "fused" not in k), 1)
    finally:
        close_device(dev)
    print(json.dumps(res, indent=1))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())