3
I4W^   ã               @   s,   d dl 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 )	Údetect_anomalyag  Context-manager that enable anomaly detection for the autograd engine.

    This does two things:
    - Running the forward pass with detection enabled will allow the backward
    pass to print the traceback of the forward operation that created the failing
    backward function.
    - Any backward computation that generate "nan" value will raise an error.

    .. warning::
        This mode should be enabled only for debugging as the different tests
        will slow down your program execution.

    Example:

        >>> import torch
        >>> from torch import autograd
        >>> class MyFunc(autograd.Function):
        ...     @staticmethod
        ...     def forward(ctx, inp):
        ...         return inp.clone()
        ...     @staticmethod
        ...     def backward(ctx, gO):
        ...         # Error during the backward pass
        ...         raise RuntimeError("Some error in backward")
        ...         return gO.clone()
        >>> def run_fn(a):
        ...     out = MyFunc.apply(a)
        ...     return out.sum()
        >>> inp = torch.rand(10, 10, requires_grad=True)
        >>> out = run_fn(inp)
        >>> out.backward()
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
              File "/your/pytorch/install/torch/tensor.py", line 93, in backward
                torch.autograd.backward(self, gradient, retain_graph, create_graph)
              File "/your/pytorch/install/torch/autograd/__init__.py", line 90, in backward
                allow_unreachable=True)  # allow_unreachable flag
              File "/your/pytorch/install/torch/autograd/function.py", line 76, in apply
                return self._forward_cls.backward(self, *args)
              File "<stdin>", line 8, in backward
            RuntimeError: Some error in backward
        >>> with autograd.detect_anomaly():
        ...     inp = torch.rand(10, 10, requires_grad=True)
        ...     out = run_fn(inp)
        ...     out.backward()
            Traceback of forward call that caused the error:
              File "tmp.py", line 53, in <module>
                out = run_fn(inp)
              File "tmp.py", line 44, in run_fn
                out = MyFunc.apply(a)
            Traceback (most recent call last):
              File "<stdin>", line 4, in <module>
              File "/your/pytorch/install/torch/tensor.py", line 93, in backward
                torch.autograd.backward(self, gradient, retain_graph, create_graph)
              File "/your/pytorch/install/torch/autograd/__init__.py", line 90, in backward
                allow_unreachable=True)  # allow_unreachable flag
              File "/your/pytorch/install/torch/autograd/function.py", line 76, in apply
                return self._forward_cls.backward(self, *args)
              File "<stdin>", line 8, in backward
            RuntimeError: Some error in backward

    c             C   s   t jƒ | _d S )N)ÚtorchÚis_anomaly_enabledÚprev)Úself© r   ú</tmp/pip-build-e66j97b2/torch/torch/autograd/anomaly_mode.pyÚ__init__D   s    zdetect_anomaly.__init__c             C   s   t jdƒ d S )NT)r   Úset_anomaly_enabled)r   r   r   r   Ú	__enter__G   s    zdetect_anomaly.__enter__c             G   s   t j| jƒ dS )NF)r   r
   r   )r   Úargsr   r   r   Ú__exit__J   s    zdetect_anomaly.__exit__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 )	Úset_detect_anomalyaå  Context-manager that sets the anomaly detection for the autograd engine on or off.

    ``set_detect_anomaly`` will enable or disable the autograd anomaly detection
    based on its argument :attr:`mode`.
    It can be used as a context-manager or as a function.

    See ``detect_anomaly`` above for details of the anomaly detection behaviour.

    Arguments:
        mode (bool): Flag whether to enable anomaly detection (``True``),
                     or disable (``False``).

    c             C   s   t jƒ | _t j|ƒ d S )N)r   r   r   r
   )r   Úmoder   r   r   r	   ^   s    
zset_detect_anomaly.__init__c             C   s   d S )Nr   )r   r   r   r   r   b   s    zset_detect_anomaly.__enter__c             G   s   t j| jƒ dS )NF)r   r
   r   )r   r   r   r   r   r   e   s    zset_detect_anomaly.__exit__N)r   r   r   r   r	   r   r   r   r   r   r   r   O   s   r   )r   Úobjectr   r   r   r   r   r   Ú<module>   s   K