a
    Ad                     @   s.   d dl Z d dlZd dlZe  dddZdS )    N      ?c              	      s:  du ri n| |jd gdddlmm} d fdd	}	jd d }
|du r|
dkrd	}d}|
d
kr|
d }d}||
||  d  d  jdd|d |dgi}ni }fdd| D }g }tt	d D ]}|
| |d  f |d |v r||d  \}}}|d }ttjt| dd}||k r|||  |  jddd }|dkr|d8 }|dd t|dd |dd D  qqd}tj||dD ]^\}}|du r|}n4||k r"|tjj|| |d |d  d   }|	|||}|}q|S )a  Implements restart sampling in Restart Sampling for Improving Generative Processes (2023)
    Restart_list format: {min_sigma: [ restart_steps, restart_times, max_sigma]}
    If restart_list is None: will choose restart_list automatically, otherwise will use the given restart_list
    Nr   )to_dget_sigmas_karrasTc                    s   | | fi }| ||} d ur> | |||d || }|dksR|s`| ||  } nF| ||  }|| fi }|||}	||	 d }
| |
|  } d7 | S )N)xisigma	sigma_hatdenoisedr          )r   	old_sigma	new_sigmasecond_orderr	   ddtx_2
denoised_2d_2d_prime)callback
extra_argsmodels_instep_idr   r   A/var/www/html/stable-diffusion-webui/modules/sd_samplers_extra.py	heun_step   s    z"restart_sampler.<locals>.heun_stepr      	   $      r
   )deviceg?c                    s,   i | ]$\}}t tjt | d d|qS )r   dim)inttorchargminabs).0keyvalue)sigmasr   r   
<dictcomp>2       z#restart_sampler.<locals>.<dictcomp>r#   c                 S   s   g | ]\}}||fqS r   r   )r)   r   r   r   r   r   
<listcomp>?   r.   z#restart_sampler.<locals>.<listcomp>)disableg      ?)T)new_onesshapek_diffusion.samplingr   r   itemr"   itemsrangelenappendr%   r&   r'   r(   extendziptqdmk_diffusionsampling
randn_like)r   r   r,   r   r   r1   s_noiseZrestart_listr   r   stepsZrestart_stepsZrestart_timesZ	step_listr   Zrestart_maxmin_idxmax_idxZsigma_restartZ
last_sigmar   r   r   )r   r   r   r   r,   r   r   r   restart_sampler   sL    **0

*rD   )NNNr   N)r&   r<   r4   r=   no_gradrD   r   r   r   r   <module>   s   