Download code/tt_depthpro2/tt/__init__.py from changh95/DepthPro2: direct link, hf CLI and curl.
- Browser
- Download file 2.88 kB
-
https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tt/__init__.py
- Command line
-
hf download hf://changh95/DepthPro2/code/tt_depthpro2/tt/__init__.py
-
curl -L -o __init__.py https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tt/__init__.py
2.88 kB
| # 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 | |
| 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}") | |
| def gh(self) -> int: | |
| return self.hp // PATCH | |
| def gw(self) -> int: | |
| return self.wp // PATCH | |
| def n(self) -> int: | |
| """Patch tokens per frame.""" | |
| return self.gh * self.gw | |
| def np_(self) -> int: | |
| """Patch rows per frame in the stage-contract layouts (``ceil32(N)``).""" | |
| return ceil32(self.n) | |
| 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) | |
| def key(self) -> str: | |
| return f"T{self.frames}_{self.hp}x{self.wp}" | |