U
    KcC                     @   s,  d dl Z d dlZd dlZd dlmZ ddddddd	d
dddddgZdd ZG dd deZG dd deZ	G dd deZ
G dd dZejjjejjjejjjdZde Zd!edeejjf ejjdddZdd ZedddZdd ZedddZejd"eedd dZe
 Ze ZdS )#    N)Unionis_builtcuFFTPlanCacheAttrContextPropcuFFTPlanCachecuFFTPlanCacheManagercuBLASModulepreferred_linalg_librarycufft_plan_cachematmulenable_flash_sdpflash_sdp_enabledmath_sdp_enabledenable_math_sdp
sdp_kernelc                   C   s   t jjS )zReturns whether PyTorch is built with CUDA support.  Note that this
    doesn't necessarily mean CUDA is available; just that if this PyTorch
    binary were run a machine with working CUDA drivers and devices, we
    would be able to use it.)torch_CZhas_cuda r   r   @/tmp/pip-unpacked-wheel-gikjz4vx/torch/backends/cuda/__init__.pyr      s    c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 C   s   || _ || _d S N)gettersetter)selfr   r   r   r   r   __init__   s    z&cuFFTPlanCacheAttrContextProp.__init__c                 C   s   |  |jS r   )r   device_index)r   objZobjtyper   r   r   __get__   s    z%cuFFTPlanCacheAttrContextProp.__get__c                 C   s(   t | jtrt| j| |j| d S r   )
isinstancer   strRuntimeErrorr   )r   r   valr   r   r   __set__   s    
z%cuFFTPlanCacheAttrContextProp.__set__N)__name__
__module____qualname__r   r   r    r   r   r   r   r      s   c                   @   s:   e Zd ZdZdd ZeejdZeej	ej
Zdd ZdS )r   z
    Represents a specific plan cache for a specific `device_index`. The
    attributes `size` and `max_size`, and method `clear`, can fetch and/ or
    change properties of the C++ cuFFT plan cache.
    c                 C   s
   || _ d S r   )r   )r   r   r   r   r   r   )   s    zcuFFTPlanCache.__init__z.size is a read-only property showing the number of plans currently in the cache. To change the cache capacity, set cufft_plan_cache.max_size.c                 C   s   t | jS r   )r   Z_cufft_clear_plan_cacher   r   r   r   r   clear4   s    zcuFFTPlanCache.clearN)r!   r"   r#   __doc__r   r   r   Z_cufft_get_plan_cache_sizesizeZ_cufft_get_plan_cache_max_sizeZ_cufft_set_plan_cache_max_sizemax_sizer%   r   r   r   r   r   #   s   c                       s<   e Zd ZdZdZdd Zdd Zdd Z fd	d
Z  Z	S )r   aL  
    Represents all cuFFT plan caches. When indexed with a device object/index,
    this object returns the `cuFFTPlanCache` corresponding to that device.

    Finally, this object, when used directly as a `cuFFTPlanCache` object (e.g.,
    setting the `.max_size`) attribute, the current device's cuFFT plan cache is
    used.
    Fc                 C   s   g | _ d| _d S )NT)caches#_cuFFTPlanCacheManager__initializedr$   r   r   r   r   D   s    zcuFFTPlanCacheManager.__init__c                 C   sr   t jj|}|dk s$|t j kr:tdt j |t| jdkrh| j	dd t
t j D  | j| S )Nr   zOcufft_plan_cache: expected 0 <= device index < {}, but got device with index {}c                 s   s   | ]}t |V  qd S r   )r   ).0indexr   r   r   	<genexpr>O   s     z4cuFFTPlanCacheManager.__getitem__.<locals>.<genexpr>)r   cuda_utilsZ_get_device_indexZdevice_countr   formatlenr)   extendrange)r   Zdevicer,   r   r   r   __getitem__H   s      z!cuFFTPlanCacheManager.__getitem__c                 C   s   t | tj  |S r   )getattrr   r.   current_devicer   namer   r   r   __getattr__R   s    z!cuFFTPlanCacheManager.__getattr__c                    s2   | j rt| tj  ||S tt| ||S d S r   )r*   setattrr   r.   r6   superr   __setattr__r   r8   value	__class__r   r   r<   U   s    z!cuFFTPlanCacheManager.__setattr__)
r!   r"   r#   r&   r*   r   r4   r9   r<   __classcell__r   r   r?   r   r   8   s   	
c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s4   |dkrt j S |dkr$t j S td| d S NZ
allow_tf32Z&allow_fp16_reduced_precision_reductionzUnknown attribute )r   r   Z_get_cublas_allow_tf32Z2_get_cublas_allow_fp16_reduced_precision_reductionAssertionErrorr7   r   r   r   r9   ]   s
    

zcuBLASModule.__getattr__c                 C   s8   |dkrt j|S |dkr(t j|S td| d S rB   )r   r   Z_set_cublas_allow_tf32Z2_set_cublas_allow_fp16_reduced_precision_reductionrC   r=   r   r   r   r<   d   s
    zcuBLASModule.__setattr__N)r!   r"   r#   r9   r<   r   r   r   r   r   \   s   )defaultZcusolverZmagmaz, )backendreturnc                 C   sl   | dkr
nXt | tr>| tkr,tdt dtjt|   n$t | tjjrZtj|  ntdtj	 S )a  
    .. warning:: This flag is experimental and subject to change.

    When PyTorch runs a CUDA linear algebra operation it often uses the cuSOLVER or MAGMA libraries,
    and if both are available it decides which to use with a heuristic.
    This flag (a :class:`str`) allows overriding those heuristics.

    * If `"cusolver"` is set then cuSOLVER will be used wherever possible.
    * If `"magma"` is set then MAGMA will be used wherever possible.
    * If `"default"` (the default) is set then heuristics will be used to pick between
      cuSOLVER and MAGMA if both are available.
    * When no input is given, this function returns the currently preferred library.

    Note: When a library is preferred other libraries may still be used if the preferred library
    doesn't implement the operation(s) called.
    This flag may achieve better performance if PyTorch's heuristic library selection is incorrect
    for your application's inputs.

    Currently supported linalg operators:

    * :func:`torch.linalg.inv`
    * :func:`torch.linalg.inv_ex`
    * :func:`torch.linalg.cholesky`
    * :func:`torch.linalg.cholesky_ex`
    * :func:`torch.cholesky_solve`
    * :func:`torch.cholesky_inverse`
    * :func:`torch.linalg.lu_factor`
    * :func:`torch.linalg.lu`
    * :func:`torch.linalg.lu_solve`
    * :func:`torch.linalg.qr`
    * :func:`torch.linalg.eigh`
    * :func:`torch.linalg.eighvals`
    * :func:`torch.linalg.svd`
    * :func:`torch.linalg.svdvals`
    Nz"Unknown input value. Choose from: .zUnknown input value type.)
r   r   _LinalgBackendsr   _LinalgBackends_strr   r   Z_set_linalg_preferred_backend_LinalgBackendZ_get_linalg_preferred_backend)rE   r   r   r   r   r   s    %
c                   C   s
   t j S )zy
    .. warning:: This flag is experimental and subject to change.

    Returns whether flash sdp is enabled or not.
    )r   r   Z_get_flash_sdp_enabledr   r   r   r   r      s    Zenabledc                 C   s   t j|  dS )zk
    .. warning:: This flag is experimental and subject to change.

    Enables or disables flash sdp.
    N)r   r   Z_set_sdp_use_flashrK   r   r   r   r      s    c                   C   s
   t j S )zx
    .. warning:: This flag is experimental and subject to change.

    Returns whether math sdp is enabled or not.
    )r   r   Z_get_math_sdp_enabledr   r   r   r   r      s    c                 C   s   t j|  dS )zj
    .. warning:: This flag is experimental and subject to change.

    Enables or disables math sdp.
    N)r   r   Z_set_sdp_use_mathrK   r   r   r   r      s    T)enable_flashenable_mathc              
   c   sj   t  }t }zFzt|  t| i V  W n& tk
rN } z|W 5 d}~X Y nX W 5 t| t| X dS )z
    .. warning:: This flag is experimental and subject to change.

    This context manager can be used to temporarily enable or disable flash sdp and math sdp.
    Upon exiting the context manager, the previous state of the flags will be restored.
    N)r   r   r   r   r   )rL   rM   Zprevious_flashZprevious_matherrr   r   r   r      s    
)N)TT) sysr   
contextlibtypingr   __all__r   objectr   r   r   r   r   rJ   ZDefaultZCusolverZMagmarH   joinkeysrI   r   r   r   boolr   r   r   contextmanagerr   r	   r
   r   r   r   r   <module>   sB          $$4				