U
    d                     @   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l	m
Z
 G dd de jjjZdd Zdd	d
Zdd ZdS )    NT)string_classes)Anyc                       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 )autocastz
    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 )NZcudar   )torch_jit_internalis_scriptingZ_enabledZdeviceZ
fast_dtypesuper__init__)selfr   r   r   	__class__ @/tmp/pip-unpacked-wheel-ua33x9lu/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   &   s    
zautocast.__call__)__name__
__module____qualname____doc__r	   Zfloat16boolr   r   r   r   r   r   __classcell__r   r   r   r   r      s
   "r   c                    s   t | tjr8|  o$| jo$| jtjk	}|r4|  S | S t | trF| S t	rZt | t
jrZ| S t | tjjr~ fdd|  D S t | tjjrt fdd| }t | tst | trt| |S |S n| S d S )Nc                    s"   i | ]\}}t | t | qS r   _cast).0kvr   r   r   
<dictcomp>7   s      z_cast.<locals>.<dictcomp>c                    s
   t |  S r   r!   )r%   r&   r   r   <lambda>9       z_cast.<locals>.<lambda>)
isinstancer	   ZTensorZis_floating_pointZis_cudar   Zfloat64tor   	HAS_NUMPYnpZndarraycollectionsabcMappingitemsIterablemaplisttupletype)valuer   Zis_eligibleiterabler   r&   r   r"   .   s    
r"   c                    s   dkr@t |dkrd nt |dks*t|d  tjt dS t |dkrRd nt |dksbt|d  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      cast_inputs)r:   c               
      sz    d kr t  | d _| |S t  }d| d _|rltdd$ t|  t| W  5 Q R  S Q R X n
| |S d S )Nr   F)r   )r	   Zis_autocast_enabled_fwd_used_autocastr   r"   )argskwargsZautocast_contextr:   fwdr   r   decorate_fwdj   s    

*z custom_fwd.<locals>.decorate_fwd)lenAssertionError	functoolspartial
custom_fwdwraps)r?   r=   r@   r   r>   r   rE   L   s    rE   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               
      s0   t | d j  | |W  5 Q R  S Q R X d S )Nr   )r   r;   )r<   r=   bwdr   r   decorate_bwd   s    z custom_bwd.<locals>.decorate_bwd)rC   rF   )rH   rI   r   rG   r   
custom_bwd}   s    rJ   )N)r	   rC   r.   Znumpyr-   r,   ModuleNotFoundErrorZ
torch._sixr   typingr   ampZautocast_moder   r"   rE   rJ   r   r   r   r   <module>   s   
!
1