o
    H&i                     @   s   d dl Z d dlZd dlZzd dlZdZW n ey   dZY nw d dlmZ g d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                       sl   e Zd ZdZdejdfdedejdef fddZ fdd	Z	d
e
de
de
f fddZ fddZ  ZS )r   zSee :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cuda)r   r   r   )torch_jit_internalis_scriptingZ_enabledZdeviceZ
fast_dtypesuper__init__)selfr   r   r   	__class__ GC:\wamp64\www\opt\env\Lib\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    r   c                    s   t | tjr|  o| jo| jtju}|r|  S | S t | tt	fr%| S t
r/t | tjr/| S t | tjjrA fdd|  D S t | tjjr` fdd| D }t | ttfr^t| |S |S | 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u rt jt dS t  fdd}|S )aa  
    Create a helper decorator for ``forward`` methods of custom autograd functions.

    Autograd functions are 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 u rt  | d _| i |S t  }d| d _|rHtdd t|  i t| W  d    S 1 sAw   Y  d S | i |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_fwdn   s   
$z custom_fwd.<locals>.decorate_fwd)	functoolspartialr   wraps)rA   r;   rB   r   r@   r   r   Y   s
   r   c                    s   t   fdd}|S )a7  Create a helper decorator for backward methods of custom autograd functions.

    Autograd functions are 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                     sJ   t | d j| d jd  | i |W  d    S 1 sw   Y  d S )Nr   )r   r   )r   r=   r<   )r>   r?   bwdr   r   decorate_bwd   s   $z custom_bwd.<locals>.decorate_bwd)rC   rE   )rG   rH   r   rF   r   r      s   r   r   )r0   rC   r
   numpyr/   r.   ModuleNotFoundErrortypingr   __all__ampZautocast_moder   r#   r   r   r   r   r   r   <module>   s    ( *