-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_generation_engine.py
More file actions
153 lines (123 loc) · 5.39 KB
/
test_generation_engine.py
File metadata and controls
153 lines (123 loc) · 5.39 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from __future__ import annotations
from pathlib import Path
from types import SimpleNamespace
import pytest
from app.core.config import settings
from app.core.schema import Candidate, StrategyConfig, Session
from app.engine.generation import DiffusersGenerationEngine, MockGenerationEngine, build_generation_engine, parse_image_size, resolve_prepared_model_path
def test_parse_image_size_parses_dimensions() -> None:
assert parse_image_size("512x768") == (512, 768)
def test_parse_image_size_rejects_invalid_values() -> None:
with pytest.raises(ValueError):
parse_image_size("oops")
def test_mock_backend_is_rejected_without_test_opt_in(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setattr(settings, "allow_test_mock_backend", False)
with pytest.raises(RuntimeError, match="reserved for tests"):
build_generation_engine(
backend="mock",
artifacts_dir=tmp_path / "artifacts",
)
def test_mock_backend_is_available_with_explicit_test_opt_in(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setattr(settings, "allow_test_mock_backend", True)
engine = build_generation_engine(
backend="mock",
artifacts_dir=tmp_path / "artifacts",
)
assert isinstance(engine, MockGenerationEngine)
def test_auto_backend_refuses_when_model_is_missing(tmp_path: Path) -> None:
with pytest.raises(FileNotFoundError, match="Prepared model not found"):
build_generation_engine(
backend="auto",
model_id="runwayml/stable-diffusion-v1-5",
models_root=tmp_path / "models",
artifacts_dir=tmp_path / "artifacts",
)
def test_diffusers_backend_requires_prepared_model_when_remote_download_disabled(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setattr(settings, "allow_remote_model_download", False)
with pytest.raises(FileNotFoundError):
build_generation_engine(
backend="diffusers",
model_id="runwayml/stable-diffusion-v1-5",
models_root=tmp_path / "models",
artifacts_dir=tmp_path / "artifacts",
)
def test_resolve_prepared_model_path_uses_slugged_model_directory(tmp_path: Path) -> None:
path = resolve_prepared_model_path("runwayml/stable-diffusion-v1-5", tmp_path)
assert path == tmp_path / "runwayml--stable-diffusion-v1-5"
def test_diffusers_engine_requires_cuda_when_gpu_is_mandatory(tmp_path: Path) -> None:
engine = DiffusersGenerationEngine(
model_source=str(tmp_path / "model"),
artifacts_dir=tmp_path / "artifacts",
device="cuda",
require_gpu=True,
)
with pytest.raises(RuntimeError, match="CUDA-capable GPU"):
engine._resolve_device(SimpleNamespace(cuda=SimpleNamespace(is_available=lambda: False)))
def test_mock_render_uses_session_generation_config(tmp_path: Path) -> None:
engine = MockGenerationEngine(tmp_path / "artifacts")
session = Session(
experiment_id="exp_test",
prompt="A configurable test prompt",
negative_prompt="blurry",
model_name="custom/model",
config=StrategyConfig(
image_size="320x640",
anchor_strength=0.8,
guidance_scale=9.5,
num_inference_steps=28,
model_name="custom/model",
),
)
candidate = Candidate(
round_id="rnd_test",
candidate_index=0,
z=[0.1, 0.2, -0.1],
sampler_role="baseline_prompt",
seed=123,
generation_params={},
)
rendered = engine.render_candidate(session, candidate)
svg_path = tmp_path / "artifacts" / f"{candidate.id}.svg"
svg = svg_path.read_text(encoding="utf-8")
assert rendered.generation_params["image_size"] == "320x640"
assert rendered.generation_params["guidance_scale"] == 9.5
assert rendered.generation_params["num_inference_steps"] == 28
assert rendered.generation_params["anchor_strength"] == 0.8
assert rendered.generation_params["model_source"] == "custom/model"
assert 'width="320"' in svg
assert 'height="640"' in svg
assert "CFG: 9.50" in svg
assert "Steps: 28" in svg
assert "Anchor strength: 0.80" in svg
def test_diffusers_engine_resolves_per_session_model_name(tmp_path: Path) -> None:
prepared = tmp_path / "models" / "custom--model"
prepared.mkdir(parents=True)
original_models_dir = settings.models_dir
settings.models_dir = tmp_path / "models"
engine = DiffusersGenerationEngine(
model_source=str(tmp_path / "models" / "runwayml--stable-diffusion-v1-5"),
artifacts_dir=tmp_path / "artifacts",
device="cuda",
require_gpu=True,
)
session = Session(
experiment_id="exp_test",
prompt="A model resolution test",
model_name="custom/model",
config=StrategyConfig(model_name="custom/model"),
)
try:
resolved = engine._resolve_model_source(session)
assert resolved == str(prepared)
finally:
settings.models_dir = original_models_dir
def test_auto_backend_uses_diffusers_engine_when_model_exists(tmp_path: Path) -> None:
prepared = tmp_path / "models" / "runwayml--stable-diffusion-v1-5"
prepared.mkdir(parents=True)
engine = build_generation_engine(
backend="auto",
model_id="runwayml/stable-diffusion-v1-5",
models_root=tmp_path / "models",
artifacts_dir=tmp_path / "artifacts",
)
assert isinstance(engine, DiffusersGenerationEngine)