
    shu                        S SK Jr  S SKJrJrJr  S SKJrJr  S SK	r	S SK
Jr  S SKJrJr  SSKJrJr  \SS\R"                  S	\\   4S
 jj5       r " S S\5      r\" \5      S\R"                  S\R"                  4S j5       rg)    )	Generator)AbstractContextManagercontextmanagernullcontext)AnyOptionalN)'_checkpoint_without_reentrant_generator_DEFAULT_DETERMINISM_MODE   )_Statecontractmoduleuser_ctxc              #   V  #    U(       a  UO	[        5          [        R                  U 5      R                  nS[        R                  U 5      l         Sv   U[        R                  U 5      l         SSS5        g! U[        R                  U 5      l        f = f! , (       d  f       g= f7f)zg
Disable hooks installed by checkpoint to avoid unintentional recursion
during backward recomputation.
FN)r   
checkpointstateenable_hook)r   r   orig_enable_hooks      ڇ/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/distributed/_composable/checkpoint_activation.py_no_hookr      s      ;=	0%++F3??/4
 ,	D3CJV$0 
1	0 4DJV$0 
1	0s4   B):BA9B0	B)9BB
B&"B)c                   :    \ rS rSr% Sr\\S'   \\S      \S'   Sr	g)_CheckpointState    Fr   )NNN_ac_generator N)
__name__
__module____qualname____firstlineno__r   bool__annotations__r   r   __static_attributes__r       r   r   r       s    KI&6788r#   r   returnc                   ^^^^ [         R                  R                  S5        UR                  SS5      nU(       a  [	        S5      eUR                  SS5      mUR                  SS5      mUR                  S	[
        5      mUR                  S
S5      mU(       a$  [        SSR                  S U 5       5      -   5      eS[        R                  S[        [        S4   S[        [        [        4   SS4UUUU4S jjnS[        R                  S[        [        S4   S[        S[        4S jnS[        R                  U 5      l        U R#                  USS9  U R%                  USSS9  U $ )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) -> None:
    >>>         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.checkpointuse_reentrantFzsuse_reentrant=True is not supported in composable checkpoint. Please use torch.utils.checkpoint.checkpoint instead.preserve_rng_stateT
context_fnNdeterminism_checkdebugzUnexpected keyword arguments: ,c              3   $   #    U  H  ov   M     g 7fNr   ).0args     r   	<genexpr>checkpoint.<locals>.<genexpr>U   s     7Nvvs   r   args.kwargsr$   c                    >^  [         R                  T 5      R                  (       a@  U U4S jn[        T TUTT/UQ70 UD6nU[         R                  T 5      l        [        U5        g g )Nc                  b   > Tb  T" 5       u  pU [        TU5      4$ [        5       [        T5      4$ r-   )r   r   )ctx1ctx2r   user_context_fnss     r   context_fns9checkpoint.<locals>.forward_pre_hook.<locals>.context_fns]   s7    #/!1!3JD&$!777&=(6*:::r#   )r   r   r   r	   r   next)	r   r2   r3   r9   genr*   r)   r'   r8   s	   `    r   forward_pre_hook$checkpoint.<locals>.forward_pre_hookX   sn     F#//; :"!  C 69JV$2I' 0r#   inputsoutputc                    [         R                  U 5      R                  (       a;   [         R                  U 5      R                  nUc   e[	        U5        [        S5      eS [         R                  U 5      l        g ! [         a     N'f = f)NzWExpected non-reentrant activation checkpoint generator to be exhausted, but it was not!)r   r   r   r   r;   RuntimeErrorStopIteration)r   r?   r@   r<   s       r   forward_hook checkpoint.<locals>.forward_hookp   s}    F#//	 &&v.<<&S	 #m  26
 . ! s   /A; ;
BB)with_kwargs)prependalways_call)torch_C_log_api_usage_oncepopNotImplementedErrorr
   
ValueErrorjoinnnModuletupler   dictstrr   r   r   register_forward_pre_hookregister_forward_hook)	r   r3   r&   r=   rD   r*   r)   r'   r8   s	        @@@@r   r   r   %   s]   B 
HH  !?@JJ6M!D
 	
  $8$?zz,5

#68QRJJw&E,sxx7Nv7N/NN
 	
		!&sCx:>sCx.	 06RYY 6c3h 6 6QT 6" ,0JV(
$$%54$H
  t NMr#   r-   )collections.abcr   
contextlibr   r   r   typingr   r   rI   torch.nnrP   torch.utils.checkpointr	   r
   r   r   rQ   r   r   r   r   r#   r   <module>r\      s    % J J    
 ' DRYY D(3I*J D D9v 9
 

^ryy ^ryy ^ ^r#   