o
    GiS                     @   s   d dl mZ d dlZd dlZd dlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZ dd	lmZmZ dd
lmZmZmZ eeZG dd deZG dd deZdS )    )AnyN   )
FrozenDict)VaeImageProcessor)AutoencoderKL)	deprecatelogging   )ModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParamc                   @   s   e Zd ZdZedee fddZedefddZ	edee
eef  fddZedee fd	d
Zedd Ze dedefddZdS )StableDiffusionXLDecodeStepstable-diffusion-xlreturnc                 C   s"   t dtt dttddiddgS )Nvaeimage_processorvae_scale_factor   from_configconfigdefault_creation_method)r   r   r   r   self r   l/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/modular_pipelines/stable_diffusion_xl/decoders.pyexpected_components&   s   
z/StableDiffusionXLDecodeStep.expected_componentsc                 C   s   dS )Nz2Step that decodes the denoised latents into imagesr   r   r   r   r   description2   s   z'StableDiffusionXLDecodeStep.descriptionc                 C   s   t dddt ddtjddgS )Noutput_typepil)defaultlatentsTz,The denoised latents from the denoising step)required	type_hintr   )r   torchTensorr   r   r   r   inputs6   s   
z"StableDiffusionXLDecodeStep.inputsc                 C   s,   t dttjj ttj B ttj B ddgS )NimageszMThe generated images, can be a PIL.Image.Image, torch.Tensor or a numpy arrayr%   r   )r   listPILImager&   r'   nparrayr   r   r   r   intermediate_outputsB   s   z0StableDiffusionXLDecodeStep.intermediate_outputsc                 C   s    t ddd | jjtjd d S )N
upcast_vaez1.0.0z`upcast_vae` is deprecated. Please use `pipe.vae.to(torch.float32)`. For more details, please refer to: https://github.com/huggingface/diffusers/pull/12619#issue-3606633695.dtype)r   r   tor&   float32)
componentsr   r   r   r1   L   s   z&StableDiffusionXLDecodeStep.upcast_vaestatec                 C   s  |  |}|jdks|j}|jjtjko|jjj|_	|j	r1| 
| |tt|jj j}n|j|jjkrFtjj rF|j|j|_t|jjdoS|jjjd u|_t|jjdob|jjjd u|_|jr|jrt|jjjdddd|j|j|_t|jjjdddd|j|j|_||j |jjj |j }n||jjj }|jj|ddd |_|j	r|jjtjd	 n|j|_t|d
r|jd ur|j|j|_|j j!|j|jd|_| "|| ||fS )Nlatentlatents_meanlatents_std      F)return_dictr   r2   	watermark)r    )#get_block_stater    r#   r   r3   r&   float16r   force_upcastneeds_upcastingr1   r4   nextiterpost_quant_conv
parametersbackendsmpsis_availablehasattrr9   has_latents_meanr:   has_latents_stdtensorviewdevicescaling_factordecoder)   r>   apply_watermarkr   postprocessset_block_state)r   r6   r7   block_stater#   r   r   r   __call__V   sD   


&&z$StableDiffusionXLDecodeStep.__call__N)__name__
__module____qualname__
model_namepropertyr+   r   r   strr   tupler   r(   r0   staticmethodr1   r&   no_gradr   rV   r   r   r   r   r   #   s    	
r   c                   @   sp   e Zd ZdZedefddZedee fddZ	edee
eef  fddZe d	edefd
dZdS )'StableDiffusionXLInpaintOverlayMaskStepr   r   c                 C   s   	 dS )NzA post-processing step that overlays the mask on the image (inpainting task only).
only needed when you are using the `padding_mask_crop` option when pre-processing the image and maskr   r   r   r   r   r      s   z3StableDiffusionXLInpaintOverlayMaskStep.descriptionc                 C   s   t dttddiddgS )Nr   r   r   r   r   )r   r   r   r   r   r   r   r      s   
z;StableDiffusionXLInpaintOverlayMaskStep.expected_componentsc              	   C   sR   t dt dt dt dttjj ttj B ttj B ddt dtt	t	f ddgS )	Nimage
mask_imagepadding_mask_cropr)   z)The generated images from the decode stepr*   crops_coordszThe crop coordinates to use for preprocess/postprocess the image and mask, for inpainting task only. Can be generated in vae_encode step.)
r   r+   r,   r-   r&   r'   r.   r/   r]   intr   r   r   r   r(      s   
z.StableDiffusionXLInpaintOverlayMaskStep.inputsr7   c                    sJ   |  |  jd ur jd ur fdd jD  _| |  |fS )Nc                    s$   g | ]}j  j j| jqS r   )r   apply_overlayrb   ra   rd   ).0irU   r6   r   r   
<listcomp>   s    zDStableDiffusionXLInpaintOverlayMaskStep.__call__.<locals>.<listcomp>)r?   rc   rd   r)   rT   )r   r6   r7   r   ri   r   rV      s   
z0StableDiffusionXLInpaintOverlayMaskStep.__call__N)rW   rX   rY   rZ   r[   r\   r   r+   r   r   r]   r   r(   r&   r_   r   rV   r   r   r   r   r`      s    
r`   )typingr   numpyr.   r,   r&   configuration_utilsr   r   r   modelsr   utilsr   r   modular_pipeliner
   r   modular_pipeline_utilsr   r   r   
get_loggerrW   loggerr   r`   r   r   r   r   <module>   s   
n