a
    Ad=                     @   s  U d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZmZmZmZmZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl Zejj!j"j#Z$ejj%j&j'Z(ejj%j&j)j#Z*ejj!j"ejj!_+ejj!j"ejj!j,j-d< ej.ejj!_/ej.ejj%j&_/ej.ej0_/ej.ej1j2j3_/g Z4da5ej6e7d	< d
d Z8d#ddZ9dd Z:dd Z;d$ddZ<dd Z=dd Z>dd Z?G dd dZ@G dd de jAjBZCdd  ZDe@ ZEd!d" ZFeFej1j2jGjH_FeFej1j2jIjJ_FdS )%    N)silu)
MethodType)devicessd_hijack_optimizationssharedscript_callbackserrorssd_unet)hypernetwork)cmd_opts)sd_hijack_clipsd_hijack_open_clipsd_hijack_unetsd_hijack_xlmrxlmrzsoftmax-xformerscurrent_optimizerc                  C   s>   t  } dd | D } t| dd dd} t  t|  d S )Nc                 S   s   g | ]}|  r|qS  )is_available.0xr   r   9/var/www/html/stable-diffusion-webui/modules/sd_hijack.py
<listcomp>,       z#list_optimizers.<locals>.<listcomp>c                 S   s   | j S N)priority)r   r   r   r   <lambda>.   r   z!list_optimizers.<locals>.<lambda>T)keyreverse)r   list_optimizers_callbacksorted
optimizersclearextend)Znew_optimizersr   r   r   list_optimizers)   s
    r$   c                    s:  t   ttdkrd adS ttjjj_	t
jtjjj_ttjjj_	t
jtjjj_td urbt  d a| pltjj  dkrttdkrttdd tD td }ntt fddtD d } dkrd }n& dkrtjjrd }n|d u rtd }|d ur*td|j d	dd
 |  td |atjS td dS d S )Nr    	Automaticc                 S   s&   g | ]}|j rttj|j d r|qS )F)Zcmd_optgetattrr   r   r   r   r   r   r   J   r   z'apply_optimizations.<locals>.<listcomp>c                    s   g | ]}|   kr|qS r   )titler   	selectionr   r   r   L   r   Nonez!Applying attention optimization: z... )endzdone.z Disabling attention optimization)undo_optimizationslenr!   r   r   ldmmodulesdiffusionmodulesmodelnonlinearityr   thopenaimodelsgmundor   optscross_attention_optimizationnextiterr   disable_opt_split_attentionprintnameapply)optionZmatching_optimizerr   r)   r   apply_optimizations4   s:    
rA   c                   C   sT   t tjjj_tjtjjj	_
ttjjjj_
t tjjj_tjtjjj	_
ttjjjj_
d S r   )#diffusionmodules_model_nonlinearityr/   r0   r1   r2   r3   r
    attention_CrossAttention_forward	attentionCrossAttentionforward(diffusionmodules_model_AttnBlock_forward	AttnBlockr6   r   r   r   r   r-   `   s    r-   c                   C   s   dS )zcheckpoints are now added and removed in embedding/hypernet code, since torch doesn't want
    checkpoints to be added when not training (there's a warning)Nr   r   r   r   r   fix_checkpointj   s    rI   Tc                 C   s<   | j ||dd}t| dd }|d ur,||9 }|r8| S |S )NF)mean_custom_loss_weight)_old_get_lossr'   rJ   )sd_modelpredtargetrJ   lossweightr   r   r   weighted_lossq   s
    rR   c                 O   s   z|| _ t| ds| j| _tt| | _| j||g|R i |W z| ` W n tyZ   Y n0 t| drr| j| _| `S z| ` W n ty   Y n0 t| dr| j| _| `n4z| ` W n ty   Y n0 t| dr| j| _| `0 d S )NrL   )rK   hasattrget_lossrL   r   rR   rF   AttributeError)rM   r   cwargskwargsr   r   r   weighted_forward}   s8    



rZ   c                 C   s   t t| | _d S r   )r   rZ   rM   r   r   r   apply_weighted_forward   s    r\   c                 C   s    z| ` W n ty   Y n0 d S r   )rZ   rU   r[   r   r   r   undo_weighted_forward   s    r]   c                   @   sb   e Zd ZdZdZdZdZdZdd ZdddZ	dd Z
d	d
 Zdd Zdd Zdd Zdd ZdS )StableDiffusionModelHijackNFc                 C   s4   dd l }i | _g | _|jj | _| jtj d S )Nr   )	Z+modules.textual_inversion.textual_inversionextra_generation_paramscommentstextual_inversionZEmbeddingDatabaseembedding_dbZadd_embedding_dirr   embeddings_dir)selfr0   r   r   r   __init__   s
    z#StableDiffusionModelHijack.__init__c              
   C   sJ   zt || _W n6 tyD } zt|d t  W Y d }~n
d }~0 0 d S )Nz%applying cross attention optimization)rA   optimization_method	Exceptionr   displayr-   )rd   r@   er   r   r   rA      s
    z.StableDiffusionModelHijack.apply_optimizationsc                    sX  t |dd }|rg }tt|jD ]}|j| }t|j}|dkrxt|jj| |j_t	
|| |j|< ||j|  |dkr|jjj}t|j| |_t|| |j|< ||j|  |dkr$t|jj| dd|j_t	|| |j|< ||j|  q$t|dkr|d |_n||_t|jtjkrX|jjj}t|j| |_t|j| |_nt|jtjjjjkr|jjjj}t|j| |_t|j| |_n>t|jtjjjjkrt|jjj| |jj_t	
|j| |_t| |j d	krt!"  | #  |j| _$ fd
d  || _%t&tjj'j(dsDtjj'j(j)j*tjj'j(_+t,j-tjj'j(j)_*d S )NconditionerFrozenOpenCLIPEmbedderFrozenCLIPEmbedderFrozenOpenCLIPEmbedder2Zclip_g)textual_inversion_key   r   editc                    s2    fdd|   D }| g}|D ]}||7 }q |S )Nc                    s   g | ]} |qS r   r   )r   childrenflattenr   r   r      r   zFStableDiffusionModelHijack.hijack.<locals>.flatten.<locals>.<listcomp>)rq   )el	flattenedresrV   rr   r   r   rs      s
    
z2StableDiffusionModelHijack.hijack.<locals>.flatten#copy_of_UNetModel_forward_for_webui).r'   ranger.   	embedderstype__name__EmbeddingsWithFixesr2   token_embeddingr   %FrozenOpenCLIPEmbedderWithCustomWordsappendtransformer
text_model
embeddingsr   (FrozenCLIPEmbedderForSDXLWithCustomWords&FrozenOpenCLIPEmbedder2WithCustomWordscond_stage_modelr   Z!BertSeriesModelWithTransformationrobertaZword_embeddingsr   !FrozenXLMREmbedderWithCustomWordsr/   r0   encodersrl   !FrozenCLIPEmbedderWithCustomWordsrk   r\   cond_stage_keyr   Zhijack_ddpm_editrA   cliplayersrS   r1   r5   	UNetModelrF   rw   r	   ZUNetModel_forward)rd   mrj   Ztext_cond_modelsiembeddertypenamemodel_embeddingsr   rr   r   hijack   sV    




z!StableDiffusionModelHijack.hijackc                 C   sv  t |dd }|rtt|jD ]p}|j| }t|tjtjfr\|jj	j
j|jj	_
|j|j|< t|tjr|jjjjj
j|jjjj_
|j|j|< qt|drt|d nt|jtjkr|jj|_nvt|jtjkr|jj|_|jjjj}t|j
tkr8|j
j|_
n2t|jtjkr8|jjj	j
j|jjj	_
|jj|_t  t| | d d | _d | _tjjj j!tjjj j"_#d S )Nrj   r   F)$r'   rx   r.   ry   
isinstancer   r~   r   wrappedr2   r}   r   r   r   r   r   rS   delattrrz   r   r   r   r   r|   r-   r]   apply_circularr   r   r/   r0   r1   r5   rw   r   rF   )rd   r   rj   r   r   r   r   r   r   undo_hijack   s8    




z&StableDiffusionModelHijack.undo_hijackc                 C   s<   | j |krd S || _ dd | jD D ]}|r0dnd|_q$d S )Nc                 S   s    g | ]}t |tjjkr|qS r   )rz   torchnnConv2d)r   layerr   r   r   r   "  r   z=StableDiffusionModelHijack.apply_circular.<locals>.<listcomp>circularzeros)circular_enabledr   padding_mode)rd   enabler   r   r   r   r     s
    
z)StableDiffusionModelHijack.apply_circularc                 C   s   g | _ i | _d S r   )r`   r_   )rd   r   r   r   clear_comments%  s    z)StableDiffusionModelHijack.clear_commentsc                 C   s0   | j d u rdS | j |g\}}|| j |fS )N)-r   )r   Zprocess_textsZget_target_prompt_token_count)rd   text_Ztoken_countr   r   r   get_prompt_lengths)  s    
z-StableDiffusionModelHijack.get_prompt_lengthsc                 C   s   |  | | | d S r   )r   r   )rd   r   r   r   r   redo_hijack1  s    
z&StableDiffusionModelHijack.redo_hijack)N)r{   
__module____qualname__fixesr   r   r   rf   re   rA   r   r   r   r   r   r   r   r   r   r   r^      s   	
?%	r^   c                       s&   e Zd Zd fdd	Zdd Z  ZS )r|   clip_lc                    s    t    || _|| _|| _d S r   )superre   r   r   rn   )rd   r   r   rn   	__class__r   r   re   7  s    
zEmbeddingsWithFixes.__init__c              	   C   s   | j j}d | j _| |}|d u sDt|dksDtdd |D dkrH|S g }t||D ]\}}|D ]\}}t|jtr|j| j	 n|j}	t
|	}
t|jd | d |
jd }t|d|d  |
d| ||d | d  g}qb|| qVt|S )Nr   c                 S   s   g | ]}t |qS r   )r.   r   r   r   r   r   C  r   z/EmbeddingsWithFixes.forward.<locals>.<listcomp>ro   )r   r   r   r.   maxzipr   vecdictrn   r   cond_cast_unetminshaper   catr   stack)rd   	input_idsZbatch_fixesinputs_embedsvecsr   tensoroffset	embeddingr   embZemb_lenr   r   r   rF   =  s    
*
6zEmbeddingsWithFixes.forward)r   )r{   r   r   re   rF   __classcell__r   r   r   r   r|   6  s   r|   c                     s$   t jjj  fdd} | t jj_d S )Nc                    s    | g|R ddi|S )Nr   r   r   )rd   rX   rY   Zconv2d_constructorr   r   conv2d_constructor_circularV  s    zCadd_circular_option_to_conv_2d.<locals>.conv2d_constructor_circular)r   r   r   re   )r   r   r   r   add_circular_option_to_conv_2dS  s    
r   c                 C   sL   t |tjkr<|jtjkr<|jtjtjj dkr4tjndd}t| || dS )z-
    Fix register buffer bug for Mac OS.
    mpsN)devicedtype)rz   r   Tensorr   r   tofloat32setattr)rd   r>   attrr   r   r   register_buffer_  s    "r   )N)T)Kr   Ztorch.nn.functionalr   typesr   r0   r   r   r   r   r   r	   modules.hypernetworksr
   modules.sharedr   r   r   r   r   r   ldm.modules.attentionr/   "ldm.modules.diffusionmodules.model(ldm.modules.diffusionmodules.openaimodelldm.models.diffusion.ddimZldm.models.diffusion.plmsldm.modules.encoders.modulesZsgm.modules.attentionr6   Z"sgm.modules.diffusionmodules.modelZ(sgm.modules.diffusionmodules.openaimodelsgm.modules.encoders.modulesrD   rE   rF   rC   r1   r2   r3   rB   rH   rG   MemoryEfficientCrossAttentionBasicTransformerBlockATTENTION_MODES	ldm_printr=   utilmodels	diffusionddpmr!   r   ZSdOptimization__annotations__r$   rA   r-   rI   rR   rZ   r\   r]   r^   r   Moduler|   r   model_hijackr   ddimDDIMSamplerZplmsZPLMSSamplerr   r   r   r   <module>   sV   
 

,

 	