Skip to content

fix: raise informative ValueError on architecture mismatch when loading LoRA weights (SD 1.5 into SDXL)#13938

Open
Liauuu wants to merge 1 commit into
huggingface:mainfrom
Liauuu:main
Open

fix: raise informative ValueError on architecture mismatch when loading LoRA weights (SD 1.5 into SDXL)#13938
Liauuu wants to merge 1 commit into
huggingface:mainfrom
Liauuu:main

Conversation

@Liauuu

@Liauuu Liauuu commented Jun 13, 2026

Copy link
Copy Markdown

Description

This PR addresses and fixes Issue #11286, where loading an incompatible, older architecture LoRA (e.g., Stable Diffusion 1.5) into a newer pipeline (e.g., Stable Diffusion XL) triggers an unhandled RuntimeError: size mismatch deep within the tensor operation layers.

Instead of allowing the program to mutably inject adapter shells and crash abruptly midway through tensor addition, this fix introduces a proactive, defensive dimension validation step.

Key Changes

  1. Added _validate_lora_weight_compatibility helper in src/diffusers/utils/peft_utils.py:
    • Inspects the incoming state_dict keys (lora_A and lora_B weights).
    • Dynamically fetches target submodules using model.get_submodule().
    • Cross-references incoming tensor shapes and ranks against expected base layer dimensions (in_features/in_channels and ndim).
  2. Injected Validation Gateways:
    • Integrated inside PeftAdapterMixin.load_lora_adapter (src/diffusers/loaders/peft.py) before PEFT commits mutations via inject_adapter_in_model.
    • Integrated inside lora_base.py for text encoder pathways before executing text_encoder.load_adapter.
  3. Informative Error Reporting:
    • Throws a clean, informative ValueError mapping out the precise architecture discrepancy (e.g., 4D Conv LoRA vs 2D Linear layers, or 768-dim context vs 2048-dim target dimensions) so users instantly recognize version mismatches.

Testing

  • Added a full automated regression test suite inside tests/lora/test_lora_layers_sdxl.py: test_load_sd15_lora_into_sdxl_raises_incompatibility_error.
  • The test generates a synthetic, cross-architecture incompatible state_dict and asserts the exact ValueError criteria and error strings.
  • Local verification status: PASSED [100%] (Ran locally using pytest).

Fixes #11286

@github-actions github-actions Bot added fixes-issue size/M PR with diff < 200 LOC lora tests utils and removed size/M PR with diff < 200 LOC labels Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error while loading Lora

1 participant