U
    ©Ê¹jõ  ã                   @   s’   d dl Z d dlZd dlZzd dlZdZW n ek
r@   dZY nX d dlmZ dddgZ	G dd„ dej
jjƒZdd	„ Zddd
œdd„Zdd„ ZdS )é    NT)ÚAnyÚautocastÚ
custom_fwdÚ
custom_bwdc                       sd   e Zd ZdZdejdfeejedœ‡ fdd„Z‡ fdd„Z	e
e
e
dœ‡ fd	d
„Z‡ fdd„Z‡  ZS )r   z„
    See :class:`torch.autocast`.
    ``torch.cuda.amp.autocast(args...)`` is equivalent to ``torch.autocast("cuda", args...)``
    T©ÚenabledÚdtypeÚcache_enabledc                    s8   t j ¡ r || _d| _|| _d S tƒ jd|||d� d S )NÚcudar   )ÚtorchÚ_jit_internalÚis_scriptingZ_enabledZdeviceZ
fast_dtypeÚsuperÚ__init__)Úselfr   r   r	   ©Ú	__class__© ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/cuda/amp/autocast_mode.pyr      s    
   ÿzautocast.__init__c                    s   t j ¡ r| S tƒ  ¡ S ©N)r   r   r   r   Ú	__enter__)r   r   r   r   r   &   s    
zautocast.__enter__)Úexc_typeÚexc_valÚexc_tbc                    s   t j ¡ rd S tƒ  |||¡S r   )r   r   r   r   Ú__exit__)r   r   r   r   r   r   r   r   ,   s    
zautocast.__exit__c                    s   t j ¡ r|S tƒ  |¡S r   )r   r   r   r   Ú__call__)r   Úfuncr   r   r   r   1   s    
zautocast.__call__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Zfloat16Úboolr   r   r   r   r   r   Ú__classcell__r   r   r   r   r      s   üüc                    sÊ   t | tjƒr8|  ¡ o$| jo$| jtjk	}|r4|  ˆ ¡S | S t | tt	fƒrJ| S t
r^t | tjƒr^| S t | tjjƒr‚‡ fdd„|  ¡ D ƒS t | tjjƒrÂ‡ fdd„| D ƒ}t | ttfƒr¼t| ƒ|ƒS |S n| S d S )Nc                    s"   i | ]\}}t |ˆ ƒt |ˆ ƒ“qS r   ©Ú_cast)Ú.0ÚkÚv©r   r   r   Ú
<dictcomp>F   s      z_cast.<locals>.<dictcomp>c                 3   s   | ]}t |ˆ ƒV  qd S r   r#   )r%   r'   r(   r   r   Ú	<genexpr>H   s     z_cast.<locals>.<genexpr>)Ú
isinstancer   ZTensorZis_floating_pointZis_cudar   Zfloat64ÚtoÚstrÚbytesÚ	HAS_NUMPYÚnpZndarrayÚcollectionsÚabcÚMappingÚitemsÚIterableÚlistÚtupleÚtype)Úvaluer   Zis_eligibleÚiterabler   r(   r   r$   9   s&    ÿ
ýr$   ©Úcast_inputsc                   s2   ˆdkrt jtˆ d�S t  ˆ¡‡ ‡fdd„ƒ}|S )a>  
    Helper decorator for ``forward`` methods of custom autograd functions (subclasses of
    :class:`torch.autograd.Function`).  See the :ref:`example page<amp-custom-examples>` for more detail.

    Args:
        cast_inputs (:class:`torch.dtype` or None, optional, default=None):  If not ``None``,
            when ``forward`` runs in an autocast-enabled region, casts incoming
            floating-point CUDA Tensors to the target dtype (non-floating-point Tensors are not affected),
            then executes ``forward`` with autocast disabled.
            If ``None``, ``forward``'s internal ops execute with the current autocast state.

    .. note::
        If the decorated ``forward`` is called outside an autocast-enabled region,
        :func:`custom_fwd<custom_fwd>` is a no-op and ``cast_inputs`` has no effect.
    Nr;   c               
      sˆ   t  ¡ | d _ˆ d kr.t  ¡ | d _ˆ| |ŽS t  ¡ }d| d _|rztdd��$ ˆt| ˆ ƒt|ˆ ƒŽW  5 Q R £ S Q R X n
ˆ| |ŽS d S )Nr   F)r   )r   Zget_autocast_gpu_dtypeÚ_dtypeZis_autocast_enabledÚ_fwd_used_autocastr   r$   )ÚargsÚkwargsZautocast_context©r<   Úfwdr   r   Údecorate_fwdl   s    

*z custom_fwd.<locals>.decorate_fwd)Ú	functoolsÚpartialr   Úwraps)rB   r<   rC   r   rA   r   r   Y   s
    c                    s   t  ˆ ¡‡ fdd„ƒ}|S )a  
    Helper decorator for backward methods of custom autograd functions (subclasses of
    :class:`torch.autograd.Function`).
    Ensures that ``backward`` executes with the same autocast state as ``forward``.
    See the :ref:`example page<amp-custom-examples>` for more detail.
    c               
      s:   t | d j| d jd�� ˆ | |ŽW  5 Q R £ S Q R X d S )Nr   )r   r   )r   r>   r=   )r?   r@   ©Úbwdr   r   Údecorate_bwd‰   s    z custom_bwd.<locals>.decorate_bwd)rD   rF   )rH   rI   r   rG   r   r   �   s    )N)r1   rD   r   Únumpyr0   r/   ÚModuleNotFoundErrorÚtypingr   Ú__all__ÚampZautocast_moder   r$   r   r   r   r   r   r   Ú<module>   s   

( (