a
    OSic                     @   s:   d dl Z d dlmZmZ G dd dZG dd deZdS )    N)CallableAnyc                   @   sN   e Zd ZdZeejgef eegejf dddZdd Z	eddd	Z
d
S )saved_tensors_hooksa	  Context-manager that sets a pair of pack / unpack hooks for saved tensors.

    Use this context-manager to define how intermediary results of an operation
    should be packed before saving, and unpacked on retrieval.

    In that context, the ``pack_hook`` function will be called everytime an
    operation saves a tensor for backward (this includes intermediary results
    saved using
    :func:`~torch.autograd.function._ContextMethodMixin.save_for_backward` but
    also those recorded by a PyTorch-defined operation). The output of
    ``pack_hook`` is then stored in the computation graph instead of the
    original tensor.

    The ``unpack_hook`` is called when the saved tensor needs to be accessed,
    namely when executing :func:`torch.Tensor.backward()` or
    :func:`torch.autograd.grad()`. It takes as argument the *packed* object
    returned by ``pack_hook`` and should return a tensor which has the same
    content as the original tensor (passed as input to the corresponding
    ``pack_hook``).

    The hooks should have the following signatures:

        pack_hook(tensor: Tensor) -> Any

        unpack_hook(Any) -> Tensor

    where the return value of ``pack_hook`` is a valid input to ``unpack_hook``.

    In general, you want ``unpack_hook(pack_hook(t))`` to be equal to ``t`` in terms
    of value, size, dtype and device.

    Example::

        >>> def pack_hook(x):
        ...     print("Packing", x)
        ...     return x
        >>>
        >>> def unpack_hook(x):
        ...     print("Unpacking", x)
        ...     return x
        >>>
        >>> a = torch.ones(5, requires_grad=True)
        >>> b = torch.ones(5, requires_grad=True) * 2
        >>> with torch.autograd.graph.saved_tensors_hooks(pack_hook, unpack_hook):
        ...     y = a * b
        Packing tensor([1., 1., 1., 1., 1.])
        Packing tensor([2., 2., 2., 2., 2.])
        >>> y.sum().backward()
        Unpacking tensor([1., 1., 1., 1., 1.])
        Unpacking tensor([2., 2., 2., 2., 2.])

    .. warning ::
        Performing an inplace operation on the input to either hooks may lead
        to undefined behavior.

    .. warning ::
        Only one pair of hooks is allowed at a time. When recursively nesting this
        context-manager, only the inner-most pair of hooks will be applied.
    	pack_hookunpack_hookc                 C   s   || _ || _d S Nr   )selfr   r    r
   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/autograd/graph.py__init__@   s    zsaved_tensors_hooks.__init__c                 C   s   t jj| j| j d S r   )torch_C	_autograd!_push_saved_tensors_default_hooksr   r   )r	   r
   r
   r   	__enter__D   s    zsaved_tensors_hooks.__enter__)argsc                 G   s   t jj  d S r   )r   r   r    _pop_saved_tensors_default_hooks)r	   r   r
   r
   r   __exit__G   s    zsaved_tensors_hooks.__exit__N)__name__
__module____qualname____doc__r   r   Tensorr   r   r   r   r
   r
   r
   r   r      s   ;(r   c                       s"   e Zd ZdZd fdd	Z  ZS )save_on_cpua  Context-manager under which tensors saved by the forward pass will be
    stored on cpu, then retrieved for backward.

    When performing operations within this context manager, intermediary
    results saved in the graph during the forward pass will be moved to CPU,
    then copied back to the original device when needed for the backward pass.
    If the graph was already on CPU, no tensor copy is performed.

    Use this context-manager to trade compute for GPU memory usage (e.g.
    when your model doesn't fit in GPU memory during training).

    Args:
        pin_memory (bool): If ``True`` tensors will be saved to CPU pinned memory
                           during packing and copied to GPU asynchronously during unpacking.
                           Defaults to ``False``.
                           Also see :ref:`cuda-memory-pinning`.


    Example::

        >>> a = torch.randn(5, requires_grad=True, device="cuda")
        >>> b = torch.randn(5, requires_grad=True, device="cuda")
        >>> c = torch.randn(5, requires_grad=True, device="cuda")
        >>>
        >>> def f(a, b, c):
        ...     prod_1 = a * b           # a and b are saved on GPU
        ...     with torch.autograd.graph.save_on_cpu():
        ...         prod_2 = prod_1 * c  # prod_1 and c are saved on CPU
        ...     y = prod_2 * a           # prod_2 and a are saved on GPU
        ...     return y
        >>>
        >>> y = f(a, b, c)
        >>> del a, b, c  # for illustration only
        >>> # the content of a, b, and prod_2 are still alive on GPU
        >>> # the content of prod_1 and c only live on CPU
        >>> y.sum().backward()  # all CPU tensors are moved back to GPU, for backward
        >>> # all intermediary tensors are released (deleted) after the call to backward

    Fc                    s*    fdd} fdd}t  || d S )Nc                    sN    s| j |  fS tj|  | j| jtj o4| j	 d}|
|  | j |fS )N)dtypelayout
pin_memory)devicecpur   emptysizer   r   cudais_available	is_sparsecopy_)tensorpackedr   r
   r   pack_to_cput   s    
z)save_on_cpu.__init__.<locals>.pack_to_cpuc                    s   | \}}|j | dS )N)non_blocking)to)r'   r   r&   r(   r
   r   unpack_from_cpu   s    z-save_on_cpu.__init__.<locals>.unpack_from_cpu)superr   )r	   r   r)   r,   	__class__r(   r   r   s   s    zsave_on_cpu.__init__)F)r   r   r   r   r   __classcell__r
   r
   r.   r   r   K   s   'r   )r   typingr   r   r   r   r
   r
   r
   r   <module>   s   G