
    
3j                    @	   S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSK
r
SSKrSSKrSSKJr  SSKJrJrJrJrJrJrJrJr  SS	KJrJrJrJrJrJr  SS
KJ r   SSK!J"r"  SSK#J$r$  \" 5       (       a  SSK%J&r&  \" 5       (       a  SSK'J(r(  SSK)J*r*  \RV                  " \,5      r-0 SS_SS_SS_SS_SS_SSS/_SS_SS _S!S"_S#S$_S%S&_S'S(_S)S*_S+S,_S-S._S/S0_S1S2_0 S3S4_S5S6_S7S8_S9S:S;/_S<S=S>/_S?S@_SASB_SCSD_SES_SFSG_SH/ SIQ_SJSKSL/_SM/ SNQ_SOSP_SQSR_SSSTSU/_SVSW_ES&SXSY/SZS[/S\S]/ S^QS_S`/SaSbScSd/ SeQ/ SfQSgSh// SiQSj.Er.0 SSkSl0_SSkSm0_SnSkSo0_S#SkSp0_SSkSq0_S%SkSr0_SsSkSt0_SSkSu0_SSkSv0_S!SkSw0_SxSkSy0_SSkSz0_SSkS{0_S5S|S}S~._SS|SS~._S7SSS~._SSSS~._0 S9SkS0_S<SkS0_SSkS0_SSkS0_SASkS0_SSkS0_SCSkS0_SESkS0_SFSkS0_SHSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SMSkS0_SSkS0_E0 SSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SSSkS0_SVSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SSkS0_SSkS0_ESkS0SkS0SkS0SkS0SkS0SkS0SkS0SkS0SkS0SkS0SkS0SkS0SkS0SkS0S.Er/SSSSSSSSSSSS.r0SSSSSSSSSSS.
SSSSS.SSSSS.S.SSSSSSSSSSS.
SSSSS.SSSSS.S.0 SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SSSGS GSGS.GSGSGSGSGSGSGS	SSGS
.	GS.GS.r1/ GSQr2GSGSGSGSGSGSGSGSGSGSGSGS.r3GSGS/r4GSr5GSr6GSr7GSr8GS GS!/r9Sr:GS"GS#/r;/ GS$Qr< " GS% GS&\=5      r>GS' r?GS( r@GS) rAGS* rBGS+ rC        GShGS, jrDGSiGS- jrEGS. rFGS/ rGGS0 rHGS1 rIGS2 rJGS3 rKGS4 rLGS5 rMGS6 rNGS7 rOGSjGS8 jrPGS9 rQ GSkGS: jrRGSjGS; jrSGSlGS< jrTGSjGS= jrUGS> rVGS? rWGS@ rXGSA rYGSiGSB jrZGSC r[GSD r\GSjGSE jr] GSmGSF jr^     GSnGSG jr_ GSjGSH jr`GSoGSI jraGSJ rbGSK rcGSL rdGSM reGSN rfGSO rgGSP rhGSQ ri    GSpGSR jrjGSS rkGST rlGSU rmGSV rnGSW roGSX rpGSY rqGSZ rrGS[ rsGS\ rtGS] ruGS^ rvGS_ rwGS` rxGSa ryGSb rzGSc r{GSd r|GSe r}GSf r~GSg rg(q  z7Conversion script for the Stable Diffusion checkpoints.    N)nullcontext)BytesIO)urlparse   )load_state_dict)DDIMSchedulerDPMSolverMultistepSchedulerEDMDPMSolverMultistepSchedulerEulerAncestralDiscreteSchedulerEulerDiscreteSchedulerHeunDiscreteSchedulerLMSDiscreteSchedulerPNDMScheduler)SAFETENSORS_WEIGHTS_NAMEWEIGHTS_NAME	deprecateis_accelerate_availableis_transformers_availablelogging)DIFFUSERS_REQUEST_TIMEOUT)_get_model_file)empty_device_cache)AutoImageProcessor)init_empty_weights)load_model_dict_into_metav1z?model.diffusion_model.output_blocks.11.0.skip_connection.weightv2zMmodel.diffusion_model.input_blocks.2.1.transformer_blocks.0.attn2.to_k.weightxl_basezEconditioner.embedders.1.model.transformer.resblocks.9.mlp.c_proj.bias
xl_refinerzEconditioner.embedders.0.model.transformer.resblocks.9.mlp.c_proj.biasupscalez<model.diffusion_model.input_blocks.10.0.skip_connection.bias
controlnetz!control_model.time_embed.0.weight(controlnet_cond_embedding.conv_in.weightcontrolnet_xladd_embedding.linear_1.weightcontrolnet_xl_largezAdown_blocks.1.attentions.0.transformer_blocks.0.attn1.to_k.weightcontrolnet_xl_midz&down_blocks.1.attentions.0.norm.weightplayground-v2-5edm_mean
inpaintingz-model.diffusion_model.input_blocks.0.0.weightclipzLcond_stage_model.transformer.text_model.embeddings.position_embedding.weight	clip_sdxlzSconditioner.embedders.0.transformer.text_model.embeddings.position_embedding.weightclip_sd3zPtext_encoders.clip_l.transformer.text_model.embeddings.position_embedding.weight	open_clipz-cond_stage_model.model.token_embedding.weightopen_clip_sdxlz2conditioner.embedders.1.model.positional_embeddingopen_clip_sdxl_refinerz-conditioner.embedders.0.model.text_projectionopen_clip_sd3zPtext_encoders.clip_g.transformer.text_model.embeddings.position_embedding.weightstable_cascade_stage_bz$down_blocks.1.0.channelwise.0.weightstable_cascade_stage_cclip_txt_mapper.weightsd3z4joint_blocks.0.context_block.adaLN_modulation.1.biaszJmodel.diffusion_model.joint_blocks.0.context_block.adaLN_modulation.1.bias
sd35_largez&joint_blocks.37.x_block.mlp.fc1.weightz<model.diffusion_model.joint_blocks.37.x_block.mlp.fc1.weightanimatediffzjdown_blocks.0.motion_modules.0.temporal_transformer.transformer_blocks.0.attention_blocks.0.pos_encoder.peanimatediff_v2z9mid_block.motion_modules.0.temporal_transformer.norm.biasanimatediff_sdxl_betaz=up_blocks.2.motion_modules.0.temporal_transformer.norm.weightanimatediff_scribbleanimatediff_rgbz controlnet_cond_embedding.weightauraflow)zdouble_layers.0.attn.w2q.weightzdouble_layers.0.attn.w1q.weightcond_seq_linear.weightt_embedder.mlp.0.weightfluxz,double_blocks.0.img_attn.norm.key_norm.scalezBmodel.diffusion_model.double_blocks.0.img_attn.norm.key_norm.scale	ltx-video)z*model.diffusion_model.patchify_proj.weightz=model.diffusion_model.transformer_blocks.27.scale_shift_tablezpatchify_proj.weightz'transformer_blocks.27.scale_shift_tablez"vae.decoder.last_scale_shift_tablez6vae.decoder.up_blocks.9.res_blocks.0.conv1.conv.weightautoencoder-dcz.decoder.stages.1.op_list.0.main.conv.conv.biasautoencoder-dc-sanaencoder.project_in.conv.biasmochi-1-previewz0model.diffusion_model.blocks.0.attn.qkv_x.weightzblocks.0.attn.qkv_x.weighthunyuan-videoz@txt_in.individual_token_refiner.blocks.0.adaLN_modulation.1.biasz+model.diffusion_model.cap_embedder.0.weightzcap_embedder.0.weightz8model.diffusion_model.layers.0.adaLN_modulation.0.weightz"layers.0.adaLN_modulation.0.weightz!control_all_x_embedder.2-1.weightz+control_layers.14.adaLN_modulation.0.weight)z#blocks.0.cross_attn.q_linear.weightz!blocks.0.cross_attn.q_linear.biasz$blocks.0.cross_attn.kv_linear.weightz"blocks.0.cross_attn.kv_linear.biasz%model.diffusion_model.head.modulationhead.modulation!decoder.middle.0.residual.0.gammazvace_blocks.0.after_proj.bias#motion_encoder.dec.direction.weightz4double_stream_blocks.0.block.adaLN_modulation.1.bias)net.x_embedder.proj.1.weight3net.blocks.block1.blocks.0.block.attn.to_q.0.weightz net.extra_pos_embedder.pos_emb_h)rH   z$net.blocks.0.self_attn.q_proj.weightz"net.pos_embedder.dim_spatial_rangez9model.diffusion_model.single_stream_modulation.lin.weightz#single_stream_modulation.lin.weight)zWmodel.diffusion_model.av_ca_a2v_gate_adaln_single.emb.timestep_embedder.linear_1.weightz(vae.per_channel_statistics.mean-of-meansz.audio_vae.per_channel_statistics.mean-of-means)instruct-pix2pixlumina2z-image-turboz-image-turbo-controlnetz-image-turbo-controlnet-2.xsanawanwan_vaewan_vacewan_animatehidream
cosmos-1.0
cosmos-2.0flux2ltx2pretrained_model_name_or_pathz(stabilityai/stable-diffusion-xl-base-1.0z+stabilityai/stable-diffusion-xl-refiner-1.0
xl_inpaintz0diffusers/stable-diffusion-xl-1.0-inpainting-0.1z-playgroundai/playground-v2.5-1024px-aestheticz(stabilityai/stable-diffusion-x4-upscalerz1stable-diffusion-v1-5/stable-diffusion-inpaintinginpainting_v2z)stabilityai/stable-diffusion-2-inpaintingz"lllyasviel/control_v11p_sd15_cannyz#diffusers/controlnet-canny-sdxl-1.0z'diffusers/controlnet-canny-sdxl-1.0-midcontrolnet_xl_smallz)diffusers/controlnet-canny-sdxl-1.0-smallz stabilityai/stable-diffusion-2-1z+stable-diffusion-v1-5/stable-diffusion-v1-5zstabilityai/stable-cascadedecoder)rY   	subfolderstable_cascade_stage_b_litedecoder_litez stabilityai/stable-cascade-priorpriorstable_cascade_stage_c_lite
prior_litez/stabilityai/stable-diffusion-3-medium-diffusersz&stabilityai/stable-diffusion-3.5-largesd35_mediumz'stabilityai/stable-diffusion-3.5-mediumanimatediff_v1z&guoyww/animatediff-motion-adapter-v1-5z(guoyww/animatediff-motion-adapter-v1-5-2animatediff_v3z(guoyww/animatediff-motion-adapter-v1-5-3z+guoyww/animatediff-motion-adapter-sdxl-betaz&guoyww/animatediff-sparsectrl-scribblez!guoyww/animatediff-sparsectrl-rgbzfal/AuraFlow-v0.3flux-devzblack-forest-labs/FLUX.1-dev	flux-fillz!black-forest-labs/FLUX.1-Fill-dev
flux-depthz"black-forest-labs/FLUX.1-Depth-devflux-schnellz black-forest-labs/FLUX.1-schnell
flux-2-devzblack-forest-labs/FLUX.2-devzdiffusers/LTX-Video-0.9.0ltx-video-0.9.1zdiffusers/LTX-Video-0.9.1ltx-video-0.9.5zLightricks/LTX-Video-0.9.5ltx-video-0.9.7zLightricks/LTX-Video-0.9.7-devautoencoder-dc-f128c512z,mit-han-lab/dc-ae-f128c512-mix-1.0-diffusersautoencoder-dc-f64c128z+mit-han-lab/dc-ae-f64c128-mix-1.0-diffusersautoencoder-dc-f32c32z*mit-han-lab/dc-ae-f32c32-mix-1.0-diffusersautoencoder-dc-f32c32-sanaz+mit-han-lab/dc-ae-f32c32-sana-1.0-diffuserszgenmo/mochi-1-previewz#hunyuanvideo-community/HunyuanVideorJ   ztimbrooks/instruct-pix2pixrK   zAlpha-VLLM/Lumina-Image-2.0rO   z1Efficient-Large-Model/Sana_1600M_1024px_diffuserswan-t2v-1.3Bz Wan-AI/Wan2.1-T2V-1.3B-Diffuserswan-t2v-14BzWan-AI/Wan2.1-T2V-14B-Diffuserswan-i2v-14Bz$Wan-AI/Wan2.1-I2V-14B-480P-Diffuserswan-animate-14Bz#Wan-AI/Wan2.2-Animate-14B-Diffuserswan-vace-1.3Bz!Wan-AI/Wan2.1-VACE-1.3B-diffuserswan-vace-14Bz Wan-AI/Wan2.1-VACE-14B-diffuserszHiDream-ai/HiDream-I1-Devz)nvidia/Cosmos-1.0-Diffusion-7B-Text2Worldz*nvidia/Cosmos-1.0-Diffusion-14B-Text2Worldz*nvidia/Cosmos-1.0-Diffusion-7B-Video2Worldz+nvidia/Cosmos-1.0-Diffusion-14B-Video2Worldz$nvidia/Cosmos-Predict2-2B-Text2Imagez%nvidia/Cosmos-Predict2-14B-Text2Imagez%nvidia/Cosmos-Predict2-2B-Video2Worldz&nvidia/Cosmos-Predict2-14B-Video2WorldzTongyi-MAI/Z-Image-Turboz'hlky/Z-Image-Turbo-Fun-Controlnet-Unionz+hlky/Z-Image-Turbo-Fun-Controlnet-Union-2.0z+hlky/Z-Image-Turbo-Fun-Controlnet-Union-2.1zLightricks/LTX-2)rT   cosmos-1.0-t2w-7Bcosmos-1.0-t2w-14Bcosmos-1.0-v2w-7Bcosmos-1.0-v2w-14Bcosmos-2.0-t2i-2Bcosmos-2.0-t2i-14Bcosmos-2.0-v2w-2Bcosmos-2.0-v2w-14BrL   rM   z-image-turbo-controlnet-2.0z-image-turbo-controlnet-2.1ltx2-dev   i      )r   r   rZ   r'   r    r)   r[   r!   rJ   r   r   time_embed.0.weightztime_embed.0.biasztime_embed.2.weightztime_embed.2.biaszinput_blocks.0.0.weightzinput_blocks.0.0.biaszout.0.weightz
out.0.biaszout.2.weightz
out.2.bias)
time_embedding.linear_1.weighttime_embedding.linear_1.biastime_embedding.linear_2.weighttime_embedding.linear_2.biasconv_in.weightconv_in.biaszconv_norm_out.weightzconv_norm_out.biaszconv_out.weightzconv_out.biaszlabel_emb.0.0.weightzlabel_emb.0.0.biaszlabel_emb.0.2.weightzlabel_emb.0.2.bias)zclass_embedding.linear_1.weightzclass_embedding.linear_1.biaszclass_embedding.linear_2.weightzclass_embedding.linear_2.bias)r$   zadd_embedding.linear_1.biaszadd_embedding.linear_2.weightzadd_embedding.linear_2.bias)layersclass_embed_typeaddition_embed_typezinput_hint_block.0.weightzinput_hint_block.0.biaszinput_hint_block.14.weightzinput_hint_block.14.bias)
r   r   r   r   r   r   r"   z&controlnet_cond_embedding.conv_in.biasz)controlnet_cond_embedding.conv_out.weightz'controlnet_cond_embedding.conv_out.biasencoder.conv_in.weightencoder.conv_in.biasencoder.conv_out.weightencoder.conv_out.biaszencoder.conv_norm_out.weightzencoder.norm_out.weightzencoder.conv_norm_out.biaszencoder.norm_out.biasdecoder.conv_in.weightdecoder.conv_in.biasdecoder.conv_out.weightdecoder.conv_out.biaszdecoder.conv_norm_out.weightzdecoder.norm_out.weightzdecoder.conv_norm_out.biaszdecoder.norm_out.biasquant_conv.weightquant_conv.biaspost_quant_conv.weightpost_quant_conv.biaspositional_embeddingztoken_embedding.weightzln_final.weightzln_final.biastext_projection)z/text_model.embeddings.position_embedding.weightz,text_model.embeddings.token_embedding.weightz"text_model.final_layer_norm.weightz text_model.final_layer_norm.biastext_projection.weightz
resblocks.ln_1ln_2z.c_fc.z.c_proj.z.attnz	ln_final.)	ztext_model.encoder.layers.layer_norm1layer_norm2z.fc1.z.fc2.z
.self_attnz(transformer.text_model.final_layer_norm.z8transformer.text_model.embeddings.token_embedding.weightz;transformer.text_model.embeddings.position_embedding.weight)r   transformer)unetr!   vaeopenclip)zAcond_stage_model.model.transformer.resblocks.23.attn.in_proj_biaszCcond_stage_model.model.transformer.resblocks.23.attn.in_proj_weightzBcond_stage_model.model.transformer.resblocks.23.attn.out_proj.biaszDcond_stage_model.model.transformer.resblocks.23.attn.out_proj.weightz9cond_stage_model.model.transformer.resblocks.23.ln_1.biasz;cond_stage_model.model.transformer.resblocks.23.ln_1.weightz9cond_stage_model.model.transformer.resblocks.23.ln_2.biasz;cond_stage_model.model.transformer.resblocks.23.ln_2.weightz=cond_stage_model.model.transformer.resblocks.23.mlp.c_fc.biasz?cond_stage_model.model.transformer.resblocks.23.mlp.c_fc.weightz?cond_stage_model.model.transformer.resblocks.23.mlp.c_proj.biaszAcond_stage_model.model.transformer.resblocks.23.mlp.c_proj.weightz&cond_stage_model.model.text_projectionscaled_linearg_QK?g~jt?linear  epsilon      ?FT   leading)beta_schedule
beta_startbeta_endinterpolation_typenum_train_timestepsprediction_typesample_max_valueset_alpha_to_oneskip_prk_stepssteps_offsettimestep_spacingzfirst_stage_model.vae.g{P?      ?model.diffusion_model.zcontrol_model.zcond_stage_model.transformer.z$conditioner.embedders.0.transformer.r   scheduler_type)zhttps://huggingface.co/zhuggingface.co/zhf.co/zhttps://hf.co/c                   ,   ^  \ rS rSrSU 4S jjrSrU =r$ )SingleFileComponentErrori  c                 D   > Xl         [        TU ]	  U R                   5        g N)messagesuper__init__)selfr   	__class__s     ]/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/loaders/single_file_utils.pyr   !SingleFileComponentError.__init__  s    &    )r   r   )__name__
__module____qualname____firstlineno__r   __static_attributes____classcell__)r   s   @r   r   r     s    ' 'r   r   c                 `    [        U 5      nUR                  (       a  UR                  (       a  ggNTF)r   schemenetloc)urlresults     r   is_valid_urlr     s    c]F}}r   c                     [         R                  R                  U 5      (       a  g[        U 5      (       d  g[	        U 5      u  p[        U=(       a    U5      $ r   )ospathisfiler   !_extract_repo_id_and_weights_namebool)rY   repo_idweight_names      r   _is_single_file_path_or_urlr     sF    	ww~~344566<=Z[G'K((r   c                 4   [        U 5      (       d  [        S5      eSnS nSn[         H  nU R                  US5      n M     [        R
                  " X5      nU(       d  X24$ UR                  S5       SUR                  S5       3nUR                  S5      nX24$ )	NzOInvalid `pretrained_model_name_or_path` provided. Please set it to a valid URL.z#([^/]+)/([^/]+)/(?:blob/main/)?(.+)r    r   /r      )r   
ValueErrorVALID_URL_PREFIXESreplacerematchgroup)rY   patternweights_namer   prefixr   s         r   r   r     s    566jkk4GLG$(E(M(MfVX(Y% %HHW<E$$Q %++a.!12G;;q>L  r   c                     [         R                  R                  X5      nSn[        [        4 HH  n[         R                  R                  [         R                  R                  X$5      5      (       d  MF  SnMJ     U$ )NFT)r   r   joinr   r   r   )cached_foldernamerY   weights_existr   s        r   "_is_model_weights_in_cached_folderr     sY    $&GGLL$E!M%'?@77>>"'',,'DSTT M A r   c                 B    [        S U R                  5        5       5      $ )Nc              3   2   #    U  H  o[         ;   v   M     g 7fr   )SCHEDULER_LEGACY_KWARGS.0ks     r   	<genexpr>._is_legacy_scheduler_kwargs.<locals>.<genexpr>  s     C]++]s   )anykeys)kwargss    r   _is_legacy_scheduler_kwargsr     s    CV[[]CCCr   c	                     Uc  SSS.n[         R                  R                  U 5      (       a  U n O[        U 5      u  p[	        U	U
UUUUUUUS9	n [        XS9nSU;   a  US   nSU;   a  M  U$ )Nsingle_filepytorch)	file_type	framework)r   force_download	cache_dirproxieslocal_files_onlytokenrevision
user_agent)disable_mmap
state_dict)r   r   r   r   r   r   )pretrained_model_link_or_pathr  r  r  r  r  r  r
  r	  r   r   
checkpoints               r   load_single_file_checkpointr    s     #0yI
	ww~~344(E% !BB_ `(7%)-!
)
% !!>ZJ *
$-
 *
$ r   c                    [         R                  R                  U 5      (       a&  [        U S5       nUR	                  5       n S S S 5        OZ[        U 5      (       a?  U(       a  [        S5      e[        [        R                  " U [        S9R                  5      n O[        S5      e[        R                  " U 5      nU$ ! , (       d  f       N&= f)Nrz|`local_files_only` is set to True, but a URL was provided as `original_config_file`. Please provide a valid local file path.)timeoutzSInvalid `original_config_file` provided. Please set it to a valid file path or URL.)r   r   r   openreadr   r   r   requestsgetr   contentyaml	safe_load)original_config_filer  fporiginal_configs       r   fetch_original_configr    s    	ww~~*++&,#%779  -, 
*	+	+: 
  'x||4HRk'l't'tu noonn%9:O# -,s   B==
Cc                      [         S   U ;   a  gg)Nr*   TFCHECKPOINT_KEY_NAMESr  s    r   is_clip_modelr!    s    F#z1r   c                      [         S   U ;   a  gg)Nr+   TFr  r   s    r   is_clip_sdxl_modelr#        K(J6r   c                      [         S   U ;   a  gg)Nr,   TFr  r   s    r   is_clip_sd3_modelr&    s    J':5r   c                      [         S   U ;   a  gg)Nr-   TFr  r   s    r   is_open_clip_modelr(    r$  r   c                      [         S   U ;   a  gg)Nr.   TFr  r   s    r   is_open_clip_sdxl_modelr*  "  s    ,-;r   c                      [         S   U ;   a  gg)Nr0   TFr  r   s    r   is_open_clip_sd3_modelr,  )  s    O,
:r   c                      [         S   U ;   a  gg)Nr/   TFr  r   s    r   is_open_clip_sdxl_refiner_modelr.  0  s    45Cr   c           
          [        [        U5      [        U5      [        U5      [	        U5      [        U5      [        U5      /5      nU R                  S:X  d  U R                  S:X  a  U(       a  gg)NCLIPTextModelCLIPTextModelWithProjectionTF)r   r!  r&  r(  r*  r.  r,  r   )	class_objr  is_clip_in_checkpoints      r   is_clip_model_in_single_filer4  7  si    *%j)z*#J/+J7":.	
	 	o-1C1CGd1d
r   c                 D  ^  [         S   T ;   aa  T [         S      R                  S   S:X  aD  [         S   T ;   a!  T [         S      R                  S   S:X  a  SnU$ [         S   T ;   a  S	nU$ Sn U$ [         S   T ;   a!  T [         S      R                  S   S:X  a  SnU$ [         S
   T ;   a  S
nU$ [         S   T ;   a  SnU$ [         S   T ;   a  SnU$ [         S   T ;   a  SnU$ [        U 4S j[         S    5       5      (       a9  [         S   T ;   a'  [         S   T ;   a  SnU$ [         S   T ;   a  SnU$ Sn U$ Sn U$ [         S   T ;   a!  T [         S      R                  S   S:X  a  SnU$ [         S   T ;   a!  T [         S      R                  S   S:X  a  SnU$ [         S   T ;   a!  T [         S      R                  S   S:X  a  SnU$ [         S   T ;   a!  T [         S      R                  S   S:X  a  SnU$ [        U 4S j[         S    5       5      (       a`  [        U 4S j[         S    5       5      (       a?  ST ;   a  SnOS nT U   R                  S   S!:X  a  SnU$ T U   R                  S   S":X  a  S#nW$ [        U 4S$ j[         S%    5       5      (       a  S%nU$ [         S&   T ;   az  [         S'   T ;   a  S'nU$ [         S(   T ;   a  S(nU$ [         S)   T ;   a  S)nU$ T [         S*      R                  S   S+:X  a  S*nU$ T [         S&      R                  S   S,:X  a  S-nU$ S.n U$ [        U 4S/ j[         S0    5       5      (       a  S1nU$ [        U 4S2 j[         S3    5       5      (       ac  [        U 4S4 jS5 5       5      (       aD  S6T ;   a  S6nOS7nT U   R                  S   S8:X  a  S9nU$ T U   R                  S   S::X  a  S;nU$ S<n U$ S=n U$ [        U 4S> j[         S?    5       5      (       aP  S@T ;   n[        SA T  5       5      (       a  SBnU$ U(       a  T SC   R                  S   S:X  a  SDnU$ SET ;   a  SFnU$ S?n U$ [         SG   T ;   az  SHnSIn[         SJ   T ;   a  SKnU$ T U   R                  S   SL:X  a  T U   R                  S   SM:X  a  SNnU$ T U   R                  S   SL:X  a  T U   R                  S   S::X  a  SOnU$ SPn U$ [        U 4SQ j[         SR    5       5      (       a  SRnU$ [         SS   T ;   a  SSnU$ [        U 4ST j[         SU    5       5      (       a  SUnU$ [         SV   T ;   a!  T [         SV      R                  S   SW:X  a  SVnU$ [        U 4SX j[         SY    5       5      (       a  SYnU$ [        U 4SZ j[         S[    5       5      (       a  S[nU$ [        U 4S\ j[         S]    5       5      (       a  S]nU$ [        U 4S^ j[         S_    5       5      (       a  S`T ;   a  S`nOSan[         Sb   T ;   a1  T U   R                  S   S:X  a  ScnOT U   R                  S   Sd:X  a  Sen[         Sf   T ;   a  SgnU$ T U   R                  S   S:X  a  ShnU$ T U   R                  S   Sd:X  a  T U   R                  S   Si:X  a  SjnU$ Skn U$ [         Sl   T ;   a  SjnU$ [         Sm   T ;   a  SmnU$ [        U 4Sn j[         So    5       5      (       aX  T [         So   S      R                  nUS   Sp:X  a  US   Sq:X  a  SrOSsnU$ US   St:X  a  US   Sq:X  a  SuOSvnU$ [	        SwU Sx35      e[        U 4Sy j[         Sz    5       5      (       aX  T [         Sz   S      R                  nUS   Sp:X  a  US   S:X  a  S{OS|nU$ US   St:X  a  US   S:X  a  S}OS~nU$ [	        SwU S35      e[         S   T ;   aC  T R                  SS 5      nUc  SnU$ Ub"  [        R                  " US:H  5      (       a  SnU$ Sn U$ [         S   T ;   a  SnU$ [        U 4S j[         S    5       5      (       a  SnU$ SnU$ )Nr)   r   	   r   r   r[   r   rZ   r'   r   r    c              3   ,   >#    U  H	  oT;   v   M     g 7fr    r   keyr  s     r   r   -infer_diffusers_model_type.<locals>.<genexpr>e       M*L3J*L   r!   r#   r%   r&   r\   r2   r   i   rb      r1   i@  r_   i  c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<         F*E3J*Er>  r4   c              3   \   >#    U  H!  oT;   a  TU   R                   S    S:H  OSv   M#     g7f)r7  i $  FNshaper:  s     r   r   r<    s4      OUpcJ->
3b!T)EIUps   ),zmodel.diffusion_model.pos_embed	pos_embedi   i @ rd   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    r=  r>  r5   r6   r9   r:   r7   r8   i@     re   rf   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    s     H*G3J*Gr>  rW   rk   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<         G*F3J*Fr>  r>   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  )r   gr  s     r   r   r<    s      
%uO%ur>  )guidance_in.in_layer.biasz/model.diffusion_model.guidance_in.in_layer.biasz#model.diffusion_model.img_in.weightimg_in.weighti  rh      ri   rg   rj   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    s     L*K3J*Kr>  r?   zvae.encoder.conv_in.conv.biasc              3   B   #    U  H  oR                  S 5      v   M     g7f)z'transformer_blocks.47.scale_shift_tableN)endswithr   r;  s     r   r   r<    s     ]R\3||EFFR\   rn    vae.encoder.conv_out.conv.weightrm   @vae.decoder.last_time_embedder.timestep_embedder.linear_1.weightrl   r@   z!encoder.project_in.conv.conv.biasz#decoder.project_in.main.conv.weightrA   rr   @       rq   rp   ro   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    s     R*Q3J*Qr>  rC   rD   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    s     K*J3J*Jr>  r;   rJ      c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    s     P*O3J*Or>  rL   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    s     J*I3J*Ir>  rK   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    rJ  r>  rO   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    rA  r>  rP   z,model.diffusion_model.patch_embedding.weightzpatch_embedding.weightrR   rw   i   rx   rS   rv   rs      rt   ru   rQ   rT   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    r=  r>  rU   D   i   ry   rz   H   r{   r|   zUnexpected x_embedder shape: z when loading Cosmos 1.0 model.c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<    r=  r>  rV   r}   r~   r   r   z when loading Cosmos 2.0 model.rN   z*control_noise_refiner.0.before_proj.weightr   g        r   rM   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r9  r:  s     r   r   r<  .  rJ  r>  rX   r   r   )r  rD  r   allr   r  torch)	r  
model_typer;  has_vaeencoder_keydecoder_key
target_keyx_embedder_shapebefore_proj_weights	   `        r   infer_diffusers_model_typero  J  s+   \*j8+L9:@@CqH%3
CWX\C]8^8d8deg8hlp8p(JH G "),
:%JD A &J@ } 
d	#z	1jAUVZA[6\6b6bce6fjn6n
z w 
/	0J	>&
t q 
i	(J	6
n k 
l	+z	9!
h e 
i	(J	6
b _ 
M*>|*LM	M	M0J>#$9:jH2
X W &&9:jH0
T Q 3
P M &JL G 	56*D+,DEFLLQOSWW2
@ { 	56*D+,DEFLLQOSWW-
t o 	56*D+,DEFLLRPTWW2
h c 	56*D+,DEFLLRPTWW-
\ Y 
F*>u*EF	F	F3 OUijoUpO L L -
:3CCc?  #u,JF E _""1%/&JB  
M*>|*LM	M	M!
| y 
m	,
	: 67:E/Jt q ""34
B*Jn k ""23zA)Jh e ,-DEFLLRPTWW0Jb _ ,];<BB1EK)J\ W *JV S 
H*>w*GH	H	H!
P M 
G*>v*FG	G	G 
%u
 
 
 5
B;%#$$Q'3.(
x w C&&q)S0)
t q (
p m (Jl i 
L*>{*KL	L	L1Z?]R\]]]*Jb a $FGMMaPTXX*J^ ] PS]]*JZ W %JV S 
.	/:	=9; 56*D5JH E $**2."4K9P9V9VWX9Y]_9_0JB  $**2."4K9P9V9VWX9Y]`9`1J| w 3Jv s 
R*>?P*QR	R	R&
p m 
o	.*	<$
j g 
K*>z*JK	K	K
d _ 	/0J>+,>?@FFqIQN'
X U 
P*>*OP	P	P$
R O 
J*>y*IJ	J	J
L I 
G*>v*FG	G	G
F C 
F*>u*EF	F	F9ZGGJ1J
+z9*%++A.$6,
J'--a0D8+
.*<*Jh e 
#))!,4'Jb a 
#))!,4J9O9U9UVW9X\^9^&J^ [ 'JZ W 
i	(J	6"
R O 
i	(J	6
L I 
M*>|*LM	M	M%&:<&H&KLRRA"$0@0Ct0K,QeJB A a B&0@0Ct0K,QeJ> ; <=M<NNmnoo	M*>|*LM	M	M%&:<&H&KLRRA"$0@0Ct0K,QeJ0 / a B&0@0Ct0K,QeJ, ) <=M<NNmnoo	<	=	K'^^,XZ^_%7J   +		:LPS:S0T0T7J  8J  
8	9Z	G/
  
G*>v*FG	G	G

  
r   c                 Z    [        U 5      n[        U   n[        R                  " U5      nU$ r   )ro   DIFFUSERS_DEFAULT_PIPELINE_PATHScopydeepcopy)r  rh  
model_paths      r   fetch_diffusers_configru  7  s*    +J7J1*=Jz*Jr   c                 @    U(       a  U$ [        U 5      n[        U   nU$ r   )ro  'DIFFUSERS_TO_LDM_DEFAULT_IMAGE_SIZE_MAP)r  
image_sizerh  s      r   set_image_sizery  ?  s%    +J7J8DJr   c                 D   [        U R                  5       5      n/ SQnU H}  nSR                  UR                  S5      SS  5      U;   a'  X   R                  S:  a  X   S S 2S S 2SS4   X'   MO  MQ  SU;   d  MY  X   R                  S:  d  Mm  X   S S 2S S 2S4   X'   M     g )N)zquery.weightz
key.weightzvalue.weight.r   r   zproj_attn.weight)listr   r   splitndim)r  r   	attn_keysr;  s       r   conv_attn_to_linearr  J  s    
!"D>I88CIIcN23'(I5##a'",/!Q1*"=
 (3&##a'",/!Q'":
 r   c                 4   Ub  Sn[        SSU5        [        XS9nSU S   S   ;   a  U S   S   S   b  U S   S   S   S   nOU S   S   S	   S   nUb  S
n[        SSU5        UnOUS   nU S   S   S   S   S   nUS    V	s/ s H
  oS   U	-  PM     n
n	/ nSn[        [        U
5      5       H9  nXS   ;   a  SOSnUR	                  U5        U[        U
5      S-
  :w  d  M4  US-  nM;     / n[        [        U
5      5       H%  nXS   ;   a  SOSnUR	                  U5        US-  nM'     US   b,  [        US   [        5      (       a  US   O[        US   5      nOSnS[        US   5      S-
  -  nSU;   a  US   OSnSU;   a  US   OSnU(       a/  Uc,  US   US   -  n[        US   5       Vs/ s H  nUU-  PM
     nnSnSnSnSnSnUS   b%  [        US   [        5      (       a  US   OUS   S   nSU;   a#  US   S :X  a  US!;   a  S"nS#nOS$nS%U;   d   eUS%   nUU-  UUU
US&   UUUUUUUUS'.nUb  S(n[        SSU5        UUS)'   S*U;   a  US*   US+'   SU;   a   [        US   [        5      (       a  US   US,'   US-   US-'   UUS.'   U$ s  sn	f s  snf )/J
Creates a config for the diffusers based on the config of the LDM model.
NzConfiguring UNet2DConditionModel with the `image_size` argument to `from_single_file`is deprecated and will be ignored in future versions.rx  1.0.0rx  unet_configmodelparamsnetwork_configzConfiguring UNet2DConditionModel with the `num_in_channels` argument to `from_single_file`is deprecated and will be ignored in future versions.in_channelsfirst_stage_configddconfigchannel_multmodel_channelsr   attention_resolutionsCrossAttnDownBlock2DDownBlock2Dr   CrossAttnUpBlock2D	UpBlock2Dtransformer_depthch_mult	num_headsuse_linear_in_transformerFnum_head_channelscontext_dimr   num_classes
sequential)r?  i   	text_time   
projectionadm_in_channelsnum_res_blocks)sample_sizer  down_block_typesblock_out_channelslayers_per_blockcross_attention_dimattention_head_dimuse_linear_projectionr   r   addition_time_embed_dim%projection_class_embeddings_input_dimtransformer_layers_per_blockzConfiguring UNet2DConditionModel with the `upcast_attention` argument to `from_single_file`is deprecated and will be ignored in future versions.upcast_attentiondisable_self_attentionsonly_cross_attentionnum_class_embedsout_channelsup_block_types)r   ry  rangelenappend
isinstanceintr}  )r  r  rx  r  num_in_channelsdeprecation_messageunet_paramsr  
vae_paramsmultr  r  
resolutioni
block_typer  r  vae_scale_factorhead_dimr  head_dim_multcr   r   r  r  r  configs                               r   %create_unet_diffusers_config_from_ldmr  V  s    D 	 	,)<=
BJ 	1(;;G$X.}=I%g.x8GQ%g.x89IJ8T"D 	 	,)<=%!-0 )(34HI(ST^_JKVWeKfgKf4&67$>KfgJ3)*+/9I`=a/a+gt

+&'!++!OJ	 , N3)*+-7G^;_-_)ep
j)q
 ,
 &'3 +&9:C@@ +,k"567 	% ()$SI!67!;<+6++E{;'4H4OS^4^/0di  '(89[I\=]]M37N8S3TU3Ta)3THU",0)K=!- +m4c:: &]+A. 	 #}%5l*&1#*-'#/ $3334?@Q4R1 "%55",0'(89*&!6,2#:1V(DF  #D 	 	,)<=%5!" K/)45N)O%&#
;}3Ms(S(S%0%?!"(8F>-FM} hF Vs   J1Jc                     Ub  Sn[        SSU5        [        XS9nU S   S   S   S   n[        XS9nUS   US	   US
   US   US   US   US   US   US   US   US   US   US   S.nU$ )NzoConfiguring ControlNetModel with the `image_size` argumentis deprecated and will be ignored in future versions.rx  r  r  r  r  control_stage_confighint_channelsr  r  r  r  r  r  r  r   r   r  r  r  )conditioning_channelsr  r  r  r  r  r  r  r   r   r  r  r  )r   ry  r  )r  r  rx  r   r  r  diffusers_unet_configcontrolnet_configs           r   +create_controlnet_diffusers_config_from_ldmr    s    D 	 	,)<=
BJ!'*845KLXVKA/i "-_!=,];12DE34HI12DE45JK34HI!67N!O12DE45JK#89R#S1FGn1o(=>\(]  r   c           
         Ub  Sn[        SSU5        [        XS9nSU;   a  SU;   a  US   nUS   nOSnSnU S   S	   S
   S	   S   nUc  Ub
  Ub  [        nO$Uc  SU S   S	   ;   a  U S   S	   S   nO	Uc  [        nUS    Vs/ s H
  oS   U-  PM     n	nS/[	        U	5      -  n
S/[	        U	5      -  nUUS   US   U
UU	US   US   US.	nUb  Ub  UR                  XVS.5        U$ s  snf )r  NzmConfiguring AutoencoderKL with the `image_size` argumentis deprecated and will be ignored in future versions.rx  r  r  r(   edm_stdr  r  r  r  scale_factorr  chDownEncoderBlock2DUpDecoderBlock2Dr  out_ch
z_channelsr  )	r  r  r  r  r  r  latent_channelsr  scaling_factor)latents_meanlatents_std)r   ry  PLAYGROUND_VAE_SCALING_FACTORLDM_VAE_DEFAULT_SCALING_FACTORr  update)r  r  rx  r  r  r  r  r  r  r  r  r  r  s                r   $create_vae_diffusers_config_from_ldmr    sr    D 	 	,)<=
BJZI$;!*- + )(34HI(ST^_J\%=KD[6

 ~9QRZ9['[(1(;NK		7>H>ST>SdT*T1>ST,-4F0GG()C0B,CCN "!-0"8,,(0%l3&'78(
F K$;|PQM% Us   C'c                 >   U  H  nUR                  SS5      R                  SS5      R                  SS5      R                  SS5      R                  S	S
5      R                  SS5      nU(       a  UR                  US   US   5      nUR                  U5      X'   M     g )Nzin_layers.0norm1zin_layers.2conv1zout_layers.0norm2zout_layers.3conv2zemb_layers.1time_emb_projskip_connectionconv_shortcutoldnewr   r  ldm_keysnew_checkpointr  mappingldm_keydiffusers_keys         r   #update_unet_resnet_ldm_to_diffusersr  ,  s    OOM73W]G,W^W-W^W-W^_5W&8 	 )11'%.'%.QM(2w(?% r   c                 l    U  H.  nUR                  US   US   5      nUR                  U5      X'   M0     g )Nr  r  r  r  s         r   &update_unet_attention_ldm_to_diffusersr  ;  s5    G(2w(?% r   c                     U  H>  nUR                  US   US   5      R                  SS5      nUR                  U5      X'   M@     g )Nr  r  nin_shortcutr  r  )r   r  r  r  r  r  s         r   "update_vae_resnet_ldm_to_diffusersr  A  sD    GOOP^`op(2w(?% r   c                 T   U  GH!  nUR                  US   US   5      R                  SS5      R                  SS5      R                  SS5      R                  S	S
5      R                  SS5      R                  SS5      R                  SS5      R                  SS5      R                  SS5      R                  SS5      nUR                  U5      X'   X   R                  n[        U5      S:X  a  X   S S 2S S 2S4   X'   M  [        U5      S:X  d  GM  X   S S 2S S 2SS4   X'   GM$     g )Nr  r  znorm.weightzgroup_norm.weightz	norm.biaszgroup_norm.biaszq.weightto_q.weightzq.bias	to_q.biaszk.weightto_k.weightzk.bias	to_k.biaszv.weightto_v.weightzv.bias	to_v.biasproj_out.weightto_out.0.weightproj_out.biasto_out.0.biasr   r      )r   r  rD  r  )r   r  r  r  r  r  rD  s          r   &update_vae_attentions_ldm_to_diffusersr  G  s   OOGENGEN;W]$78W["34WZ/WX{+WZ/WX{+WZ/WX{+W&(9:W_o6 	 )3w(?% -33u:?,:,I!QPQ',RN)Z1_,:,I!QPQST*,UN)- r   c                 |   SU ;   nU(       Gag  0 nU R                  5        GHN  nUR                  S5      (       a[  X   R                  SS5      nUS   X4R                  SS5      '   US   X4R                  SS5      '   US	   X4R                  SS
5      '   Mu  UR                  S5      (       a[  X   R                  SS5      nUS   X4R                  SS5      '   US   X4R                  SS5      '   US	   X4R                  SS5      '   M  UR                  S5      (       a  X   nXSUR                  SS5      '   GM  UR                  S5      (       a  X   nXSUR                  SS5      '   GMH  X   X4'   GMQ     U$ 0 nU R                  5        GH  nUR                  S5      (       a[  X   R                  SS5      nUS   X4R                  SS5      '   US   X4R                  SS5      '   US	   X4R                  SS
5      '   Mu  UR                  S5      (       a[  X   R                  SS5      nUS   X4R                  SS5      '   US   X4R                  SS5      '   US	   X4R                  SS5      '   M  UR                  S5      (       a  X   nXSUR                  SS5      '   GM  UR                  S5      (       a  X   nXSUR                  SS5      '   GMH  UR                  S5      (       a  X   nXSUR                  SS5      '   GMy  UR                  S5      (       a  X   nXSUR                  SS5      '   GM  X   X4'   GM     U$ )Nr3   in_proj_weightr   r   zattn.in_proj_weightr  r   r  r   r  in_proj_biaszattn.in_proj_biasr  r  r  zout_proj.weightzattn.out_proj.weightr  zout_proj.biaszattn.out_proj.biasr  zclip_mapper.weightzclip_txt_pooled_mapper.weightzclip_mapper.biaszclip_txt_pooled_mapper.bias)r   rR  chunkr   )r  r   
is_stage_cr  r;  weightss         r   4convert_stable_cascade_unet_single_file_to_diffusersr  a  s$   )Z7J
??$C||,--$///15PWXYPZ
;;'<mLMPWXYPZ
;;'<mLMPWXYPZ
;;'<mLMn--$///15LSTUJ
;;':KHILSTUJ
;;':KHILSTUJ
;;':KHI/00$/U\3;;'=?PQRo..$/QX3;;';_MN",/
% %` 9 
??$C||,--$///15PWXYPZ
;;'<mLMPWXYPZ
;;'<mLMPWXYPZ
;;'<mLMn--$///15LSTUJ
;;':KHILSTUJ
;;':KHILSTUJ
;;':KHI/00$/U\3;;'=?PQRo..$/QX3;;';_MN233$/ah3;;';=\]^011$/]d3;;'9;XYZ",/
3 %6 r   c                 |   0 n[        U R                  5       5      n[        n[        S U 5       5      S:  a  U(       a  [        R                  S5        [        R                  S5        U Hd  nUR                  S5      (       d  M  SSR                  UR                  S5      S	S
 5      -   nU R                  U5      XGR                  US5      '   Mf     Oo[        S U 5       5      S:  a  [        R                  S5        U H>  nUR                  U5      (       d  M  U R                  U5      XGR                  US5      '   M@     0 n	[        S   S   n
U
R                  5        H  u  pX;  a  M  XL   X'   M     SU;   a4  US   S;   a+  [        S   S   nUR                  5        H  u  pXL   X'   M     SU;   a4  US   S:X  a+  [        S   S   nUR                  5        H  u  pXL   X'   M     SU;   a  US   b  SU;   a  US   U	S'   [        U Vs1 s H.  nSU;   d  M  SR                  UR                  S5      S
S 5      iM0     sn5      n[        U5       VVs0 s H"  nUU Vs/ s H  nSU 3U;   d  M  UPM     sn_M$     nnn[        U Vs1 s H.  nSU;   d  M  SR                  UR                  S5      S
S 5      iM0     sn5      n[        U5       VVs0 s H"  nUU Vs/ s H  nSU 3U;   d  M  UPM     sn_M$     nnn[        U Vs1 s H.  nSU;   d  M  SR                  UR                  S5      S
S 5      iM0     sn5      n[        U5       VVs0 s H"  nUU Vs/ s H  nSU 3U;   d  M  UPM     sn_M$     nnn[        S	U5       H  nUS	-
  US   S	-   -  nUS	-
  US   S	-   -  nUU    Vs/ s H  nSU S3U;   d  M  SU S3U;  d  M  UPM     nn[        UU	USU S3S U S!U 3S".5        SU S#3U;   a8  UR                  SU S#35      U	S U S$3'   UR                  SU S%35      U	S U S&3'   UU    Vs/ s H  nSU S'3U;   d  M  UPM     nnU(       d  M  [!        UU	USU S'3S U S(U 3S".5        M     UR                  5        HM  n[#        US	-
  S)5      nUS-  S):X  a  [        UU   U	USU 3S*U 3S".S+9  M5  [!        UU   U	USU 3S,U 3S".S+9  MO     [        U5       GH  nUUS   S	-   -  nUUS   S	-   -  nUU    Vs/ s H  nSU S3U;   d  M  SU S3U;  d  M  UPM     nn[        UU	USU S3S-U S!U 3S".5        UU    Vs/ s H  nSU S'3U;   d  M  SU S.3U;  d  M  UPM     nnU(       a  [!        UU	USU S'3S-U S(U 3S".5        SU S/3U;   a   USU S/3   U	S-U S03'   USU S13   U	S-U S23'   SU S33U;   d  M  USU S33   U	S-U S03'   USU S43   U	S-U S23'   GM
     U	$ s  snf s  snf s  snnf s  snf s  snf s  snnf s  snf s  snf s  snnf s  snf s  snf s  snf s  snf )5zF
Takes a state dict and a config, and returns a converted checkpoint.
c              3   B   #    U  H  oR                  S 5      v   M     g7f	model_emaN
startswithr   s     r   r   .convert_ldm_unet_checkpoint.<locals>.<genexpr>  s     
3d<<$$drT  d   z,Checkpoint has both EMA and non-EMA weights.zIn this conversion only the EMA weights are extracted. If you want to instead extract the non-EMA weights (useful to continue fine-tuning), please make sure to remove the `--extract_ema` flag.zmodel.diffusion_modelz
model_ema.r   r{  r   Nc              3   B   #    U  H  oR                  S 5      v   M     g7fr  r
  r   s     r   r   r    s     7$Q||K(($rT  zIn this conversion only the non-EMA weights are extracted. If you want to instead extract the EMA weights (usually better for inference), please make sure to add the `--extract_ema` flag.r   r   r   )timestepr  r   r  r  zlabel_emb.weightzclass_embedding.weightinput_blocksr   input_blocks.middle_blockmiddle_block.output_blockszoutput_blocks.r  .0.0.opdown_blocks.	.resnets.r  r  .0.op.weight.downsamplers.0.conv.weight
.0.op.bias.downsamplers.0.conv.bias.1.attentions.r   mid_block.resnets.r  mid_block.attentions.
up_blocks.z.1.convz.1.conv.weight.upsamplers.0.conv.weightz.1.conv.bias.upsamplers.0.conv.biasz.2.conv.weightz.2.conv.bias)r}  r   LDM_UNET_KEYsumloggerwarningr  r   r~  r  r   DIFFUSERS_TO_LDM_MAPPINGitemsr  r  r  r  max)r  r  extract_emar   unet_state_dictr   unet_keyr;  flat_ema_keyr  ldm_unet_keysr  r  class_embed_keysaddition_embed_keyslayernum_input_blockslayer_idr  num_middle_blocksmiddle_blocksnum_output_blocksr  r  block_idlayer_in_block_idresnets
attentionss                               r   convert_ldm_unet_checkpointr>    s$   
 O
!"DH 
3d
33c9kEFn	
 C~~566+bggciinQR6H.II=G^^L=YHb 9: 
 7$77#=NNm C~~h''=G^^C=PHb 9:  N,V4X>M"/"5"5"7)(7(@% #8
 	f$62D+EIc+c3F;<NO&6&<&<&>"M,;,DN) '? 	'f5J.K{.Z6v>?TU&9&?&?&A"M,;,DN) 'B V#%&29K9^7FGY7ZN34 su[imr[r:CHHU[[%5bq%9:st .//H 	/W/3}XJ5OSV5V3/WW/   t\jns\s;SXXekk#&6r&:;tu /00H 	/W/3}XJ5OSV5V3/WW0   u\kot\t;SXXekk#&6r&:;uv /00H 	/X/3~hZ5PTW5W3/XX0   1&'Ev&89A=>Uv.@'AA'EF (?
*CaS.Cs.JCQ^_`^aafOgorOrC? 	 
 	,#A3b)L
)TeSf2gh		
 1#\*o=SbSfSfs,/TN\(3NOP RaQdQds*-RN\(3LMN &2!_U_c-s"8MQT8Tc_
U:2's"-XJl[lZm6no	1 (@ !!#C!GQ7a</c""/u 5@RS`Ra>bc	 3c""/u 5@UVcUd>ef	 $$ $% 23a78(:!;a!?@ )+
+Cs"/E/LCSabcaddiQjruQuC+ 	 
 	,$QCr*ZzSdRe3fg		
 )+
+Cs"/E/LCSabcaddkQltwQwC+ 	 
 2(2.H:\ZkYl7mn	 A3n-@O^ >2PNZz1JKL N] <0NNZz1HIJ A3n-@O^ >2PNZz1JKL N] <0NNZz1HIJI &P _ tW uW vX
$ V@

s   
W:'W:X#W?4W?:X
X
'X
X"X3X9X
X'XX$!X2X8X$4X*X*X*>X/X/X4X4+X4X9'X93X9?XXX$c                    SU ;   a  U $ SU ;   a  U nOe0 n[        U R                  5       5      n[        nU H>  nUR                  U5      (       d  M  U R	                  U5      X6R                  US5      '   M@     0 n[        S   S   nUR                  5        H  u  pX;  a  M  X:   Xy'   M     [        U Vs1 s H.  nSU;   d  M  SR                  UR                  S5      S S 5      iM0     sn5      n[        U5       VVs0 s H!  nX Vs/ s H  nS	U 3U;   d  M  UPM     sn_M#     nnn[        S
U5       H  nUS
-
  US   S
-   -  nUS
-
  US   S
-   -  nX    Vs/ s H  nS	U S3U;   d  M  S	U S3U;  d  M  UPM     nn[        UUUS	U S3SU SU 3S.5        S	U S3U;   a8  UR	                  S	U S35      USU S3'   UR	                  S	U S35      USU S3'   X    Vs/ s H  nS	U S3U;   d  M  UPM     nnU(       d  M  [        UUUS	U S3SU SU 3S.5        M     [        U5       H;  nUR	                  SU S35      USU S3'   UR	                  SU S35      USU S3'   M=     [        U Vs1 s H.  nSU;   d  M  SR                  UR                  S5      S S 5      iM0     sn5      n[        U5       VVs0 s H!  nX Vs/ s H  nSU 3U;   d  M  UPM     sn_M#     nnnUR                  5        HM  n[        US
-
  S5      n	US-  S:X  a  [        UU   UUSU 3S U	 3S.S!9  M5  [        UU   UUSU 3S"U	 3S.S!9  MO     UR	                  S#5      US$'   UR	                  S%5      US&'   U Vs1 s H>  nS'U;   d  M  S(U;  d  M  S)U;  d  M  SR                  UR                  S5      S S 5      iM@     nn[        U5      n[        S
US
-   5       HE  nUS
-
  nSU-  nUR	                  S*U S35      US+U S3'   UR	                  S*U S35      US+U S3'   MG     U$ s  snf s  snf s  snnf s  snf s  snf s  snf s  snf s  snnf s  snf ),Nr   r   r   r!   r   r  r{  r   r  r   r  r  r  r  r  r  r  r  r  r  r  r  zzero_convs.z	.0.weightzcontrolnet_down_blocks..weightz.0.bias.biasr  r  r   r   r!  r"  zmiddle_block_out.0.weightzcontrolnet_mid_block.weightzmiddle_block_out.0.biaszcontrolnet_mid_block.biasinput_hint_blockzinput_hint_block.0zinput_hint_block.14zinput_hint_block.z!controlnet_cond_embedding.blocks.)r}  r   LDM_CONTROLNET_KEYr  r  r   r*  r+  r  r   r~  r  r  r  r,  )r  r  r   controlnet_state_dictr   controlnet_keyr;  r  ldm_controlnet_keysr  r  r4  r5  r6  r  r  r:  r;  r<  r=  r7  r8  cond_embedding_blocksnum_cond_embedding_blocksidxdiffusers_idxcond_block_ids                              r   convert_controlnet_checkpointrL  @  s    (:5 
* * !#JOO%&+C~~n--ISX[I\%kk."&EF  N2<@J"5";";"=/(=(F% #> 5Jf5JEn`eNe	'%++c"2A&	'5Jf
 .//H 	"7]"73]8*;UY\;\3"7]]/   1&'Ev&89A=>Uv.@'AA'EF (?
*CaS.Cs.JCQ^_`^aafOgorOrC? 	 
 	,!#A3b)L
)TeSf2gh		
 1#\*.CCShSlSls,/TN\(3NOP RgQjQjs*-RN\(3LMN &2_U_c-s"8MQT8Tc_
U:2%'s"-XJl[lZm6no	1 (@ #$?T?X?X[fghfiirYs?t07;<=R=V=VYdefdggnWo=p059: %
 5Jf5JEn`eNe	'%++c"2A&	'5Jf
 /00H 	"7]"73]8*;UY\;\3"7]]0   !!#C!GQ7a</c"%"/u 5@RS`Ra>bc	 3c"%"/u 5@UVcUd>ef	 $$ 5J4M4MNi4jN012G2K2KLe2fN./
 +*E& 	',@,M 	'TiqvTv 	'S!"1%&*  
 !$$9 :Q1A56aCUjUnUng6V
:=/QR TiSlSle4T
:=/OP 7 } 	g ^
$ V  	g ^8s   4
P&'P&>
P0P+P+P0P6,P68P6#P;5P;8
Q 'Q 
Q
QQ#Q
:
QQQ'Q+P0Q
c           
        ^ 0 n[        U R                  5       5      nSn[         H!  m[        U4S jU 5       5      (       d  M  TnM#     U H>  nUR	                  U5      (       d  M  U R                  U5      X%R                  US5      '   M@     0 n[        S   nUR                  5        H  u  pX;  a  M  X)   Xh'   M     [        US   5      n
[        U
5       VVs0 s H!  oU Vs/ s H  nSU 3U;   d  M  UPM     sn_M#     nnn[        U
5       H  nX    Vs/ s H  nSU 3U;   d  M  SU S3U;  d  M  UPM     nn[        UUUSU S3SU S	3S
.S9  SU S3U;   d  MQ  UR                  SU S35      USU S3'   UR                  SU S35      USU S3'   M     U Vs/ s H  nSU;   d  M  UPM     nnSn[        SUS-   5       H8  nU Vs/ s H  nSU 3U;   d  M  UPM     nn[        UUUSU 3SUS-
   3S
.S9  M:     U Vs/ s H  nSU;   d  M  UPM     nn[        UXbSSS
.S9  [        US   5      n[        U5       VVs0 s H!  oU Vs/ s H  nSU 3U;   d  M  UPM     sn_M#     nnn[        U5       Hz  nUS-
  U-
  nUU    Vs/ s H  nSU 3U;   d  M  SU S3U;  d  M  UPM     nn[        UUUSU S3SU S	3S
.S9  SU S 3U;   d  MZ  USU S 3   US!U S"3'   USU S#3   US!U S$3'   M|     U Vs/ s H  nS%U;   d  M  UPM     nnSn[        SUS-   5       H8  nU Vs/ s H  nS&U 3U;   d  M  UPM     nn[        UUUSU 3SUS-
   3S
.S9  M:     U Vs/ s H  nS'U;   d  M  UPM     nn[        UXbSSS
.S9  [        U5        U$ s  snf s  snnf s  snf s  snf s  snf s  snf s  snf s  snnf s  snf s  snf s  snf s  snf )(Nr   c              3   D   >#    U  H  oR                  T5      v   M     g 7fr   r
  )r   r   ldm_vae_keys     r   r   -convert_ldm_vae_checkpoint.<locals>.<genexpr>  s     7$Q||K(($s    r   r  zdown.z.downsamplez.blockr  z.resnetsr  r!  zencoder.down.z.downsample.conv.weightencoder.down_blocks.r  z.downsample.conv.biasr  zencoder.mid.blockr   r   zencoder.mid.block_z
mid.block_r   zencoder.mid.attnz
mid.attn_1zmid_block.attentions.0r  zup.z	.upsampler#  zdecoder.up.z.upsample.conv.weightdecoder.up_blocks.r$  z.upsample.conv.biasr%  zdecoder.mid.blockzdecoder.mid.block_zdecoder.mid.attn)r}  r   LDM_VAE_KEYSr   r  r  r   r*  r+  r  r  r  r  r  )r  r  vae_state_dictr   vae_keyr;  r  vae_diffusers_ldm_mapr  r  num_down_blocksr6  down_blocksr  r<  mid_resnetsnum_mid_res_blocksmid_attentionsnum_up_blocks	up_blocksr:  rO  s                        @r   convert_ldm_vae_checkpointr^    s    N
!"DG#7$777!G $ >>'""7A~~c7JN;;w34  N4U;"7"="="?((6(?% #@ &!345O_det_u_uS[.N.3eH:4F#4M3.NN_u   ?#"-.m.3eA3K34F3USTRUU`KailKl3.m*#A3f-QCx6PQ		
 1#45GTbTfTfs"9:UN1!4OPQ SaRdRds"78SN1!4MNO $  #1O.34G34N3.KO1(1,-"-Q+33EaS1IS1P3+Q*(,7I!a%5QR		
 . &4Q^c7IS7Pc^NQ*]u@v
 /01M]bcp]q]qQY.L.3c(4D4K3.LL]q   =! 1$q($X.
.CCz2Bc2ICPST\S]]fNgorNrC. 	 
 	+!(62Zs(;ST		
 
"78NJP^hZ'<=QN/s2KLM O]hZ':;ON/s2IJK "& #1O.34G34N3.KO1(1,-"-Q+33EaS1IS1P3+Q*(,7I!a%5QR		
 . &4Q^c7IS7Pc^NQ*]u@v '[ O
 n P R R M
" P R Rs   
N%N6N<NN$+N$7N$"
N)0N)N.!N.	
N3N3	
N=N8$N8*N=O"O.O
OO/O O(
O6ON8N=c                 8   [        U R                  5       5      n0 n/ nUR                  [        5        U(       a  UR	                  U5        U HI  nU H@  nUR                  U5      (       d  M  UR                  US5      nU R                  U5      X7'   MB     MK     U$ )Nr   )r}  r   extendLDM_CLIP_PREFIX_TO_REMOVEr  r  r   r  )r  remove_prefixr   text_model_dictremove_prefixesr;  r   r  s           r   convert_ldm_clip_checkpointre  '  s    
!"DOO45}-%F~~f%% #FB 71;1D. &  r   c                    0 nUS-   nXA;   a  [        X   R                  S   5      nO8[        U R                  S5      (       a  U R                  R                  nO[
        n[        UR                  5       5      n[        n[        S   S   nUR                  5        HS  u  pX*-   n
X;  a  M  X;   a  M  U
R                  S5      (       a   X   R                  R                  5       X9'   MM  X   X9'   MU     U GH  nX;   a  M  UR                  US-   5      (       d  M&  UR                  US-   S5      n	[        S   S   nUR                  5        H6  u  pU	R                  X5      R                  S	S5      R                  S
S5      n	M8     UR                  S	5      (       a  UR!                  U5      nUS U2S S 24   R#                  5       R%                  5       X9S-   '   XUS-  2S S 24   R#                  5       R%                  5       X9S-   '   XS-  S 2S S 24   R#                  5       R%                  5       X9S-   '   GMC  UR                  S
5      (       a  UR!                  U5      nUS U R#                  5       R%                  5       X9S-   '   XUS-   R#                  5       R%                  5       X9S-   '   XS-  S  R#                  5       R%                  5       X9S-   '   GM  UR!                  U5      X9'   GM     U$ )Nr   r   hidden_sizer   r   ztransformer.r   r   z.in_proj_weightz.in_proj_biasz.q_proj.weightr   z.k_proj.weightz.v_proj.weightz.q_proj.biasz.k_proj.biasz.v_proj.bias)r  rD  hasattrr  rg  !LDM_OPEN_CLIP_TEXT_PROJECTION_DIMr}  r    SD_2_TEXT_ENCODER_KEYS_TO_IGNOREr*  r+  rR  T
contiguousr  r   r  clonedetach)
text_modelr  r   rc  text_proj_keytext_proj_dimr   keys_to_ignoreopenclip_diffusers_ldm_mapr  r  r;   transformer_diffusers_to_ldm_mapnew_keyold_keyweight_values                   r   convert_open_clip_checkpointrx  9  s   
 O..M"J5;;A>?	""M	2	2"))559
!"D5N!9*!Eh!O"<"B"B"D"$$-..-7-@-B-B-M-M-OO*-7-@O* #E  ~~f~566F^$;R@+CJ+OP]+^( @ F F HG%%g7??@QSUV^^_nprs  !I
 <<)**%>>#.L@L^m^]^M^@_@e@e@g@n@n@pO,<<=]Q->>ABHHJQQS ,<<= AM]^M^M`bcMc@d@j@j@l@s@s@uO,<<=\\/**%>>#.L>J>M>Z>`>`>b>i>i>kON:;]Q->?EEGNNP N:; ?K[\K\K^>_>e>e>g>n>n>pON:;-7^^C-@O*? B r   c                    U(       a  SU0nO[        U5      nU(       a_  [        R                  S5        [        U5      (       d  [	        U5      (       a	  SnXsS'   SnO![        U5      (       a	  SnXsS'   SnOSnXsS'   SnU R                  R                  " S0 UDX%S.D6n[        5       (       a  [        O[        n	U	" 5          U " U5      n
S S S 5        W
R                  R                  R                  R                  R                  S	   n[        U5      (       a  [!        U5      nGOF[	        U5      (       a*  U["        S
      R                  S	   U:X  a  [!        U5      nGO[%        U5      (       aC  U["        S      R                  S	   U:X  a&  [!        US5      n[&        R(                  " U5      US'   O[        U5      (       a  Sn[+        XUS9nO[-        U5      (       a*  U["        S      R                  S	   U:X  a  Sn[+        XUS9nOb[/        U5      (       a  Sn[+        XUS9nOE[1        U5      (       a*  U["        S      R                  S	   U:X  a  [!        US5      nO[3        S5      e[        5       (       a  [5        XUS9  [7        5         OU
R9                  USS9  Ub  U
R;                  U5        U
R=                  5         U
$ ! , (       d  f       GN= f)NrY   zDetected legacy CLIP loading behavior. Please run `from_single_file` with `local_files_only=False once to update the local cache directory with the necessary CLIP model config files. Attempting to load CLIP model from legacy cache directory.openai/clip-vit-large-patch14r   stabilityai/stable-diffusion-2text_encoder(laion/CLIP-ViT-bigG-14-laion2B-39B-b160kr^   r  r7  r+   r,   z!text_encoders.clip_l.transformer.r   cond_stage_model.model.)r   r.   zconditioner.embedders.1.model.zconditioner.embedders.0.model.r0   z!text_encoders.clip_g.transformer.zDThe provided checkpoint does not seem to contain a valid CLIP model.dtypeF)strictr9  )ru  r(  r)  r!  r#  r(  config_classfrom_pretrainedr   r   r   ro  
embeddingsposition_embeddingweightrD  re  r  r&  rg  eyerx  r*  r.  r,  r   r   r   r   toeval)clsr  r^   r  torch_dtyper  is_legacy_loadingclip_configmodel_configctxr  position_embedding_dimdiffusers_format_checkpointr   s                 r   $create_diffusers_clip_model_from_ldmr  {  s    16:'
3
 M	
 $$(::(F(F9K6A23I
++:K6A23&I EK6A23I##33ufu	uL 7 9 9
{C	L! 
 #--88KKRRXXY[\Z  &A*&M# 	:&&+K89??CG]]&A*&M# 	*%%+J78>>rBF\\&A*Nq&r#@E		J`@a#$<=	J	'	'*&B5]c&d# 	 
+++,<=>DDRHLbb1&B5]c&d#	(	4	41&B5]c&d# 	z**+O<=CCBGKaa&A*Nq&r# _``  !%KX9%H	JJLLq 
s    	K
Kc                    UR                  SS 5      nUR                  SS 5      nUb  Sn[        SSU5        Ub  Sn[        SSU5        [        n[        US9n	SU;   a  US   OS n
U(       a  [	        US   S	   S
S5      nOSnXS'   U	S:X  a  Uc
  U
S:X  a  SOSnOU=(       d    SnXhS'   U	S;   a  SnOZU	S:X  a  SnOQU(       a/  US   S	   R                  S5      nUS   S	   R                  S5      nOSnSnXS'   XS'   SUS'   SUS'   SUS'   US :X  a  U R                  SSS!S"SSS S#S$.5      $ Uc  U R                  U5      $ US%:X  a  S"US&'   [        R
                  " U5      nU$ US':X  a  [        R
                  " U5      nU$ US(:X  a  [        R
                  " U5      nU$ US:X  a  [        R
                  " U5      nU$ US):X  a  [        R
                  " U5      nU$ US*:X  a  [        R
                  " U5      nU$ US+:X  a  [        R
                  " U5      nU$ US:X  a  S,S-SS.S"SSS/S0S1S2S3S4S5SS6.n[        S90 UD6nU$ [        S7U S835      e):Nr   r   a&  Please pass an instance of a Scheduler object directly to the `scheduler` argument in `from_single_file`

Example:

from diffusers import StableDiffusionPipeline, DDIMScheduler

scheduler = DDIMScheduler()
pipe = StableDiffusionPipeline.from_single_file(<checkpoint path>, scheduler=scheduler)
r  a~  Please configure an instance of a Scheduler with the appropriate `prediction_type` and pass the object directly to the `scheduler` argument in `from_single_file`.

Example:

from diffusers import StableDiffusionPipeline, DDIMScheduler

scheduler = DDIMScheduler(prediction_type="v_prediction")
pipe = StableDiffusionPipeline.from_single_file(<checkpoint path>, scheduler=scheduler)
r   global_stepr  r  	timestepsr   r   r   iY r   v_prediction)r   r   euler
playgroundedm_dpm_solver_multisteplinear_start
linear_endg{Gz?g(\µ?r   r   r   r   Fclip_sampler   low_res_schedulerg-C6?Tfixed_small)r   r   r   r  r   r   trained_betasvariance_typepndmr   lmsheunzeuler-ancestraldpmddimzdpmsolver++gףp=
?zerog      @r   r   g      T@gMb`?r   midpoint)algorithm_typedynamic_thresholding_ratioeuler_at_finalfinal_sigmas_typelower_order_finalr   r   rhor   
sigma_data	sigma_max	sigma_minsolver_ordersolver_typethresholdingzScheduler of type z doesn't exist!r9  )r  r   SCHEDULER_DEFAULT_CONFIGro  getattrfrom_configr   r   r   r   r   r	   r   r
   r   )r  r  component_namer  r   r   r   r  scheduler_configrh  r  r   r   r   	schedulers                  r   _legacy_load_schedulerr    sC    ZZ 0$7Njj!2D9O!h 	 	"G-@A"h 	 	#W.AB/+zBJ/<
/J*]+PTK%og&>x&H+W[\".A*+T"+6&+@inO *6Y*9&'.. 	|	#3(1(;??OJ&w/9==lKH JH)3&'/$,;)*/'/4+, ,, !0$#'+#,!%!.	
 	
 /00	6	!-1)*!--.>?	V S 
5	 (445EF	P M 
6	!)556FG	J G 
7	"*667GH	D A 
,	,3??@PQ	> ; 
5	 /;;<LM	8 5 
6	!!--.>?	2 / 
5	5+*/#!'!%#'( #%!
" 3F5EF	
  -n-=_MNNr   c                     U(       a  SU0nO[        U5      n[        U5      (       d  [        U5      (       a	  SnXBS'   SnO![        U5      (       a	  SnXBS'   SnOSnXBS'   SnU R                  " S0 UDXSS.D6nU$ )	NrY   rz  r   r{  	tokenizerr}  r~  r9  )ru  r!  r#  r(  r  )r  r  r  r  r  r^   r  s          r   _legacy_load_clip_tokenizerr  k  s    16:'
3Z  $6z$B$B52=./		J	'	'62=./	 A2=./	##efe	eIr   c                 `    SSK Jn  [        R                  " SXS9nUR                  SXS9nXCS.$ )Nr   )StableDiffusionSafetyCheckerz'CompVis/stable-diffusion-safety-checker)r  r  )safety_checkerfeature_extractor))pipelines.stable_diffusion.safety_checkerr  r   r  )r  r  r  r  r  s        r   _legacy_load_safety_checkerr    sH     Y*::1DT 2AA1DT B N -UUr   c                 V    U R                  SSS9u  p#[        R                  " X2/SS9nU$ Nr   r   dimr  rg  cat)r  r  shiftscale
new_weights        r   swap_scale_shiftr    s.    <<q<)LEE>q1Jr   c                 V    U R                  SSS9u  p[        R                  " X!/SS9nU$ r  r  )r  projgater  s       r   swap_proj_gater    s.    aQ'JDD<Q/Jr   c                     / nU R                  5        H9  nSU;   d  M  [        UR                  S5      S   5      nUR                  U5        M;     [	        [        [        U5      5      5      $ )Nzattn2.r{  r   )r   r  r~  r  tuplesortedset)r  attn2_layersr;  	layer_nums       r   get_attn2_layersr    s[    L s?CIIcN1-.I	*	 ! L)*++r   c                 *    U S   R                   S   nU$ )Ncontext_embedder.weightr   rC  )r  caption_projection_dims     r   get_caption_projection_dimr    s    '(ABHHK!!r   c                    0 n[        U R                  5       5      nU H.  nSU;   d  M  U R                  U5      XR                  SS5      '   M0     [        [	        S U  5       5      5      S   S-   n[        U 5      n[        U 5      n[        S U R                  5        5       5      nU R                  S5      US'   U R                  S	5      US
'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   [        U5       GHD  n	[        R                  " U R                  SU	 S 35      S!S"S#9u  pn[        R                  " U R                  SU	 S$35      S!S"S#9u  pn[        R                  " U R                  SU	 S%35      S!S"S#9u  nnn[        R                  " U R                  SU	 S&35      S!S"S#9u  nnn[        R                  " U
/5      US'U	 S(3'   [        R                  " U/5      US'U	 S)3'   [        R                  " U/5      US'U	 S*3'   [        R                  " U/5      US'U	 S+3'   [        R                  " U/5      US'U	 S,3'   [        R                  " U/5      US'U	 S-3'   [        R                  " U/5      US'U	 S.3'   [        R                  " U/5      US'U	 S/3'   [        R                  " U/5      US'U	 S03'   [        R                  " U/5      US'U	 S13'   [        R                  " U/5      US'U	 S23'   [        R                  " U/5      US'U	 S33'   U(       ap  U R                  SU	 S435      US'U	 S53'   U R                  SU	 S635      US'U	 S73'   U R                  SU	 S835      US'U	 S93'   U R                  SU	 S:35      US'U	 S;3'   U R                  SU	 S<35      US'U	 S=3'   U R                  SU	 S>35      US'U	 S?3'   XS-
  :X  d8  U R                  SU	 S@35      US'U	 SA3'   U R                  SU	 SB35      US'U	 SC3'   X;   Ga  [        R                  " U R                  SU	 SD35      S!S"S#9u  nnn[        R                  " U R                  SU	 SE35      S!S"S#9u  nnn[        R                  " U/5      US'U	 SF3'   [        R                  " U/5      US'U	 SG3'   [        R                  " U/5      US'U	 SH3'   [        R                  " U/5      US'U	 SI3'   [        R                  " U/5      US'U	 SJ3'   [        R                  " U/5      US'U	 SK3'   U(       a8  U R                  SU	 SL35      US'U	 SM3'   U R                  SU	 SN35      US'U	 SO3'   U R                  SU	 SP35      US'U	 SQ3'   U R                  SU	 SR35      US'U	 SS3'   U R                  SU	 ST35      US'U	 SU3'   U R                  SU	 SV35      US'U	 SW3'   XS-
  :X  d9  U R                  SU	 SX35      US'U	 SY3'   U R                  SU	 SZ35      US'U	 S[3'   OH[        U R                  SU	 SX35      US#9US'U	 SY3'   [        U R                  SU	 SZ35      US#9US'U	 S[3'   U R                  SU	 S\35      US'U	 S]3'   U R                  SU	 S^35      US'U	 S_3'   U R                  SU	 S`35      US'U	 Sa3'   U R                  SU	 Sb35      US'U	 Sc3'   XS-
  :X  a  GM  U R                  SU	 Sd35      US'U	 Se3'   U R                  SU	 Sf35      US'U	 Sg3'   U R                  SU	 Sh35      US'U	 Si3'   U R                  SU	 Sj35      US'U	 Sk3'   GMG     U R                  Sl5      USm'   U R                  Sn5      USo'   [        U R                  Sp5      US#9USq'   [        U R                  Sr5      US#9USs'   U$ )tNr   r   c              3   n   #    U  H+  nS U;   d  M  [        UR                  SS5      S   5      v   M-     g7f)joint_blocksr{  r   r   Nr  r~  r   s     r   r   Bconvert_sd3_transformer_checkpoint_to_diffusers.<locals>.<genexpr>  s2     ]:a[\I\1#aggc1oa011:   
5%5r7  r   c              3   ,   #    U  H
  nS U;   v   M     g7f)ln_qNr9  rS  s     r   r   r    s     A/@fm/@   rE  pos_embed.pos_embedx_embedder.proj.weightpos_embed.proj.weightx_embedder.proj.biaspos_embed.proj.biasr=   1time_text_embed.timestep_embedder.linear_1.weightt_embedder.mlp.0.bias/time_text_embed.timestep_embedder.linear_1.biast_embedder.mlp.2.weight1time_text_embed.timestep_embedder.linear_2.weightt_embedder.mlp.2.bias/time_text_embed.timestep_embedder.linear_2.biasr  context_embedder.biaszy_embedder.mlp.0.weight-time_text_embed.text_embedder.linear_1.weightzy_embedder.mlp.0.bias+time_text_embed.text_embedder.linear_1.biaszy_embedder.mlp.2.weight-time_text_embed.text_embedder.linear_2.weightzy_embedder.mlp.2.bias+time_text_embed.text_embedder.linear_2.biaszjoint_blocks.z.x_block.attn.qkv.weightr   r   r  z.context_block.attn.qkv.weightz.x_block.attn.qkv.biasz.context_block.attn.qkv.biastransformer_blocks..attn.to_q.weight.attn.to_q.bias.attn.to_k.weight.attn.to_k.bias.attn.to_v.weight.attn.to_v.biasz.attn.add_q_proj.weightz.attn.add_q_proj.biasz.attn.add_k_proj.weightz.attn.add_k_proj.biasz.attn.add_v_proj.weightz.attn.add_v_proj.biasz.x_block.attn.ln_q.weightz.attn.norm_q.weightz.x_block.attn.ln_k.weightz.attn.norm_k.weightz.context_block.attn.ln_q.weightz.attn.norm_added_q.weightz.context_block.attn.ln_k.weightz.attn.norm_added_k.weightz.x_block.attn.proj.weightz.attn.to_out.0.weightz.x_block.attn.proj.biasz.attn.to_out.0.biasz.context_block.attn.proj.weightz.attn.to_add_out.weightz.context_block.attn.proj.biasz.attn.to_add_out.biasz.x_block.attn2.qkv.weightz.x_block.attn2.qkv.bias.attn2.to_q.weight.attn2.to_q.bias.attn2.to_k.weight.attn2.to_k.bias.attn2.to_v.weight.attn2.to_v.biasz.x_block.attn2.ln_q.weightz.attn2.norm_q.weightz.x_block.attn2.ln_k.weightz.attn2.norm_k.weightz.x_block.attn2.proj.weight.attn2.to_out.0.weightz.x_block.attn2.proj.bias.attn2.to_out.0.biasz".x_block.adaLN_modulation.1.weight.norm1.linear.weightz .x_block.adaLN_modulation.1.biasz.norm1.linear.biasz(.context_block.adaLN_modulation.1.weightz.norm1_context.linear.weightz&.context_block.adaLN_modulation.1.biasz.norm1_context.linear.biasz.x_block.mlp.fc1.weightz.ff.net.0.proj.weightz.x_block.mlp.fc1.biasz.ff.net.0.proj.biasz.x_block.mlp.fc2.weightz.ff.net.2.weightz.x_block.mlp.fc2.biasz.ff.net.2.biasz.context_block.mlp.fc1.weightz.ff_context.net.0.proj.weightz.context_block.mlp.fc1.biasz.ff_context.net.0.proj.biasz.context_block.mlp.fc2.weightz.ff_context.net.2.weightz.context_block.mlp.fc2.biasz.ff_context.net.2.biasfinal_layer.linear.weightr  final_layer.linear.biasr  %final_layer.adaLN_modulation.1.weightnorm_out.linear.weight#final_layer.adaLN_modulation.1.biasnorm_out.linear.bias)r}  r   popr   r  r  r  r   r  rg  r  r  r  )r  r   converted_state_dictr   r   
num_layersdual_attention_layersr  has_qk_normr  sample_qsample_ksample_v	context_q	context_k	context_vsample_q_biassample_k_biassample_v_biascontext_q_biascontext_k_biascontext_v_bias	sample_q2	sample_k2	sample_v2sample_q2_biassample_k2_biassample_v2_biass                               r   /convert_sd3_transformer_checkpoint_to_diffusersr%    sX   
!"D#q(BL..QRBSJyy!92>?  c]:]]^_abeffJ,Z87
CAz/@AAK 3=..2M./4>NNC[4\012<..AW2X./ Q[P^P^!QLM OYnn]tNuJKPZP^P^!QLM OYnn]tNuJK 7AnnE^6_234>NNCZ4[01 MWNN[tLuHIJT..YpJqFGLVNN[tLuHIJT..YpJqFG :',{{NN]1#-EFGPQ(
$H +0++NN]1#-KLMqVW+
'	i 7<kkNN]1#-CDEqa7
3}m :?NN]1#-IJKQTU:
6 LQ99V^U_K`21#5FGHINTaSbIc21#_EFKP99V^U_K`21#5FGHINTaSbIc21#_EFKP99V^U_K`21#5FGHINTaSbIc21#_EFQVQZQZ\e[fQg21#5LMNOTyyZhYiOj21#5JKLQVQZQZ\e[fQg21#5LMNOTyyZhYiOj21#5JKLQVQZQZ\e[fQg21#5LMNOTyyZhYiOj21#5JKL Q[Q_Q_s";<R #6qc9L!MN R\Q_Q_s";<R #6qc9L!MN XbWeWes"ABX #6qc9R!ST XbWeWes"ABX #6qc9R!ST
 PZ~~A378P
21#5JKL NX^^A356N
21#5HIJ !^#U_UcUcs"ABV #6qc9P!QR T^SaSas"?@T #6qc9N!OP %.3kkqc1JKLaUV/+Iy) >C[[qc1HIJAST>:NNN QVPYPY[dZePf #6qc9K!LMNSiiYgXhNi #6qc9I!JKPUPYPY[dZePf #6qc9K!LMNSiiYgXhNi #6qc9I!JKPUPYPY[dZePf #6qc9K!LMNSiiYgXhNi #6qc9I!JK V`VdVd#A3&@AW$':1#=Q%RS WaVdVd#A3&@AW$':1#=Q%RS
 U_TbTbs"<=U #6qc9O!PQ S]R`R`s":;S #6qc9M!NO
 OYnnA3@AO
21#5IJK MWNNA3>?M
21#5GHI !^#ZdZhZhs"JK[ #6qc9U!VW YcXfXfs"HIY #6qc9S!TU [kqc1YZ[*[ #6qc9U!VW Yiqc1WXY*Y #6qc9S!TU PZ~~A356P
21#5JKL NX^^A334N
21#5HIJ KU..A356K
21#5EFG ISA334I
21#^DE !^#[e[i[is"?@\ #6qc9V!WX ZdYgYgs"=>Z #6qc9T!UV WaVdVds"?@W #6qc9Q!RS U_TbTbs"=>U #6qc9O!PQU ^ /9nn=X.Y*+,6NN;T,U)5E>?E[612 4D<=CY4/0  r   c                     SU ;   a  gg)Nz-text_encoders.t5xxl.transformer.shared.weightTFr9  r   s    r   is_t5_in_single_filer'  u  s    6*Dr   c                     [        U R                  5       5      n0 nS/nU HI  nU H@  nUR                  U5      (       d  M  UR                  US5      nU R	                  U5      X&'   MB     MK     U$ )Nz text_encoders.t5xxl.transformer.r   )r}  r   r  r   r  )r  r   rc  rd  r;  r   r  s          r   &convert_sd3_t5_checkpoint_to_diffusersr)  |  sl    
!"DO9:O%F~~f%% #FB 71;1D. &  r   c                   ^ U(       a  SU0nO[        U5      nU R                  R                  " S0 UDX%S.D6n[        5       (       a  [        O[
        nU" 5          U " U5      nS S S 5        [        U5      n	[        5       (       a  [        WXS9  [        5         OWR                  U	5        U R                  S L=(       a    U[        R                  :H  n
U
(       a  UR                  nO/ nUbd  UR                  5        HP  u  mn[        U4S jU 5       5      (       d  M"  UR                  R!                  [        R"                  5      Ul        MR     U$ ! , (       d  f       N= f)NrY   r~  r  c              3   J   >#    U  H  oTR                  S 5      ;   v   M     g7f)r{  N)r~  )r   module_to_keep_in_fp32r   s     r   r   <create_diffusers_t5_model_from_checkpoint.<locals>.<genexpr>  s     p[oAWTZZ_<[os    #r9  )ru  r  r  r   r   r   r)  r   r   r   _keep_in_fp32_modulesrg  float16named_parametersr   datar  float32)r  r  r^   r  r  r  r  r  r  r  use_keep_in_fp32_moduleskeep_in_fp32_modulesparamr   s                @r   )create_diffusers_t5_model_from_checkpointr6    s"    16:'
3##33ufu	uL 7 9 9
{C	L! 
 #I"T  !%)DX9: # 9 9 EiK[`[h[hLh$::!' 113KD%p[oppp"ZZ]]5==9
 4
 L/ 
s   	E
E!c                    0 nU R                  5        Hr  u  p4SU;   a  M  UUUR                  SS5      R                  SS5      R                  SS5      R                  SS	5      R                  S
S5      R                  SS5      '   Mt     U$ )Npos_encoderz.norms.0z.norm1z.norms.1z.norm2z.ff_normz.norm3z.attention_blocks.0.attn1z.attention_blocks.1.attn2z.temporal_transformerr   )r+  r   )r  r   r  r   vs        r   +convert_animatediff_checkpoint_to_diffusersr<    s      "A  !		*h/X.X..9.90"5 #  r   c                    0 n[        U R                  5       5      nU H.  nSU;   d  M  U R                  U5      XR                  SS5      '   M0     [        [	        S U  5       5      5      S   S-   n[        [	        S U  5       5      5      S   S-   nSnSnS	 n	U R                  S
5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   [        S U  5       5      n
U
(       aP  U R                  S5      US'   U R                  S5      US'   U R                  S5      US '   U R                  S!5      US"'   U R                  S#5      US$'   U R                  S%5      US&'   U R                  S'5      US('   U R                  S)5      US*'   [        U5       GH  nS+U S,3nU R                  S-U S.35      X, S/3'   U R                  S-U S035      X, S13'   U R                  S-U S235      X, S33'   U R                  S-U S435      X, S53'   [        R                  " U R                  S-U S635      S7S8S99u  pn[        R                  " U R                  S-U S:35      S7S8S99u  nnn[        R                  " U R                  S-U S;35      S7S8S99u  nnn[        R                  " U R                  S-U S<35      S7S8S99u  nnn[        R                  " U/5      X, S=3'   [        R                  " U/5      X, S>3'   [        R                  " U/5      X, S?3'   [        R                  " U/5      X, S@3'   [        R                  " U/5      X, SA3'   [        R                  " U/5      X, SB3'   [        R                  " U/5      X, SC3'   [        R                  " U/5      X, SD3'   [        R                  " U/5      X, SE3'   [        R                  " U/5      X, SF3'   [        R                  " U/5      X, SG3'   [        R                  " U/5      X, SH3'   U R                  S-U SI35      X, SJ3'   U R                  S-U SK35      X, SL3'   U R                  S-U SM35      X, SN3'   U R                  S-U SO35      X, SP3'   U R                  S-U SQ35      X, SR3'   U R                  S-U SS35      X, ST3'   U R                  S-U SU35      X, SV3'   U R                  S-U SW35      X, SX3'   U R                  S-U SY35      X, SZ3'   U R                  S-U S[35      X, S\3'   U R                  S-U S]35      X, S^3'   U R                  S-U S_35      X, S`3'   U R                  S-U Sa35      X, Sb3'   U R                  S-U Sc35      X, Sd3'   U R                  S-U Se35      X, Sf3'   U R                  S-U Sg35      X, Sh3'   GM     [        U5       GH  nSiU S,3nU R                  SjU Sk35      X, Sl3'   U R                  SjU Sm35      X, Sn3'   [        X-  5      nXUU4n[        R                  " U R                  SjU So35      US8S99u  nnnn[        R                  " U R                  SjU Sp35      US8S99u  nnn n![        R                  " U/5      X, S=3'   [        R                  " U/5      X, S>3'   [        R                  " U/5      X, S?3'   [        R                  " U/5      X, S@3'   [        R                  " U/5      X, SA3'   [        R                  " U /5      X, SB3'   [        R                  " U/5      X, Sq3'   [        R                  " U!/5      X, Sr3'   U R                  SjU Ss35      X, SJ3'   U R                  SjU St35      X, SL3'   U R                  SjU Su35      X, Sv3'   U R                  SjU Sw35      X, Sx3'   GM     U R                  Sy5      USv'   U R                  Sz5      USx'   U	" U R                  S{5      5      US|'   U	" U R                  S}5      5      US~'   U$ )Nr   r   c              3   n   #    U  H+  nS U;   d  M  [        UR                  SS5      S   5      v   M-     g7fdouble_blocks.r{  r   r   Nr  r   s     r   r   Cconvert_flux_transformer_checkpoint_to_diffusers.<locals>.<genexpr>  3     _:aIY]^I^1#aggc1oa011:r  r7  r   c              3   n   #    U  H+  nS U;   d  M  [        UR                  SS5      S   5      v   M-     g7fsingle_blocks.r{  r   r   Nr  r   s     r   r   rA    3      f*QP`dePe!8QWWS!_Q%7!8!8*r        @   c                 V    U R                  SSS9u  p[        R                  " X!/SS9nU$ r  r  r  r  r  r  s       r   r  Jconvert_flux_transformer_checkpoint_to_diffusers.<locals>.swap_scale_shift  .    ||A1|-YY~15
r   ztime_in.in_layer.weightr  ztime_in.in_layer.biasr  ztime_in.out_layer.weightr  ztime_in.out_layer.biasr  zvector_in.in_layer.weightr  zvector_in.in_layer.biasr  zvector_in.out_layer.weightr  zvector_in.out_layer.biasr  c              3   ,   #    U  H
  nS U;   v   M     g7f)guidanceNr9  r   s     r   r   rA    s     ;
1zQ
r  zguidance_in.in_layer.weightz1time_text_embed.guidance_embedder.linear_1.weightrM  z/time_text_embed.guidance_embedder.linear_1.biaszguidance_in.out_layer.weightz1time_text_embed.guidance_embedder.linear_2.weightzguidance_in.out_layer.biasz/time_text_embed.guidance_embedder.linear_2.biastxt_in.weightr  txt_in.biasr  rN  x_embedder.weightimg_in.biasx_embedder.biasr  r{  r@  z.img_mod.lin.weightnorm1.linear.weightz.img_mod.lin.biasnorm1.linear.biasz.txt_mod.lin.weightnorm1_context.linear.weightz.txt_mod.lin.biasnorm1_context.linear.bias.img_attn.qkv.weightr   r   r  .txt_attn.qkv.weight.img_attn.qkv.bias.txt_attn.qkv.biasattn.to_q.weightattn.to_q.biasattn.to_k.weightattn.to_k.biasattn.to_v.weightattn.to_v.biasattn.add_q_proj.weightattn.add_q_proj.biasattn.add_k_proj.weightattn.add_k_proj.biasattn.add_v_proj.weightattn.add_v_proj.bias.img_attn.norm.query_norm.scaleattn.norm_q.weight.img_attn.norm.key_norm.scaleattn.norm_k.weight.txt_attn.norm.query_norm.scaleattn.norm_added_q.weight.txt_attn.norm.key_norm.scaleattn.norm_added_k.weight.img_mlp.0.weightff.net.0.proj.weight.img_mlp.0.biasff.net.0.proj.bias.img_mlp.2.weightff.net.2.weight.img_mlp.2.biasff.net.2.bias.txt_mlp.0.weightff_context.net.0.proj.weight.txt_mlp.0.biasff_context.net.0.proj.bias.txt_mlp.2.weightff_context.net.2.weight.txt_mlp.2.biasff_context.net.2.bias.img_attn.proj.weightattn.to_out.0.weight.img_attn.proj.biasattn.to_out.0.bias.txt_attn.proj.weightattn.to_add_out.weight.txt_attn.proj.biasattn.to_add_out.biassingle_transformer_blocks.rE  z.modulation.lin.weightznorm.linear.weightz.modulation.lin.biasznorm.linear.bias.linear1.weight.linear1.biasproj_mlp.weightproj_mlp.bias.norm.query_norm.scale.norm.key_norm.scale.linear2.weightr  .linear2.biasr  r  r	  r
  r  r  r  )r}  r   r  r   r  r   r  rg  r  r  r  r~  )"r  r   r  r   r   r  num_single_layers	mlp_ratio	inner_dimr  has_guidancer  block_prefixr  r  r  r  r  r  r  r  r  r  r  r  mlp_hidden_dim
split_sizeqr;  mlpq_biask_biasv_biasmlp_biass"                                     r   0convert_flux_transformer_checkpoint_to_diffusersr    s
   
!"D#q(BL..QRBSJyy!92>?  c_:__`acdghhJS f* ffghjknooII Q[P^P^!QLM OYnn]tNuJKPZP^P^"QLM OYnn]uNvJK MWNN[vLwHIJT..YrJsFGLVNN$MHI KU..YsJtFG ;
;;LT^TbTb)U
PQ S]R`R`'S
NO U_TbTb*U
PQ S]R`R`(S
NO
 7Ann_6U234>NN=4Q01 1;0O,-.8nn].K*+ :,QCq1 FP^^QC23F
~-@AB DN>>QC01D
~->?@ NX^^QC23N
~-HIJ LV>>QC01L
~-FGH (-{{:>>NSTRUUiBj3kmntu'v$H*/++NN^A3.BCDaQ+
'	9i 7<kkNN^A3.@ABA17
3}m :?NN^A3.@ABA1:
6 CH))XJBW~-=>?@E		=/@Z~^<=BG))XJBW~-=>?@E		=/@Z~^<=BG))XJBW~-=>?@E		=/@Z~^<=HM		S\R]H^~-CDEFKiiQ_P`Fa~-ABCHM		S\R]H^~-CDEFKiiQ_P`Fa~-ABCHM		S\R]H^~-CDEFKiiQ_P`Fa~-ABCDNNNQC>?E
~-?@A EONNQC<=E
~-?@A KU..QC>?K
~-EFG KU..QC<=K
~-EFG GQnnQC01G
~-ABC EONNUcdecffuSvDw~-?@AAKR`ab`cctPuAv~_=>?I~~P^_`^aapNq?r~];<NXnnQC01O
~-IJK MWNNQC/M
~-GHI JTQC01J
~-DEF HR~~QC/H
~-BCD GQnnQC45G
~-ABC EONNQC23E
~-?@A ISQC45I
~-CDE GQnnQC23G
~-ABCe n $%3A3a8DNNNQC56E
~-?@A CM..QC34C
~-=>? Y23I~F
{{:>>N1#_2U#VXbhij1a+0;;NN^A3m<=zq,
( CH))QC.~-=>?@E		6(@S~^<=BG))QC.~-=>?@E		6(@S~^<=BG))QC.~-=>?@E		6(@S~^<=AFC5AQ~_=>?Dyy(?T~];<DNNNQC56E
~-?@A EONNQC34E
~-?@A BLR`ab`ccrPsAt~_=>?I~~P^_`^aanNo?p~];<C &F /9nn=X.Y*+,6NN;T,U)5E>?612 4D<=4/0  r   c                    [        U R                  5       5       Vs0 s H  nSU;  d  M  X R                  U5      _M     nnSSSSSS.n0 n[        UR                  5       5       HB  nUnUR                  5        H  u  pxUR	                  Xx5      nM     UR                  U5      X6'   MD     [        UR                  5       5       H+  nUR                  5        H  u  pX;  a  M  U
" X#5        M     M-     U$ s  snf )Nr   r   proj_in
time_embednorm_qnorm_k)r   patchify_projadaln_singleq_normk_norm)r}  r   r  r+  r   )r  r   r;  r  TRANSFORMER_KEYS_RENAME_DICTTRANSFORMER_SPECIAL_KEYS_REMAPru  replace_key
rename_keyspecial_keyhandler_fn_inplaces              r   /convert_ltx_transformer_checkpoint_to_diffusersr  	  s   @DZ__EV@Wl@W[`hk[k4C!44@Wl #%"$$  &("(--/0'C'I'I'K#Kook>G (L(<(@(@(E%	 1 (--/0/M/S/S/U+K%s9 0V 1  1 ms
   
C4C4c                    [        U R                  5       5       Vs0 s H  nSU;   d  M  X R                  U5      _M     nnS[        4S jn0 SS_SS_SS_SS	_S
S_SS_SS_SS_SS_SS_SS
_SS_SS_SS_SS_SS_SS_SSSSS S!S"S#S$S%.	EnSS&SS	SSSSS
S'S(S).n0 SS_SS&_SS_S
S	_SS_SS_SS_SS_SS
_SS_SS_SS_SS_SS_SS_S*S_S+S,_SS'S(S-.EnUUUS..nUS/   R                  S0   S1:X  a  UR                  U5        OS2U;   a  UR                  U5        [        UR                  5       5       HB  nUn	UR                  5        H  u  pU	R                  X5      n	M     UR                  U5      X9'   MD     [        UR                  5       5       H+  nUR                  5        H  u  pX;  a  M  U" X#5        M     M-     U$ s  snf )3Nr   r;  c                 &    UR                  U 5        g r   r  r;  r  s     r   remove_keys_=convert_ltx_vae_checkpoint_to_diffusers.<locals>.remove_keys_	      sr   r   up_blocks.0	mid_blockup_blocks.1up_blocks.2up_blocks.1.upsamplers.0up_blocks.3up_blocks.4zup_blocks.2.conv_inup_blocks.5up_blocks.2.upsamplers.0up_blocks.6up_blocks.7zup_blocks.3.conv_inup_blocks.8zup_blocks.3.upsamplers.0zup_blocks.9down_blocks.0down_blocks.1down_blocks.0.downsamplers.0down_blocks.2zdown_blocks.0.conv_outdown_blocks.3down_blocks.4down_blocks.1.downsamplers.0down_blocks.5zdown_blocks.1.conv_outdown_blocks.2.downsamplers.0zconv_shortcut.convr<  norm3r  r  )	down_blocks.6down_blocks.7down_blocks.8zdown_blocks.9r  
res_blocksz
norm3.norm$per_channel_statistics.mean-of-means#per_channel_statistics.std-of-meansup_blocks.0.upsamplers.0time_embedderscale_shift_table)r  r  r  r  r  r  r  r  r  last_time_embedderlast_scale_shift_tabler  r  down_blocks.3.downsamplers.0)r  r  r  )per_channel_statistics.channelr  #per_channel_statistics.mean-of-stdsrU  r   r?  rV  )r}  r   r  strrD  r  r+  r   )r  r   r;  r  r  VAE_KEYS_RENAME_DICTVAE_091_RENAME_DICTVAE_095_RENAME_DICTVAE_SPECIAL_KEYS_REMAPru  r  r  r  r  s                 r   'convert_ltx_vae_checkpoint_to_diffusersr  	  sZ   @DZ__EV@Wi@W[aeh[h4C!44@Wi#  	{	
 	} 	1 	} 	, 	1 	} 	, 	1 	} 	  	7!" 	1#$ 	%& 	7'( 	1)* )7($-0>/<=F #1$1$1$1$-"5 { 	1 	}	
 	1 	} 	1 	} 	1 	} 	 	7 	 	7  	!" 	7#$ 	%& 	7'( %-"5/6 +70</; >?EEaHDP##$78	KOc	c##$78(--/0';'A'A'C#Kook>G (D(<(@(@(E%	 1 (--/0/E/K/K/M+K%s9 0N 1  O js
   
GGc                    [        U R                  5       5       Vs0 s H  o"U R                  U5      _M     nnS[        4S jnS[        4S jn0 SS_SS_SS_S	S
_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_S S!_S"S#_S$S%S&S'S(S).EnS*S+S,.nUUS-.nS.U;  a  UR	                  U5        [        UR                  5       5       HE  nUS S  n	UR                  5        H  u  pU	R                  X5      n	M     UR                  U5      X9'   MG     [        UR                  5       5       H+  nUR                  5        H  u  pX;  a  M  U" X#5        M     M-     U$ s  snf )/Nr;  c                     UR                  U 5      n[        R                  " USSS9u  p4nU R                  S5      u  n  nUR	                  5       X S3'   UR	                  5       X S3'   UR	                  5       X S3'   g )Nr   r   r  z.qkv.conv.weightz.to_q.weightz.to_k.weightz.to_v.weight)r  rg  r  
rpartitionsqueeze)r;  r  qkvr  r   r;  parent_module_s           r   
remap_qkv_Bconvert_autoencoder_dc_checkpoint_to_diffusers.<locals>.remap_qkv_
  s{    nnS!++c1!,a!nn-?@q!56YY[
_L1256YY[
_L1256YY[
_L12r   c                 v    U R                  S5      u  n  nUR                  U 5      R                  5       X S3'   g )Nz.proj.conv.weightz.to_out.weight)r  r  r  )r;  r  r  r  s       r   remap_proj_conv_Hconvert_autoencoder_dc_checkpoint_to_diffusers.<locals>.remap_proj_conv_
  s9    !nn-@Aq!7A~~c7J7R7R7T
_N34r   zmain.r   zop_list.context_moduleattnlocal_moduleconv_outz
aggreg.0.0zto_qkv_multiscale.0.proj_inz
aggreg.0.1zto_qkv_multiscale.0.proj_outzdepth_conv.conv
conv_depthzinverted_conv.convconv_invertedzpoint_conv.conv
conv_pointzpoint_conv.normnormz
conv.conv.zconv.z
conv1.convr  
conv2.convr  z
conv2.normz	proj.normnorm_outencoder.project_in.convzencoder.conv_inzencoder.project_out.0.convzencoder.conv_outzencoder.down_blockszdecoder.conv_inzdecoder.norm_outzdecoder.conv_outzdecoder.up_blocks)zencoder.stageszdecoder.project_in.convzdecoder.project_out.0decoder.project_out.2.convzdecoder.stageszencoder.conv_in.convzdecoder.conv_out.conv)r  r  )zqkv.conv.weightzproj.conv.weightrB   )r}  r   r  r  r  r+  r   )r  r   r;  r  r  r  AE_KEYS_RENAME_DICTAE_F32C32_F64C128_F128C512_KEYSAE_SPECIAL_KEYS_REMAPru  r  r  r  r  s                 r   .convert_autoencoder_dc_checkpoint_to_diffusersr  
  s    @DZ__EV@WX@W!44@WXA AUc U 	B 	&	
 	
 	3 	4 	< 	o 	< 	6 	g 	g  	g!" 	f#$ 	Z%( 	"#4)* 	%&8+, 0#4!3&8-7> $:&='# &, &-AA""#BC(--/0a&':'@'@'B#Kook>G (C(<(@(@(E%	 1 (--/0/D/J/J/L+K%s9 0M 1  I Ys   Ec                    0 n[        U R                  5       5      nU H.  nSU;   d  M  U R                  U5      XR                  SS5      '   M0     U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S	5      US
'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   Sn[	        U5       GHx  nS U S!3nS"U S!3nU R                  US#-   5      X'S$-   '   U R                  US%-   5      X'S&-   '   XeS'-
  :  a3  U R                  US(-   5      X'S)-   '   U R                  US*-   5      X'S+-   '   O2U R                  US(-   5      X'S,-   '   U R                  US*-   5      X'S--   '   U R                  US.-   5      n	U	R                  S/S0S19u  pnXUS2-   '   XBUS3-   '   XUS4-   '   U R                  US5-   5      X'S6-   '   U R                  US7-   5      X'S8-   '   U R                  US9-   5      X'S:-   '   U R                  US;-   5      X'S<-   '   U R                  US=-   5      n	U	R                  S/S0S19u  pnXUS>-   '   XBUS?-   '   XUS@-   '   U R                  USA-   5      X'SB-   '   U R                  USC-   5      X'SD-   '   XeS'-
  :  a2  U R                  USE-   5      X'SF-   '   U R                  USG-   5      X'SH-   '   [        U R                  USI-   5      5      X'SJ-   '   U R                  USK-   5      X'SL-   '   XeS'-
  :  d  GM=  [        U R                  USM-   5      5      X'SN-   '   U R                  USO-   5      X'SP-   '   GM{     [        U R                  SQ5      S0S19USR'   [        U R                  SS5      S0S19UST'   U R                  SU5      USV'   U R                  SW5      USX'   U R                  SY5      USY'   U$ )ZNr   r   r  patch_embed.proj.weightr  patch_embed.proj.biasr=   z,time_embed.timestep_embedder.linear_1.weightr  z*time_embed.timestep_embedder.linear_1.biasr  z,time_embed.timestep_embedder.linear_2.weightr  z*time_embed.timestep_embedder.linear_2.biaszt5_y_embedder.to_kv.weightztime_embed.pooler.to_kv.weightzt5_y_embedder.to_kv.biasztime_embed.pooler.to_kv.biaszt5_y_embedder.to_q.weightztime_embed.pooler.to_q.weightzt5_y_embedder.to_q.biasztime_embed.pooler.to_q.biaszt5_y_embedder.to_out.weightztime_embed.pooler.to_out.weightzt5_y_embedder.to_out.biasztime_embed.pooler.to_out.biaszt5_yproj.weightztime_embed.caption_proj.weightzt5_yproj.biasztime_embed.caption_proj.bias0   r  r{  blocks.zmod_x.weightrT  z
mod_x.biasrU  r   zmod_y.weightrV  z
mod_y.biasrW  znorm1_context.linear_1.weightznorm1_context.linear_1.biaszattn.qkv_x.weightr   r   r  zattn1.to_q.weightzattn1.to_k.weightzattn1.to_v.weightzattn.q_norm_x.weightzattn1.norm_q.weightzattn.k_norm_x.weightzattn1.norm_k.weightzattn.proj_x.weightzattn1.to_out.0.weightzattn.proj_x.biaszattn1.to_out.0.biaszattn.qkv_y.weightzattn1.add_q_proj.weightzattn1.add_k_proj.weightzattn1.add_v_proj.weightzattn.q_norm_y.weightzattn1.norm_added_q.weightzattn.k_norm_y.weightzattn1.norm_added_k.weightzattn.proj_y.weightzattn1.to_add_out.weightzattn.proj_y.biaszattn1.to_add_out.biaszmlp_x.w1.weightrq  zmlp_x.w2.weightru  zmlp_y.w1.weightry  zmlp_y.w2.weightr}  zfinal_layer.mod.weightr  zfinal_layer.mod.biasr  r  r  r	  r  pos_frequencies)r}  r   r  r   r  r  r  r  )r  r   r  r   r   r  r  r  
old_prefix
qkv_weightr  r;  s               r   1convert_mochi_transformer_checkpoint_to_diffusersr  [
  s    
!"D#q(BL..QRBSJyy!92>? 
 7AnnE]6^234>NNCY4Z01 LV>>ZsKtGHISXoIpEFKU>>ZsKtGHISXoIpEF=G^^Lh=i9:;E>>Jd;e78<FNNKf<g89:D..Ib:c67>HnnMj>k:;<FNNKf<g89=G^^L]=^9:;E>>/;Z78 J:,QCq1qc^
 FP^^T^aoToEp,AABCM>>R\_kRkCl,??@A~Q[Q_Q_^+R 0M!MN PZ~~\)P 0K!KL T^SaSa^+T 0O!OP R\Q_Q_\)R 0M!MN
  ^^J1D$DE
""1!",aCD\,??@CD\,??@CD\,??@EO^^//F
,AAB FP^^//F
,AAB HR~~--H
,CCD FP^^T^asTsEt,AAB  ^^J1D$DE
""1!",aIJ\,EEFIJ\,EEFIJ\,EEFKU>>//L
,GGH LV>>//L
,GGH A~MW^^11N 0I!IJ LV>>//L 0G!GH
 GUNN:(99:G
,BBC BLPZ]nPnAo,==>A~R`z,==>S 0N!NO NX^^..N 0I!IJM V 6FjnnUmFntu5v123CJNNSiDjpq3r/0.8nn=X.Y*+,6NN;T,U).8nn=N.O*+r   c                    S nS nS nS nS n0 SS_SS	_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_S S!_S"S#_S$S%_S&S'_S(S)S%S*S+SS,S-S.S/S0S1S2S3.EnUUUUUS4.nS5 n	[        U R                  5       5       H;  n
U
S S  nUR                  5        H  u  pUR                  X5      nM     U	" X
U5        M=     [        U R                  5       5       H+  n
UR                  5        H  u  pX;  a  M  U" X5        M     M-     U $ )6Nc                     UR                  U 5      nUR                  SSS9u  p4[        R                  " XC/SS9nXQU R	                  SS5      '   g )Nr   r   r  final_layer.adaLN_modulation.1norm_out.linear)r  r  rg  r  r   )r;  r  r  r  r  r  s         r   remap_norm_scale_shift_Oconvert_hunyuan_video_transformer_to_diffusers.<locals>.remap_norm_scale_shift_
  sK    $||A1|-YY~15
Wa3;;?ARSTr   c                 .   S nSU ;   as  UR                  U 5      nUR                  SSS9u  pEnXAU" U R                  SS5      5      '   XQU" U R                  SS5      5      '   XaU" U R                  SS5      5      '   g UR                  U 5      X" U 5      '   g )	Nc                    U R                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S
5      nUR                  SS5      nUR                  SS5      nU$ )Nzindividual_token_refiner.blocksztoken_refiner.refiner_blocksadaLN_modulation.1r  txt_incontext_embeddert_embedder.mlp.0*time_text_embed.timestep_embedder.linear_1t_embedder.mlp.2*time_text_embed.timestep_embedder.linear_2
c_embedderztime_text_embed.text_embedderr  ff)r   )r;  ru  s     r   r  Yconvert_hunyuan_video_transformer_to_diffusers.<locals>.remap_txt_in_.<locals>.rename_key
  s    kk"CEcdGoo&:<MNGooh0BCGoo&8:fgGoo&8:fgGool4STGooeT2GNr   self_attn_qkvr   r   r  	attn.to_q	attn.to_k	attn.to_vr  r  r   )r;  r  r  r  to_qto_kto_vs          r   remap_txt_in_Econvert_hunyuan_video_transformer_to_diffusers.<locals>.remap_txt_in_
  s    	 c!^^C(F%||A1|5DPTz#++o{"KLMPTz#++o{"KLMPTz#++o{"KLM*4..*=Jz#'r   c                     UR                  U 5      nUR                  SSS9u  p4nX1U R                  SS5      '   XAU R                  SS5      '   XQU R                  SS5      '   g )Nr   r   r  img_attn_qkvr  r  r  r  r;  r  r  r  r  r  s         r   remap_img_attn_qkv_Kconvert_hunyuan_video_transformer_to_diffusers.<locals>.remap_img_attn_qkv_
  s`    $!<<q<1D?C3;;~{;<?C3;;~{;<?C3;;~{;<r   c                     UR                  U 5      nUR                  SSS9u  p4nX1U R                  SS5      '   XAU R                  SS5      '   XQU R                  SS5      '   g )Nr   r   r  txt_attn_qkvattn.add_q_projattn.add_k_projattn.add_v_projr  r#  s         r   remap_txt_attn_qkv_Kconvert_hunyuan_video_transformer_to_diffusers.<locals>.remap_txt_attn_qkv_
  sc    $!<<q<1DEI3;;~/@ABEI3;;~/@ABEI3;;~/@ABr   c                    SnSU ;   a  UR                  U 5      nX"X#R                  S5      SU-  -
  4n[        R                  " X4SS9u  pVpxU R	                  SS5      R                  S5      n	XQU	 S	3'   XaU	 S
3'   XqU	 S3'   XU	 S3'   g SU ;   a  UR                  U 5      n
X"X*R                  S5      SU-  -
  4n[        R                  " XSS9u  ppU R	                  SS5      R                  S5      n	XU	 S3'   XU	 S3'   XU	 S3'   XU	 S3'   g U R	                  SS5      n	U	R	                  SS5      n	U	R	                  SS5      n	U	R	                  SS5      n	UR                  U 5      X'   g )NrH  zlinear1.weightr   r   r  single_blockssingle_transformer_blocksr  r  r  r  z.proj_mlp.weightzlinear1.biasr  r  r  r  z.proj_mlp.biaslinear2proj_outr  attn.norm_qr  attn.norm_k)r  sizerg  r~  r   removesuffix)r;  r  rg  linear1_weightr  r  r   r;  r  ru  linear1_biasr  r  r  r  s                  r    remap_single_transformer_blocks_Xconvert_hunyuan_video_transformer_to_diffusers.<locals>.remap_single_transformer_blocks_
  s   s"'^^C0N%KATATUVAWZ[^iZiAijJ ;;~qILA!kk/3NO\\]noG89'"34589'"34589'"3457:'"234s"%>>#.L%KARARSTAUXY\gXgAghJ/4{{<YZ/[,FFkk/3NO\\]lmG6<'/236<'/236<'/235='.12 kk/3NOGooi<Gooh>Gooh>G",.."5Jr   img_in
x_embedderztime_in.mlp.0r  ztime_in.mlp.2r  zguidance_in.mlp.0z*time_text_embed.guidance_embedder.linear_1zguidance_in.mlp.2z*time_text_embed.guidance_embedder.linear_2zvector_in.in_layerz&time_text_embed.text_embedder.linear_1zvector_in.out_layerz&time_text_embed.text_embedder.linear_2double_blockstransformer_blocksimg_attn_q_normr2  img_attn_k_normr3  img_attn_projattn.to_out.0txt_attn_q_normattn.norm_added_qtxt_attn_k_normattn.norm_added_ktxt_attn_projattn.to_add_outzimg_mod.linearnorm1.linear	img_norm1
norm1.norm	img_norm2r  r  znorm1_context.linearnorm2_context
ff_contextznorm.linearz	norm.normznorm_out.normr1  
net.0.projnet.2r  )img_mlpztxt_mod.linear	txt_norm1	txt_norm2txt_mlpself_attn_projzmodulation.linearpre_normzfinal_layer.norm_finalfinal_layer.linearfc1fc2input_embedder)r  r"  r'  r.  r  c                 *    U R                  U5      X'   g r   r  r  rv  ru  s      r   update_state_dict_Jconvert_hunyuan_video_transformer_to_diffusers.<locals>.update_state_dict_=      (nnW5
r   )r}  r   r+  r   )r  r   r	  r  r$  r+  r8  r  r  r\  r;  ru  r  r  r  r  s                   r   .convert_hunyuan_video_transformer_to_diffusersr_  
  s   b>(DJ6<$,$E$ 	E$ 	I	$
 	I$ 	F$ 	G$ 	-$ 	=$ 	=$ 	$ 	.$ 	.$ 	*$ 	.$  	\!$" 	W#$$ 0!$)*"1(#=$ D  ++9*A&"6 JOO%&a&'C'I'I'K#Kook>G (L:G4	 ' JOO%&/M/S/S/U+K%s/ 0V ' r   c                    0 n[        U R                  5       5      nU R                  SS 5      US'   U R                  SS 5      US'   U R                  SS 5      US'   U R                  SS 5      US'   U R                  SS 5      US	'   U R                  S
S 5      US'   S nU" USS9nU" USS9n[        U5       H  nSSS.nSSSS.n	UR	                  5        HG  u  pU	R	                  5        H.  u  pU R                  SU SU
 SU S3S 5      USU SU SU S3'   M0     MI     SSS.nUR	                  5        H(  u  pU R                  SU SU
 S3S 5      USU SU S3'   M*     S S!S"S#S$.nS%S&S'S(S).nX4 H?  nUR	                  5        H(  u  pU R                  SU S*U S3S 5      USU S*U S3'   M*     MA     GM     [        U5       H  nSSSS.nUR	                  5        H(  u  pU R                  S+U S,U S3S 5      US-U S.U S3'   M*     U R                  S+U S/3S 5      US-U S03'   S S!S"S#S).nUR	                  5        H(  u  pU R                  S+U S*U S3S 5      US-U S*U S3'   M*     M     U R                  S1S 5      US2'   U R                  S3S 5      nUb  [        US S49US5'   OS US5'   U R                  S65      US7'   U R                  S85      US9'   U R                  S:5      US;'   U$ )<Nregister_tokensr=   ztime_step_proj.linear_1.weightr  ztime_step_proj.linear_1.biasr  ztime_step_proj.linear_2.weightr  ztime_step_proj.linear_2.biasr<   r  c                     [        5       nU  H8  nX;   d  M
  [        UR                  S5      S   5      nUR                  U5        M:     [	        U5      $ )Nr{  r   )r  r  r~  addr  )r   
key_prefixr   r   r  s        r   calculate_layersNconvert_auraflow_transformer_checkpoint_to_diffusers.<locals>.calculate_layers`  sF    AQ0	

9%  6{r   double_layers)rd  single_layersr  rM  )mlpXmlpClinear_1linear_2out_projection)c_fc1c_fc2c_projzdouble_layers.r{  r@  zjoint_transformer_blocks.r  norm1_context)modXmodCz	.1.weightz.linear.weightr  r  r  to_out.0)w2qw2kw2vw2o
add_q_proj
add_k_proj
add_v_proj
to_add_out)w1qw1kw1vw1oz.attn.zsingle_layers.z.mlp.r  z.ff.z.modCX.1.weightr  zfinal_linear.weightr  zmodF.1.weightr  r  positional_encodingr  zinit_x_linear.weightr  zinit_x_linear.biasr  )r}  r   r  r  r+  r  )r  r   r  state_dict_keysre  mmdit_layerssingle_dit_layersr  path_mappingweight_mappingorig_k
diffuser_kr   r;  x_attn_mappingcontext_attn_mappingattn_mappingr  norm_weights                      r   4convert_auraflow_transformer_checkpoint_to_diffusersr  O  s   :??,-O /9nn=NPT.U*+ >H^^Legk=l9:;E>>Jacg;h78=G^^Legk=l9:;E>>Jacg;h78 7AnnE]_c6d23 $OPL(_U <  $l;#-
N^_"."4"4"6F&,,.`j`n`n$QCq!G<da$'@1ZLPQRSQTT[%\] / #7 !(A"."4"4"6F_i_m_m 1VHI6` #<QCqN![\ #7 "(vjY'3LQ]frs+BL$**,XbXfXf$QCvaS8$Y$'@6!G%TU - C) !6 $%&GWXMMODAS]SaSa 573TT #=aSQCw!OP $ V`UcUcQC/V
9!<PQR
 "(vjY"((*DAU_UcUc 6!G4dV #=aSqc!QR + &( /9nn=RTX.Y*+ ..$7K9I+[_9`569=562<..AV2W./4>NNCY4Z012<..AU2V./r   c                    0 nU R                  SS 5        [        U R                  5       5      nU H.  nSU;   d  M  U R                  U5      XR                  SS5      '   M0     SSSSSS	S
SSSSS.nSSS.nSSSSS.nSSS.nS n	U H  n
U
nUR	                  5        H  u  pLUR                  XL5      nM     UR	                  5        H  u  pLUR                  XL5      nM     UR	                  5        H  u  pLUR                  XL5      nM     UR	                  5        H  u  pLUR                  XL5      nM     SU;   a)  UR                  U	" U R                  U
5      U5      5        M  U R                  U
5      X+'   M     U$ )Nnorm_final.weightr   r   z#time_caption_embed.caption_embedderz-time_caption_embed.timestep_embedder.linear_1z-time_caption_embed.timestep_embedder.linear_2r  
.to_out.0.r  r  rk  rl  linear_3rH  )cap_embedderr  r  	attentionz.out.r  r  w1w2w3r  rJ  r  )attention_norm1attention_norm2zcontext_refiner.0.norm1zcontext_refiner.0.norm2zcontext_refiner.1.norm1zcontext_refiner.1.norm2)z!context_refiner.0.attention_norm1z!context_refiner.0.attention_norm2z!context_refiner.1.attention_norm1z!context_refiner.1.attention_norm2norm_out.linear_1norm_out.linear_2)r  rV  c                     SnS=p4[         R                  " XX4/SS9u  pVnUR                  SS5      UUR                  SS5      UUR                  SS5      U0$ )	Ni 	  r   r   r  r  r  r  r  )rg  r~  r   )tensorr  q_dimk_dimv_dimr  r  r  s           r    convert_lumina_attn_to_diffusersFconvert_lumina2_to_diffusers.<locals>.convert_lumina_attn_to_diffusers  sk     ;;vu/D!LD !!%0$!!%0$!!%0$
 	
r   r  )r  r}  r   r   r+  r  )r  r   r  r   r   LUMINA_KEY_MAPATTENTION_NORM_MAPCONTEXT_REFINER_MAPFINAL_LAYER_MAPr  r;  r  r;  s                r   convert_lumina2_to_diffusersr    s    NN&- 
!"D#q(BL..QRBSJyy!92>? 
 >KK,N ("
 .G-F-F-F	 +>1O


 '--/DA)11!7M 0#))+DA)11!7M ,&,,.DA)11!7M /"((*DA)11!7M + M! ''(HX[I\^k(lm2<..2E /    r   c                 j   0 n[        U R                  5       5      nU H.  nSU;   d  M  U R                  U5      XR                  SS5      '   M0     [        [	        S U  5       5      5      S   S-   nU R                  S5        U R                  S5      US'   U R                  S	5      US
'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5        U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S 5      US!'   [        U5       GHP  nU R                  S"U S#35      US$U S#3'   [        R                  " U R                  S"U S%35      S&S'S(9u  pxn	[        R                  " U/5      US$U S)3'   [        R                  " U/5      US$U S*3'   [        R                  " U	/5      US$U S+3'   U R                  S"U S,35      US$U S-3'   U R                  S"U S.35      US$U S/3'   U R                  S"U S035      US$U S13'   U R                  S"U S235      US$U S33'   [        R                  " U R                  S"U S435      S5S'S(9u  p[        R                  " U R                  S"U S635      S5S'S(9u  pXS$U S73'   XS$U S83'   XS$U S93'   XS$U S:3'   U R                  S"U S;35      US$U S<3'   U R                  S"U S=35      US$U S>3'   U R                  S"U S?35      US$U S@3'   U R                  S"U SA35      US$U SB3'   U R                  S"U SC35      US$U SD3'   U R                  S"U SE35      US$U SF3'   U R                  S"U SG35      US$U SH3'   GMS     U R                  SI5      USJ'   U R                  SK5      USL'   U R                  SM5      USN'   U$ )ONr   r   c              3   n   #    U  H+  nS U;   d  M  [        UR                  SS5      S   5      v   M-     g7f)blocksr{  r   r   Nr  r   s     r   r   8convert_sana_transformer_to_diffusers.<locals>.<genexpr>  s1     W:aUV1#aggc1oa011:r  r7  r   rE  r  r  r  r  r=   z0time_embed.emb.timestep_embedder.linear_1.weightr  z.time_embed.emb.timestep_embedder.linear_1.biasr  z0time_embed.emb.timestep_embedder.linear_2.weightr  z.time_embed.emb.timestep_embedder.linear_2.biaszt_block.1.weightztime_embed.linear.weightzt_block.1.biasztime_embed.linear.biaszy_embedder.y_embeddingzy_embedder.y_proj.fc1.weightz"caption_projection.linear_1.weightzy_embedder.y_proj.fc1.biasz caption_projection.linear_1.biaszy_embedder.y_proj.fc2.weightz"caption_projection.linear_2.weightzy_embedder.y_proj.fc2.biasz caption_projection.linear_2.biaszattention_y_norm.weightzcaption_norm.weightr   z.scale_shift_tabler  z.attn.qkv.weightr   r   r  z.attn1.to_q.weightz.attn1.to_k.weightz.attn1.to_v.weightz.attn.proj.weightz.attn1.to_out.0.weightz.attn.proj.biasz.attn1.to_out.0.biasz.cross_attn.q_linear.weightr  z.cross_attn.q_linear.biasr   z.cross_attn.kv_linear.weightr   z.cross_attn.kv_linear.biasr  r  r  r  z.cross_attn.proj.weightr  z.cross_attn.proj.biasr  z.mlp.inverted_conv.conv.weightz.ff.conv_inverted.weightz.mlp.inverted_conv.conv.biasz.ff.conv_inverted.biasz.mlp.depth_conv.conv.weightz.ff.conv_depth.weightz.mlp.depth_conv.conv.biasz.ff.conv_depth.biasz.mlp.point_conv.conv.weightz.ff.conv_point.weightr  r  r	  r  zfinal_layer.scale_shift_tabler  )	r}  r   r  r   r  r  rg  r  r  )r  r   r  r   r   r  r  r  r  r  linear_sample_klinear_sample_vlinear_sample_k_biaslinear_sample_v_biass                 r   %convert_sana_transformer_to_diffusersr    s   
!"D#q(BL..QRBSJyy!92>?  cW:WWXY[\_``J NN;6@nnE]6^234>NNCY4Z01 PZ~~!PKL NX^^\sMtIJOY~~!PKL NX^^\sMtIJ7A~~FX7Y345?^^DT5U12 NN+,AKPnAo=>?I~~Nj?k;<AKPnAo=>?I~~Nj?k;<2<..AZ2[./:LVNNaS*+M
21#5GHI
 (-{{:>>GA3N^B_3`bcij'k$HLQIIW_V`La21#5GHILQIIW_V`La21#5GHILQIIW_V`La21#5GHI Q[P^P^aS)*Q
21#5KLM OYnnaS(O
21#5IJK
 MWNNaS34M
21#5GHI KU..aS12K
21#5EFG ,1;;NNWQC'CDEqa,
( 6;[[NNWQC'ABCQA6
2 M\21#5GHIL[21#5GHIJ^21#5EFGJ^21#5EFG Q[P^P^aS/0Q
21#5KLM OYnnaS-.O
21#5IJK
 S]R`R`aS67S
21#5MNO Q[P^P^aS45Q
21#5KLM PZ~~aS34P
21#5JKL NX^^aS12N
21#5HIJ PZ~~aS34P
21#5JKLw @ /9nn=X.Y*+,6NN;T,U)0:?^0_,-r   c                    S nS nS nS n0 n[        U R                  5       5      nU H.  nSU;   d  M  U R                  U5      XR                  SS5      '   M0     0 SS_S	S
_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS _S!S"_S#S$_S%S&_S'S$_S(S)S*S+S,S-S.S/S0S1S&S2.En	0 n
[	        S3 U R                  5        5       5      (       a  U	R                  U" 5       5        USS/4U
S4'   [	        S5 U R                  5        5       5      (       a  U	R                  U" 5       5        [        U R                  5       5       H  nU" X5        M     [        U R                  5       5       HB  nUnU	R                  5        H  u  pUR                  X5      nM     U R                  U5      Xl'   MD     [        UR                  5       5       H2  nU
R                  5        H  u  nu  nnX;  a  M  U" XUU5          M0     M4     U$ )6Nc                      SSSSSS.n [        S5       HK  nUS-   nU R                  S	U S
3SU S3S	U S3SU S3S	U S3SU S3S	U S3SU S3S	U S3SU S305        MM     U $ )Nz&motion_encoder.motion_synthesis_weightzmotion_encoder.conv_in.weightz"motion_encoder.conv_in.act_fn.biaszmotion_encoder.conv_out.weightzmotion_encoder.motion_network)rG   z+motion_encoder.enc.net_app.convs.0.0.weightz)motion_encoder.enc.net_app.convs.0.1.biasz)motion_encoder.enc.net_app.convs.8.weightzmotion_encoder.enc.fc   r   !motion_encoder.enc.net_app.convs.z.conv1.0.weightzmotion_encoder.res_blocks..conv1.weightz.conv1.1.biasz.conv1.act_fn.biasz.conv2.1.weight.conv2.weightz.conv2.2.biasz.conv2.act_fn.biasz.skip.1.weightz.conv_skip.weight)r  r  )mappingsr  conv_idxs      r    generate_motion_encoder_mappingsNconvert_wan_transformer_to_diffusers.<locals>.generate_motion_encoder_mappings]  s    3[;Z9]9Y%D
 qA1uHOO7zQUopqorr  TA7zOSmnomp  qC  RD7zQUopqorr  TA7zOSmnomp  qC  RD7zPTnopnq  rC  SD  r   c            	          SSSSSSSSS	.$ )
Nface_adapterz.norm_k.z.norm_q..to_q.z.to_out.conv1_localr  conv3)zface_adapter.fuser_blocksz.k_norm.z.q_norm.z.linear1_q.z	.linear2.zconv1_local.convr  z
conv3.convr9  r9  r   r   generate_face_adapter_mappingsLconvert_wan_transformer_to_diffusers.<locals>.generate_face_adapter_mappingst  s$    )7""## -!!	
 		
r   c                     UR                  U 5      nUR                  S   S-  nU R                  X#S   5      nU R                  X#S   5      nUS U X'   XES  X'   g )Nr   r   r   )r  rD  r   )r;  r  split_patterntarget_keysr  	split_idx	new_key_1	new_key_2s           r   split_tensor_handlerBconvert_wan_transformer_to_diffusers.<locals>.split_tensor_handler  sb    $LLOq(	KK1~>	KK1~>	 &z	 2
 &z 2
r   c                 >    SU ;   a  SU ;   a  X   SS S 2SS4   X'   g g g )Nr  rA  r   r9  r  s     r   reshape_bias_handlerBconvert_wan_transformer_to_diffusers.<locals>.reshape_bias_handler  s/    .#5'S.(oaAqj9JO ;I5r   r   r   ztime_embedding.0z)condition_embedder.time_embedder.linear_1ztime_embedding.2z)condition_embedder.time_embedder.linear_2ztext_embedding.0z)condition_embedder.text_embedder.linear_1ztext_embedding.2z)condition_embedder.text_embedder.linear_2ztime_projection.1zcondition_embedder.time_proj
cross_attnattn2	self_attnattn1z.o.r  z.q.r  z.k.z.to_k.z.v.z.to_v.z.k_img.z.add_k_proj.z.v_img.z.add_v_proj.z.norm_k_img.z.norm_added_k.rE   r  z	head.headr1  
modulationzffn.net.0.projz	ffn.net.2norm__placeholderr  r  z'condition_embedder.image_embedder.norm1z/condition_embedder.image_embedder.ff.net.0.projz*condition_embedder.image_embedder.ff.net.2z'condition_embedder.image_embedder.norm2r  )zffn.0zffn.2r  r  r  zimg_emb.proj.0zimg_emb.proj.1zimg_emb.proj.3zimg_emb.proj.4before_proj
after_projc              3   ,   #    U  H
  nS U;   v   M     g7f)r  Nr9  r   s     r   r   7convert_wan_transformer_to_diffusers.<locals>.<genexpr>  s     
:(91>Q(9r  z.linear1_kv.c              3   ,   #    U  H
  nS U;   v   M     g7f)motion_encoderNr9  r   s     r   r   r    s     
<*;Qq *;r  )r}  r   r  r   r   r  r+  )r  r   r  r  r  r  r  r   r   r  SPECIAL_KEYS_HANDLERSr;  ru  r  r  r   
handler_fnr  s                     r   $convert_wan_transformer_to_diffusersr  \  s   .

3:  
!"D#q(BL..QRBSJyy!92>? 
 $G $G $ 	G $ 	G	 $
 	; $ 	g $ 	W $ 	| $ 	x $ 	x $ 	x $ 	> $ 	> $ 	( $ 	. $  	Z! $" 	)# $$ "$$CKFC  ? $ D 

:
(9
:::$++,J,LM1ERZG[0\n-

<*//*;
<<<$++,L,NOJOO%&S- ' JOO%&'C'I'I'K#Kook>G (L(2s(;%	 ' (--/02G2M2M2O.G.j+!s';G	 3P 1  r   c                 b   0 n0 SS_SS_SS_SS_S	S
_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS _S!S"_S#S$S%S&S'S(S)S*.EnS+S,S-S.S/S0S1S2S3S4S5.
nS6S7S8S9S:S;S<.nS=S>S?S@SA.nU R                  5        GH;  u  pxXs;   a
  X7   n	XU	'   M  Xt;   a
  XG   n	XU	'   M$  Xu;   a
  XW   n	XU	'   M3  Xv;   a
  Xg   n	XU	'   MB  USB:X  a  XSC'   MN  USD:X  a  XSE'   MZ  USF:X  a  XSG'   Mf  USH:X  a  XSI'   Mr  UR                  SJ5      (       a  UR                  SJSK5      n	SLU	;   a  U	R                  SLSM5      n	OSNU	;   a  U	R                  SNSO5      n	OSPU	;   a  U	R                  SPSQ5      n	O|SRU	;   a  U	R                  SRSS5      n	OcSTU	;   a  U	R                  STSU5      n	OJSVU	;   a  U	R                  SVSW5      n	O1SXU	;   a  U	R                  SXSY5      n	OSZU	;   a  U	R                  SZS[5      n	XU	'   GMh  UR                  S\5      (       Ga  UR                  S]5      n
[	        U
S^   5      nS_U;   a  US`;   a  SanUnO1USb;   a  ScnUSd-
  nO#USe;   a  S^nUSf-
  nOUSg;   a  ShnUSi-
  nOXU'   GM  SLU;   a
  SjU SkU SM3n	ORSNU;   a
  SjU SkU SO3n	OBSPU;   a
  SjU SkU SQ3n	O2SRU;   a
  SjU SkU SS3n	O"STU;   a
  SjU SkU SU3n	OSVU;   a
  SjU SkU SW3n	OUn	XU	'   GMI  SlU;   aV  USd:X  a%  UR                  SlSm5      n	U	R                  SnSo5      n	O$UR                  S\Sj5      n	U	R                  SlSp5      n	XU	'   GM  SqU;   d  SrU;   am  USh:X  a  UR                  S\U 3Ss5      n	OJUSt:X  a  UR                  S\U 3Su5      n	O.USv:X  a  UR                  S\U 3Sw5      n	OUR                  S\Sj5      n	XU	'   GM  UR                  S\Sj5      n	XU	'   GM7  XU'   GM>     U$ )xNz!encoder.middle.0.residual.0.gammaz'encoder.mid_block.resnets.0.norm1.gammaz encoder.middle.0.residual.2.biasz&encoder.mid_block.resnets.0.conv1.biasz"encoder.middle.0.residual.2.weightz(encoder.mid_block.resnets.0.conv1.weightz!encoder.middle.0.residual.3.gammaz'encoder.mid_block.resnets.0.norm2.gammaz encoder.middle.0.residual.6.biasz&encoder.mid_block.resnets.0.conv2.biasz"encoder.middle.0.residual.6.weightz(encoder.mid_block.resnets.0.conv2.weightz!encoder.middle.2.residual.0.gammaz'encoder.mid_block.resnets.1.norm1.gammaz encoder.middle.2.residual.2.biasz&encoder.mid_block.resnets.1.conv1.biasz"encoder.middle.2.residual.2.weightz(encoder.mid_block.resnets.1.conv1.weightz!encoder.middle.2.residual.3.gammaz'encoder.mid_block.resnets.1.norm2.gammaz encoder.middle.2.residual.6.biasz&encoder.mid_block.resnets.1.conv2.biasz"encoder.middle.2.residual.6.weightz(encoder.mid_block.resnets.1.conv2.weightrF   z'decoder.mid_block.resnets.0.norm1.gammaz decoder.middle.0.residual.2.biasz&decoder.mid_block.resnets.0.conv1.biasz"decoder.middle.0.residual.2.weightz(decoder.mid_block.resnets.0.conv1.weightz!decoder.middle.0.residual.3.gammaz'decoder.mid_block.resnets.0.norm2.gammaz decoder.middle.0.residual.6.biasz&decoder.mid_block.resnets.0.conv2.biasz(decoder.mid_block.resnets.0.conv2.weightz'decoder.mid_block.resnets.1.norm1.gammaz&decoder.mid_block.resnets.1.conv1.biasz(decoder.mid_block.resnets.1.conv1.weightz'decoder.mid_block.resnets.1.norm2.gammaz&decoder.mid_block.resnets.1.conv2.biasz(decoder.mid_block.resnets.1.conv2.weight)z"decoder.middle.0.residual.6.weightz!decoder.middle.2.residual.0.gammaz decoder.middle.2.residual.2.biasz"decoder.middle.2.residual.2.weightz!decoder.middle.2.residual.3.gammaz decoder.middle.2.residual.6.biasz"decoder.middle.2.residual.6.weightz)encoder.mid_block.attentions.0.norm.gammaz,encoder.mid_block.attentions.0.to_qkv.weightz*encoder.mid_block.attentions.0.to_qkv.biasz*encoder.mid_block.attentions.0.proj.weightz(encoder.mid_block.attentions.0.proj.biasz)decoder.mid_block.attentions.0.norm.gammaz,decoder.mid_block.attentions.0.to_qkv.weightz*decoder.mid_block.attentions.0.to_qkv.biasz*decoder.mid_block.attentions.0.proj.weightz(decoder.mid_block.attentions.0.proj.bias)
zencoder.middle.1.norm.gammazencoder.middle.1.to_qkv.weightzencoder.middle.1.to_qkv.biaszencoder.middle.1.proj.weightzencoder.middle.1.proj.biaszdecoder.middle.1.norm.gammazdecoder.middle.1.to_qkv.weightzdecoder.middle.1.to_qkv.biaszdecoder.middle.1.proj.weightzdecoder.middle.1.proj.biaszencoder.norm_out.gammar   r   zdecoder.norm_out.gammar   r   )zencoder.head.0.gammazencoder.head.2.biaszencoder.head.2.weightzdecoder.head.0.gammazdecoder.head.2.biaszdecoder.head.2.weightr   r   r   r   )zconv1.weightz
conv1.biaszconv2.weightz
conv2.biaszencoder.conv1.weightr   zencoder.conv1.biasr   zdecoder.conv1.weightr   zdecoder.conv1.biasr   zencoder.downsamples.rQ  z.residual.0.gammaz.norm1.gammaz.residual.2.biasz.conv1.biasz.residual.2.weightr  z.residual.3.gammaz.norm2.gammaz.residual.6.biasz.conv2.biasz.residual.6.weightr  z.shortcut.biasz.conv_shortcut.biasz.shortcut.weightz.conv_shortcut.weightzdecoder.upsamples.r{  r   residual)r   r   r   r   )r        r   r  )r[  r6  
   r[  )         r   r  rR  r  z
.shortcut.z.resnets.0.conv_shortcut.zdecoder.upsamples.4zdecoder.up_blocks.1z.conv_shortcut.z
.resample.z.time_conv.z decoder.up_blocks.0.upsamplers.0r  z decoder.up_blocks.1.upsamplers.0   z decoder.up_blocks.2.upsamplers.0)r+  r  r   r~  r  )r  r   r  middle_key_mappingattention_mappinghead_mappingquant_mappingr;  valueru  parts	block_idxnew_block_idx
resnet_idxs                 r   convert_wan_vae_to_diffusersr    s{   +-V 	+,T 	-.X	
 	,-V 	+,T 	-.X 	,-V 	+,T 	-.X 	,-V 	+,T 	-.X 	,-V  	+,T!" 	-.X#$ 	,-V%& 	+,T'( /Y-V,T.X-V,T.X5@ (S*X(T(T&P'R*X(T(T&P$ !96!: 86!:	L ,'0,	M !&&(
$(-G,1)%',G,1) "'G,1)!#(G,1)**=B!9:((;@!78**=B!9:((;@!78^^233kk"8:PQG #g-!//*=~N#w.!//*<mL%0!//*>P$/!//*=~N#w.!//*<mL%0!//*>P!W,!//*:<QR#w.!//*<>UV,1) ^^011IIcNEE!HI S 	)$%M!*J)+$%M!*QJ*,$%M!*QJ,.$%M!*RJ 16- '#- 2=/:,VbcG'3. 2=/:,VabG)S0 2=/:,VcdG(C/ 2=/:,VbcG'3. 2=/:,VabG)S0 2=/:,VcdG!G05W- $>!kk,8STG%oo.CEZ[G!kk*>@TUG%ool<MNG05W- $(<>!kk,>yk*JLnoG!^!kk,>yk*JLnoG"_!kk,>yk*JLnoG!kk*>@TUG05W-++&:<PQ05W- ).%w )z  r   c                     [        U R                  5       5      nU H.  nSU;   d  M  U R                  U5      XR                  SS5      '   M0     U $ )Nr   r   )r}  r   r  r   )r  r   r   r   s       r   (convert_hidream_transformer_to_diffusersr    sK    
!"D#q(BL..QRBSJyy!92>?  r   c                 Z   0 n[        U R                  5       5      nU H.  nSU;   d  M  U R                  U5      XR                  SS5      '   M0     [        [	        S U  5       5      5      S   S-   n[        [	        S U  5       5      5      S   S-   n[        [	        S U  5       5      5      S   S-   nSnS	n	S
 n
U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   U R                  S5      US'   [        U5       H  nSU S3nU R                  SU S35      X, S3'   U R                  SU S35      X, S3'   U R                  SU S35      X, S3'   U R                  SU S35      X, S3'   U R                  SU S35      USU S3'   M     U R                  S5      US'   U R                  S5      US'   U R                  S 5      US!'   U R                  S"5      US#'   [        U5       GH  nS$U S3n[        R                  " U R                  S%U S&35      S'S(S)9u  pn[        R                  " U R                  S%U S*35      S'S(S)9u  nnn[        R                  " U R                  S%U S+35      S'S(S)9u  nnn[        R                  " U R                  S%U S,35      S'S(S)9u  nnn[        R                  " U/5      X, S-3'   [        R                  " U/5      X, S.3'   [        R                  " U/5      X, S/3'   [        R                  " U/5      X, S03'   [        R                  " U/5      X, S13'   [        R                  " U/5      X, S23'   [        R                  " U/5      X, S33'   [        R                  " U/5      X, S43'   [        R                  " U/5      X, S53'   [        R                  " U/5      X, S63'   [        R                  " U/5      X, S73'   [        R                  " U/5      X, S83'   U R                  S%U S935      X, S:3'   U R                  S%U S;35      X, S<3'   U R                  S%U S=35      X, S>3'   U R                  S%U S?35      X, S@3'   U R                  S%U SA35      X, SB3'   U R                  S%U SC35      X, SD3'   U R                  S%U SE35      X, SF3'   U R                  S%U SG35      X, SH3'   U R                  S%U SI35      X, SJ3'   U R                  S%U SK35      X, SL3'   U R                  S%U SM35      X, SN3'   U R                  S%U SO35      X, SP3'   U R                  S%U SQ35      X, SR3'   U R                  S%U SS35      X, ST3'   U R                  S%U SU35      X, SV3'   U R                  S%U SW35      X, SX3'   GM     [        U5       GH  nSYU S3n[        X-  5      nXU	U4n[        R                  " U R                  SZU S[35      US(S)9u  nnnn[        R                  " U R                  SZU S\35      US(S)9u  nnn n![        R                  " U/5      X, S-3'   [        R                  " U/5      X, S.3'   [        R                  " U/5      X, S/3'   [        R                  " U/5      X, S03'   [        R                  " U/5      X, S13'   [        R                  " U /5      X, S23'   [        R                  " U/5      X, S]3'   [        R                  " U!/5      X, S^3'   U R                  SZU S_35      X, S:3'   U R                  SZU S`35      X, S<3'   U R                  SZU Sa35      X, Sb3'   U R                  SZU Sc35      X, Sd3'   GM     U R                  Se5      USb'   U R                  Sf5      USd'   U$ )gNr   r   c              3   n   #    U  H+  nS U;   d  M  [        UR                  SS5      S   5      v   M-     g7fr?  r  r   s     r   r   Econvert_chroma_transformer_checkpoint_to_diffusers.<locals>.<genexpr>  rB  r  r7  r   c              3   n   #    U  H+  nS U;   d  M  [        UR                  SS5      S   5      v   M-     g7frD  r  r   s     r   r   r    rF  r  c              3   n   #    U  H+  nS U;   d  M  [        UR                  SS5      S   5      v   M-     g7f) distilled_guidance_layer.layers.r{  r   r   Nr  r   s     r   r   r    s3     h*Q@bfg@g(QWWS!_Q'((*r  rG  rH  c                 V    U R                  SSS9u  p[        R                  " X!/SS9nU$ r  r  rJ  s       r   r  Lconvert_chroma_transformer_checkpoint_to_diffusers.<locals>.swap_scale_shift  rL  r   z%distilled_guidance_layer.in_proj.biasz'distilled_guidance_layer.in_proj.weightz&distilled_guidance_layer.out_proj.biasz(distilled_guidance_layer.out_proj.weightr  r{  z.in_layer.biaszlinear_1.biasz.in_layer.weightzlinear_1.weightz.out_layer.biaszlinear_2.biasz.out_layer.weightzlinear_2.weightzdistilled_guidance_layer.norms..scaler@  rO  r  rP  r  rN  rQ  rR  rS  r  r@  rX  r   r   r  rY  rZ  r[  r\  r]  r^  r_  r`  ra  rb  rc  rd  re  rf  rg  rh  ri  rj  rk  rl  rm  rn  ro  rp  rq  rr  rs  rt  ru  rv  rw  rx  ry  rz  r{  r|  r}  r~  r  r  r  r  r  r  r  r  r  r  rE  r  r  r  r  r  r  r  r  r  r  r  r	  )r}  r   r  r   r  r  rg  r  r  r  r~  )"r  r   r  r   r   r  r  num_guidance_layersr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r;  r  r  r  r  r  s"                                     r   2convert_chroma_transformer_checkpoint_to_diffusersr    s	   
!"D#q(BL..QRBSJyy!92>?  c_:__`acdghhJS f* ffghjknooSh*hhijlmpqq  II EONN/E@A GQnn1GBC FP^^0FAB HR~~2HCD &'9!A>?I~~.qc@@
~];< BL.qc1ABB
~_=> @J~~.qcA@
~];< BL.qc1BCB
~_=> NX^^-aS7N
>qcIJ (& 7Ann_6U234>NN=4Q01 1;0O,-.8nn].K*+ :,QCq1',{{:>>NSTRUUiBj3kmntu'v$H*/++NN^A3.BCDaQ+
'	9i 7<kkNN^A3.@ABA17
3}m :?NN^A3.@ABA1:
6 CH))XJBW~-=>?@E		=/@Z~^<=BG))XJBW~-=>?@E		=/@Z~^<=BG))XJBW~-=>?@E		=/@Z~^<=HM		S\R]H^~-CDEFKiiQ_P`Fa~-ABCHM		S\R]H^~-CDEFKiiQ_P`Fa~-ABCHM		S\R]H^~-CDEFKiiQ_P`Fa~-ABCDNNNQC>?E
~-?@A EONNQC<=E
~-?@A KU..QC>?K
~-EFG KU..QC<=K
~-EFG GQnnQC01G
~-ABC EONNUcdecffuSvDw~-?@AAKR`ab`cctPuAv~_=>?I~~P^_`^aapNq?r~];<NXnnQC01O
~-IJK MWNNQC/M
~-GHI JTQC01J
~-DEF HR~~QC/H
~-BCD GQnnQC45G
~-ABC EONNQC23E
~-?@A ISQC45I
~-CDE GQnnQC23G
~-ABCG P $%3A3a8Y23I~F
{{:>>N1#_2U#VXbhij1a+0;;NN^A3m<=zq,
( CH))QC.~-=>?@E		6(@S~^<=BG))QC.~-=>?@E		6(@S~^<=BG))QC.~-=>?@E		6(@S~^<=AFC5AQ~_=>?Dyy(?T~];<DNNNQC56E
~-?@A EONNQC34E
~-?@A BLR`ab`ccrPsAt~_=>?I~~P^_`^aanNo?p~];<5 &8 /9nn=X.Y*+,6NN;T,U)r   c           	         [        U R                  5       5       Vs0 s H  o"U R                  U5      _M     nnS[        4S jnS[        4S jn0 SS_SS_SS	_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_S S!_S"S#_S$S%_S&S'S(S)S*S+S,S-.EnUUUUUS..n0 SS_S/S_S0S1_S2S3_S4S5_S6S7_S8S9_S:S;_S<S=_S>S?_S@SA_SBS_SCS_SDS#_SESF_SGS_SHS!_SISJSKS*S+S,SL.EnUUUUUUUUSM.n	SNn
SOU ;   a  UnUnOUnU	n[        UR                  5       5      nU Hn  nUS S  nUR	                  U
5      (       a  UR                  U
5      nUR                  5        H  u  nnUR                  UU5      nM     UR                  U5      X>'   Mp     [        UR                  5       5      nU H-  nUR                  5        H  u  nnUU;  a  M  U" X#5        M     M/     U$ s  snf )PNr;  c                 &    UR                  U 5        g r   r  r  s     r   r  Hconvert_cosmos_transformer_checkpoint_to_diffusers.<locals>.remove_keys_O  r  r   c                     [        U R                  S5      S   R                  S5      5      nU nSU 3nSU 3nXSR                  U5      -   nUR                  U 5      X'   g )Nr{  r   blockblocks.blockr  )r  r~  removeprefixr  )r;  r  block_indexru  r  
new_prefixs         r   rename_transformer_blocks_Vconvert_cosmos_transformer_checkpoint_to_diffusers.<locals>.rename_transformer_blocks_R  se    #))C.+88AB#K=1
*;-8
33J??(nnS1
r   zt_embedder.1ztime_embed.t_embedderaffline_normztime_embed.normz.blocks.0.block.attnr9  z.blocks.1.block.attnr:  z.blocks.2.blockz.ffz.blocks.0.adaLN_modulation.1z.norm1.linear_1z.blocks.0.adaLN_modulation.2z.norm1.linear_2z.blocks.1.adaLN_modulation.1z.norm2.linear_1z.blocks.1.adaLN_modulation.2z.norm2.linear_2z.blocks.2.adaLN_modulation.1z.norm3.linear_1z.blocks.2.adaLN_modulation.2z.norm3.linear_2zto_q.0r  zto_q.1r  zto_k.0r  zto_k.1r  zto_v.0r  layer1rN  rO  r  patch_embedlearnable_pos_embedr  r  r1  )layer2zproj.1r;  extra_pos_embedderr  zfinal_layer.adaLN_modulation.2rV  )r   zlogvar.0.freqszlogvar.0.phaseszlogvar.1.weightpos_embedder.seqt_embedding_normr  r=  zadaln_modulation_self_attn.1znorm1.linear_1zadaln_modulation_self_attn.2znorm1.linear_2zadaln_modulation_cross_attn.1znorm2.linear_1zadaln_modulation_cross_attn.2znorm2.linear_2zadaln_modulation_mlp.1znorm3.linear_1zadaln_modulation_mlp.2znorm3.linear_2r  r  r  r  q_projk_projv_projoutput_projrt  r  r  zff.net.0.projzff.net.2zpatch_embed.proj)z
mlp.layer1z
mlp.layer2zx_embedder.proj.1zfinal_layer.adaln_modulation.1zfinal_layer.adaln_modulation.2rV  )accum_video_sample_counteraccum_image_sample_counteraccum_iterationaccum_train_in_hoursr  zpos_embedder.dim_spatial_rangezpos_embedder.dim_temporal_range_extra_stateznet.rI   )r}  r   r  r  r  r  r+  r   )r  r   r;  r  r  r  'TRANSFORMER_KEYS_RENAME_DICT_COSMOS_1_0)TRANSFORMER_SPECIAL_KEYS_REMAP_COSMOS_1_0'TRANSFORMER_KEYS_RENAME_DICT_COSMOS_2_0)TRANSFORMER_SPECIAL_KEYS_REMAP_COSMOS_2_0
PREFIX_KEYr  r  r  ru  r  r  r  r  s                      r   2convert_cosmos_transformer_checkpoint_to_diffusersr  L  sr   @DZ__EV@WX@W!44@WX# 2 2///)/ 	/ 		/
 	5/ 	'(9/ 	'(9/ 	'(9/ 	'(9/ 	'(9/ 	'(9/ 	&/ 	(/ 	&/ 	(/  	&!/" 	,#/$ #3*=*=(1/+8 3&''(1-///-/ 	&/ 	'(8	/
 	'(8/ 	()9/ 	()9/ 	!"2/ 	!"2/ 	W/ 	g/ 	&/ 	&/ 	&/ 	z/  	(!/" 	(#/$ & /*=*=(//+6 '3&2' ,(*6+7$	1- J<
J'N$)R&'N$)R&/4467Oa&j))**:6G'C'I'I'K#Kook:>G (L(<(@(@(E%  /4467O/M/S/S/U+K+#%s9 0V   [ Ys   Gc                 :  ^^^ SSSSSSSSS	S
S.
nSS0mSSSSSSSSSSS.
mSSSSS.mS[         S[        [         [        4   SS 4U4S jjnS[         S[        [         [        4   SS 4U4S  jjnS[         S[        [         [        4   SS 4U4S! jjnS[        [         [        4   S"[         S#[         SS 4S$ jnUUUS%.n[        U R	                  5       5       Vs0 s H  oU R                  U5      _M     n	n[        U	R	                  5       5       H;  nUS S  n
UR                  5        H  u  pU
R                  X5      n
M     U" XU
5        M=     [        U	R	                  5       5       H+  nUR                  5        H  u  pX;  a  M  U" X5        M     M-     U	$ s  snf )&Nr;  r  z.time_guidance_embed.timestep_embedder.linear_1z.time_guidance_embed.timestep_embedder.linear_2z.time_guidance_embed.guidance_embedder.linear_1z.time_guidance_embed.guidance_embedder.linear_2z#double_stream_modulation_img.linearz#double_stream_modulation_txt.linearzsingle_stream_modulation.linearr1  )
r:  r  ztime_in.in_layerztime_in.out_layerzguidance_in.in_layerzguidance_in.out_layerz double_stream_modulation_img.linz double_stream_modulation_txt.linzsingle_stream_modulation.linrV  r  r  r2  r3  rA  zff.linear_inzff.linear_outrC  rE  rG  zff_context.linear_inzff_context.linear_out)
zimg_attn.norm.query_normzimg_attn.norm.key_normzimg_attn.projz	img_mlp.0z	img_mlp.2ztxt_attn.norm.query_normztxt_attn.norm.key_normztxt_attn.projz	txt_mlp.0z	txt_mlp.2zattn.to_qkv_mlp_projzattn.to_out)linear1r0  znorm.query_normznorm.key_normr;  r  returnc                   > SU ;  a  SU ;  a  SU ;  a  g SnSU ;   ad  U R                  S5      nUS   nSR                  USS	 5      nUS	   nUS
:X  a  SnT
U   nSR                  X$Xv/5      nUR                  U 5      n	XU'   g )Nr@  rA  r  r/  rE  r{  r   r   r7  r  r  )r~  r   r  )r;  r  r  r  r  within_block_name
param_typenew_within_block_nameru  r5  &FLUX2_TRANSFORMER_SINGLE_BLOCK_KEY_MAPs             r   "convert_flux2_single_stream_blocks]convert_flux2_transformer_checkpoint_to_diffusers.<locals>.convert_flux2_single_stream_blocks  s    CG3$683;N 1
s"IIcNEaI #q 5rJW$%
$JK\$]!hh
7LYZGNN3'E"'wr   c                    > SU ;  a  g SU ;   aH  U R                  SSS9u  p#TU   nSR                  XC/5      n[        UR                  U 5      S5      nXaU'   g )Nr@  adaLN_modulationr{  r   )maxsplitr   )rsplitr   r  r  )r;  r  key_without_param_typer"  new_key_without_param_typeru  swapped_weight(FLUX2_TRANSFORMER_ADA_LAYER_NORM_KEY_MAPs          r   convert_ada_layer_norm_weightsYconvert_flux2_transformer_checkpoint_to_diffusers.<locals>.convert_ada_layer_norm_weights  sl    C $14C!1L.")QRh)i&hh :GHG-jnnS.A1EN"0wr   c                   > SU ;  a  SU ;  a  SU ;  a  g SnSU ;   Ga/  U R                  S5      nUS   nUS   nSR                  USS	 5      nUS	   nUS
:X  a  SnSU;   a  UR                  U 5      n[        R                  " USSS9u  pnSU;   a   [        R                  " USSS9u  pnSnSnSnO%SU;   a  [        R                  " USSS9u  pnSnSnSnSR                  X$WU/5      nSR                  X$WU/5      nSR                  X$WU/5      nXU'   XU'   XU'   g TU   nSR                  X$UU/5      nUR                  U 5      nUUU'   g )Nr@  rA  r  r=  r@  r{  r   r   r7  r  r  r  r   r   r  imgr  r  r  txtr(  r)  r*  )r~  r   r  rg  r  )r;  r  r  r  r  modality_block_namer!  r"  fused_qkv_weightto_q_weightto_k_weightto_v_weight
new_q_name
new_k_name
new_v_name	new_q_key	new_k_key	new_v_keyr#  ru  r5  &FLUX2_TRANSFORMER_DOUBLE_BLOCK_KEY_MAPs                        r   "convert_flux2_double_stream_blocks]convert_flux2_transformer_checkpoint_to_diffusers.<locals>.convert_flux2_double_stream_blocks  s   CG3$683;N)
s"IIcNEaI"'( #q 5rJW$%
))#->>##6 8=DTVW]^8_5+//<AKKHXZ[ab<c9Kk!,J!,J!,J11<AKKHXZ[ab<c9Kk!2J!2J!2JHHjZ%TU	HHjZ%TU	HHjZ%TU	(39%(39%(39% 	 )OO`(a%((J;PR\#]^"s+&+
7#r   rv  ru  c                 *    U R                  U5      X'   g r   r  r[  s      r   update_state_dictLconvert_flux2_transformer_checkpoint_to_diffusers.<locals>.update_state_dictD  r^  r   )r(  r<  r.  r  dictobjectr}  r   r  r+  r   )r  r   "FLUX2_TRANSFORMER_KEYS_RENAME_DICTr%  r/  r@  rC  r  r;  r  ru  r  r  r  r  r.  r?  r$  s                  @@@r   1convert_flux2_transformer_checkpoint_to_diffusersrI    s    $LM P!Q,Q,Q(I )*&& 	)*;0, %2"/(#$$7"5*+,.* * (&	.* c6kAR W[ 8C T#v+=N SW $+ +c6kAR +W[ +Z6d3;&7 6# 6PS 6X\ 6 ;;;&" AEZ__EV@WX@W!44@WX (--/0a&'I'O'O'Q#Kook>G (R 	.W= 1 (--/0/M/S/S/U+K%s9 0V 1  % Ys   Fc                    SSSSSSSS.nS	[         S
[        [         [        4   SS 4S jnSU0nS
[        [         [        4   S[         S[         SS 4S jn[        U R	                  5       5       Vs0 s H  ofU R                  U5      _M     nn[        UR	                  5       5       H;  nUS S  nUR                  5        H  u  pUR                  X5      nM     U" XvU5        M=     SUR	                  5       ;   a  UR                  S5      n[        UR	                  5       5       H+  nUR                  5        H  u  pX;  a  M  U" Xg5        M     M-     U$ s  snf )Nzall_final_layer.2-1.zall_x_embedder.2-1.z.attention.to_out.0.biasz.attention.norm_k.weightz.attention.norm_q.weightz.attention.to_out.0.weightr   )zfinal_layer.zx_embedder.z.attention.out.biasz.attention.k_norm.weightz.attention.q_norm.weightz.attention.out.weightr   r;  r  r  c                     SU ;  a  g UR                  U 5      n[        R                  " USSS9u  p4nU R                  SS5      nU R                  SS5      nU R                  SS5      nX1U'   XAU'   XQU'   g )N.attention.qkv.weightr   r   r  z.attention.to_q.weightz.attention.to_k.weightz.attention.to_v.weight)r  rg  r  r   )	r;  r  r5  r6  r7  r8  r9  r:  r;  s	            r   convert_z_image_fused_attention\convert_z_image_transformer_checkpoint_to_diffusers.<locals>.convert_z_image_fused_attentionm  s    "#-%>>#.05<LaUV0W-+[[!8:RS
[[!8:RS
[[!8:RS
!,:!,:!,:r   rL  rv  ru  c                 *    U R                  U5      X'   g r   r  r[  s      r   rC  Nconvert_z_image_transformer_checkpoint_to_diffusers.<locals>.update_state_dict  r^  r   r  rE  )r  r   Z_IMAGE_KEYS_RENAME_DICTrM  r  rC  r;  r  ru  r  r  r  r  r  s                 r   3convert_z_image_transformer_checkpoint_to_diffusersrR  b  s~   .,9$>$>!="$ S d3;>O TX   	 !@&"6d3;&7 6# 6PS 6X\ 6 AEZ__EV@WX@W!44@WX (--/0a&'?'E'E'G#Kook>G (H 	.W= 1 27799 $$%89 (--/0/M/S/S/U+K%s9 0V 1  + Ys   0Ec                     US   c  U $ US   S:X  a  U $ US   S:X  aQ  [        U R                  5       5       Vs0 s H+  o3R                  S5      (       a  M  X0R                  U5      _M-     nnU$ [	        S5      es  snf )Nadd_control_noise_refinercontrol_noise_refinercontrol_layerszcontrol_noise_refiner.z&Unknown Z-Image Turbo ControlNet type.)r}  r   r  r  r   )r  r  r   r;  r  s        r   2convert_z_image_controlnet_checkpoint_to_diffusersrW    s    )*2	+	,0G	G	+	,0@	@04Z__5F0G 
0G~~^vOw$C$$0G 	  
 $#ABB 
s   A9A9c                 B   SSSSSSSSS	S
SS.nS[         S[         SS 4S jnS[         SS 4S jnS[         SS 4S jnUUUS.n[        U R                  5       5       Vs0 s H  owU R                  U5      _M     nn[        UR                  5       5       H;  nUS S  n	UR	                  5        H  u  pU	R                  X5      n	M     U" XU	5        M=     [        UR                  5       5       H+  nUR	                  5        H  u  pX;  a  M  U" Xx5        M     M-     U$ s  snf )Nr   r  audio_proj_inav_cross_attn_video_scale_shiftav_cross_attn_video_a2v_gateav_cross_attn_audio_scale_shiftav_cross_attn_audio_v2a_gate&video_a2v_cross_attn_scale_shift_table&audio_a2v_cross_attn_scale_shift_tabler  r  )r   r  audio_patchify_proj$av_ca_video_scale_shift_adaln_singleav_ca_a2v_gate_adaln_single$av_ca_audio_scale_shift_adaln_singleav_ca_v2a_gate_adaln_singlescale_shift_table_a2v_ca_videoscale_shift_table_a2v_ca_audior  r  rv  ru  r  c                 *    U R                  U5      X'   g r   r  r[  s      r   update_state_dict_inplaceHconvert_ltx2_transformer_to_diffusers.<locals>.update_state_dict_inplace  r^  r   r;  c                 &    UR                  U 5        g r   r  r  s     r   remove_keys_inplaceBconvert_ltx2_transformer_to_diffusers.<locals>.remove_keys_inplace  r  r   c                    SU ;  a  SU ;  a  g U R                  S5      (       a'  U R                  SS5      nUR                  U 5      nX1U'   U R                  S5      (       a'  U R                  SS5      nUR                  U 5      nX1U'   g )Nr@  rA  zadaln_single.ztime_embed.zaudio_adaln_single.zaudio_time_embed.)r  r   r  )r;  r  ru  r5  s       r   %convert_ltx2_transformer_adaln_singleTconvert_ltx2_transformer_to_diffusers.<locals>.convert_ltx2_transformer_adaln_single  s    CG3$6>>/**kk/=AGNN3'E"'w>>/00kk"79LMGNN3'E"'wr   )video_embeddings_connectoraudio_embeddings_connectorr  r  r}  r   r  r+  r   )r  r   $LTX_2_0_TRANSFORMER_KEYS_RENAME_DICTrh  rk  rn  &LTX_2_0_TRANSFORMER_SPECIAL_KEYS_REMAPr;  r  ru  r  r  r  r  s                 r   %convert_ltx2_transformer_to_diffusersru    s`    #%". 1R'E0Q'E +S*R',(,6s 6S 6T 6 T 3 t $ ':&9=.* AEZ__EV@WX@W!44@WX (--/0a&'K'Q'Q'S#Kook>G (T 	""6WE 1 (--/0/U/[/[/]+K%s9 0^ 1  % Ys   Dc                    0 SS_SS_SS_SS_SS_S	S_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SSSS.EnS[         S [         S!S 4S" jnS#[         S!S 4S$ jnUUS%.n[        U R                  5       5       Vs0 s H  ofU R                  U5      _M     nn[        UR                  5       5       H;  nUS S  nUR	                  5        H  u  pUR                  X5      nM     U" XvU5        M=     [        UR                  5       5       H+  nUR	                  5        H  u  pX;  a  M  U" Xg5        M     M-     U$ s  snf )&Nr   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r<  r  r  )r  r  r  rv  ru  r  c                 *    U R                  U5      X'   g r   r  r[  s      r   rh  @convert_ltx2_vae_to_diffusers.<locals>.update_state_dict_inplace  r^  r   r;  c                 &    UR                  U 5        g r   r  r  s     r   rk  :convert_ltx2_vae_to_diffusers.<locals>.remove_keys_inplace  r  r   )r  r  rr  )r  r   LTX_2_0_VIDEO_VAE_RENAME_DICTrh  rk  LTX_2_0_VAE_SPECIAL_KEYS_REMAPr;  r  ru  r  r  r  r  s                r   convert_ltx2_vae_to_diffusersr}    s   %% 		%
 	7% 	% 	7% 	% 	7% 	% 	7% 	% 	{% 	1%  	}!%" 	1#%$ 	}%%& 	1'%( 	})%.  0>/<3%!86s 6S 6T 6 T  +>/B&"
 AEZ__EV@WX@W!44@WX (--/0a&'D'J'J'L#Kook>G (M 	""6WE 1 (--/0/M/S/S/U+K%s9 0V 1  % Ys   <D;c                 b   SSSS.nS[         S[         SS 4S jn[        U R                  5       5       Vs0 s H  oDU R                  U5      _M     nn[        UR                  5       5       H;  nUS S  nUR	                  5        H  u  pxUR                  Xx5      nM     U" XTU5        M=     U$ s  snf )	Nr   r  r  )z
audio_vae.r  r  rv  ru  r  c                 *    U R                  U5      X'   g r   r  r[  s      r   rh  Fconvert_ltx2_audio_vae_to_diffusers.<locals>.update_state_dict_inplace7  r^  r   rr  )	r  r   LTX_2_0_AUDIO_VAE_RENAME_DICTrh  r;  r  ru  r  r  s	            r   #convert_ltx2_audio_vae_to_diffusersr  /  s     0>/<	%!6s 6S 6T 6 AEZ__EV@WX@W!44@WX (--/0a&'D'J'J'L#Kook>G (M 	""6WE 1   Ys   B,)FNNNNNFN)Fr   )NNN)NN)r  )r   NNNF)NF)r   NNN)__doc__rr  r   r   
contextlibr   ior   urllib.parser   r  rg  r  models.modeling_utilsr   
schedulersr   r	   r
   r   r   r   r   r   utilsr   r   r   r   r   r   utils.constantsr   utils.hub_utilsr   utils.torch_utilsr   transformersr   
accelerater   models.model_loading_utilsr   
get_loggerr   r(  r  rq  rw  r*  rj  r  rS  r  r  r&  rC  ra  ri  r   r   	Exceptionr   r   r   r   r   r   r  r  r!  r#  r&  r(  r*  r,  r.  r4  ro  ru  ry  r  r  r  r  r  r  r  r  r  r>  rL  r^  re  rx  r  r  r  r  r  r  r  r  r%  r'  r)  r6  r<  r  r  r  r  r  r_  r  r  r  r  r  r  r  r  rI  rR  rW  ru  r}  r  r9  r   r   <module>r     s   >  	 	 "  !    3	 	 	  8 - 2 /-F			H	%_
K_
Y_ V_ Y	_
 M_ +2_ 4_ ^_ A_ z_ A_  Z!_" f#_$ b%_& @'_( J)_* M+_, g-_. D/_0 61_2 
>T3_: 0F;_B C_D QE_F \G_H FI_J 9K_L  M_X 6LY_`  a_p Fq_r 9s_t JLhiu_v Ww_x H=?VWB, !D$Q 45FG2/8E

 JKpqu_ BQ$/1[\Q$24abQ$ 24fgQ$ 79hi	Q$
 /1[\Q$ 24ghQ$ 57bcQ$ 24XYQ$ ;=bcQ$ 9;deQ$ ;=hiQ$ 	*,N
OQ$ 	*,Y
ZQ$ @\ktuQ$ ")E#$Q$& )K'Q$. ")K!$/Q$6 
')Z7Q$< ')Q=Q$B ')RCQ$H 68`aIQ$J 68bcKQ$L 68bcMQ$N =?lmOQ$P <>fgQQ$R 79\]SQ$T 02EFUQ$V 02PQWQ$X 13VWYQ$Z 24XY[Q$\ 46XY]Q$^ 24RS_Q$` 13NOaQ$b 79TUcQ$d 79UVeQ$f 79YZgQ$h  ?AopiQ$j >@mnkQ$l =?klmQ$n !#BDq"roQ$p 79PQqQ$r 57\]sQ$t 8:VWuQ$v /1NOwQ$x ,.abyQ$z 46XY{Q$| 35VW}Q$~ 35[\Q$@ 79^_AQ$B 57Z[CQ$D 46XYEQ$F 01LM9;fg:<hi9;gh:<ij9;ab:<cd9;bc:<de57QR!@Bk l%DFs$t%DFs$t02DEaQ$  j 

+ '$ /D,?.C,?73$2".-)
 0F-A/E-A	
 .D+?-C+?	 
'8 /D,?.C,?738S6O9U7Q
 0F-A/E-A	
 .D+?-C+?	 
'4 ": 6 	"#< 	 !8	
 	'(A 	%&= 	!": 	 6 	"#< 	 !8 	'(A 	%&= 	0 	, 	!":  	 6!( @V<T2C0?&7
 +7!!!8CH`Ka

O[ z$  $ %" !  %f-!(  # '% #*  %) !,.>? _ 'y ')!&D 
%P,&jZ	; Z^@F@0f@@@V45pdN|~eP* %?J cT 	GT4V$,"
 D$ &R &B J 8h VE Po dDY xE Pf Ru pC Lf Rn bb J6 rCF R: z r   