
    Phe                         d dl mZmZ d dlmZmZ d dlZd dlmZ d dl	m
Z
mZ ddlmZ edej                  fd       Z e       dej                  d	ej                  fd
       Zy)    )contextmanagernullcontext)AnyTupleN)'_checkpoint_without_reentrant_generator_DEFAULT_DETERMINISM_MODE   )contractmodulec              #      K   t         j                  |       j                  }dt         j                  |       _        	 d |t         j                  |       _        y# |t         j                  |       _        w xY ww)zs
    Disable hooks installed by checkpoint to avoid unintentional recursion
    during backward recomputation.
    FN)
checkpointstateenable_hook)r   orig_enable_hooks     ~C:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/_composable/checkpoint_activation.py_no_hookr      sb      "''/;;+0JV(@/?
 ,/?
 ,s   :A;A A;A88A;returnc                 j   t         j                  j                  d       dt        j                  dt
        t        df   ddfd}dt        j                  dt
        t        df   dt        dt        fd	}d
t        j                  |       _	        | j                  |       | j                  |d
d
       | S )a  
    This is a composable activation checkpointing API. Unlike functional
    activation checkpointing APIs, this one does not require changing model
    source code. Unlike ``nn.Module`` wrapper activation checkpointing APIs,
    this one does not modify model structure or fully-qualified names either.
    Under the hood, it registers activation checkpointing logic as pre- and
    post-forward hooks. Hence, this API can be easily applied to any model or
    sub-modules in the model.

    Args:
        module (nn.Module): the target model or sub-module to apply activation
            checkpointing.

    Example::
        >>> # xdoctest: +SKIP
        >>> import torch.nn as nn
        >>>
        >>> class MyModel(nn.Module):
        >>>     def __init__(self):
        >>>         super().__init__()
        >>>         self.l1 = nn.Linear(10, 10)
        >>>         self.l2 = nn.Linear(10, 10)
        >>>
        >>>     def forward(self, x):
        >>>         return self.l2(self.l1(x))
        >>>
        >>> model = MyModel()
        >>> checkpoint(model.l1)  # apply activation checkpointing only to l1
        >>> model(torch.zeros(2, 10)).sum().backward()

    ztorch.distributed.checkpointr   inputs.r   Nc                      t         j                         j                  rZ fd}t         d|t        dg| t         j                         _        t        t         j                         j
                         y y )Nc                  .    t               t               fS )N)r   r   )r   s   r   context_fnsz9checkpoint.<locals>.forward_pre_hook.<locals>.context_fnsB   s    "}hv&666    TF)r   r   r   r   r   _ac_generatornext)r   r   r   s   `  r   forward_pre_hookz$checkpoint.<locals>.forward_pre_hook?   sq    F#//7
 Fk+DeNT  !!&)778 0r   outputc                     t         j                  |       j                  r4	 t        t         j                  |       j                         t        d      d t         j                  |       _        y # t        $ r Y &w xY w)NzWExpected non-reentrant activation checkpoint generator to be exhausted, but it was not!)r   r   r   r   r   RuntimeErrorStopIteration)r   r   r   s      r   forward_hookz checkpoint.<locals>.forward_hookL   so    F#//Z%%f-;;< #m  26
 . ! s   (A/ /	A;:A;T)prependalways_call)torch_C_log_api_usage_oncennModuler   r   r   r   r   register_forward_pre_hookregister_forward_hook)r   r   r!   s      r   r   r      s    B 
HH  !?@9 9E#s(O 9 96RYY 6c3h 6 6QT 6 ,0JV(
$$%56
  t NMr   )
contextlibr   r   typingr   r   r$   torch.nnr'   torch.utils.checkpointr   r   r
   r(   r   r    r   r   <module>r0      sm    2   
  
@RYY 
@ 
@ 
Aryy ARYY A Ar   