File size: 2,877 Bytes
b7e49b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""Device side of the DepthPro2 port.

Stage engines (``ttnn_enc`` / ``ttnn_dec`` / ``ttnn_fov`` and the megakernel ``mk_*`` modules) share one contract
(research/depthpro2/SPEC.md §3):

* every engine module exports ``Engine``; ``Engine(device, weights, shape)`` uploads the per-shape constants and
  compiles nothing that has to happen after a trace capture; ``Engine.run(inputs) -> outputs`` only enqueues device
  work (trace-capturable, no host sync);
* an engine class MAY define ``Engine.prepare_weights(device, host_weights, size) -> obj`` (staticmethod): ``tt/model.py``
  calls it once per stage and passes ``obj`` as ``weights`` to every per-shape engine (weights are uploaded once and
  shared by all buckets / clip lengths); without it the engine receives the host dict (name -> float32 torch tensor);
* ``shape`` is a :class:`StageShape`;
* an ENC engine MAY support a runtime valid-frame count: ``Engine.supports_valid_frames = True`` and
  ``Engine.set_valid_frames(n)`` (host call before a run / trace replay; writes persistent device tensors only): the
  T-frame program then processes frames ``[0, n)`` as an n-frame clip (frames ``>= n`` excluded from every
  cross-frame block's joint keys and pooled-prefix mean; their outputs don't-care). Contract: BASELINE.md §9.2.

Token-grid geometry for a padded frame ``Hp x Wp`` (multiples of 32): ``gh = Hp / 16``, ``gw = Wp / 16``,
``N = gh * gw`` patches, ``Np = ceil32(N)``.
"""

from __future__ import annotations

from dataclasses import dataclass

PATCH = 16
NUM_PREFIX = 5
TILE = 32


def ceil32(n: int) -> int:
    return (n + TILE - 1) // TILE * TILE


@dataclass(frozen=True)
class StageShape:
    """One compiled configuration: ``frames`` (T) frames of ``hp x wp`` padded pixels (multiples of 32)."""

    frames: int
    hp: int
    wp: int

    def __post_init__(self):
        if self.hp % 32 or self.wp % 32 or self.hp <= 0 or self.wp <= 0:
            raise ValueError(f"padded size must be positive multiples of 32, got {self.hp}x{self.wp}")
        if not 1 <= self.frames <= 64:
            raise ValueError(f"frames must be in 1..64, got {self.frames}")

    @property
    def gh(self) -> int:
        return self.hp // PATCH

    @property
    def gw(self) -> int:
        return self.wp // PATCH

    @property
    def n(self) -> int:
        """Patch tokens per frame."""
        return self.gh * self.gw

    @property
    def np_(self) -> int:
        """Patch rows per frame in the stage-contract layouts (``ceil32(N)``)."""
        return ceil32(self.n)

    @property
    def lp(self) -> int:
        """Rows per frame of the ttnn encoder's token tensor: [N patches ; 5 prefix ; pad] = ``ceil32(N + 5)``."""
        return ceil32(self.n + NUM_PREFIX)

    @property
    def key(self) -> str:
        return f"T{self.frames}_{self.hp}x{self.wp}"