a
    AþdÒ#  ã                   @   s   d dl mZ d dlZd dlmZ d dlmZ d dlZ	d dl
mZmZmZmZ d dlZd dlZdd„ Zedg d¢ƒZd	d
„ ZG dd„ dejƒZdS )é    )Ú
namedtupleN)Útrange)Ú
processingÚsharedÚsd_samplersÚsd_samplers_commonc                    s°  | j }| |jd g¡}tjjdkr8tj tj¡}d}ntj 	tj¡}d}| 
|¡ d¡}	|tj_tdt|	ƒƒD ]&}
tj jd7  _t |gd ¡‰ t |	|
 | gd ¡}t ||g¡}t | jgd ¡}|g|gdœ}‡ fdd„| |¡|d … D ƒ\}}| |¡}tjjˆ | ||d}ˆ ||   d¡\}}||| |  }|| |	|
  }|	|
 |	|
d   }|||  }t |¡ Š ~~~~~~~~~~~qptj ¡  || ¡  S )	Nr   Úvé   é   ©Úc_concatÚc_crossattnc                    s   g | ]}t j |ˆ j¡‘qS © ©ÚKÚutilsÚappend_dimsÚndim©Ú.0Úk©Úx_inr   ú:/var/www/html/stable-diffusion-webui/scripts/img2imgalt.pyÚ
<listcomp>&   ó    z(find_noise_for_image.<locals>.<listcomp>©Úcond)Úinit_latentÚnew_onesÚshaper   Úsd_modelÚparameterizationr   ÚexternalÚCompVisVDenoiserÚCompVisDenoiserÚ
get_sigmasÚflipÚstateÚsampling_stepsr   ÚlenÚsampling_stepÚtorchÚcatÚimage_conditioningÚget_scalingsÚ
sigma_to_tÚapply_modelÚchunkr   Ústore_latentÚnextjobÚstd©Úpr   ÚuncondÚ	cfg_scaleÚstepsÚxÚs_inZdnwÚskipÚsigmasÚiÚsigma_inÚcond_inr.   Úc_outÚc_inÚtÚepsÚdenoised_uncondZdenoised_condÚdenoisedÚdÚdtr   r   r   Úfind_noise_for_image   s:    $


rJ   ÚCached)Únoiser9   r:   ÚlatentÚoriginal_promptÚoriginal_negative_promptÚsigma_adjustmentc                    s  | j }| |jd g¡}tjjdkr8tj tj¡}d}ntj 	tj¡}d}| 
|¡ d¡}	|tj_tdt|	ƒƒD ]x}
tj jd7  _t |gd ¡‰ t |	|
d  | gd ¡}t ||g¡}t | jgd ¡}|g|gdœ}‡ fdd„| |¡|d … D ƒ\}}|
dkr.| t |	|
 | gd ¡¡}n
| |¡}tjjˆ | ||d}ˆ ||   d¡\}}||| |  }|
dkr”|| d|	|
   }n|| |	|
d   }|	|
 |	|
d   }|||  }t |¡ Š ~~~~~~~~~~~qptj ¡  ||	d	  S )
Nr   r   r	   r
   r   c                    s   g | ]}t j |ˆ j¡‘qS r   r   r   r   r   r   r   Z   r   z9find_noise_for_image_sigma_adjustment.<locals>.<listcomp>r   éÿÿÿÿ)r   r   r    r   r!   r"   r   r#   r$   r%   r&   r'   r(   r)   r   r*   r+   r,   r-   r.   r/   r0   r1   r2   r   r3   r4   r6   r   r   r   Ú%find_noise_for_image_sigma_adjustmentB   sB    $
 



rR   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚScriptc                 C   s
   d | _ d S ©N)Úcache©Úselfr   r   r   Ú__init__z   s    zScript.__init__c                 C   s   dS )Nzimg2img alternative testr   rV   r   r   r   Útitle}   s    zScript.titlec                 C   s   |S rT   r   )rW   Ú
is_img2imgr   r   r   Úshow€   s    zScript.showc                 C   s  t  d¡}t jdd|  d¡d}t jdd|  d¡d}t jdd	|  d
¡d}t jdd	|  d¡d}t jdd|  d¡d}t jdd	dd	d|  d¡d}t jdd|  d¡d}	t jddddd|  d¡d}
t jddddd|  d¡d}t jd d!|  d"¡d}||||||||	|
||gS )#Nz5
        * `CFG Scale` should be 2 or lower.
        zAOverride `Sampling method` to Euler?(this method is built for it)TÚoverride_sampler)ÚlabelÚvalueÚelem_idzPOverride `prompt` to the same value as `original prompt`?(and `negative prompt`)Úoverride_promptzOriginal promptr	   rN   )r]   Úlinesr_   zOriginal negative promptrO   z>Override `Sampling Steps` to the same value as `Decode steps`?Úoverride_stepsúDecode stepsé–   é2   Úst)r]   ÚminimumÚmaximumÚstepr^   r_   z#Override `Denoising strength` to 1?Úoverride_strengthúDecode CFG scaleg        g      .@gš™™™™™¹?ç      ð?ÚcfgÚ
Randomnessg{®Gáz„?Ú
randomnessz,Sigma adjustment for finding noise for imageFrP   )ÚgrÚMarkdownÚCheckboxr_   ÚTextboxÚSlider)rW   rZ   Úinfor\   r`   rN   rO   rb   rf   rj   rm   ro   rP   r   r   r   Úuiƒ   s$    
úz	Script.uic                    s˜   |r
dˆ_ |rˆˆ_ˆˆ_|r$ˆˆ_|	r.dˆ_‡ ‡‡‡‡‡‡‡fdd„}|ˆ_ˆˆjd< ˆˆjd< ˆ ˆjd< ˆˆjd< ˆˆjd	< ˆˆjd
< t ˆ¡}|S )NÚEulerrl   c              	      sº  ˆj  ¡  ¡ d  t¡}ˆjd uo\ˆjjˆ ko\ˆjjˆko\ˆjjˆko\ˆjj	ˆko\ˆjj
ˆk}|oŠˆjjj|jkoŠt ˆjj| ¡ ¡ dk }|ršˆjj}	nttj jd7  _ˆj ˆjˆg ¡}
ˆj ˆjˆg ¡}ˆrètˆ|
|ˆ ˆƒ}	ntˆ|
|ˆ ˆƒ}	t|	ˆ ˆ|ˆˆˆƒˆ_tjˆj jdd … ||ˆjˆjˆjˆd}dˆ |	 ˆ|  ˆd dˆ d  d  }t  ˆj!ˆj¡}|j" #ˆj¡}|ˆj |d   }ˆj$d ˆ_$|j%ˆˆj || |ˆj&dS )	Né
   éd   r	   )ÚseedsÚsubseedsÚsubseed_strengthÚseed_resize_from_hÚseed_resize_from_wr7   r
   g      à?r   )r.   )'r   ÚcpuÚnumpyÚastypeÚintrU   r9   r:   rN   rO   rP   rM   r    ÚnpÚabsÚsumrL   r   r(   Ú	job_countr!   Úget_learned_conditioningÚ
batch_sizerR   rJ   rK   r   Úcreate_random_tensorsr|   r}   r~   r   Úcreate_samplerÚsampler_nameÚ
model_wrapr&   ÚseedÚsample_img2imgr.   )ÚconditioningÚunconditional_conditioningrz   r{   r|   ÚpromptsÚlatZsame_paramsZsame_everythingZ	rec_noiser   r8   Z
rand_noiseZcombined_noiseÚsamplerr>   Znoise_dt©rm   rO   rN   r7   ro   rW   rP   rf   r   r   Úsample_extra¬   s2    "
ÿ
þ
ý.
*,z Script.run.<locals>.sample_extrazDecode promptzDecode negative promptrk   rc   rn   zSigma Adjustment)	r‹   ÚpromptÚnegative_promptr:   Údenoising_strengthÚsampleÚextra_generation_paramsr   Úprocess_images)rW   r7   Ú_r\   r`   rN   rO   rb   rf   rj   rm   ro   rP   r•   Ú	processedr   r”   r   Úrun    s&    #






z
Script.runN)Ú__name__Ú
__module__Ú__qualname__rX   rY   r[   rv   rž   r   r   r   r   rS   y   s
   rS   )Úcollectionsr   r€   rƒ   Útqdmr   Úmodules.scriptsÚscriptsÚgradiorp   Úmodulesr   r   r   r   r,   Úk_diffusionr   rJ   rK   rR   rS   r   r   r   r   Ú<module>   s   07