Skip to content

Commit ea80295

Browse files
dxqbclaudesayakpaul
authored
Fix Kohya UNet LoRA key conversion for conv_in/conv_out/time_embedding (#14006)
* Fix Kohya UNet LoRA key conversion for conv_in/conv_out/time_embedding _convert_unet_lora_key() had no mapping for these three top-level UNet submodules, so Kohya-format keys touching them (e.g. lora_unet_conv_in, lora_unet_time_embed_0/2) came out as conv.in/conv.out/time.embed.0/2 instead of conv_in/conv_out/time_embedding.linear_1/2, and were reported as unexpected keys instead of being applied. * Handle both sgm and diffusers spellings for conv/time_embedding keys The initial fix mapped conv_in/conv_out in the diffusers spelling (conv.in/ conv.out) and time_embedding in the sgm spelling (time_embed.0/.2), so neither SD1.x nor SDXL was fully covered. Add the missing spellings: - sgm conv_in/conv_out: input_blocks.0.0 / out.2 (kohya-ss SDXL sgm UNet), mapped before the block renames so input_blocks.0.0 does not become down_blocks.0.0. - diffusers time_embedding: time_embedding.linear_1/2 (kohya-ss trains SD1.x on the diffusers UNet). Verified against kohya-ss source (sdxl_original_unet.py, networks/lora.py) and the diffusers UNet module names; regression set unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Map SDXL sgm label_emb LoRA keys and pass UNet top-level modules through The conv_in/conv_out/time_embedding fix only reached _convert_unet_lora_key; for the SDXL sgm UNet those keys never got there, because _maybe_map_sgm_blocks_to_diffusers treats every non-text key as a down/mid/up block. The top-level modules that live outside that block structure (time_embed, label_emb, out = conv_out, and input_blocks.0.0 = conv_in) hit the "layer not supported" raise, or crashed the inner block-index int() parse. - Pass those top-level modules through unchanged so _convert_unet_lora_key maps them, instead of block-remapping or raising. - Map the sgm label_emb (SDXL added-conditioning MLP) to diffusers add_embedding: label_emb.0.0/0.2 -> add_embedding.linear_1/2, before the SDXL index-strip heuristic that would otherwise collapse the layer index. All additions follow the kohya/sgm naming pattern and are no-ops on real kohya-ss files (which contain none of these top-level UNet LoRA keys); verified end-to-end loading a full SDXL sgm UNet LoRA into the diffusers pipeline with no unexpected/missing adapter keys. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
1 parent 29a59fd commit ea80295

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/diffusers/loaders/lora_conversion_utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ def _maybe_map_sgm_blocks_to_diffusers(state_dict, unet_config, delimiter="_", b
7171
for layer in all_keys:
7272
if "text" in layer:
7373
new_state_dict[layer] = state_dict.pop(layer)
74+
elif not any(p in layer for p in sgm_patterns) or f"input_blocks{delimiter}0{delimiter}0" in layer:
75+
# SDXL's sgm UNet has modules outside the input/middle/output block structure that
76+
# _convert_unet_lora_key maps directly: time_embed, label_emb, out (out.2 = conv_out)
77+
# and input_blocks.0.0 (= conv_in). Pass these through instead of block-remapping
78+
# (conv_in's input_blocks.0 would otherwise be parsed as a down-block) or raising.
79+
new_state_dict[layer] = state_dict.pop(layer)
7480
else:
7581
layer_id = int(layer.split(delimiter)[:block_slice_pos][-1])
7682
if sgm_patterns[0] in layer:
@@ -263,6 +269,12 @@ def _convert_unet_lora_key(key):
263269
"""
264270
diffusers_name = key.replace("lora_unet_", "").replace("_", ".")
265271

272+
# kohya-ss trains SDXL on its own sgm/LDM UNet, so conv_in / conv_out arrive as
273+
# input_blocks.0.0 / out.2. Map these before the block renames below, otherwise
274+
# input_blocks.0.0 would become down_blocks.0.0 instead of conv_in.
275+
diffusers_name = diffusers_name.replace("input.blocks.0.0", "conv_in")
276+
diffusers_name = diffusers_name.replace("out.2", "conv_out")
277+
266278
# Replace common U-Net naming patterns.
267279
diffusers_name = diffusers_name.replace("input.blocks", "down_blocks")
268280
diffusers_name = diffusers_name.replace("down.blocks", "down_blocks")
@@ -278,6 +290,19 @@ def _convert_unet_lora_key(key):
278290
diffusers_name = diffusers_name.replace("proj.in", "proj_in")
279291
diffusers_name = diffusers_name.replace("proj.out", "proj_out")
280292
diffusers_name = diffusers_name.replace("emb.layers", "time_emb_proj")
293+
diffusers_name = diffusers_name.replace("conv.in", "conv_in")
294+
diffusers_name = diffusers_name.replace("conv.out", "conv_out")
295+
diffusers_name = diffusers_name.replace("time.embed.0", "time_embedding.linear_1")
296+
diffusers_name = diffusers_name.replace("time.embed.2", "time_embedding.linear_2")
297+
# sgm label_emb (SDXL added-conditioning MLP) -> diffusers add_embedding. Map before the
298+
# SDXL index-strip heuristic below, which would otherwise collapse the layer index.
299+
diffusers_name = diffusers_name.replace("label.emb.0.0", "add_embedding.linear_1")
300+
diffusers_name = diffusers_name.replace("label.emb.0.2", "add_embedding.linear_2")
301+
# kohya-ss trains SD 1.x on the diffusers UNet (not the sgm UNet it uses for SDXL),
302+
# so the time-embedding MLP keeps the diffusers spelling time_embedding.linear_N
303+
# rather than the sgm time_embed.N handled above.
304+
diffusers_name = diffusers_name.replace("time.embedding.linear.1", "time_embedding.linear_1")
305+
diffusers_name = diffusers_name.replace("time.embedding.linear.2", "time_embedding.linear_2")
281306

282307
# SDXL specific conversions.
283308
if "emb" in diffusers_name and "time.emb.proj" not in diffusers_name:

0 commit comments

Comments
 (0)