a
    –¥»a”$  ã                   @   sª   d dl Z 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G dd„ deƒZdS )é    N)ÚAnyÚCallableÚTypeVarÚcast)Úno_gradÚenable_gradÚset_grad_enabledÚinference_mode.Ú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© úR/home/yuhw/wc/pochoir/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                  ?   sB  ˆ | i |¤Ž}t ˆƒ}zú|ƒ � | d ¡}W d   ƒ n1 s>0    Y  z
|V }W n† ty”   |ƒ � | ¡  W d   ƒ n1 s„0    Y  ‚ Y qH tyØ   |ƒ � |jt ¡ Ž }W d   ƒ n1 sÊ0    Y  Y qH0 |ƒ � | |¡}W d   ƒ qH1 �s0    Y  qHW n, t�y< } z|j	W  Y d }~S d }~0 0 d S r   )
ÚtypeÚsendÚGeneratorExitÚcloseÚBaseExceptionÚthrowÚsysÚexc_infoÚStopIterationÚvalue)r   r   ÚgenÚclsÚresponseÚrequestÚer   r   r   Úgenerator_context!   s$    (
&20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__N   s    z"_DecoratorContextManager.__enter__©Úexc_typeÚ	exc_valueÚ	tracebackr   c                 C   s   t ‚d S r   r0   ©r   r5   r6   r7   r   r   r   Ú__exit__Q   s    z!_DecoratorContextManager.__exit__)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r3   r   r9   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   ai  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.)

    .. note::
        No-grad is one of several mechanisms that can enable or
        disable gradients locally see :ref:`locally-disable-grad-doc` for
        more information on how they compare.

    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)ÚtorchÚ_jit_internalÚis_scriptingÚsuperÚ__init__Úprevr2   r   r   r   rC   w   s    

zno_grad.__init__c                 C   s   t  ¡ | _t  d¡ d S r>   )r?   Úis_grad_enabledrD   r   r2   r   r   r   r3   |   s    
zno_grad.__enter__Nr4   c                 C   s   t  | j¡ d S r   )r?   r   rD   r8   r   r   r   r9   €   s    zno_grad.__exit__©	r:   r;   r<   r=   rC   r3   r   r9   Ú__classcell__r   r   r   r   r   U   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.)

    .. note::
        enable_grad is one of several mechanisms that can enable or
        disable gradients locally see :ref:`locally-disable-grad-doc` for
        more information on how they compare.

    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©r?   rE   rD   Ú_CÚ_set_grad_enabledr2   r   r   r   r3   §   s    
zenable_grad.__enter__r4   c                 C   s   t j | j¡ d S r   ©r?   rI   rJ   rD   r8   r   r   r   r9   «   s    zenable_grad.__exit__)r:   r;   r<   r=   r3   r   r9   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   ao  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.

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

    .. note::
        set_grad_enabled is one of several mechanisms that can enable or
        disable gradients locally see :ref:`locally-disable-grad-doc` for
        more information on how they compare.

    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   rH   ©r   rL   r   r   r   rC   Õ   s    
zset_grad_enabled.__init__r/   c                 C   s   d S r   r   r2   r   r   r   r3   Ù   s    zset_grad_enabled.__enter__r4   c                 C   s   t j | j¡ d S r   rK   r8   r   r   r   r9   Ü   s    zset_grad_enabled.__exit__)	r:   r;   r<   r=   ÚboolrC   r3   r   r9   r   r   r   r   r   ¯   s   %r   c                       s>   e Zd ZdZd‡ fdd„	Zdd„ Zeeeddœd	d
„Z‡  ZS )r	   aX  Context-manager that enables or disables inference mode

    InferenceMode is a new context manager analogous to :class:`~no_grad`
    to be used when you are certain your operations will have no interactions
    with autograd (e.g., model training). Code run under this mode gets better
    performance by disabling view tracking and version counter bumps.

    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.)

    .. note::
        Inference mode is one of several mechanisms that can enable or
        disable gradients locally see :ref:`locally-disable-grad-doc` for
        more information on how they compare.

    Args:
        mode (bool): Flag whether to enable or disable inference mode

    Example::
        >>> import torch
        >>> x = torch.ones(1, 2, 3, requires_grad=True)
        >>> with torch.inference_mode():
        ...   y = x * x
        >>> y.requires_grad
        False
        >>> y._version
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        RuntimeError: Inference tensors do not track version counter.
        >>> @torch.inference_mode()
        ... def func(x):
        ...   return x * x
        >>> out = func(x)
        >>> out.requires_grad
        False

    Tc                    s$   t j ¡ stƒ  ¡  d | _|| _d S r   )r?   r@   rA   rB   rC   Ú_inference_mode_raii_guardrL   rM   r   r   r   rC     s    

zinference_mode.__init__c                 C   s   t j | j¡| _d S r   )r?   rI   Z_InferenceModerL   rO   r2   r   r   r   r3     s    zinference_mode.__enter__Nr4   c                 C   s   | ` d S r   )rO   r8   r   r   r   r9     s    zinference_mode.__exit__)TrF   r   r   r   r   r	   à   s   'r	   )r%   r?   r   r   Útypingr   r   r   r   Ú__all__ZFuncTyper
   r   r   r   Úobjectr   r	   r   r   r   r   Ú<module>   s   C/+1