152 lines
5.0 KiB
Python
152 lines
5.0 KiB
Python
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
|
|
def test_candidate_stage_uses_private_caches_after_budget_setup(tmp_path, monkeypatch):
|
|
from llm_services import adapters, runner
|
|
from llm_services.dialogue import LATENTSYNC_PREVIEW
|
|
|
|
monkeypatch.setattr(runner, "WORKSPACE", tmp_path)
|
|
monkeypatch.setattr(
|
|
adapters, "budgeted_child_environment", lambda _: {"HF_HOME": "/shared"}
|
|
)
|
|
monkeypatch.setattr(adapters, "memory_policy", lambda: (24, 1))
|
|
monkeypatch.setattr(adapters, "admit_gpu", lambda: None)
|
|
seen = {}
|
|
|
|
def stop_before_launch(command, environment, *args):
|
|
seen.update(environment)
|
|
raise RuntimeError("reached stage boundary")
|
|
|
|
monkeypatch.setattr(runner, "_run_child", stop_before_launch)
|
|
request = {
|
|
"task_type": "video.lipsync",
|
|
"parameters": {"profile": LATENTSYNC_PREVIEW},
|
|
"output_dir": str(tmp_path / "result"),
|
|
}
|
|
with pytest.raises(RuntimeError, match="reached stage boundary"):
|
|
runner.execute(request)
|
|
cache = tmp_path / "models" / LATENTSYNC_PREVIEW / ".cache"
|
|
for name in (
|
|
"HF_HOME",
|
|
"HF_HUB_CACHE",
|
|
"HUGGINGFACE_HUB_CACHE",
|
|
"TRANSFORMERS_CACHE",
|
|
"MPLCONFIGDIR",
|
|
):
|
|
assert Path(seen[name]).is_relative_to(cache)
|
|
assert seen["HF_HUB_OFFLINE"] == seen["TRANSFORMERS_OFFLINE"] == "1"
|
|
|
|
|
|
def test_candidate_needs_its_own_operator_ticket(tmp_path, monkeypatch):
|
|
from types import SimpleNamespace
|
|
|
|
from llm_services.dialogue import profiles
|
|
from llm_services.dialogue.protocol import validate
|
|
|
|
d = tmp_path / "inputs"
|
|
d.mkdir()
|
|
for name in ("video", "tracks", "wave"):
|
|
(d / name).touch()
|
|
actor = "b" * 32
|
|
inputs = {
|
|
"video_path": str(d / "video"),
|
|
"face_tracks_path": str(d / "tracks"),
|
|
"speech_tracks": [
|
|
{
|
|
"speaker_id": actor,
|
|
"target_track_id": actor,
|
|
"audio_path": str(d / "wave"),
|
|
}
|
|
],
|
|
}
|
|
p = {
|
|
"profile": "latentsync16-preview-v1",
|
|
"fps": 24,
|
|
"num_frames": 36,
|
|
"speech_silence_policy": "source-rest-v2",
|
|
}
|
|
validate("video.lipsync", inputs, p, tmp_path)
|
|
for bad in (
|
|
{"num_frames": 145},
|
|
{"speech_silence_policy": "neutral-rest-v1"},
|
|
{"audio_context_shift_frames": 3},
|
|
):
|
|
with pytest.raises(ValueError):
|
|
validate("video.lipsync", inputs, {**p, **bad}, tmp_path)
|
|
with pytest.raises(ValueError):
|
|
validate(
|
|
"video.dialogue.inspect",
|
|
{**inputs, "base_video_path": inputs["video_path"]},
|
|
p,
|
|
tmp_path,
|
|
)
|
|
monkeypatch.setattr(
|
|
profiles,
|
|
"capabilities",
|
|
lambda _: pytest.fail("must not borrow a production certificate"),
|
|
)
|
|
with pytest.raises(ValueError, match="ticket"):
|
|
profiles.admit(SimpleNamespace(app_root=tmp_path), "video.lipsync", p, inputs)
|
|
|
|
|
|
@pytest.mark.parametrize("count", [1, 9, 16, 17, 56, 143, 144])
|
|
def test_real_context_frames_cover_each_source_timestamp_once(count):
|
|
from llm_services.dialogue.latentsync_preview import windows
|
|
|
|
seen = []
|
|
for start, end, first, last in windows(count):
|
|
assert 0 <= start <= first < last <= end <= count
|
|
assert end - start == min(count, 16)
|
|
seen.extend(range(first, last))
|
|
assert seen == list(range(count))
|
|
|
|
|
|
def test_quiet_frames_never_written_and_short_model_output_is_rejected():
|
|
from llm_services.dialogue.latentsync_preview import preview_frames
|
|
|
|
source = np.arange(21, dtype=np.uint8)[:, None, None, None] * np.ones(
|
|
(21, 1, 2, 3), np.uint8
|
|
)
|
|
active = np.zeros(21, bool)
|
|
active[[2, 17]] = True
|
|
|
|
def predict(start, context):
|
|
return np.full_like(context, 128)
|
|
|
|
def paste(i, original, pred):
|
|
assert active[i]
|
|
out = original.copy()
|
|
out[:, :1] = pred[:, :1]
|
|
return out
|
|
|
|
out = np.array(list(preview_frames(source, active, predict, paste)))
|
|
assert np.array_equal(out[~active], source[~active])
|
|
assert np.array_equal(out[:, :, 1], source[:, :, 1])
|
|
assert np.all(out[active, :, 0] == 128)
|
|
with pytest.raises(ValueError):
|
|
list(preview_frames(source, active, lambda s, c: c[:-1], paste))
|
|
|
|
|
|
@pytest.mark.parametrize("count", [1, 9, 16, 17, 33])
|
|
def test_identical_real_context_is_predicted_once(count):
|
|
from llm_services.dialogue.latentsync_preview import preview_frames
|
|
|
|
source = np.arange(count)
|
|
calls = set()
|
|
|
|
def predict(start, context):
|
|
interval = (start, start + len(context))
|
|
assert interval not in calls, "The same context resampled the VAE posterior"
|
|
calls.add(interval)
|
|
assert np.array_equal(context, source[start : start + len(context)])
|
|
return context.copy()
|
|
|
|
active = np.ones(count, dtype=bool)
|
|
result = list(preview_frames(source, active, predict, lambda _, src, dst: dst))
|
|
assert np.array_equal(result, source)
|
|
if count <= 16:
|
|
assert calls == {(0, count)}
|