§
    ŠŠtj  ã                   óV   — d Z ddlZddlZddgZ G d„ d¦  «        Z G d„ d¦  «        ZdS )zAutograd anomaly mode.é    NÚdetect_anomalyÚset_detect_anomalyc                   ó4   — e Zd ZdZd	d
d„Zd
d„Zdeddfd„ZdS )r   aì  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.
    - If ``check_nan`` is ``True``, any backward computation that generate "nan"
      value will raise an error. Default ``True``.

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

    Example:
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_ANOMALY)
        >>> 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

    TÚreturnNc                 ó    — t          j        ¦   «         | _        || _        t          j        ¦   «         | _        t          j        dd¬¦  «         d S )NzqAnomaly Detection has been enabled. This mode will increase the runtime and should only be enabled for debugging.é   )Ú
stacklevel)ÚtorchÚis_anomaly_enabledÚprevÚ	check_nanÚis_anomaly_check_nan_enabledÚprev_check_nanÚwarningsÚwarn)Úselfr   s     úY/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/autograd/anomaly_mode.pyÚ__init__zdetect_anomaly.__init__O   sW   € ÝÔ,Ñ.Ô.ˆŒ	Ø"ˆŒÝ#Ô@ÑBÔBˆÔÝŒð8ð ð		
ñ 	
ô 	
ð 	
ð 	
ð 	
ó    c                 ó:   — t          j        d| j        ¦  «         d S )NT)r
   Úset_anomaly_enabledr   ©r   s    r   Ú	__enter__zdetect_anomaly.__enter__Z   s   € ÝÔ! $¨¬Ñ7Ô7Ð7Ð7Ð7r   Úargsc                 óD   — t          j        | j        | j        ¦  «         d S ©N©r
   r   r   r   ©r   r   s     r   Ú__exit__zdetect_anomaly.__exit__]   ó    € ÝÔ! $¤)¨TÔ-@ÑAÔAÐAÐAÐAr   ©T©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úobjectr   © r   r   r   r      sx   € € € € € ð@ð @ðD	
ð 	
ð 	
ð 	
ð 	
ð8ð 8ð 8ð 8ðB˜fð B¨ð Bð Bð Bð Bð Bð Br   c                   ó@   — e Zd ZdZddededdfd„Zdd„Zd	eddfd
„ZdS )r   aT  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.

    Args:
        mode (bool): Flag whether to enable anomaly detection (``True``),
                     or disable (``False``).
        check_nan (bool): Flag whether to raise an error when the backward
                          generate "nan"

    TÚmoder   r   Nc                 ó�   — t          j        ¦   «         | _        t          j        ¦   «         | _        t          j        ||¦  «         d S r   )r
   r   r   r   r   r   )r   r*   r   s      r   r   zset_detect_anomaly.__init__r   s<   € ÝÔ,Ñ.Ô.ˆŒ	Ý#Ô@ÑBÔBˆÔÝÔ! $¨	Ñ2Ô2Ð2Ð2Ð2r   c                 ó   — d S r   r(   r   s    r   r   zset_detect_anomaly.__enter__w   s   € Øˆr   r   c                 óD   — t          j        | j        | j        ¦  «         d S r   r   r   s     r   r   zset_detect_anomaly.__exit__z   r    r   r!   r"   )	r#   r$   r%   r&   Úboolr   r   r'   r   r(   r   r   r   r   a   sŠ   € € € € € ðð ð 3ð 3˜Tð 3¨dð 3¸dð 3ð 3ð 3ð 3ð
ð ð ð ðB˜fð B¨ð Bð Bð Bð Bð Bð Br   )r&   r   r
   Ú__all__r   r   r(   r   r   ú<module>r0      sŸ   ðà Ð à €€€à €€€ð Ð1Ð
2€ðRBð RBð RBð RBð RBñ RBô RBð RBðjBð Bð Bð Bð Bñ Bô Bð Bð Bð Br   