
    HhF                     D    d dl Z  G d de j        j                  Zd ZdS )    Nc                   :    e Zd Zed             Zed             ZdS )CheckpointFunctionc                     || _         t          |d |                   | _        t          ||d                    | _        t	          j                    5   | j         | j         }d d d            n# 1 swxY w Y   |S )N)run_functionlistinput_tensorsinput_paramstorchno_grad)ctxr   lengthargsoutput_tensorss        \/data/cameron/tmprepos/clean_dreammesh/UniRig/src/model/pointcept/models/utils/checkpoint.pyforwardzCheckpointFunction.forward   s    ' gvg//VWW..]__ 	B 	B-S-s/@AN	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	Bs   A//A36A3c                 .   d | j         D             | _         t          j                    5  d | j         D             } | j        | }d d d            n# 1 swxY w Y   t          j                            || j         | j        z   |d          }| ` | `~d|z   S )Nc                 \    g | ])}|                                                     d           *S )T)detachrequires_grad_.0xs     r   
<listcomp>z/CheckpointFunction.backward.<locals>.<listcomp>   s.    XXXQXXZZ66t<<XXX    c                 8    g | ]}|                     |          S  )view_asr   s     r   r   z/CheckpointFunction.backward.<locals>.<listcomp>   s"    FFFqaiillFFFr   T)allow_unused)NN)r   r
   enable_gradr   autogradgradr	   )r   output_gradsshallow_copiesr   input_gradss        r   backwardzCheckpointFunction.backward   s    XXcFWXXX   	? 	? GFC4EFFFN-S-~>N	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? n)) 00	 * 
 
 k))s   AAAN)__name__
__module____qualname__staticmethodr   r%   r   r   r   r   r      sH          \ * * \* * *r   r   c                     |r?t          |          t          |          z   }t          j        | t          |          g|R  S  | | S )a  
    Evaluate a function without caching intermediate activations, allowing for
    reduced memory at the expense of extra compute in the backward pass.
    :param func: the function to evaluate.
    :param inputs: the argument sequence to pass to `func`.
    :param params: a sequence of parameters `func` depends on but does not
                   explicitly take as arguments.
    :param flag: if False, disable gradient checkpointing.
    )tupler   applylen)funcinputsparamsflagr   s        r   
checkpointr2   $   sO      V}}uV}},!'c&kkADAAAAtV}r   )r
   r    Functionr   r2   r   r   r   <module>r4      sW    * * * * *0 * * *@    r   