Ë
    P/æjÆ  ã                   óâ   — d dl mZmZ d dlmZmZmZ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dej                   dee   fd	„«       Z e«       dej                   d
ej                   fd„«       Zy)é    )ÚcontextmanagerÚnullcontext)ÚAnyÚContextManagerÚDictÚOptionalÚTupleN)Ú'_checkpoint_without_reentrant_generatorÚ_DEFAULT_DETERMINISM_MODEé   )ÚcontractÚmoduleÚuser_ctxc              #   óB  K  — |r|n	t        «       5  t        j                  | «      j                  }dt        j                  | «      _        	 d–— |t        j                  | «      _        	 ddd«       y# |t        j                  | «      _        w xY w# 1 sw Y   yxY w­w)zs
    Disable hooks installed by checkpoint to avoid unintentional recursion
    during backward recomputation.
    FN)r   Ú
checkpointÚstateÚenable_hook)r   r   Úorig_enable_hooks      úv/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/distributed/_composable/checkpoint_activation.pyÚ_no_hookr      s�   è ø€ ñ ‰¤;£=ñ DÜ%×+Ñ+¨FÓ3×?Ñ?ÐØ/4Œ
×Ñ˜Ó Ô,ð	DÛà3CŒJ×Ñ˜VÓ$Õ0÷Dð Døð 4DŒJ×Ñ˜VÓ$Õ0ú÷Dð Düs4   ‚B‘:BÁA4ÁBÁ+	BÁ4BÂBÂBÂBÚreturnc                 óÈ  ‡‡‡‡— t         j                  j                  d«       |j                  dd«      }|rt	        d«      ‚|j                  dd«      Š|j                  dd«      Š|j                  d	t
        «      Š|j                  d
d«      Š|r$t        ddj                  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        j                  | «      _        | j#                  |d¬«       | 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) -> 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   ó    K  — | ]  }|–— Œ y ­w©N© )Ú.0Úargs     r   ú	<genexpr>zcheckpoint.<locals>.<genexpr>P   s   è ø€ Ò7NÀ¼Ñ7Nùs   ‚r   Úargs.Úkwargsr   c                 óú   •‡ — t         j                  ‰ «      j                  rZˆ ˆfd„}t        ‰ ‰|‰‰g|¢­i |¤Žt         j                  ‰ «      _        t        t         j                  ‰ «      j                  «       y y )Nc                  ób   •— ‰� ‰«       \  } }| t        ‰|«      fS t        «       t        ‰«      fS r    )r   r   )Úctx1Úctx2r   Úuser_context_fnss     €€r   Úcontext_fnsz9checkpoint.<locals>.forward_pre_hook.<locals>.context_fnsX   s9   ø€ Ø#Ð/Ù!1Ó!3‘J�D˜$Ø¤¨&°$Ó!7Ð7Ð7ä&›=¬(°6Ó*:Ð:Ð:ó    )r   r   r   r
   Ú_ac_generatorÚnext)r   r%   r&   r,   r   r   r   r+   s   `   €€€€r   Úforward_pre_hookz$checkpoint.<locals>.forward_pre_hookS   s‚   ù€ ô ×Ñ˜FÓ#×/Ò/õ;ô FØØ"ØØ!Øðð òð ñô ×ÑØóäô ”×!Ñ! &Ó)×7Ñ7Õ8ð) 0r-   ÚinputsÚ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   r1   r2   s      r   Úforward_hookz checkpoint.<locals>.forward_hookl   so   € Ü×Ñ˜FÓ#×/Ò/ðÜ”Z×%Ñ% fÓ-×;Ñ;Ô<ô #Ømóð ð 26Œ
×Ñ˜Ó Õ.øô !ò Ùðús   ¡(A/ Á/	A;Á:A;)Úwith_kwargs)ÚprependÚalways_call)ÚtorchÚ_CÚ_log_api_usage_onceÚpopÚNotImplementedErrorr   Ú
ValueErrorÚjoinÚnnÚModuler	   r   r   Ústrr   r   r   Úregister_forward_pre_hookÚregister_forward_hook)	r   r&   r   r0   r6   r   r   r   r+   s	        @@@@r   r   r       s]  û€ ôB 
‡H�H× Ñ Ð!?Ô@à—J‘J˜°Ó6€MÙÜ!ðDó
ð 	
ð  Ÿ™Ð$8¸$Ó?ÐØ—z‘z ,°Ó5ÐØŸ
™
Ð#6Ô8QÓRÐØ�J‰J�w Ó&€EáÜØ,¨s¯x©xÑ7NÀvÔ7NÓ/NÑNó
ð 	
ð9Ü—	‘	ð9Ü!&¤s¨C x¡ð9Ü:>¼sÄC¸x¹.ð9à	÷9ð 9ð26œRŸY™Yð 6´´c¸3°h±ð 6Ìð 6ÔQTó 6ð ,0„J×Ñ�VÓÔ(Ø
×$Ñ$Ð%5À4Ð$ÔHØ
× Ñ  °tÈÐ ÔNØ€Mr-   r    )Ú
contextlibr   r   Útypingr   r   r   r   r	   r:   Útorch.nnrA   Útorch.utils.checkpointr
   r   r   rB   r   r   r!   r-   r   ú<module>rJ      s   ð÷ 3ß =Õ =ã Ý ÷õ
 ð ñD�R—Y‘Yð D¨(°>Ñ*Bò Dó ðDñ 
ƒð]�r—y‘yð ]¨r¯y©yò ]ó ñ]r-   