o
    }oi!                     @   s   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Zd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZ d
d Zdd Zdd Zdd Zdd Zdd ZdddZdS )    N)defaultdict)chain)Image)DDIMSampler)ParaDDIMSampler)PLMSSampler)DPMSolverSampler)DataParallelWrapperc                 C   s:   |  |}|dkr|  t|dg }||fS d }||fS )N      ? )encodelen)cond_stage_modelpromptsunconditional_guidance_scalecuc r   o/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/parts/stable_diffusion/pipeline.pyencode_prompt   s   
r   c                 C   s`   |dkr
t | }|S |dkrt| }|S |dkrt| }|S |dkr(t| }|S td| d)NDDIMPLMSDPM	PARA_DDIMzSampler z is not supported.)r   r   r   r   
ValueError)modelsampler_typesamplerr   r   r   initialize_sampler'   s   	r   c                 C   s&   |  |}tj|d d ddd}|S )Nr
   g       @g        )minmax)decode_first_stagetorchclamp)r   samplesimagesr   r   r   decode_images5   s   
r&   c                 C   s6   | j dkr	| d } | d  d} dd | D }|S )zD
    Convert a numpy image or a batch of images to a PIL image.
       )N.   uint8c                 S   s   g | ]}t |qS r   )r   	fromarray).0imager   r   r   
<listcomp>D   s    z numpy_to_pil.<locals>.<listcomp>)ndimroundastype)r%   
pil_imagesr   r   r   numpy_to_pil=   s
   
r2   c                 C   s   dd | D }|S )Nc                 S   s(   g | ]}|   d ddd qS )r      r'      )floatcpupermutenumpyr+   xr   r   r   r-   J   s   ( z"torch_to_numpy.<locals>.<listcomp>r   )r%   numpy_imagesr   r   r   torch_to_numpyI   s   r<   c                 C   sD   | j ^}}tj|| g|R d| ji}t| |gt||gfS )Ndevice)shaper"   zerosr=   cat)condu_cond
batch_sizebr>   fillerr   r   r   pad_with_zerosN   s   
rF   Tc           /         sB  |j dd}|j dd|j dd |j dg }|j dd}|j d	d}|j d
d}|j dd}	|j dd}
|j dd}|j dd}|j dd}|j dd}|j dd}|j dd}|j dd}|
dkr|	ds}td|dkstd |dkrtd| d  t| jjtt	|d!| j_|j
jd"v rtj}n|j
jd#v rtj}n|j
jd$v rtj}ntd%t  tjjj|tjtjfv |d& | jjj}t| |	 }g }g }t|tr|g}fd'd(|D  fd)d(t	dt D }tt|d   |D ]}t }|}t| j||\}}t|| \}}t }|| }||| || g}tj  ||| || g|d*!tj" } t|t| ksfJ t|t| ft }|j#|| |d+|||| |
|d,\}!}"t }|| }#t }t$| |!}$|$d t| }$t }|| }%t }&|&| }'|%|$ |%||#|%|'|d- q|d.krtj&|dd/}nt'|}|dkrd0d( |D }|r?t(j)|dd1 |dkrt*+|}t*+|}(t,t-})t.||(D ]%\}*}+|)|* },|)|*  d7  < |+/t(j01||*d d  d2|, d3 qn2t2t(j01|d4d5}-t34||- W d    n	1 s9w   Y  n|W  d    W  d    S i }.|d 5 D ]t6fd6d(|D t| |.d7 < qW|rvt|. W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )8Nr   g      @num_images_per_promptr4   rC   r   heighti   widthdown_factor   r   r   sampler_parallelismsampler_toleranceg?inference_steps2   output_typepilsave_to_fileTout_pathr   etar   devicesPARAz1Parallel sampler is required when parallelism > 1z<It is recommended to run parallel sampler with multiple GPUsz Running DataParallel model with z GPUs.)
device_ids)bf16z
bf16-mixed)    32z32-true)   16z16-mixedz%precision must be in [32, 16, "bf16"])enableddtypec                    s   g | ]}t  D ]}|qqS r   )range)r+   p_)rG   r   r   r-      s    zpipeline.<locals>.<listcomp>c                    s   g | ]
}||   qS r   r   )r+   i)rC   multi_promptsr   r   r-      s    )	generatorF)SconditioningrC   r>   verboser   unconditional_conditioningrT   x_Tparallelism	tolerance)ztext-conditioning-timezsampling-timezdecode-timez
total-timezsampling-stepsr"   )dimc                 S   s   g | ]}t |qS r   )r2   r9   r   r   r   r-          )exist_okra   z.pngz
output.pklwbc                    s   g | ]}|  qS r   r   )r+   dicts)keyr   r   r-      rm   zavg-)7inferget
startswithr   printr	   r   diffusion_modellistr_   trainer	precisionr"   bfloat16r5   halfno_gradcudaampautocastin_channelsr   upper
isinstancestrr   r   timeperf_counterr   r   rF   randntocurrent_devicesampler&   appendr@   r<   osmakedirsr   from_iterabler   intzipsavepathjoinopenpickledumpkeyssum)/r   cfgrg   rngr   r   rH   rI   downsampling_factorr   rL   rM   rN   rP   rR   rS   rT   num_devicesautocast_dtyper   r   output
throughputbatched_promptsbatchtic	tic_totalrA   rB   tocconditioning_timelatent_shapelatentsr$   intermediatessampling_timer%   decode_time	toc_total
total_timepilscountstext_promptr,   idxfave_metricsr   )rC   rq   rc   rG   r   pipelineT   s   




 &







,_,Tr   )TN)r   r   r   collectionsr   	itertoolsr   r"   PILr   Onemo.collections.multimodal.models.text_to_image.stable_diffusion.samplers.ddimr   Tnemo.collections.multimodal.models.text_to_image.stable_diffusion.samplers.para_ddimr   Onemo.collections.multimodal.models.text_to_image.stable_diffusion.samplers.plmsr   Vnemo.collections.multimodal.models.text_to_image.stable_diffusion.samplers.sampler_dpmr   8nemo.collections.multimodal.parts.stable_diffusion.utilsr	   r   r   r&   r2   r<   rF   r   r   r   r   r   <module>   s&   	