o
    Zh                     @   sF  U 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	m
Z
mZ zd dlmZ W n ey:   dZY nw daee ed< edurLdd Znd	d Zd
d ZejejejhZdd Zdd Z					dddZe					dddZG dd deZeeje eeje< eed< eed< eed< eed< eed< dS )     N)contextmanager)Optional)__allow_nonbracketed_mutationContextProp
PropModule)_cudnn__cudnn_versionc            	         s   t d u rrt a t } t }| \}}}|\}}}||kr!d}n|dk s(tjs-||k}n||k}|srtjdddkr>dS d| d|  d	}d
tjv rntjd
d t	 fdddD rgt
| d| t
| dt
|dS )NF   Z&PYTORCH_SKIP_CUDNN_COMPATIBILITY_CHECK01Tz=cuDNN version incompatibility: PyTorch was compiled  against z but found runtime version z. PyTorch already comes bundled with cuDNN. One option to resolving this error is to ensure PyTorch can find the bundled cuDNN. LD_LIBRARY_PATH c                 3   s    | ]}| v V  qd S N ).0Z	substringZld_library_pathr   L/var/www/auris/lib/python3.10/site-packages/torch/backends/cudnn/__init__.py	<genexpr>:   s    
z_init.<locals>.<genexpr>)cudaZcudnnzLooks like your LD_LIBRARY_PATH contains incompatible version of cudnn. Please either remove it from the path or install cudnn zHone possibility is that there is a conflicting cuDNN in LD_LIBRARY_PATH.)r   r   ZgetVersionIntZgetRuntimeVersionZgetCompileVersionZis_cudaosenvirongetanyRuntimeError)	Zruntime_versionZcompile_versionZruntime_majorZruntime_minor_Zcompile_majorZcompile_minorZcudnn_compatibleZbase_error_msgr   r   r   _init   sF   



	r   c                   C   s   dS )NFr   r   r   r   r   r   O   s   c                   C   s   t  sdS tS )zReturn the version of cuDNN.N)r   r   r   r   r   r   versionS   s   r   c                   C   s   t jjS )z9Return a bool indicating if CUDNN is currently available.)torch_CZ
_has_cudnnr   r   r   r   is_availablea   s   r   c                 C   sl   t j sdS | jjdks| jtvrdS t st	d dS t
 s4t	djdddtjdd	 dS d
S )NFr   zPyTorch was compiled without cuDNN/MIOpen support. To use cuDNN/MIOpen, rebuild PyTorch making sure the library is visible to the build system.z4cuDNN/MIOpen library not found. Check your {libpath}ZDYLD_LIBRARY_PATHPATH)darwinwin32r   )ZlibpathT)r   r   _get_cudnn_enabledZdevicetypeZdtypeCUDNN_TENSOR_DTYPESr   warningswarnr   formatr   sysplatform)Ztensorr   r   r   is_acceptablef   s&   

r+   c                 C   s   t j t j t sd nt j t j t j f}| d ur%t j|  |d ur/t j	| |d ur<t r<t j
| |d urFt j| |d urPt j| |S r   )r   r   r#   _get_cudnn_benchmarkr   _cuda_get_cudnn_benchmark_limit_get_cudnn_deterministic_get_cudnn_allow_tf32_set_cudnn_enabled_set_cudnn_benchmark_cuda_set_cudnn_benchmark_limit_set_cudnn_deterministic_set_cudnn_allow_tf32)Z_enabledZ
_benchmarkZ_benchmark_limitZ_deterministicZ_allow_tf32
orig_flagsr   r   r   	set_flags}   s"   r6   F
   Tc                 c   s    t   t| ||||}W d    n1 sw   Y  zd V  W t   t|  W d    d S 1 s4w   Y  d S t   t|  W d    w 1 sMw   Y  w r   )r   r6   )enabled	benchmarkbenchmark_limitdeterministic
allow_tf32r5   r   r   r   flags   s   

*
r=   c                       s   e Zd Z fddZeejjejjZ	eejj
ejjZeejjejjZdZe r3eejjejjZeejjejjZ  ZS )CudnnModulec                    s   t  || d S r   )super__init__)selfmname	__class__r   r   r@      s   zCudnnModule.__init__N)__name__
__module____qualname__r@   r   r   r   r#   r0   r8   r.   r3   r;   r,   r1   r9   r:   r   r-   r2   r/   r4   r<   __classcell__r   r   rD   r   r>      s$    r>   r8   r;   r9   r<   r:   )NNNNN)FFr7   FT) r   r)   r&   
contextlibr   typingr   r   Ztorch.backendsr   r   r   Ztorch._Cr   ImportErrorr   int__annotations__r   r   Zhalffloatdoubler%   r   r+   r6   r=   r>   modulesrF   boolr   r   r   r   <module>   sV   
	
4
