§
    v-j  ã                  óh   — d dl mZ d dlmZmZ d dlmZ erd dlmZ d dl	m
Z
 g Z G d„ d¦  «        ZdS )	é    )Úannotations)ÚTYPE_CHECKINGÚAny)Úcore)ÚCallable)ÚTensorc                  ó*   — e Zd ZdZdd„Zdd	„Zdd„ZdS )Úsaved_tensors_hooksaã  
    Dynamic graph, registers a pair of pack / unpack hooks for saved tensors.

    Parameters:
        pack_hook (function): The pack hook will be called every time the forward
            operation inputs/outputs tensors need be saved for backward. Then you
            can save it to CPU or Disk. The input of `pack_hook` is a tensor need
            be saved. The output of `pack_hook` is then stored information instead
            of the original tensor. `pack_hook` will also be called while any
            tensor need be saved by `PyLayerContext.save_for_backward`. If a tensor
            saved for backward is no need buffer, `pack_hook` will not be called.
            Only the tensor saved for backward is DenseTensor, `pack_hook` will be
            called.
        unpack_hook (function): The unpack hook will be called every time the
            backward need use the saved inputs/outputs tensors. Then you can reload
            the tensor and return it to paddle framework. The input of `unpack_hook`
            is the information returned by `pack_hook`. The output of `unpack_hook`
            is a tensor reloaded by the information, and the tensor must has the same
            content as the original tensor passed as input to the corresponding
            `pack_hook`.

    Returns:
            None

    Examples:
        .. code-block:: python
            :name: code-example1

            >>> # Example1
            >>> import paddle

            >>> def pack_hook(x):
            ...     print("Packing", x)
            ...     return x.numpy()

            >>> def unpack_hook(x):
            ...     print("UnPacking", x)
            ...     return paddle.to_tensor(x)

            >>> a = paddle.ones([3,3])
            >>> b = paddle.ones([3,3]) * 2
            >>> a.stop_gradient = False
            >>> b.stop_gradient = False
            >>> with paddle.autograd.saved_tensors_hooks(pack_hook, unpack_hook):
            ...     y = paddle.multiply(a, b)
            >>> y.sum().backward()

        .. code-block:: python
            :name: code-example2

            >>> # Example2
            >>> import paddle
            >>> from paddle.autograd import PyLayer

            >>> class cus_multiply(PyLayer):
            ...     @staticmethod
            ...     def forward(ctx, a, b):
            ...         y = paddle.multiply(a, b)
            ...         ctx.save_for_backward(a, b)
            ...         return y
            ...
            ...     @staticmethod
            ...     def backward(ctx, dy):
            ...         a,b = ctx.saved_tensor()
            ...         grad_a = dy * a
            ...         grad_b = dy * b
            ...         return grad_a, grad_b

            >>> def pack_hook(x):
            ...     print("Packing", x)
            ...     return x.numpy()

            >>> def unpack_hook(x):
            ...     print("UnPacking", x)
            ...     return paddle.to_tensor(x)

            >>> a = paddle.ones([3,3])
            >>> b = paddle.ones([3,3]) * 2
            >>> a.stop_gradient = False
            >>> b.stop_gradient = False
            >>> with paddle.autograd.saved_tensors_hooks(pack_hook, unpack_hook):
            ...     y = cus_multiply.apply(a, b)
            >>> y.sum().backward()
    Ú	pack_hookúCallable[[Tensor], Any | None]Úunpack_hookúCallable[[Any], Tensor | None]ÚreturnÚNonec                ó"   — || _         || _        d S ©N)r   r   )Úselfr   r   s      úc/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/autograd/saved_tensors_hooks.pyÚ__init__zsaved_tensors_hooks.__init__q   s   € ð
 #ˆŒØ&ˆÔÐÐó    c                óZ   — t           j                             | j        | j        ¦  «         d S r   )r   ÚeagerÚregister_saved_tensors_hooksr   r   )r   s    r   Ú	__enter__zsaved_tensors_hooks.__enter__y   s0   € ÝŒ
×/Ò/ØŒN˜DÔ,ñ	
ô 	
ð 	
ð 	
ð 	
r   ÚargsÚobjectc                óB   — t           j                             ¦   «          d S r   )r   r   Úreset_saved_tensors_hooks)r   r   s     r   Ú__exit__zsaved_tensors_hooks.__exit__~   s   € ÝŒ
×,Ò,Ñ.Ô.Ð.Ð.Ð.r   N)r   r   r   r   r   r   )r   r   )r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   © r   r   r
   r
      s]   € € € € € ðSð Sðj'ð 'ð 'ð 'ð
ð 
ð 
ð 
ð
/ð /ð /ð /ð /ð /r   r
   N)Ú
__future__r   Útypingr   r   Úpaddle.baser   Úcollections.abcr   Úpaddler   Ú__all__r
   r$   r   r   ú<module>r+      s°   ðð #Ð "Ð "Ð "Ð "Ð "à %Ð %Ð %Ð %Ð %Ð %Ð %Ð %à Ð Ð Ð Ð Ð àð Ø(Ð(Ð(Ð(Ð(Ð(àÐÐÐÐÐØ
€ðd/ð d/ð d/ð d/ð d/ñ d/ô d/ð d/ð d/ð d/r   