U
    ‰d9  ã                   @   s€   d dl Z d dlmZ d dlmZ eG dd„ dƒƒZdd„ ZeG dd	„ d	ƒƒZdd
dœeed dœdd„Zeee	 dœdd„Z
dS )é    N)ÚIterator)Ú	dataclassc                   @   s   e Zd ZU eed< eed< dS )ÚMetaInitErrorInfoÚ	mode_nameÚmode_class_nameN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__© r   r   ú;/tmp/pip-unpacked-wheel-ua33x9lu/torch/utils/_mode_utils.pyr      s   
r   c                    s*   t ƒ ‰t ˆ ¡ˆdœ‡ ‡‡fdd„
ƒ}|S )N©Úinnerc                   s8   |ˆkr"t dˆj› dˆj› dƒ‚|| _ˆ | f|ž|ŽS )Nz:missing inner keyword argument; instead of constructing a z( directly, pass the constructor to push_Ú_mode)Ú	TypeErrorr   r   r   )Úselfr   ÚargsÚkwargs©ÚfÚmeta_init_error_infoZundefr   r   Úwrapped   s    ÿz_wrap_init.<locals>.wrapped)ÚobjectÚ	functoolsÚwraps)r   r   r   r   r   r   Ú
_wrap_init   s    r   c                   @   s>   e Zd ZU eed< eed< eed< dd„ Zdd„ Zdd	„ Zd
S )Ú	_ModeInfor   Ú
mode_classÚbase_mode_classc                 C   s   | j jS )N)r   r   ©r   r   r   r   r   /   s    z_ModeInfo.mode_class_namec                 C   s
   t ƒ ‚dS )z+gets the current mode for this type of modeN©ÚNotImplementedErrorr    r   r   r   Úget_mode2   s    z_ModeInfo.get_modec                 C   s
   t ƒ ‚dS )zÄ
        set mode to for this type of mode. Note that no checks are done on this, it's the unsafe
        version where checks are assumed to have been already done by the helper function
        Nr!   )r   Úmoder   r   r   Úset_mode6   s    z_ModeInfo.set_modeN)	r   r   r	   r
   r   Útyper   r#   r%   r   r   r   r   r   )   s   
r   F)ÚreplaceÚignore_preexisting)Ú	mode_infoÚreturnc             	   c   s  | d ksHt | |jƒsHt | tƒr*t| |jƒrHtd| ¡ › dt| ƒ› dƒ‚| ¡ }|| krbd V  d S |d k	r¾|s¾||k	r¾t | |jƒr’d|j› d}nd| ¡ › d}td|j› d	|› d
|› ƒ‚d|j d }t| |ƒsìtd|j› d|› ƒ‚| 	| ¡ z
d V  W 5 | 	|¡ X d S )Nzexpected to get z0, Tensor-like class, or None as an argument got z insteadz	Use push_ú_mode instead.z¼If you intended to completely override the preexisting mode, pass ignore_preexisting=True.  This can result in unexpected behavior; please consider rewriting your mode to be a subclass of z to make it compositional!zAttempted to enable_z+_mode, but there is already an active mode z.  Ú__zThe argument passed to enable_z_mode must implement )
Ú
isinstancer   r&   Ú
issubclassÚ
ValueErrorr   r#   r   Úhasattrr%   )r$   r)   r'   r(   ÚoldÚ	help_textZrequired_fnr   r   r   Ú_enable_modeA   s<    ÿ
þý
ýÿÿ
ÿ

r3   c              
   c   s°   t | |jƒr8td| ¡ › d| ¡ › d| › d|j› d	ƒ‚| ¡ }|d krV|jd d}n|}| |d}t ||jƒsŠtd|j› d| ¡ › ƒ‚| |¡ z
|V  W 5 | |¡ X d S )	NzExpected a z. constructor function, but got an instance of ú z.  Consider using enable_r+   r   zThe callable passed to push_z_modemust return a )r-   r   r/   r   r   r#   r   r%   )Zctorr)   r1   r   r$   r   r   r   Ú
_push_model   s"    &ÿ
ÿ

r5   )r   Útypingr   Zdataclassesr   r   r   r   r3   r   r5   r   r   r   r   Ú<module>   s   +