
    &Vji                        d dl mZ d dlmZ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mZ eddej        d	ee         fd
            Z G d de          Z ee          dej        dej        fd            ZdS )    )	Generator)AbstractContextManagercontextmanagernullcontext)AnyOptionalN)'_checkpoint_without_reentrant_generator_DEFAULT_DETERMINISM_MODE   )_Statecontractmoduleuser_ctxc              #   p  K   |r|nt                      5  t                              |           j        }dt                              |           _        	 dV  |t                              |           _        n$# |t                              |           _        w xY w	 ddd           dS # 1 swxY w Y   dS )zs
    Disable hooks installed by checkpoint to avoid unintentional recursion
    during backward recomputation.
    FN)r   
checkpointstateenable_hook)r   r   orig_enable_hooks      m/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/distributed/_composable/checkpoint_activation.py_no_hookr      s      	0;== D D%++F33?/4
  ,	DEEE3CJV$$003CJV$$0CCCC0D D D D D D D D D D D D D D D D D Ds)   ?B+A9 B+9!BB++B/2B/c                   @    e Zd ZU dZeed<   eed                  ed<   dS )_CheckpointStateFr   )NNN_ac_generatorN)__name__
__module____qualname__r   bool__annotations__r   r        r   r   r       s9         KI&67888888r    r   returnc                 n  	 t           j                            d           |                    dd          }|rt	          d          |                    dd          |                    dd          	|                    d	t
                    |                    d
d          |                    dd          |r/t          dd                    d |D                       z             dt          j	        dt          t          df         dt          t          t          f         ddf	fd}dt          j	        dt          t          df         dt          dt          fd}dt                              |           _        |                     |d           |                     |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) -> 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debug
early_stopzUnexpected keyword arguments: ,c              3      K   | ]}|V  d S Nr   ).0args     r   	<genexpr>zcheckpoint.<locals>.<genexpr>V   s"      7N7N7N7N7N7N7N7Nr    r   args.kwargsr!   c                      t                                          j        rK 	fd}t           |g|R i |}|t                                          _        t          |           d S d S )Nc                                   \  } }| t          |          fS t                      t                    fS r+   )r   r   )ctx1ctx2r   user_context_fnss     r   context_fnsz9checkpoint.<locals>.forward_pre_hook.<locals>.context_fns^   sG    #/!1!1!3!3JD$&$!7!777&==(6*:*:::r    )r   r   r   r	   r   next)
r   r/   r0   r6   genr'   r&   r(   r$   r5   s
   `    r   forward_pre_hookz$checkpoint.<locals>.forward_pre_hookY   s     F##/ 	; ; ; ; ; ; :"!	 	 	 	 	 	C 69JV$$2IIIII)	 	r    inputsoutputc                 &   t                               |           j        rR	 t                               |           j        }|J t	          |           t          d          # t          $ r Y nw xY wd t                               |           _        d S )NzWExpected non-reentrant activation checkpoint generator to be exhausted, but it was not!)r   r   r   r   r7   RuntimeErrorStopIteration)r   r:   r;   r8   s       r   forward_hookz checkpoint.<locals>.forward_hookr   s    F##/ 
		 &&v..<S			 #m   !    26
  ...s   2A" "
A/.A/)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   r0   r#   r9   r?   r'   r&   r(   r$   r5   s
        @@@@@r   r   r   %   s   B 
H  !?@@@JJ66M 
!D
 
 	
  $8$??zz,55

#68QRRJJw&&EL$//J 
,sxx7N7Nv7N7N7N/N/NN
 
 	
	!&sCx:>sCx.	         26RY 6c3h 6 6QT 6 6 6 6" ,0JV(
$$%54$HHH
  t NNNMr    r+   )collections.abcr   
contextlibr   r   r   typingr   r   rC   torch.nnrJ   torch.utils.checkpointr	   r
   r   r   rK   r   r   r   r   r    r   <module>rV      sf   % % % % % % J J J J J J J J J J                              
 ' & & & & & & & D DRY D(3I*J D D D D9 9 9 9 9v 9 9 9
 

`ry `ry ` ` ` ` ` `r    