summaryrefslogtreecommitdiff
path: root/src/mcp_sd15/pipeline_factory.py
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-09-12 22:26:13 +0900
committertanaka takashi <isazaka@gmail.com>2026-09-12 22:26:13 +0900
commit546eca3591515b26516922860fe2cf1b573a5f82 (patch)
tree639cb25c732ca096f576b6e56a9027562b885f24 /src/mcp_sd15/pipeline_factory.py
parent796251e5b0da524584c9e81b8ad6e1dcc0931aa3 (diff)
transformersを4.x系に固定し、text encoder付きLoDA読み込み時の
IndexErrorを解消。あわせて外部VAEロードの既存バグも修正した。 問題: kohya形式LoRA(pixelartV3, pixel_f2, pixel sprites等)のロード時に Diffusers内部でIndexError: list index out of rangeが発生し、サーバが 起動失敗していた。LCM LoRAはtext encoderキーを含まないため成功していた。 原因: Transformers 5.x系のCLIPTextModel.named_modules()が返すモジュール名 (encoder.layers.0.self_attn.k_proj)と、Diffusersがkohya形式LoRAを PEFT形式へ変換後のキー(text_model.encoder.layers.0...lora_B.weight)の 間でtext_model.プレフィックスの有無が不一致となり、rank_dictが空になって get_peft_kwargsでlist(rank_dict.values())[0]がIndexErrorになる。 Transformers 4.x系ではnamed_modules()がtext_model.プレフィックス付きの モジュール名を返すため、PEFTキーと一致し正常にロードできる。 対応: transformers>=4.45,<5 に固定。4.x系最新版の4.57.6を採用。 これに伴いdiffusers 0.40.0→0.39.0、huggingface-hub 1.31.0→0.36.2、 tokenizers 0.23.2→0.22.2に変更された。peft 0.20.0は変更なし。 LoDAロード処理のコードは変更せず、標準APIのpipeline.load_lora_weights() をそのまま使用。モンキーパッチやキー書き換え等の互換性パッチは追加していない。 あわせて外部VAEロードの既存バグも修正: AutoencoderKL.from_single_fileがネットワークからconfig.jsonを取得 しようとして失敗する問題を、_vae_config_dir()でローカルhuggingface cache内のvae/config.jsonディレクトリを動的に見つけてconfigパラメータ に渡すことで解消。 変更内容: - pyproject.toml: transformers>=4.45 を transformers>=4.45,<5 に変更 - uv.lock: transformers 5.17.0→4.57.6, diffusers 0.40.0→0.39.0, huggingface-hub 1.31.0→0.36.2, tokenizers 0.23.2→0.22.2 - pipeline_factory.py: _vae_config_dir()メソッド追加、VAEロード時に configパラメータでローカルconfigディレクトリを指定 - tests/test_pipeline_factory.py: _vae_config_dirをモンキーパッチ、 VAE config引数の検証追加 - script/smoke_matrix.py: 10パターン網羅検証スクリプト追加 (4 checkpoint × VAE × 4種LoRA × scheduler) 検証: - ruff format/check、pyright 0 errors、pytest 61 passed - 実GPU・実モデルで10パターン全て成功(10 ok / 0 fail) - 4種checkpoint単体: 全て成功 - 外部VAE差し替え: 成功 - pixel sprites / pixelartV3 / pixel_f2: 全て成功(IndexError解消) - LCM LoRA + lcm scheduler: 成功 - animekawa + 外部VAE + pixelartV3 + euler_a: 成功
Diffstat (limited to 'src/mcp_sd15/pipeline_factory.py')
-rw-r--r--src/mcp_sd15/pipeline_factory.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/mcp_sd15/pipeline_factory.py b/src/mcp_sd15/pipeline_factory.py
index b0525ea..02fe882 100644
--- a/src/mcp_sd15/pipeline_factory.py
+++ b/src/mcp_sd15/pipeline_factory.py
@@ -38,11 +38,16 @@ class PipelineFactory:
)
vae_path = self.config.model.vae_path()
if vae_path is not None:
+ # from_single_file needs a Diffusers config directory to avoid
+ # network access. Reuse the VAE config that the base pipeline
+ # already resolved from the local huggingface cache.
+ vae_config_dir = self._vae_config_dir(cache_dir)
pipeline.vae = diffusers.AutoencoderKL.from_single_file(
str(vae_path),
torch_dtype=torch.float16,
local_files_only=True,
cache_dir=cache_dir,
+ config=vae_config_dir,
)
if getattr(pipeline, "vae", None) is None:
raise ConfigError(
@@ -156,3 +161,19 @@ class PipelineFactory:
except KeyError as exc:
raise ConfigError(f"unsupported scheduler: {name}") from exc
return scheduler_class.from_config(current.config)
+
+ @staticmethod
+ def _vae_config_dir(cache_dir: str) -> str:
+ """Find the VAE config.json inside the local huggingface cache."""
+ snapshots = (
+ Path(cache_dir) / "models--stable-diffusion-v1-5--stable-diffusion-v1-5" / "snapshots"
+ )
+ if not snapshots.is_dir():
+ raise ConfigError(
+ "cannot locate VAE config: huggingface cache snapshots directory not found"
+ )
+ for snapshot in snapshots.iterdir():
+ vae_dir = snapshot / "vae"
+ if (vae_dir / "config.json").is_file():
+ return str(vae_dir)
+ raise ConfigError("cannot locate VAE config.json in the local huggingface cache")