U
    Kºcy  ã                   @   s.  d dl mZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZ zd dlZW n ek
rl   dZY nX eƒ edœdd„ƒZedœd	d
„Zeddœdd„Zedœdd„Zeddœdd„Zedœ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ƒ rdndaeƒ r&dndadS ) é    )ÚAnyN)Ú	lru_cache)Úcontextmanager)ÚContextPropÚ
PropModuleÚ__allow_nonbracketed_mutation)Úreturnc                   C   s   t dk	S )z?Returns a bool indicating if opt_einsum is currently available.N©Ú_opt_einsum© r   r   úF/tmp/pip-unpacked-wheel-gikjz4vx/torch/backends/opt_einsum/__init__.pyÚis_available   s    r   c                   C   s   t S )zOReturns the opt_einsum package if opt_einsum is currently available, else None.r	   r   r   r   r   Úget_opt_einsum   s    r   )Ú_enabledr   c                 C   s"   t ƒ s| rtd| › dƒ‚| ad S )Nz5opt_einsum is not available, so setting `enabled` to zÉ will not reap the benefits of calculating an optimal path for einsum. torch.einsum will fall back to contracting from left to right. To enable this optimal path calculation, please install opt-einsum.)r   Ú
ValueErrorÚenabled)r   r   r   r   Ú_set_enabled   s    
r   c                   C   s   t S ©N)r   r   r   r   r   Ú_get_enabled#   s    r   )Ú	_strategyr   c                 C   sH   t ƒ std| › dƒ‚ts*td| › dƒ‚| dkr@td| › ƒ‚| ad S )Nz6opt_einsum is not available, so setting `strategy` to z™ will not be meaningful. torch.einsum will bypass path calculation and simply contract from left to right. Please install opt_einsum or unset `strategy`.z6opt_einsum is not enabled, so setting a `strategy` to z¦ will not be meaningful. torch.einsum will bypass path calculation and simply contract from left to right. Please set `enabled` to `True` as well or unset `strategy`.)ÚautoZgreedyZoptimalzH`strategy` must be one of the following: [auto, greedy, optimal] but is )r   r   r   Ústrategy)r   r   r   r   Ú_set_strategy'   s    r   c                   C   s   t S r   )r   r   r   r   r   Ú_get_strategy6   s    r   c                 C   s6   t tƒ sd ntf}| d k	r"t| ƒ |d k	r2t|ƒ |S r   )r   r   r   r   r   )r   r   Ú
orig_flagsr   r   r   Ú	set_flags:   s    r   c                 c   sH   t ƒ  t| |ƒ}W 5 Q R X z
d V  W 5 t ƒ  t|Ž  W 5 Q R X X d S r   )r   r   )r   r   r   r   r   r   ÚflagsC   s    
r   c                       s:   e Zd Z‡ fdd„Zeeeƒadae	ƒ r2ee
eƒa‡  ZS )ÚOptEinsumModulec                    s   t t| ƒ ||¡ d S r   )Úsuperr   Ú__init__)ÚselfÚmÚname©Ú	__class__r   r   r   T   s    zOptEinsumModule.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   Ú__classcell__r   r   r#   r   r   S   s
   
r   TFr   )NN)NN)Útypingr   ÚwarningsÚsysÚ	functoolsr   Z
_lru_cacheÚ
contextlibr   Ztorch.backendsr   r   r   Z
opt_einsumr
   ÚImportErrorÚboolr   r   r   r   Ústrr   r   r   r   r   Úmodulesr%   r   r   r   r   r   r   Ú<module>   s.   


	