a
    Adk                     @  s4  d dl mZ d dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
 dddddZdd	d
dZdd Zeejjj_eejjj_eejjj_dd	ddZdd	ddZdd Zdd Zeejj_eejj_eejj_eejj_dd Ze	jejj_e	jejjj_e	jejjj_e	jejjj_dejj_dejj_dS )    )annotationsN)devicessharedprompt_parserz$sgm.models.diffusion.DiffusionEnginez(prompt_parser.SdConditioning | list[str])selfbatchc                 C  s  | j jD ]
}d|_qt|dd}t|dd}t|dd}|rDtjjntjj}tt	j
t	jd}|tj||gfi |t|dtjtjjtjjgfi |t|dtj||gfi |t|dtj|gfi |t|dd	}|otd
d |D }	| j ||	rdgng d}
|
S )Ng        widthi   heightis_negative_promptFdevicedtype   )txtZoriginal_size_as_tupleZcrop_coords_top_leftZtarget_size_as_tupleaesthetic_scorec                 s  s   | ]}|d kV  qdS ) N ).0xr   r   </var/www/html/stable-diffusion-webui/modules/sd_models_xl.py	<genexpr>       z+get_learned_conditioning.<locals>.<genexpr>r   )force_zero_embeddings)conditioner	embeddersucg_rategetattrr   opts sdxl_refiner_low_aesthetic_score!sdxl_refiner_high_aesthetic_scoredictr   r   r   torchtensorrepeatlensdxl_crop_topsdxl_crop_leftall)r   r   embedderr   r	   r
   r   Zdevices_argsZ
sdxl_condsZforce_zero_negative_promptcr   r   r   get_learned_conditioning   s      ( r*   )r   c                 C  s   |  |||S N)model)r   r   tcondr   r   r   apply_model$   s    r/   c                 C  s   |S r+   r   )r   r   r   r   r   get_first_stage_encoding(   s    r0   zsgm.modules.GeneralConditionerc                 C  s>   g }dd | j D D ]}|||}|| qtj|ddS )Nc                 S  s   g | ]}t |d r|qS )encode_embedding_init_texthasattrr   r(   r   r   r   
<listcomp>4   r   z.encode_embedding_init_text.<locals>.<listcomp>r   )dim)r   r1   appendr!   cat)r   Z	init_textZnvptresr(   encodedr   r   r   r1   1   s
    r1   c                 C  s.   dd | j D D ]}||  S tdd S )Nc                 S  s   g | ]}t |d r|qS )tokenizer2   r4   r   r   r   r5   <   r   ztokenize.<locals>.<listcomp>zno tokenizer available)r   r;   AssertionErrorr   textsr(   r   r   r   r;   ;   s    r;   c                 C  s&   dd | j D D ]}||  S d S )Nc                 S  s   g | ]}t |d r|qS )process_textsr2   r4   r   r   r   r5   D   r   z!process_texts.<locals>.<listcomp>)r   r?   r=   r   r   r   r?   C   s    r?   c                 C  s&   dd | j D D ]}||  S d S )Nc                 S  s   g | ]}t |d r|qS )get_target_prompt_token_countr2   r4   r   r   r   r5   I   r   z1get_target_prompt_token_count.<locals>.<listcomp>)r   r@   )r   token_countr(   r   r   r   r@   H   s    r@   c                 C  s   t | jj j}|| jj_d| j_d| _t| jj	t
jjjjrDdnd| _t
jjj }tj|jtj|d| _tj | j_dS )zjthis adds a bunch of parameters to make SDXL model look a bit more like SD1.5 to the rest of the codebase.	crossattnr   vepsr   N)nextr,   diffusion_model
parametersr   conditioning_keycond_stage_key
isinstancedenoiserscalingsgmmodulesdiffusionmodulesZdenoiser_scalingZVScalingparameterizationdiscretizerLegacyDDPMDiscretizationr!   asarrayalphas_cumprodr   r   nnModuler   wrapped)r,   r   discretizationr   r   r   extend_sdxlT   s    
 rY   TF) 
__future__r   r!   Zsgm.models.diffusionrM   Z-sgm.modules.diffusionmodules.denoiser_scalingZ(sgm.modules.diffusionmodules.discretizerrN   r   r   r   r*   r/   r0   models	diffusionDiffusionEnginer1   r;   r?   r@   GeneralConditionerrY   	ldm_print	attentionprintrO   r,   openaimodelencodersSDP_IS_AVAILABLEXFORMERS_IS_AVAILABLEr   r   r   r   <module>   s4   





