Skip to content

(#2990 continued) LoRA state dict key naming cleanup - #2993

Merged
bghira merged 2 commits into
mainfrom
pr/2990
Aug 3, 2026
Merged

(#2990 continued) LoRA state dict key naming cleanup#2993
bghira merged 2 commits into
mainfrom
pr/2990

Conversation

@bghira

@bghira bghira commented Aug 3, 2026

Copy link
Copy Markdown
Owner

This pull request makes several important improvements to LoRA (Low-Rank Adaptation) format handling, conversion, and testing. The main focus is on standardizing the naming conventions for LoRA weights, ensuring compatibility between PEFT and Diffusers formats, and adding comprehensive tests to guarantee correct conversions and saves. It also introduces new test utilities and fixes to ensure consistent behavior when saving and loading LoRA weights.

LoRA Format Handling and Conversion:

  • The detection logic in detect_state_dict_format was simplified by removing redundant checks for mixed or ambiguous LoRA naming schemes, making format detection more robust.
  • The convert_diffusers_to_comfyui function was refactored to consistently map .lora.down. to .lora_A. and .lora.up. to .lora_B., and to ensure that alpha values are attached correctly. Legacy handling for mixed naming conventions was removed.

Saving LoRA Weights:

  • The LoRA saving logic in _save_lora was updated to always use the PEFT naming scheme (i.e., .lora_A.weight and .lora_B.weight) when saving LoRA weights, instead of converting to the Diffusers format. This prevents mixed or ambiguous naming in saved checkpoints. [1] [2]

Testing and Test Utilities:

  • A comprehensive new test suite (tests/test_lora_format.py) was added to verify that LoRA format conversions are correct, that both PEFT and Diffusers spellings are handled consistently, and that saved weights maintain their intended structure and can be loaded as expected.
  • Additional test helpers and dummy classes were added or improved in tests/test_lora_metadata.py to facilitate testing of save hooks and ensure that LoRA weights are always written and loaded in a consistent, compatible manner. [1] [2] [3]

Regression and Compatibility Tests:

  • New tests were added to guarantee that the save hook never writes a mixed naming scheme, that PEFT-named weights are accepted by Diffusers loaders, and that legacy mixed checkpoints are normalized for resume.

These changes improve reliability and interoperability when training, saving, and loading LoRA weights across different toolkits and workflows.

hjinnkim and others added 2 commits August 4, 2026 00:16
_save_lora passed the denoiser adapter through diffusers'
convert_state_dict_to_diffusers(), whose PEFT_TO_DIFFUSERS table only
renames to_q/to_k/to_v/to_out.0. Any target outside attention kept its
PEFT name, so a wide-target checkpoint was written with two naming
schemes in one file. diffusers decides whether to convert back from a
single key, guesses wrong on such a file, and silently drops every
diffusers-named tensor.

Write get_peft_model_state_dict() verbatim instead, matching the
controlnet branch five lines above and the final export in trainer.py.

convert_diffusers_to_comfyui() emitted .alpha only inside its
.lora.down. branch, so it must normalise both spellings before deriving
alpha or ComfyUI exports would lose every alpha once checkpoints are
PEFT-named.
@bghira
bghira merged commit b21a141 into main Aug 3, 2026
2 checks passed
@bghira
bghira deleted the pr/2990 branch August 3, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants