o
    -wi                  
   @   s   d dl Z d dlmZmZmZmZmZmZ d dlZd dl	m
Z
 er(e
dZe
dZdefddZd	ed
ee deeef deeef fddZdeddfddZ	dded dee ddfddZd	edddefddZdS )    N)TYPE_CHECKINGAnyDictListOptionalSequence)
get_moduleznumpy.arrayztorch.FloatTensorreturnc                    s*   t d   fddtdt D S )N   c                    s   g | ]
}||   qS  r   ).0i
chunk_size
input_listr   h/home/ubuntu/sommelier/.venv/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   pipelineargskwargsc                 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)list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   5   s   & z*postprocess_pils_to_np.<locals>.<listcomp>r   axis)r   stack)r4   r   rB   r   postprocess_pils_to_np/   s   rG   Fimages	normalizec                 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   A   s    z3postprocess_np_arrays_for_video.<locals>.<listcomp>r   rD   )r      r
   r:   r;   )r   r=   rF   )rH   rI   rC   r   r   r   postprocess_np_arrays_for_video:   s   rL   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vaerQ   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   rM   rO   needs_upcastingrH   r   r   r   decode_sdxl_t2i_latentsE   s2   

$rf   )F)r    typingr   r   r   r   r   r   r,   
wandb.utilr   r5   rN   r   strr3   rG   boolrL   rf   r   r   r   r   <module>   s6     



