
    +h                     8   S SK r S SKrS SKJr  S SKJr  SSSSSS	S
SSSSSSSSS.rSSSS.rS rS r	\
S:X  ab  \ R                  " SS9r\R                  S\SSS 9  \R                  S!\SS"S 9  \R                  5       r\	" \R"                  \R$                  5        gg)#    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           	      |   0 nU  GH2  nUn[         R                  5        H  u  pEUR                  XE5      nM     [        R                  5        H  u  pgSn[        R                  " USU S35      (       d  M)  U(       a  M2  [        UR                  UR                  S5      S   5      S   R                  S5      S   5      n	[        U[        5      (       a  XS   -   n
US   nOU	n
UR                  S[        U	5      5      nUR                  S[        U
5      5      nUR                  XE5      nS	nM     X   X'   GM5     U$ )
a6  
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              p/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/kandinsky3/convert_kandinsky3_unet.pyconvert_state_dictr4   $   s-    $+MMO Goog;G %4 -8,=,=,?(KKw"[M(<==kk7==):):3)?)BCBGMMcRSTUVou55#b&99H&5a&8O#H%--c3t9=-55c3x=I!//'?" -@" )8(<%- 0      c                     [        U 5      n0 n[        U5      n[        U5      nUR                  U5        UR	                  U5        [        SU 35        g )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$$1ghF
S4Fpq
cDGijD$**+ r5   