# SPDX-License-Identifier: Apache-2.0 """Opening the Blackhole p150 the way every published number of DepthPro2 was measured. The implementation is the vendored ``ttaw.device``: dispatch on the idle ETH cores (``ttnn.DispatchCoreConfig(ttnn.DispatchCoreType.ETH)``), which gives a 12x10 = 120-core compute grid on a p150 (11x10 with WORKER dispatch, the A/B switch). ETH dispatch needs ``patches/tt-metal-eth-dispatch.patch`` on tt-metal 44d6650 (the container image is built from a patched tree); if the ETH open fails, a ``RuntimeWarning`` is issued and WORKER dispatch is used, unless ``allow_fallback=False``. Never hard-code the grid: use :func:`compute_grid` or ``device.compute_with_storage_grid_size()``. This module binds it to the open parameters of this port, :data:`DEVICE_DEFAULTS` (the model class uses the same dict), which the ``DEPTHPRO2_DISPATCH``, ``_NUM_CQS``, ``_L1_SMALL``, ``_TRACE_REGION`` and ``_WORKER_L1_SIZE`` variables and ``TT_DEVICE_ID`` override per process. The trace region holds every captured trace, so it depends on the model configuration: :func:`trace_region_for` sizes it from the configuration (``tt.model.trace_region_bytes``, from the measured trace sizes) unless ``DEPTHPRO2_TRACE_REGION`` is set. Importing it has no side effects (``ttnn`` is imported when a device is opened). """ from __future__ import annotations import contextlib import dataclasses import os from typing import Any, Dict, Iterator, Optional, Sequence, Tuple from .ttaw.device import DeviceConfig, close_device, compute_grid, core_grid, describe_device, full_core_range_set __all__ = ["ENV_PREFIX", "DEVICE_DEFAULTS", "DeviceConfig", "device_config", "open_device", "device_session", "close_device", "describe_device", "compute_grid", "core_grid", "full_core_range_set", "trace_region_for"] ENV_PREFIX = "DEPTHPRO2" # Device-open parameters of this port. The trace region holds the traces of every (bucket, clip length) the model # captures; DepthPro2.from_pretrained sizes it with trace_region_for (DEPTHPRO2_TRACE_REGION overrides it); the value # here is only the fallback of a bare open_device(). DEVICE_DEFAULTS: Dict[str, Any] = { "num_command_queues": 1, # 1, or 2 when the input upload (CQ1) overlaps the trace (CQ0) "l1_small_size": 32768, # L1_SMALL bytes per core (conv config tensors of the ttnn decoder; enough with them in DRAM) "trace_region_size": 256 << 20, # DRAM bytes for the captured traces (fallback; see trace_region_for) } def trace_region_for(size: str, buckets: Sequence[Tuple[int, int]], window: int, *, clip_lengths: Any = None, engine: Optional[Dict[str, str]] = None, fov_shapes: Optional[Sequence[Tuple[int, int]]] = None, kv_cache: bool = False, kv_streams: int = 1) -> Optional[int]: """The trace-region bytes a model configuration needs (``tt.model.trace_region_bytes`` over the clip lengths the device compiles, ``tt.model.planned_frames``), or None when ``DEPTHPRO2_TRACE_REGION`` is set (the environment wins). ``buckets``: padded device sizes; ``fov_shapes``: ``[]`` without the FOV estimator.""" if (os.environ.get(f"{ENV_PREFIX}_TRACE_REGION") or "").strip(): return None from .tt.model import planned_frames, trace_region_bytes frames = planned_frames(window, frames=(1,), clip_lengths=clip_lengths, engine=engine) return int(trace_region_bytes(size, list(buckets), frames, engine=engine, fov_shapes=fov_shapes, kv_cache=kv_cache, kv_streams=kv_streams)) def device_config(**overrides: Any) -> DeviceConfig: """:data:`DEVICE_DEFAULTS` < the ``DEPTHPRO2_*`` / ``TT_DEVICE_ID`` environment < explicit non-None ``overrides`` (``device_id``, ``dispatch``, ``num_command_queues``, ``l1_small_size``, ``trace_region_size``, ``worker_l1_size``, ``allow_fallback``): the same resolution as ``DepthPro2.from_pretrained``.""" config = DeviceConfig.from_env(ENV_PREFIX, **DEVICE_DEFAULTS) return dataclasses.replace(config, **{k: v for k, v in overrides.items() if v is not None}) def open_device(device_id: Optional[int] = None, *, dispatch: Optional[str] = None, **overrides: Any): """Open one chip like the published numbers: ETH dispatch (``dispatch="worker"`` is the A/B opt-in) and this port's sizes. Close it with :func:`close_device`, or use :func:`device_session`.""" return device_config(device_id=device_id, dispatch=dispatch, **overrides).open() @contextlib.contextmanager def device_session(device_id: Optional[int] = None, **overrides: Any) -> Iterator[Any]: """``with device_session() as dev:`` opens with :func:`open_device` and always closes, also when the body raises.""" device = open_device(device_id, **overrides) try: yield device finally: close_device(device)