U
    Kc5                     @   s  U d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	 zd dl
mZ W n ek
rj   dZY nX daedk	rd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)ContextProp
PropModule__allow_nonbracketed_mutation)_cudnnc            	         s   t d krt a t } t }| \}}}|\}}}||krBd}n |dk sPtjsZ||k}n||k}|sd| d|  d}dtjkrtjdd t	 fdd	d
D rt
| d| qt
| dnt
|dS )NF   z=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   | ]}| kV  qd S N ).0Z	substringZld_library_pathr   A/tmp/pip-unpacked-wheel-gikjz4vx/torch/backends/cudnn/__init__.py	<genexpr>1   s     z_init.<locals>.<genexpr>)cudaZcudnnz}Looks like your LD_LIBRARY_PATH contains incompatible version of cudnnPlease either remove it from the path or install cudnn zHone possibility is that there is a conflicting cuDNN in LD_LIBRARY_PATH.T)__cudnn_versionr   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   s(    



r   c                   C   s   dS )NFr   r   r   r   r   r   >   s    c                   C   s   t  s
dS tS )zReturns the version of cuDNNN)r   r   r   r   r   r   versionB   s    r   c                   C   s   t jjS )z:Returns a bool indicating if CUDNN is currently available.)torch_CZ	has_cudnnr   r   r   r   is_availableP   s    r   c                 C   sl   t j sdS | jjdks$| jtkr(dS t s<t	d dS t
 sht	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_acceptableU   s(    
 r(   c                 C   s   t j t j t sd nt j t j t j f}| d k	rJt j|  |d k	r^t j	| |d k	rxt rxt j
| |d k	rt j| |d k	rt 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_flagsi   s"    r3   F
   Tc                 c   sN   t   t| ||||}W 5 Q R X z
d V  W 5 t   t|  W 5 Q R X X d S r
   )r   r3   )enabled	benchmarkbenchmark_limitdeterministic
allow_tf32r2   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 rfeejjejjZeejjejjZ  ZS )CudnnModulec                    s   t t| || d S r
   )superr;   __init__)selfmname	__class__r   r   r=      s    zCudnnModule.__init__N)__name__
__module____qualname__r=   r   r   r   r    r-   r5   r+   r0   r8   r)   r.   r6   r7   r   r*   r/   r,   r1   r9   __classcell__r   r   rA   r   r;      s   r;   r5   r8   r6   r9   r7   )NNNNN)FFr4   FT)r&   r   r   r#   
contextlibr   Ztorch.backendsr   r   r   Ztorch._Cr   ImportErrorr   r   r   Zhalffloatdoubler"   r   r(   r3   r:   r;   modulesrC   bool__annotations__intr   r   r   r   <module>   s>    

(
