
    bi                     H   d dl Z d dlZd dlmZ d dlmZ dddddd	d
dddddddddZddddZd Zd Z	e
dk(  rk e j                  d      Zej                  dedd        ej                  d!edd"        ej                         Z e	ej"                  ej$                         yy)#    N)	load_file)Kandinsky3UNetztime_embedding.linear_1ztime_embedding.linear_2conv_inconv_norm_outconv_outdown_blocks	up_blocksz"encoder_hid_proj.projection_linearz encoder_hid_proj.projection_normadd_time_conditionto_qto_kto_vzto_out.0zattentions.0)zto_time_embed.1zto_time_embed.3in_layerzout_layer.0zout_layer.2down_samples
up_samplesprojection_linprojection_lnfeature_poolingto_queryto_keyto_valueoutput_layerself_attention_blockzresnets_in.*)zattentions.*   zresnets_out.*)zresnet_attn_blocks.*.0zresnet_attn_blocks.*.1zresnet_attn_blocks.*.2c           	      Z   i }| D ]"  }|}t         j                         D ]  \  }}|j                  ||      } t        j                         D ]  \  }}d}t	        j                  |d| d      s#|r&t        |j                  |j                  d      d         d   j                  d      d         }	t        |t              r|	|d   z   }
|d   }n|	}
|j                  dt        |	            }|j                  dt        |
            }|j                  ||      }d	} | |   ||<   % |S )
aR  
    Args:
    Convert the state dict of a U-Net model to match the key format expected by Kandinsky3UNet model.
        unet_model (torch.nn.Module): The original U-Net model. unet_kandi3_model (torch.nn.Module): The Kandinsky3UNet
        model to match keys with.

    Returns:
        OrderedDict: The converted state dictionary.
    Fz*.z.*.r   r   *T)
MAPPINGitemsreplaceDYNAMIC_MAPfnmatchintsplit
isinstancetuplestr)unet_state_dictconverted_state_dictkeynew_keypatternnew_patterndyn_patterndyn_new_patternhas_matchedstarnew_stars              q/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/pipelines/kandinsky3/convert_kandinsky3_unet.pyconvert_state_dictr4   $   sA     =$+MMO 	< G[oog{;G	< -8,=,=,? 	#(KKw"[M(<=k7==):):3)?)BCBGMMcRSTUVou5#ob&99H&5a&8O#H%--c3t9=-55c3x=I!//';?"	#" )8(<W%-=0      c                     t        |       }i }t        |      }t        |      }|j                  |       |j	                  |       t        d|        y )NzConverted model saved to )r   r4   r   load_state_dictsave_pretrainedprint)
model_pathoutput_pathr(   configr)   unets         r3   mainr>   K   sW    
+O F .o>&!D-.%	%k]
34r5   __main__z4Convert U-Net PyTorch model to Kandinsky3UNet format)descriptionz--model_pathTz(Path to the original U-Net PyTorch model)typerequiredhelpz--output_pathz Path to save the converted model)argparser"   safetensors.torchr   	diffusersr   r   r!   r4   r>   __name__ArgumentParserparseradd_argumentr'   
parse_argsargsr:   r;    r5   r3   <module>rN      s      ' $ 10"!:7+*& -1-$ N5" z$X$$1ghF
S4Fpq
cDGijD$**+ r5   