B Hpc"@sddlZddlmZddlmZmZmZddlZddlm Z ddl m Z m Z ddl mZddlmZmZmZeeZdZGd d d e jZGd d d ZdS) N) defaultdict)CallableDictUnion)_get_model_file load_dict)LoRACrossAttnProcessor)HF_CACHEPPDIFFUSERS_CACHEloggingzpaddle_lora_weights.pdparamscs,eZdZeeejfdfdd ZZS)AttnProcsLayers) state_dictcsztt|_ddt|D_ddt|D_ fdd}dd} |j |dd dS) NcSsi|]\}}||qSr).0kvrr%/home/aistudio/ppdiffusers/loaders.py $sz,AttnProcsLayers.__init__..cSsi|]\}}||qSrr)rrrrrrr%scsPi}xF|D]:\}}t|dd}|d|j|}|||<qW|S)N.rzlayers.)itemsintsplitreplacemapping)rargskwargsnew_state_dictkeyvaluenumnew_key)selfrrmap_to)s  z(AttnProcsLayers.__init__..map_toc_sZt|}xH|D]@}|ddd}||d|j|}||||<||=qWdS)Nz .processorrzlayers.)listkeysrr rev_mapping)modulerrrZall_keysrZ replace_keyr!rrrmap_from2s    z*AttnProcsLayers.__init__..map_fromT) with_module) super__init__nn LayerListvalueslayers enumerater%rr&register_state_dict_hook!register_load_state_dict_pre_hook)r"rr#r() __class__)r"rr+!s   zAttnProcsLayers.__init__) __name__ __module__ __qualname__rstrpaddleTensorr+ __classcell__rr)r3rr sr c@sReZdZeeeeejffdddZde dfeee j fe ee dddZdS) UNet2DConditionLoadersMixin)%pretrained_model_name_or_path_or_dictc s|dd}|r|dt}n |dt}|dd}|dt}t|tslt|||||d}t|dd }n|}i} td d | D} | rNt t} xR| D]F\} } d | d dd d | d d d}}| | ||<qWxn| D]R\} }|djd}|djd}|djd}t|||d| | <| | |qWnt|dfdd| D} | dS)a4 Load pretrained attention processor layers into `UNet2DConditionModel`. Attention processor layers have to be defined in [cross_attention.py](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/cross_attention.py) and be a `paddle.nn.Layer` class. This function is experimental and might change in the future Parameters: pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`): Can be either: - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co. Valid model ids should have an organization name, like `google/ddpm-celebahq-256`. - A path to a *directory* containing model weights saved using [`~ModelMixin.save_config`], e.g., `./my_model_directory/`. - A [paddle state dict]. from_hf_hub (bool, optional): whether to load from Huggingface Hub. cache_dir (`Union[str, os.PathLike]`, *optional*): Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used. subfolder (`str`, *optional*, defaults to `None`): In case the relevant files are located inside a subfolder of the model repo (either remote in huggingface.co or downloaded locally), you can specify the folder name here. from_hf_hubF cache_dir subfolderN weight_name) weights_namer>r?r=cpu) map_locationcss|]}d|kVqdS)ZloraNr)rrrrr qsz>UNet2DConditionLoadersMixin.load_attn_procs..rzto_k_lora.down.weightrrzto_k_lora.up.weight) hidden_sizecross_attention_dimrankzE does not seem to be in the correct format expected by LoRA training.cs i|]\}}|jjd|qS))dtype)torI)rrr)r"rrrsz?UNet2DConditionLoadersMixin.load_attn_procs..)popr r LORA_WEIGHT_NAME isinstancedictrrallr%rrjoinrshaper ValueErrorset_attn_processor)r"r<rr=r>r?r@ model_filerattn_processorsZis_loraZlora_grouped_dictrrZattn_processor_keysub_key value_dictrHrGrFr)r"rload_attn_procs?s@     2z+UNet2DConditionLoadersMixin.load_attn_procsTN)save_directoryis_main_processrA save_functionc Cstj|r"td|ddS|dkr0tj}tj|ddt|j }| }xPt |D]B}tj ||}| dd} || r\tj|r\|r\t|q\W||tj ||tdtj ||dS) aZ Save an attention procesor to a directory, so that it can be re-loaded using the `[`~loaders.UNet2DConditionLoadersMixin.load_attn_procs`]` method. Arguments: save_directory (`str` or `os.PathLike`): Directory to which to save. Will be created if it doesn't exist. is_main_process (`bool`, *optional*, defaults to `True`): Whether the process calling this is the main process or not. Useful when in distributed training like TPUs and need to call this function on all processes. In this case, set `is_main_process=True` only on the main process to avoid race conditions. weights_name (`str`, *optional*, defaults to `LORA_WEIGHT_NAME`): The name of weights. save_function (`Callable`): The function to use to save the state dictionary. Useful on distributed training like TPUs when one need to replace `torch.save` by another method. Can be configured with the environment variable `DIFFUSERS_SAVE_MODE`. zProvided path (z#) should be a directory, not a fileNT)exist_okz .pdparamszModel weights saved in )ospathisfileloggererrorr8savemakedirsr rUrlistdirrPr startswithremoveinfo) r"rYrZrAr[ model_to_saverfilename full_filenameZweights_no_suffixrrrsave_attn_procss   z+UNet2DConditionLoadersMixin.save_attn_procs)r4r5r6rr7rr8r9rXrLr^PathLikeboolrrlrrrrr;>s Pr;)r^ collectionsrtypingrrrr8 paddle.nnr,modeling_utilsrrZmodels.cross_attentionr utilsr r r get_loggerr4rarLLayerr r;rrrrs