summaryrefslogtreecommitdiff
path: root/tests/test_pipeline_factory.py
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-09-12 18:52:14 +0900
committertanaka takashi <isazaka@gmail.com>2026-09-12 18:52:14 +0900
commit34fe9ead73abfa5ce50fd2351cca8a844099df36 (patch)
tree1c61ecc55ccfe68cfe1ede96c28d302b72b78e2b /tests/test_pipeline_factory.py
parent3e47e9bddb5c859dec28cd39732979a5eeca51b8 (diff)
feat: CUDA 13.2でgenerate_imageの実GPU生成に対応
CPU版PyTorchが選択されていた依存設定を修正し、RTX 5050上でStable Diffusion 1.5のMCP generate_imageを実行できる構成へ更新。併せてVAEとLoRAの仕様・実装・テストを明確化した。 CUDA / 依存管理: - torch 2.14.0+cu132とtorchvision 0.29.0+cu132をruntime依存へ固定 - PyTorch CUDA 13.2のexplicit indexとtool.uv.sourcesをuv設定へ追加 - uv.lockをCUDA wheelに更新し、uv sync後もCPU版へ戻らないよう修正 - RTX 5050の認識、CUDA 13.2 runtime、GPU FP16演算を確認 モデル・オフライン実行: - model.root配下のhuggingface cacheをDiffusersへ明示的に渡すよう変更 - checkpoint、外部VAE、ControlNetのsingle-file loadをlocal_files_onlyで統一 - SD 1.5 checkpointと公式pipeline構成・tokenizerによるオフラインloadを確認 - data/config.generate-image.tomlにText-to-Image用最小設定を追加 - data/MODELS.mdにモデル出典、容量、SHA-256、cache構成、検証結果を記録 - 4.27GBのcheckpoint、cache、生成画像はGit管理対象外を維持 VAE / LoRA: - Stable Diffusion pipelineではVAE componentが必須であることを仕様・設計・設定文書に明記 - model.vaeはVAEの有無ではなく外部VAE差し替えの任意設定であることを明確化 - checkpoint内蔵VAEも外部VAEもない場合はpipeline初期化を失敗させる検証を追加 - 複数LoRAを起動時にロードし各weightでset_adaptersする既存動作を文書化 - generate_imageを含む全生成ツールへ設定済みLoRAが共通適用されることを明記 - 外部VAE差し替え、VAE欠落、複数LoRA適用のunit testを追加 動作確認: - 実backendとin-process MCP Clientを使うsmoke_generate_image.pyを追加 - RTX 5050でSD 1.5、512x512、20 stepsのgenerate_imageを実行 - PNG ImageContent返却とseed、size、mode、timestamp、処理時間metadataを確認 - GPU生成時間18.313秒 - pytest: 32 passed - Ruff lint/format: passed - Pyright: 0 errors, 0 warnings - uv lock check: passed
Diffstat (limited to 'tests/test_pipeline_factory.py')
-rw-r--r--tests/test_pipeline_factory.py99
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/test_pipeline_factory.py b/tests/test_pipeline_factory.py
new file mode 100644
index 0000000..454a5b1
--- /dev/null
+++ b/tests/test_pipeline_factory.py
@@ -0,0 +1,99 @@
+from __future__ import annotations
+
+from dataclasses import replace
+from types import SimpleNamespace
+
+import pytest
+
+from mcp_sd15.config import LoRAConfig
+from mcp_sd15.errors import ConfigError
+from mcp_sd15.pipeline_factory import PipelineFactory
+
+
+class FakePipeline:
+ def __init__(self, *, with_vae: bool = True) -> None:
+ self.vae = object() if with_vae else None
+ self.scheduler = SimpleNamespace(config={})
+ self.lora_loads: list[dict[str, object]] = []
+ self.adapters: tuple[list[str], list[float]] | None = None
+ self.device: str | None = None
+
+ def load_lora_weights(self, directory: str, **kwargs: object) -> None:
+ self.lora_loads.append({"directory": directory, **kwargs})
+
+ def set_adapters(self, names: list[str], *, adapter_weights: list[float]) -> None:
+ self.adapters = (names, adapter_weights)
+
+ def set_progress_bar_config(self, *, disable: bool) -> None:
+ assert disable is True
+
+ def to(self, device: str) -> FakePipeline:
+ self.device = device
+ return self
+
+
+class FakeScheduler:
+ @classmethod
+ def from_config(cls, config: dict[str, object]) -> object:
+ return {"scheduler": cls.__name__, **config}
+
+
+def fake_diffusers(pipeline: FakePipeline, external_vae: object) -> SimpleNamespace:
+ class StableDiffusionPipeline:
+ @classmethod
+ def from_single_file(cls, path: str, **kwargs: object) -> FakePipeline:
+ pipeline.base_load = {"path": path, **kwargs} # type: ignore[attr-defined]
+ return pipeline
+
+ class AutoencoderKL:
+ @classmethod
+ def from_single_file(cls, path: str, **kwargs: object) -> object:
+ pipeline.vae_load = {"path": path, **kwargs} # type: ignore[attr-defined]
+ return external_vae
+
+ return SimpleNamespace(
+ StableDiffusionPipeline=StableDiffusionPipeline,
+ AutoencoderKL=AutoencoderKL,
+ DPMSolverMultistepScheduler=FakeScheduler,
+ DDIMScheduler=FakeScheduler,
+ EulerDiscreteScheduler=FakeScheduler,
+ EulerAncestralDiscreteScheduler=FakeScheduler,
+ )
+
+
+def test_base_pipeline_requires_vae_and_applies_all_loras(app_config, monkeypatch) -> None:
+ model = replace(
+ app_config.model,
+ vae="external.safetensors",
+ loras=(
+ LoRAConfig("detail.safetensors", 0.8),
+ LoRAConfig("style.safetensors", 0.5),
+ ),
+ )
+ factory = PipelineFactory(replace(app_config, model=model))
+ pipeline = FakePipeline()
+ external_vae = object()
+ runtime = (SimpleNamespace(float16="float16"), fake_diffusers(pipeline, external_vae))
+ monkeypatch.setattr(factory, "_runtime", lambda: runtime)
+
+ result = factory.create_base()
+
+ assert result is pipeline
+ assert pipeline.vae is external_vae
+ assert pipeline.adapters == (["configured_lora_0", "configured_lora_1"], [0.8, 0.5])
+ assert [item["weight_name"] for item in pipeline.lora_loads] == [
+ "detail.safetensors",
+ "style.safetensors",
+ ]
+ assert all(item["local_files_only"] is True for item in pipeline.lora_loads)
+ assert pipeline.device == "cuda"
+
+
+def test_checkpoint_without_vae_fails_pipeline_initialization(app_config, monkeypatch) -> None:
+ factory = PipelineFactory(app_config)
+ pipeline = FakePipeline(with_vae=False)
+ runtime = (SimpleNamespace(float16="float16"), fake_diffusers(pipeline, object()))
+ monkeypatch.setattr(factory, "_runtime", lambda: runtime)
+
+ with pytest.raises(ConfigError, match="does not provide a VAE"):
+ factory.create_base()