File size: 14,931 Bytes
d91cab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# SPDX-FileCopyrightText: © 2026 Tenstorrent AI ULC
# SPDX-License-Identifier: Apache-2.0

"""Device-synchronised timing spans and the call-stack-shaped tree they record into.

``span`` is the one timing primitive: synchronise the device, open a node, run the body, synchronise
again, close the node with the elapsed milliseconds. It doubles as a decorator for a method whose
whole body is one span. Spans nest by one module-level stack, so attribution needs no
argument threaded down the call chain: a stage contains its blocks, a block its attention, an
attention its collectives, and the tree is that stack remembered. A layer and a model both record
into it by importing this module alone.

Three gates, read once at import so one process holds one setting:

* ``TT_DIT_STAGE_TIMING`` -- ``ENABLED``. Nothing here does anything without it; every span is
  a pass-through, so instrumented code costs nothing in a normal run.
* ``TT_DIT_BLOCK_PROF`` -- ``DEEP``. Spans opened with ``deep=True`` record only under this. They
  sit inside per-block regions, so they are numerous and each costs two device syncs; a deep run's
  totals are NOT comparable with a plain timing run.
* ``TT_DIT_STAGE_LOG`` -- ``LIVE``. One stdout line as each span opens and one as it closes, so a
  run reports its own progress and a device-side hang reads as the last ``>`` with no ``<``. Does
  not touch the tree.

Absolute totals are inflated by one ``synchronize_device`` per span open and close; the split
between siblings is what the tree is for. Not valid under trace capture: there the spans time the
capture, not the execution.
"""

from __future__ import annotations

import functools
import os
import sys
import time
from collections import OrderedDict, deque

import ttnn

#: One gate for the whole instrumentation, held here so no caller reads the environment itself:
#: import-time constants that can disagree would give a tree with partial data, and every "other"
#: remainder would then quietly lie about where time went.
ENABLED = os.environ.get("TT_DIT_STAGE_TIMING", "") not in ("", "0")

#: ``deep=True`` spans record only under this. They live inside per-block regions -- dozens more
#: device syncs per decode -- so the mode is opt-in and its totals are not comparable with ENABLED alone.
DEEP = ENABLED and os.environ.get("TT_DIT_BLOCK_PROF", "") not in ("", "0")

#: One stdout line as each span opens and one as it closes. A device-side hang then reads as the last
#: "> label" line with no matching "<", which names the culprit, and the lines keep a silence-reaping
#: job runner fed. Costs nothing beyond the syncs the spans already pay and does not touch the tree.
LIVE = ENABLED and os.environ.get("TT_DIT_STAGE_LOG", "") not in ("", "0")

ATTENTION, SDPA, ALLGATHER, MLP = "attention", "sdpa", "allgather", "mlp"
CONTEXT_INJECT, RESHAPE, UPSAMPLE = "context-inject", "reshape+permute", "upsample"
PROJ, NORM_ROPE = "projection", "norm+rope"
HOST_XFER, HOST_COMPUTE, SETUP = "host-transfer", "host-compute", "setup"

_MAX_DEPTH = 64
_WIDTH = 104
_LABEL_W = 56
_CAT_W = 16
#: Finished trees, newest last. One stack and one deque: every span is opened on the thread that runs
#: the decode, which is single-threaded, so there is nothing to keep per thread or under a lock.
_ROOTS: deque = deque(maxlen=8)
_STACK: list = []


class Node:
    __slots__ = ("label", "category", "incl_ms", "children", "parent", "flags")

    def __init__(self, label, category, parent):
        self.label, self.category, self.parent = label, category, parent
        self.incl_ms, self.children, self.flags = 0.0, [], set()

    @property
    def self_ms(self) -> float:
        """Time here that is not in a child. kv-allgather runs inside attention, so charging both in
        full would count those ms twice."""
        return max(self.incl_ms - sum(c.incl_ms for c in self.children), 0.0)

    def __repr__(self):  # debugging a live stack is the main use
        return f"<Node {self.label!r} {self.incl_ms:.1f}ms kids={len(self.children)}>"


def _live(mark: str, depth: int, label: str, tail: str = "") -> None:
    """One progress line: wall-clock stamp, depth as indent, ``>`` opening / ``<`` closing / ``!``
    aborted. Written straight to stdout and flushed, so it streams through ``pytest -s`` and a
    broker log alike instead of waiting on a buffer."""
    stamp = time.strftime("%H:%M:%S")
    sys.stdout.write(f"[stage {stamp}] {'  ' * depth}{mark} {label}{tail}\n")
    sys.stdout.flush()


def open_span(label, *, category=None, root=False):
    """Push a node. ``label`` is required: a node names itself from birth, so a span that never
    closes still says which one it was -- anonymous orphans are useless exactly when a leak needs
    finding. Returns ``None`` when disabled, and every close/abort accepts ``None``, so no call site
    needs a guard."""
    if not ENABLED:
        return None
    if root:
        del _STACK[:]
    if len(_STACK) >= _MAX_DEPTH:
        return None  # runaway guard: record nothing rather than grow the tree without bound
    parent = _STACK[-1] if _STACK else None
    node = Node(label, category, parent)
    if parent is not None:
        parent.children.append(node)
    if LIVE:
        _live(">", len(_STACK), label)
    _STACK.append(node)
    return node


def close_span(node, ms) -> None:
    if node is None or not ENABLED:
        return
    if not any(n is node for n in _STACK):
        return  # double close: drop it rather than corrupt a live parent
    while _STACK[-1] is not node:  # something opened below us never closed -- and it can name itself
        _STACK.pop().flags.add("unclosed")  # incl_ms stays 0, so it lands in the parent's remainder
    _STACK.pop()
    node.incl_ms = ms  # a node closes exactly once: assignment, not accumulation
    if LIVE:
        _live("<", len(_STACK), node.label, f"  {ms:.1f} ms")
    if node.parent is None:
        _finish(node)


def abort_span(node) -> None:
    """Exception path: keep the partial node but mark it, and never leave the stack deeper than it
    started."""
    if node is None or not ENABLED:
        return
    if not any(n is node for n in _STACK):
        return
    while _STACK[-1] is not node:
        _STACK.pop().flags.add("unclosed")
    _STACK.pop()
    node.flags.add("aborted")
    if LIVE:
        _live("!", len(_STACK), node.label, "  aborted")
    if node.parent is None:
        _finish(node)


def _finish(root: Node) -> None:
    for leftover in _STACK:  # a raise inside decode can leave these open
        leftover.flags.add("unclosed")
    del _STACK[:]
    _ROOTS.append(root)


# ---------------------------------------------------------------------------------- timing spans


class span:
    """Time the body as one node of the tree.

    Synchronises ``device`` before the clock starts and again before it stops, so the measurement is
    the body's device work and not whatever was still queued from before it. ``root=True`` starts a
    new tree; ``deep=True`` records only under ``DEEP``. Inert -- no sync, no node -- when its gate
    is off, so a span costs nothing in an untimed run.

    The label is fixed at open rather than on the way out because a node names itself from birth:
    a span that never closes is then still identifiable, which is when the name matters most. On an
    exception the node is kept, marked ``aborted`` and popped, so no later span nests under a dead
    parent; the exception propagates.

    Also a decorator, for a function whose whole body is one span. Then ``device`` is the name of
    the attribute of the first argument (``self``) holding the mesh device, or a callable of the
    call's ``(*args, **kwargs)`` returning it, and ``label`` may likewise be a callable of them::

        @span("mesh_device", lambda self, stage, *a, **k: f"stage {stage}", category=SETUP)
        def _setup(self, stage, x): ...
    """

    def __init__(self, device, label, *, category: str | None = None, root: bool = False, deep: bool = False):
        self.device, self.label, self.deep = device, label, deep
        self.opts = dict(category=category, root=root)
        self.active = ENABLED and (DEEP or not deep)

    def __enter__(self):
        if not self.active:
            return
        ttnn.synchronize_device(self.device)
        self.t0 = time.perf_counter()
        self.node = open_span(self.label, **self.opts)

    def __exit__(self, exc_type, exc, tb):
        if not self.active:
            return
        if exc_type is not None:
            abort_span(self.node)
            return
        ttnn.synchronize_device(self.device)
        close_span(self.node, (time.perf_counter() - self.t0) * 1000)

    def __call__(self, fn):
        @functools.wraps(fn)
        def wrapper(*args, **kwargs):
            device = self.device(*args, **kwargs) if callable(self.device) else getattr(args[0], self.device)
            label = self.label(*args, **kwargs) if callable(self.label) else self.label
            with span(device, label, deep=self.deep, **self.opts):
                return fn(*args, **kwargs)

        return wrapper


def root_count() -> int:
    return len(_ROOTS)


def roots() -> list:
    return list(_ROOTS)


def reset() -> None:
    """Drop recorded roots and any half-open stack. For tests; not used by the decode path."""
    del _STACK[:]
    _ROOTS.clear()


# --------------------------------------------------------------------------------------- reporting


def _pct(part: float, whole: float) -> float:
    return 100.0 * part / whole if whole else 0.0


def _pool(nodes):
    """Group siblings by exact label. A pure view -- the tree is never mutated, and nothing is merged
    for merely containing a number. ``attention`` fires once per band, so its row pools those spans;
    ``stage5 block 0..7`` keep distinct labels and stay distinct rows."""
    groups = OrderedDict()
    for n in nodes:
        groups.setdefault(n.label, []).append(n)
    return groups


def _rows(label, nodes, root_ms, parent_ms, prefix="", is_last=True, depth=0, max_depth=8, out=None):
    out = [] if out is None else out
    incl = sum(n.incl_ms for n in nodes)
    kids = [c for n in nodes for c in n.children]
    kids_ms = sum(c.incl_ms for c in kids)
    marks = " !" if kids_ms > incl * 1.01 else ""  # broken pairing: show it, never clamp it away
    if any("unclosed" in n.flags for n in nodes):
        marks += "  (never closed)"  # named, because the label was set at open
    connector = "" if depth == 0 else ("└─ " if is_last else "├─ ")
    # The category is what the roll-up charges this node's SELF time to; "-" means uncategorised, i.e.
    # it lands in the roll-up's "other" row. Pooled siblings share a call site, so the first node's
    # category speaks for all of them.
    cat = nodes[0].category or "-"
    out.append(
        (f"{prefix}{connector}{label}{marks}", incl, _pct(incl, parent_ms), _pct(incl, root_ms), len(nodes), cat)
    )
    if depth >= max_depth:
        return out
    self_ms = max(incl - kids_ms, 0.0)
    groups = [
        (lbl, ns)
        for lbl, ns in _pool(kids).items()
        if sum(n.incl_ms for n in ns) >= 0.5 or _pct(sum(n.incl_ms for n in ns), root_ms) >= 0.1
    ]
    child_prefix = prefix + ("" if depth == 0 else ("   " if is_last else "│  "))
    for i, (lbl, ns) in enumerate(groups):
        last = i == len(groups) - 1 and self_ms < 0.5
        _rows(lbl, ns, root_ms, incl, child_prefix, last, depth + 1, max_depth, out)
    if groups and self_ms >= 0.5:
        # The remainder is the parent's own self time, so it carries the parent's category -- this row
        # is exactly what that category's roll-up entry is made of.
        out.append(
            (f"{child_prefix}└─ · other (unattributed)", self_ms, _pct(self_ms, incl), _pct(self_ms, root_ms), 0, cat)
        )
    return out


def category_totals(root: Node):
    """Exclusive self-time per category, and the span count behind each. Sums to the root by
    construction: every ms belongs to exactly one node's self-time, so unlike a flat ledger this
    needs no ``untracked`` estimate to reconcile."""
    totals, spans = {}, {}

    def walk(n):
        key = n.category or "other (uncategorized)"
        totals[key] = totals.get(key, 0.0) + n.self_ms
        spans[key] = spans.get(key, 0) + 1
        for c in n.children:
            walk(c)

    walk(root)
    return OrderedDict(sorted(totals.items(), key=lambda kv: -kv[1])), spans


def render_tree(root: Node, *, title: str, measured_ms: float | None = None) -> str:
    max_depth = int(os.environ.get("TT_DIT_TREE_DEPTH", 8))
    flags = f"  [{' '.join(sorted(root.flags))}]" if root.flags else ""
    head = f"root {root.incl_ms:.1f} ms{flags}"
    if measured_ms is not None:
        head += f"  ·  test-measured {measured_ms:.0f} ms (Δ {root.incl_ms - measured_ms:+.1f})"
    out = [
        "=" * _WIDTH,
        f"DECODE TREE · {title}",
        head,
        "absolute totals inflated by one synchronize_device per span open/close",
        "-" * _WIDTH,
        f"{'label':<{_LABEL_W}}{'ms':>10}{'%par':>8}{'%tot':>8}{'n':>5}  {'category':<{_CAT_W}}",
    ]
    for lbl, ms, par, tot, n, cat in _rows(root.label, [root], root.incl_ms, root.incl_ms, max_depth=max_depth):
        out.append(f"{lbl:<{_LABEL_W}}{ms:>10.1f}{par:>7.1f}%{tot:>7.1f}%{(n or ''):>5}  {cat:<{_CAT_W}}".rstrip())
    return "\n".join(out)


def render_categories(totals, spans, total_ms: float, *, title: str = "CATEGORY ROLL-UP") -> str:
    out = [f"{title} (exclusive self time)", f"{'category':<44}{'ms':>10}{'%tot':>8}{'spans':>7}"]
    for cat, ms in totals.items():
        out.append(f"{cat:<44}{ms:>10.1f}{_pct(ms, total_ms):>7.1f}%{spans[cat]:>7}")
    tracked = sum(totals.values())
    out.append(f"{'TOTAL':<44}{tracked:>10.1f}{_pct(tracked, total_ms):>7.1f}%")
    # Self-times partition the root exactly, so anything but 100% means some parent measured shorter
    # than the children it contains and its self-time clamped at zero. Say which, rather than let a
    # reader wonder why the column does not add up.
    if abs(tracked - total_ms) > max(0.5, total_ms * 0.0005):
        out.append(
            f"{'':<44}{'':>10}  ({tracked - total_ms:+.1f} ms vs root: a parent measured shorter than its children)"
        )
    return "\n".join(out)


def render(root: Node, *, title: str, measured_ms: float | None = None) -> str:
    """One string, printed with a single print(). Not per-line logger.info: under pytest -s every
    loguru line carries a ~40-char prefix that would shift rows and destroy the column alignment."""
    totals, spans = category_totals(root)
    return "\n".join(
        [
            render_tree(root, title=title, measured_ms=measured_ms),
            "-" * _WIDTH,
            render_categories(totals, spans, root.incl_ms),
            "=" * _WIDTH,
        ]
    )