File size: 1,441 Bytes
5c33f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""Opening the p150 (vendored ``ttaw.device``): ETH dispatch by default (12 x 10 compute grid), the grid is read at
runtime, ``EMBGEMMA_DISPATCH`` / ``_NUM_CQS`` / ``_L1_SMALL`` / ``_TRACE_REGION`` / ``_WORKER_L1_SIZE`` and
``TT_DEVICE_ID`` override per process. Importing has no side effects."""
from __future__ import annotations

import contextlib
import dataclasses
from typing import Any, Dict, Iterator, Optional

from .ttaw.device import DeviceConfig, close_device, compute_grid, describe_device  # noqa: F401

ENV_PREFIX = "EMBGEMMA"

DEVICE_DEFAULTS: Dict[str, Any] = {
    "num_command_queues": 1,
    "l1_small_size": 32 << 10,        # audio subsample conv2d config tensors
    "trace_region_size": 256 << 20,   # one trace per (stage, bucket)
}


def device_config(**overrides: Any) -> DeviceConfig:
    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):
    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]:
    device = open_device(device_id, **overrides)
    try:
        yield device
    finally:
        close_device(device)