DepthPro2 / code /tt_depthpro2 /tests /test_host_config.py
changh95's picture
tt-model push depthpro2 (container)
d5afa0d verified
Raw History Blame Contribute Delete
10.2 kB
# SPDX-License-Identifier: Apache-2.0
"""Host tests of the configuration surface (no device, no weights): sizes and profiles, resolution buckets and
presets, window / overlap / clip lengths / video modes / engine / precision validation, the bucket choice, the
weights pins, and the server's start-up configuration (``server.app.load_config``) including its refusals.
PYTHONPATH=<workspace>/research/packaging/scripts:code TT_VISIBLE_DEVICES=none \
python -m pytest -p fake_ttnn_plugin code/tt_depthpro2/tests/test_host_config.py
"""
from __future__ import annotations
import pytest
from tt_depthpro2 import config as C
def test_sizes_and_profiles():
for raw, want in (("S", "S"), ("s", "S"), ("small", "S"), ("b", "B"), ("L", "L"), ("large", "L"), ("H", "H"),
("h+", "H"), ("H+", "H"), ("huge_plus", "H"), ("DepthPro2-L", "L"), ("apple/DepthPro2-B", "B")):
assert C.normalize_size(raw) == want, raw
for bad in ("XL", "", None, 3, "m"):
with pytest.raises(ValueError):
C.normalize_size(bad)
assert C.PROFILES == {"s": "S", "b": "B", "l": "L", "h": "H"}
assert set(C.WEIGHTS) == {"focal", "S", "B", "L", "H"}
for k, (repo, rev) in C.WEIGHTS.items():
assert repo.startswith("apple/DepthPro2-") and len(rev) == 40 and int(rev, 16) >= 0
def test_resolutions_presets_and_validation():
assert C.parse_resolutions(None) == ((480, 640), (640, 480))
assert C.parse_resolutions("vga, 720p ,480x640, 1080p_portrait") == ((480, 640), (720, 1280), (1920, 1080))
assert C.parse_resolutions("375X500") == ((375, 500),)
assert C.parse_resolutions([(240, 320), "qvga_portrait"]) == ((240, 320), (320, 240))
assert C.parse_resolutions((480, 640)) == ((480, 640),)
for bad in ("480x", "abc", "16x640", "480x5000", "0x0", "", [(1.5, 2)], [(True, 4)],
",".join(["vga", "qvga", "square", "720p", "1080p", "vga_portrait", "qvga_portrait", "720p_portrait",
"1080p_portrait"])):
with pytest.raises(ValueError):
C.parse_resolutions(bad)
assert C.padded_hw(720, 1280) == (736, 1280) and C.padded_hw(1080, 1920) == (1088, 1920)
assert C.padded_hw(375, 500) == (384, 512) and C.padded_hw(480, 640) == (480, 640)
def test_choose_bucket():
b = [(480, 640), (640, 480), (720, 1280)]
assert C.choose_bucket(480, 640, b) == ((480, 640), True)
assert C.choose_bucket(960, 1280, b) == ((480, 640), False) # same aspect, resized
assert C.choose_bucket(1080, 1920, b) == ((720, 1280), False) # 16:9
assert C.choose_bucket(1280, 960, b) == ((640, 480), False) # portrait
assert C.choose_bucket(470, 630, b) == ((480, 640), False) # never padded natively unless exactly a bucket
# same aspect ratio: the nearest area wins
assert C.choose_bucket(300, 400, [(480, 640), (240, 320)]) == ((240, 320), False)
assert C.choose_bucket(700, 1000, [(480, 640), (240, 320)]) == ((480, 640), False)
def test_resolve_config_defaults_env_and_explicit():
c = C.resolve_config(env={})
assert (c.size, c.resolutions, c.window, c.overlap, c.video_modes, c.clip_lengths, c.fov, c.engine,
c.precision) == ("L", ((480, 640), (640, 480)), 16, 4, ("buffered", "unbuffered"), (1, 16), True,
{"enc": "mk", "dec": "mk", "fov": "mk"}, None)
assert c.video_modes_auto and c.kv_streams == 1 and c.as_env()["DEPTHPRO2_VIDEO_MODES"] == "auto"
assert C.resolve_config(env={"DEPTHPRO2_VIDEO_MODES": "AUTO"}).video_modes_auto
b = C.resolve_config(video_modes="buffered", kv_streams="3", env={})
assert b.video_modes == ("buffered",) and not b.video_modes_auto and b.kv_streams == 3
env = {"DEPTHPRO2_SIZE": "h", "DEPTHPRO2_RESOLUTIONS": "720p", "DEPTHPRO2_WINDOW": "8", "DEPTHPRO2_OVERLAP": "2",
"DEPTHPRO2_VIDEO_MODES": "unbuffered,buffered", "DEPTHPRO2_CLIP_LENGTHS": "2,4", "DEPTHPRO2_FOV": "0",
"DEPTHPRO2_ENGINE": "enc=mk,dec=ttnn", "DEPTHPRO2_PRECISION": "BF16", "DEPTHPRO2_KV_STREAMS": "2"}
c = C.resolve_config(env=env)
assert (c.size, c.resolutions, c.window, c.overlap, c.video_modes, c.clip_lengths, c.fov, c.engine,
c.precision) == ("H", ((720, 1280),), 8, 2, ("buffered", "unbuffered"), (1, 2, 4, 8), False,
{"enc": "mk", "dec": "ttnn", "fov": "mk"}, "bf16") # dec=ttnn given explicitly
assert c.device_buckets == [(736, 1280)]
assert C.resolve_config(env=c.as_env()) == c # as_env round-trips
# explicit arguments win over the environment
c2 = C.resolve_config("S", window=2, overlap=1, resolutions="qvga", clip_lengths="auto", env=env)
assert (c2.size, c2.window, c2.overlap, c2.resolutions, c2.clip_lengths) == ("S", 2, 1, ((240, 320),), (1, 2))
# unset overlap is clamped below the window; "all" clip lengths
assert C.resolve_config(window=3, env={}).overlap == 2
assert C.resolve_config(window=1, env={}).overlap == 0
assert C.resolve_config(window=4, clip_lengths="all", env={}).clip_lengths == (1, 2, 3, 4)
assert C.parse_engine("ttnn") == {"enc": "ttnn", "dec": "ttnn", "fov": "ttnn"}
assert C.parse_engine("") == C.DEFAULT_ENGINE == {"enc": "mk", "dec": "mk", "fov": "mk"}
assert C.parse_engine({"fov": "TTNN"}) == {"enc": "mk", "dec": "mk", "fov": "ttnn"} # unnamed stages: default
assert C.parse_engine("enc=ttnn") == {"enc": "ttnn", "dec": "mk", "fov": "mk"}
def test_ttnn_decoder_fallback_and_no_guard():
assert C.resolve_config(env={"DEPTHPRO2_ENGINE": "dec=ttnn"}).engine == {"enc": "mk", "dec": "ttnn", "fov": "mk"}
assert C.resolve_config(env={"DEPTHPRO2_ENGINE": "ttnn"}).engine == {"enc": "ttnn", "dec": "ttnn", "fov": "ttnn"}
# the decoder megakernel needs no flag any more; a left-over DEPTHPRO2_EXPERIMENTAL_MK_DEC is ignored
for flag in (None, "0", "1"):
env = {"DEPTHPRO2_ENGINE": "dec=mk"} if flag is None else {"DEPTHPRO2_ENGINE": "dec=mk",
"DEPTHPRO2_EXPERIMENTAL_MK_DEC": flag}
assert C.resolve_config(env=env).engine["dec"] == "mk"
@pytest.mark.parametrize("env", [
{"DEPTHPRO2_SIZE": "XL"}, {"DEPTHPRO2_RESOLUTIONS": "480x"}, {"DEPTHPRO2_WINDOW": "0"},
{"DEPTHPRO2_WINDOW": "17"}, {"DEPTHPRO2_WINDOW": "4.5"}, {"DEPTHPRO2_OVERLAP": "16"},
{"DEPTHPRO2_OVERLAP": "4", "DEPTHPRO2_WINDOW": "4"}, {"DEPTHPRO2_OVERLAP": "-1"},
{"DEPTHPRO2_VIDEO_MODES": "live"}, {"DEPTHPRO2_CLIP_LENGTHS": "17"}, {"DEPTHPRO2_CLIP_LENGTHS": "x"},
{"DEPTHPRO2_FOV": "maybe"}, {"DEPTHPRO2_KV_STREAMS": "0"}, {"DEPTHPRO2_KV_STREAMS": "9"}, {"DEPTHPRO2_ENGINE": "enc=gpu"}, {"DEPTHPRO2_ENGINE": "foo=mk"},
{"DEPTHPRO2_ENGINE": "megakernel"}, {"DEPTHPRO2_PRECISION": "fp8"},
])
def test_resolve_config_refuses(env):
with pytest.raises(ValueError) as e:
C.resolve_config(env=env)
assert next(iter(env)) in str(e.value) # the message names the variable
def test_auxiliary_weights_parsing():
aux = C.parse_auxiliary_weights("apple/DepthPro2-S@abc, apple/DepthPro2-B ,x@")
assert aux == {"apple/DepthPro2-S": "abc", "apple/DepthPro2-B": None, "x": None}
assert C.parse_auxiliary_weights(None) == {}
def _server_env(**over):
env = {"DEPTHPRO2_SIZE": "L", "TT_AUXILIARY_WEIGHTS": ",".join(f"{r}@{v}" for k, (r, v) in C.WEIGHTS.items()
if k != "focal"),
"TT_MODEL_WEIGHTS_REVISION": C.WEIGHTS["focal"][1], "HF_MODEL": "apple/DepthPro2-focal",
"TT_MESH_SHAPE": "1x1"}
env.update(over)
return env
def test_server_load_config():
pytest.importorskip("fastapi")
from tt_depthpro2.server.app import load_config
c = load_config(_server_env())
assert c.model.size == "L" and c.revision == C.WEIGHTS["L"][1] and c.fov_revision == C.WEIGHTS["focal"][1]
assert (c.dispatch, c.num_command_queues, c.backend, c.max_frames, c.max_streams) == ("eth", None, "tt", 64, 4)
c = load_config(_server_env(DEPTHPRO2_SIZE="S", DEPTHPRO2_NUM_CQS="2", DEPTHPRO2_DISPATCH="WORKER",
DEPTHPRO2_MAX_FRAMES="8", TT_DEVICE_ID="1"))
assert (c.model.size, c.revision, c.num_command_queues, c.dispatch, c.max_frames, c.device_id) == (
"S", C.WEIGHTS["S"][1], 2, "worker", 8, 1)
assert load_config({}).revision is None # no tt-model env: the API's pins apply
@pytest.mark.parametrize("over", [
{"TT_MESH_SHAPE": "1x2"}, {"TT_MESH_SHAPE": "two"}, {"DEPTHPRO2_DISPATCH": "auto"}, {"DEPTHPRO2_NUM_CQS": "3"},
{"DEPTHPRO2_BACKEND": "gpu"}, {"TT_DEVICE_ID": "x"}, {"HF_MODEL": "apple/DepthPro2-L"},
{"DEPTHPRO2_MAX_FRAMES": "0"}, {"DEPTHPRO2_MAX_BODY_MB": "huge"}, {"DEPTHPRO2_SIZE": "XXL"},
{"DEPTHPRO2_RESOLUTIONS": "100000x2"}, {"DEPTHPRO2_OVERLAP": "99"},
])
def test_server_load_config_refuses(over):
pytest.importorskip("fastapi")
from tt_depthpro2.server.app import load_config
with pytest.raises(RuntimeError):
load_config(_server_env(**over))
def test_enc_precision_setting_and_mk_enc_options():
assert C.DEFAULT_ENC_PRECISION == "high" and C.resolve_config(env={}).enc_precision == "high"
assert C.resolve_config(env={"DEPTHPRO2_ENC_PRECISION": " Fast "}).enc_precision == "fast"
assert C.resolve_config(enc_precision="fast", env={"DEPTHPRO2_ENC_PRECISION": "high"}).enc_precision == "fast"
with pytest.raises(ValueError, match="DEPTHPRO2_ENC_PRECISION"):
C.resolve_config(env={"DEPTHPRO2_ENC_PRECISION": "medium"})
with pytest.raises(ValueError, match="enc_precision"):
C.resolve_config(enc_precision="bf16", env={})
assert C.resolve_config(env={}).as_env()["DEPTHPRO2_ENC_PRECISION"] == "high"
# fast = the encoder megakernel's v1 options; high drops them (per-size defaults); other keys are kept
assert C.mk_enc_options("fast") == "inx32=0,klo=0"
assert C.mk_enc_options("fast", "exp=1, inx32=1,klo=2") == "exp=1,inx32=0,klo=0"
assert C.mk_enc_options("high", "inx32=0,klo=0,ring_first=1") == "ring_first=1"
assert C.mk_enc_options("high") == "" and C.mk_enc_options("high", "") == ""
with pytest.raises(ValueError):
C.mk_enc_options("medium")