3
I4W^¡  ã               @   sD   d dl Z d dlZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    Nc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Úno_gradaÆ  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 mode has no effect when using :class:`~enable_grad` context manager .

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

    Also functions as a decorator.


    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             C   s   t jƒ | _t jjdƒ d S )NF)ÚtorchÚis_grad_enabledÚprevÚ_CÚset_grad_enabled)Úself© r	   ú9/tmp/pip-build-e66j97b2/torch/torch/autograd/grad_mode.pyÚ	__enter__%   s    
zno_grad.__enter__c             G   s   t j| jƒ dS )NF)r   r   r   )r   Úargsr	   r	   r
   Ú__exit__)   s    zno_grad.__exit__c                s   t jˆ ƒ‡ ‡fdd„ƒ}|S )Nc           
      s   ˆ� ˆ | |ŽS Q R X d S )Nr	   )r   Úkwargs)Úfuncr   r	   r
   Údecorate_no_grad.   s    z*no_grad.__call__.<locals>.decorate_no_grad)Ú	functoolsÚwraps)r   r   r   r	   )r   r   r
   Ú__call__-   s    zno_grad.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r	   r	   r	   r
   r      s   r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Úenable_gradaô  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.


    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

    c             C   s   t jƒ | _t jjdƒ d S )NT)r   r   r   r   r   )r   r	   r	   r
   r   T   s    
zenable_grad.__enter__c             G   s   t j| jƒ dS )NF)r   r   r   )r   r   r	   r	   r
   r   X   s    zenable_grad.__exit__c                s   t jˆ ƒ‡ ‡fdd„ƒ}|S )Nc           
      s   ˆ� ˆ | |ŽS Q R X d S )Nr	   )r   r   )r   r   r	   r
   Údecorate_enable_grad]   s    z2enable_grad.__call__.<locals>.decorate_enable_grad)r   r   )r   r   r   r	   )r   r   r
   r   \   s    zenable_grad.__call__N)r   r   r   r   r   r   r   r	   r	   r	   r
   r   5   s   r   c               @   s(   e Zd ZdZdd„ Zdd„ Z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.

    When using :class:`~enable_grad` context manager, :class:`~set_grad_enabled(False)`
    has no effect.

    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

    c             C   s   t jƒ | _t jj|ƒ d S )N)r   r   r   r   r   )r   Úmoder	   r	   r
   Ú__init__‰   s    
zset_grad_enabled.__init__c             C   s   d S )Nr	   )r   r	   r	   r
   r   �   s    zset_grad_enabled.__enter__c             G   s   t j| jƒ dS )NF)r   r   r   )r   r   r	   r	   r
   r   �   s    zset_grad_enabled.__exit__N)r   r   r   r   r   r   r   r	   r	   r	   r
   r   d   s   #r   )r   r   Úobjectr   r   r   r	   r	   r	   r
   Ú<module>   s   0/