a
    ßñhE  ã                   @   s’   d dl Z d dlZd dlZd dlmZmZmZmZ g d¢Zedef Z	ede	d�Z
G dd„ dƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZdS )é    N)ÚAnyÚCallableÚTypeVarÚcast)Úno_gradÚenable_gradÚset_grad_enabled.ÚF)Úboundc                   @   sJ   e Zd ZdZeedœdd„Zdd„ Zddœd	d
„Zeeeddœdd„Z	dS )Ú_DecoratorContextManagerz1Allow a context manager to be used as a decorator)ÚfuncÚreturnc                    s6   t  ˆ ¡rˆ ˆ ¡S t ˆ ¡‡ ‡fdd„ƒ}tt|ƒS )Nc                     s:   ˆ  ¡ � ˆ | i |¤ŽW  d   ƒ S 1 s,0    Y  d S ©N©Ú	__class__)ÚargsÚkwargs©r   Úself© úX/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/autograd/grad_mode.pyÚdecorate_context   s    
z;_DecoratorContextManager.__call__.<locals>.decorate_context)ÚinspectÚisgeneratorfunctionÚ_wrap_generatorÚ	functoolsÚwrapsr   r	   )r   r   r   r   r   r   Ú__call__   s
    

z!_DecoratorContextManager.__call__c                    s   t  ˆ ¡‡ ‡fdd„ƒ}|S )z7Wrap each generator invocation with the context managerc                  ?   sf   ˆ | i |¤Ž}z:ˆ  ¡ � t|ƒ}W d   ƒ n1 s60    Y  |V  W q ty^   Y qbY q0 qd S r   )r   ÚnextÚStopIteration)r   r   ÚgenÚxr   r   r   Úgenerator_context   s    
&
zC_DecoratorContextManager._wrap_generator.<locals>.generator_context)r   r   )r   r   r"   r   r   r   r      s    	z(_DecoratorContextManager._wrap_generatorN©r   c                 C   s   t ‚d S r   ©ÚNotImplementedError©r   r   r   r   Ú	__enter__+   s    z"_DecoratorContextManager.__enter__©Úexc_typeÚ	exc_valueÚ	tracebackr   c                 C   s   t ‚d S r   r$   ©r   r)   r*   r+   r   r   r   Ú__exit__.   s    z!_DecoratorContextManager.__exit__)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r'   r   r-   r   r   r   r   r      s
   
r   c                       s<   e Zd ZdZ‡ fdd„Zdd„ Zeeeddœdd	„Z‡  ZS )
r   a£  Context-manager that disabled gradient calculation.

    Disabling gradient calculation is useful for inference, when you are sure
    that you will not call :meth:`Tensor.backward()`. It will reduce memory
    consumption for computations that would otherwise have `requires_grad=True`.

    In this mode, the result of every computation will have
    `requires_grad=False`, even when the inputs have `requires_grad=True`.

    This context manager is thread local; it will not affect computation
    in other threads.

    Also functions as a decorator. (Make sure to instantiate with parenthesis.)


    Example::

        >>> x = torch.tensor([1], requires_grad=True)
        >>> with torch.no_grad():
        ...   y = x * 2
        >>> y.requires_grad
        False
        >>> @torch.no_grad()
        ... def doubler(x):
        ...     return x * 2
        >>> z = doubler(x)
        >>> z.requires_grad
        False
    c                    s   t j ¡ stƒ  ¡  d| _d S ©NF)ÚtorchZ_jit_internalZis_scriptingÚsuperÚ__init__Úprevr&   r   r   r   r5   P   s    

zno_grad.__init__c                 C   s   t  ¡ | _t  d¡ d S r2   )r3   Úis_grad_enabledr6   r   r&   r   r   r   r'   U   s    
zno_grad.__enter__Nr(   c                 C   s   t  | j¡ d S r   )r3   r   r6   r,   r   r   r   r-   Y   s    zno_grad.__exit__)	r.   r/   r0   r1   r5   r'   r   r-   Ú__classcell__r   r   r   r   r   2   s   r   c                   @   s2   e Zd ZdZddœdd„Zeeeddœdd„ZdS )	r   a!  Context-manager that enables gradient calculation.

    Enables gradient calculation, if it has been disabled via :class:`~no_grad`
    or :class:`~set_grad_enabled`.

    This context manager is thread local; it will not affect computation
    in other threads.

    Also functions as a decorator. (Make sure to instantiate with parenthesis.)


    Example::

        >>> x = torch.tensor([1], requires_grad=True)
        >>> with torch.no_grad():
        ...   with torch.enable_grad():
        ...     y = x * 2
        >>> y.requires_grad
        True
        >>> y.backward()
        >>> x.grad
        >>> @torch.enable_grad()
        ... def doubler(x):
        ...     return x * 2
        >>> with torch.no_grad():
        ...     z = doubler(x)
        >>> z.requires_grad
        True

    Nr#   c                 C   s   t  ¡ | _t j d¡ d S )NT©r3   r7   r6   Ú_CÚ_set_grad_enabledr&   r   r   r   r'   |   s    
zenable_grad.__enter__r(   c                 C   s   t j | j¡ d S r   ©r3   r:   r;   r6   r,   r   r   r   r-   €   s    zenable_grad.__exit__)r.   r/   r0   r1   r'   r   r-   r   r   r   r   r   ]   s   r   c                   @   sB   e Zd ZdZeddœdd„Zddœdd„Zeeedd	œd
d„ZdS )r   a¥  Context-manager that sets gradient calculation to on or off.

    ``set_grad_enabled`` will enable or disable grads based on its argument :attr:`mode`.
    It can be used as a context-manager or as a function.

    This context manager is thread local; it will not affect computation
    in other threads.

    Arguments:
        mode (bool): Flag whether to enable grad (``True``), or disable
                     (``False``). This can be used to conditionally enable
                     gradients.


    Example::

        >>> x = torch.tensor([1], requires_grad=True)
        >>> is_train = False
        >>> with torch.set_grad_enabled(is_train):
        ...   y = x * 2
        >>> y.requires_grad
        False
        >>> torch.set_grad_enabled(True)
        >>> y = x * 2
        >>> y.requires_grad
        True
        >>> torch.set_grad_enabled(False)
        >>> y = x * 2
        >>> y.requires_grad
        False

    N)Úmoder   c                 C   s   t  ¡ | _t j |¡ d S r   r9   )r   r=   r   r   r   r5   ¦   s    
zset_grad_enabled.__init__r#   c                 C   s   d S r   r   r&   r   r   r   r'   ª   s    zset_grad_enabled.__enter__r(   c                 C   s   t j | j¡ d S r   r<   r,   r   r   r   r-   ­   s    zset_grad_enabled.__exit__)	r.   r/   r0   r1   Úboolr5   r'   r   r-   r   r   r   r   r   „   s   !r   )r3   r   r   Útypingr   r   r   r   Ú__all__ZFuncTyper	   r   r   r   Úobjectr   r   r   r   r   Ú<module>   s   "+'