a
    Adh                     @   st  U d dl Z d dlZd dlmZ d dlZd dlmZmZ e jdkrLd dlm	Z	 e
dddZd	d
 Zdd Zdd Zdd Zdd Zdd Zeed edZejed< dZejed< dZejed< dZejed< dZejed< dZejed< ejZejed< ejZejed< ejZejed< dZd d! Zd"d# Z dZ!d.d$d%Z"d/d&d'Z#G d(d) d)e$Z%d*d+ Z&ed,d- Z'dS )0    N)	lru_cache)errorsshareddarwin)mac_specific)returnc                   C   s   t jdkrdS tjS d S )Nr   F)sysplatformr   has_mps r   r   7/var/www/html/stable-diffusion-webui/modules/devices.pyr
      s    
r
   c                   C   s   t jjd urdt jj S dS )Nzcuda:cuda)r   cmd_opts	device_idr   r   r   r   get_cuda_device_string   s    r   c                   C   s   t j rt S t rdS dS )Nmpscpu)torchr   is_availabler   r
   r   r   r   r   get_optimal_device_name   s
    
r   c                   C   s   t t S N)r   devicer   r   r   r   r   get_optimal_device$   s    r   c                 C   s   | t jjv rtS t S r   )r   r   use_cpur   r   )taskr   r   r   get_device_for(   s    r   c                   C   s^   t j rLt jt $ t j  t j  W d    n1 sB0    Y  t rZt	  d S r   )
r   r   r   r   r   empty_cacheipc_collectr
   r   Ztorch_mps_gcr   r   r   r   torch_gc/   s    

(r   c                   C   sL   t j rHtdd tdt j D r2dt jj_dt jjj	_
dt jj_
d S )Nc                 s   s   | ]}t j|d kV  qdS ))      N)r   r   get_device_capability).0Zdevidr   r   r   	<genexpr>?       zenable_tf32.<locals>.<genexpr>r   T)r   r   r   anyrangedevice_countbackendscudnn	benchmarkmatmul
allow_tf32r   r   r   r   enable_tf32:   s
    

r-   zEnabling TF32r   r   device_interrogatedevice_gfpgandevice_esrgandevice_codeformerdtype	dtype_vae
dtype_unetFc                 C   s   t r| tS | S r   )unet_needs_upcasttor4   inputr   r   r   cond_cast_unetT   s    r9   c                 C   s   t r|  S | S r   )r5   floatr7   r   r   r   cond_cast_floatX   s    r;   c                 C   s4   | rt  S ttjks"tjjdkr*t  S tdS )Nfullr   )	
contextlibnullcontextr2   r   float32r   r   	precisionautocastdisabler   r   r   rA   _   s
    rA   c                 C   s"   t  r| st jdddS t S )Nr   F)enabled)r   is_autocast_enabledrA   r=   r>   rB   r   r   r   without_autocasti   s    rF   c                   @   s   e Zd ZdS )NansExceptionN)__name__
__module____qualname__r   r   r   r   rG   m   s   rG   c                 C   s   t jjrd S tt|  s$d S |dkrBd}t jjsl|d7 }n*|dkrhd}t jjslt jjsl|d7 }nd}|d7 }t	|d S )	NZunetz,A tensor with all NaNs was produced in Unet.a$   This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this.vaez+A tensor with all NaNs was produced in VAE.z This could be because there's not enough precision to represent the picture. Try adding --no-half-vae commandline argument to fix this.z$A tensor with all NaNs was produced.zD Use --disable-nan-check commandline argument to disable this check.)
r   r   disable_nan_checkr   allisnanitemno_halfno_half_vaerG   )xwheremessager   r   r   test_for_nansq   s    

rU   c                  C   sf   t dtt} t jddtt}||  t dtt} t jdddtt}||  dS )z
    just do any calculation with pytorch layers - the first time this is done it allocaltes about 700MB of memory and
    spends about 2.7 seconds doing that, at least wih NVidia.
    )   rV   rV   )rV   rV      rW   )rW   rW   N)r   zerosr6   r   r2   nnLinearConv2d)rR   linearconv2dr   r   r   first_time_calculation   s    r^   )F)F)(r   r=   	functoolsr   r   modulesr   r   r	   r   boolr
   r   r   r   r   r   r-   runr   r   __annotations__r.   r/   r0   r1   float16r2   r3   r4   r5   r9   r;   Znv_rngrA   rF   	ExceptionrG   rU   r^   r   r   r   r   <module>   sB   





