o
    xi                     @  s   d dl mZ d dlZd dlmZ d dlmZmZ d dlZd dl	m
Z
 er,e
dZe
dZd#d
dZd$ddZd%ddZ	d&d'ddZd(d!d"ZdS ))    )annotationsN)Sequence)TYPE_CHECKINGAny)
get_moduleznumpy.arrayztorch.FloatTensorreturnlistc                   s*   t d   fddtdt D S )N   c                   s   g | ]
}||   qS  r
   ).0i
chunk_size
input_listr
   _/home/ubuntu/.local/lib/python3.10/site-packages/wandb/integration/diffusers/resolvers/utils.py
<listcomp>   s    zchunkify.<locals>.<listcomp>r   )maxrangelen)r   r   r
   r   r   chunkify   s   
r   pipeliner   argsSequence[Any]kwargsdict[str, Any]c                 C  s   t t| jj }t|D ]\}}|||| d < q|D ]}|d |vr/|d j||d < qd|v rQ|d }|d urM| |j	|
    dnd |d< d|v rg|d d urgtdt|d i |S )Nr   r	   	generator)seeddevicerandom_stateip_adapter_imagezIP-Adapter-Image)r   inspect	signature__call__
parametersitems	enumeratedefaultinitial_seedr   	get_statecpunumpytolistwandblogImage)r   r   r   pipeline_call_parametersidxargpipeline_parameterr   r
   r
   r   get_updated_kwargs   s*   	r3   imagenp_arrayc                   s(   t ddd  j fdd| D ddS )Nr*   PPlease ensure NumPy is installed. You can run `pip install numpy` to install it.requiredc                   s&   g | ]} j  |d ddqS )uint8)   r   r	   axes)	transposearrayastyper   imgnpr
   r   r   7   s   & z*postprocess_pils_to_np.<locals>.<listcomp>r   axis)r   stack)r4   r
   rB   r   postprocess_pils_to_np1   s   rG   Fimageslist[np_array]	normalizebool | Nonec                 C  s:   t ddd}|rdd | D n| } |j|j| dddd	S )
Nr*   r6   r7   c                 S  s   g | ]	}|d   dqS )   r9   )r?   r@   r
   r
   r   r   C   s    z3postprocess_np_arrays_for_video.<locals>.<listcomp>r   rD   )r      r	   r:   r;   )r   r=   rF   )rH   rJ   rC   r
   r
   r   postprocess_np_arrays_for_video<   s   rN   latentstorch_float_tensorc                 C  s   t ddd}| Z | jj|jko| jjj}|r+|   |t	t
| jj j}| jj|| jjj ddd }|rD| jj|jd | jdurO| j|}| jj|d	d
}|   |W  d   S 1 sgw   Y  dS )a  Decode latents generated by [`diffusers.StableDiffusionXLPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/stable_diffusion_xl#stable-diffusion-xl).

    Args:
        pipeline: (diffusers.DiffusionPipeline) The Diffusion Pipeline from
            [`diffusers`](https://huggingface.co/docs/diffusers).
        latents (torch.FloatTensor): The generated latents.

    Returns:
        List of `PIL` images corresponding to the generated latents.
    torchzPlease ensure PyTorch is installed. You can check out https://pytorch.org/get-started/locally/#start-locally for installation instructions.r7   F)return_dictr   )dtypeNpil)output_type)r   no_gradvaerS   float16configforce_upcast
upcast_vaetonextiterpost_quant_convr#   decodescaling_factor	watermarkapply_watermarkimage_processorpostprocessmaybe_free_model_hooks)r   rO   rQ   needs_upcastingrH   r
   r
   r   decode_sdxl_t2i_latentsG   s2   

$rh   )r   r   )r   r   r   r   r   r   r   r   )r4   r   r   r5   )F)rH   rI   rJ   rK   r   r5   )r   r   rO   rP   r   r   )
__future__r   r    collections.abcr   typingr   r   r,   
wandb.utilr   r5   rP   r   r3   rG   rN   rh   r
   r
   r
   r   <module>   s    


