a
    OSicic                     @   s  d dl Z d dlmZ d dlmZ d dlm  mZ d dlmZ d dl	Z	d dl
Z
d dlmZ d dlmZmZmZ G dd deZeZG dd	 d	eZG d
d dejeeZG dd deZG dd deeejeeZdd Zdd ZG dd deZd0ddZdd Zd1ddZdd Z edd  d!d"Z!ed#d  d$ed%Z"ed&d  d'd(d)Z#ed*d  d+d"Z$ed,d  d-d  d$d"Z%G d.d/ d/eZ&dS )2    N)
_functions)with_metaclass)OrderedDict)AnyListOptionalc                   @   sb   e Zd ZejdddZejdddZejdddZd	d
 ZejdddZ	e
dddZdS )FunctionCtx)tensorsc                 G   s
   || _ dS )a  Saves given tensors for a future call to :func:`~Function.backward`.

        ``save_for_backward`` should be called at most once, only from inside the
        :func:`forward` method, and only with tensors.

        All tensors intended to be used in the backward pass should be saved
        with ``save_for_backward`` (as opposed to directly on ``ctx``) to prevent
        incorrect gradients and memory leaks, and enable the application of saved
        tensor hooks. See :class:`torch.autograd.graph.saved_tensors_hooks`.

        In :func:`backward`, saved tensors can be accessed through the :attr:`saved_tensors`
        attribute. Before returning them to the user, a check is made to ensure
        they weren't used in any in-place operation that modified their content.

        Arguments can also be ``None``. This is a no-op.

        See :ref:`extending-autograd` for more details on how to use this method.

        Example::
            >>> class Func(Function):
            >>>     @staticmethod
            >>>     def forward(ctx, x: torch.Tensor, y: torch.Tensor, z: int):
            >>>         w = x * y * z
            >>>         out = x * y + y * z + w
            >>>         ctx.save_for_backward(x, y, w, out)
            >>>         ctx.z = z  # z is not a tensor
            >>>         return out
            >>>
            >>>     @staticmethod
            >>>     def backward(ctx, grad_out):
            >>>         x, y, w, out = ctx.saved_tensors
            >>>         z = ctx.z
            >>>         gx = grad_out * (y + y * z)
            >>>         gy = grad_out * (x + z + x * z)
            >>>         gz = None
            >>>         return gx, gy, gz
            >>>
            >>> a = torch.tensor(1., requires_grad=True, dtype=torch.double)
            >>> b = torch.tensor(2., requires_grad=True, dtype=torch.double)
            >>> c = 4
            >>> d = Func.apply(a, b, c)

        N)to_save)selfr	    r   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/autograd/function.pysave_for_backward   s    ,zFunctionCtx.save_for_backwardc                 G   s0   |D ] }t |tjs|du sJ dq|| _dS )a  Saves given tensors for a future call to :func:`~Function.jvp`.

        ``save_for_forward`` should be only called once, from inside the :func:`forward`
        method, and only be called with tensors.

        In :func:`jvp`, saved objects can be accessed through the :attr:`saved_tensors`
        attribute.

        Arguments can also be ``None``. This is a no-op.

        See :ref:`extending-autograd` for more details on how to use this method.

        Example::
            >>> class Func(torch.autograd.Function):
            >>>     @staticmethod
            >>>     def forward(ctx, x: torch.Tensor, y: torch.Tensor, z: int):
            >>>         ctx.save_for_backward(x, y)
            >>>         ctx.save_for_forward(x, y)
            >>>         ctx.z = z
            >>>         return x * y * z
            >>>
            >>>     @staticmethod
            >>>     def jvp(ctx, x_t, y_t, _):
            >>>         x, y = ctx.saved_tensors
            >>>         z = ctx.z
            >>>         return z * (y * x_t + x * y_t)
            >>>
            >>>     @staticmethod
            >>>     def vjp(ctx, grad_out):
            >>>         x, y = ctx.saved_tensors
            >>>         z = ctx.z
            >>>         return z * grad_out * y, z * grad_out * x, None
            >>>
            >>>     a = torch.tensor(1., requires_grad=True, dtype=torch.double)
            >>>     t = torch.tensor(1., dtype=torch.double)
            >>>     b = torch.tensor(2., requires_grad=True, dtype=torch.double)
            >>>     c = 4
            >>>
            >>>     with fwAD.dual_level():
            >>>         a_dual = fwAD.make_dual(a, t)
            >>>         d = Func.apply(a_dual, b, c)

        Nzgsave_for_forward expects all arguments to be tensors; you should save non-tensors as attributes on ctx.)
isinstancetorchTensorsaved_for_forward)r   r	   tensorr   r   r   save_for_forward<   s
    ,zFunctionCtx.save_for_forward)argsc                 G   s
   || _ dS )a  Marks given tensors as modified in an in-place operation.

        **This should be called at most once, only from inside the**
        :func:`forward` **method, and all arguments should be inputs.**

        Every tensor that's been modified in-place in a call to :func:`forward`
        should be given to this function, to ensure correctness of our checks.
        It doesn't matter whether the function is called before or after
        modification.

        Examples::
            >>> class Inplace(Function):
            >>>     @staticmethod
            >>>     def forward(ctx, x):
            >>>         x_npy = x.numpy() # x_npy shares storage with x
            >>>         x_npy += 1
            >>>         ctx.mark_dirty(x)
            >>>         return x
            >>>
            >>>     @staticmethod
            >>>     @once_differentiable
            >>>     def backward(ctx, grad_output):
            >>>         return grad_output
            >>>
            >>> a = torch.tensor(1., requires_grad=True, dtype=torch.double).clone()
            >>> b = a * a
            >>> Inplace.apply(a)  # This would lead to wrong gradients!
            >>>                   # but the engine would not know unless we mark_dirty
            >>> b.backward() # RuntimeError: one of the variables needed for gradient
            >>>              # computation has been modified by an inplace operation

        N)dirty_tensorsr   r   r   r   r   
mark_dirtyo   s    !zFunctionCtx.mark_dirtyc                 G   s   t d d S )Nzmark_shared_storage is deprecated. Tensors with shared storages are automatically tracked. Note that calls to `set_()` are not tracked)warningswarn)r   pairsr   r   r   mark_shared_storage   s    zFunctionCtx.mark_shared_storagec                 G   s
   || _ dS )a  Marks outputs as non-differentiable.

        **This should be called at most once, only from inside the**
        :func:`forward` **method, and all arguments should be tensor outputs.**

        This will mark outputs as not requiring gradients, increasing the
        efficiency of backward computation. You still need to accept a gradient
        for each output in :meth:`~Function.backward`, but it's always going to
        be a zero tensor with the same shape as the shape of a corresponding
        output.

        This is used e.g. for indices returned from a sort. See example::
            >>> class Func(Function):
            >>>     @staticmethod
            >>>     def forward(ctx, x):
            >>>         sorted, idx = x.sort()
            >>>         ctx.mark_non_differentiable(idx)
            >>>         ctx.save_for_backward(x, idx)
            >>>         return sorted, idx
            >>>
            >>>     @staticmethod
            >>>     @once_differentiable
            >>>     def backward(ctx, g1, g2):  # still need to accept g2
            >>>         x, idx = ctx.saved_tensors
            >>>         grad_input = torch.zeros_like(x)
            >>>         grad_input.index_add_(0, idx, g1)
            >>>         return grad_input

        N)non_differentiabler   r   r   r   mark_non_differentiable   s    z#FunctionCtx.mark_non_differentiable)valuec                 C   s
   || _ dS )a  Sets whether to materialize output grad tensors. Default is ``True``.

        **This should be called only from inside the** :func:`forward` **method**

        If ``True``, undefined output grad tensors will be expanded to tensors full
        of zeros prior to calling the :func:`backward` method.

        Example::
            >>> class SimpleFunc(Function):
            >>>     @staticmethod
            >>>     def forward(ctx, x):
            >>>         return x.clone(), x.clone()
            >>>
            >>>     @staticmethod
            >>>     @once_differentiable
            >>>     def backward(ctx, g1, g2):
            >>>         return g1 + g2  # No check for None necessary
            >>>
            >>> # We modify SimpleFunc to handle non-materialized grad outputs
            >>> class Func(Function):
            >>>     @staticmethod
            >>>     def forward(ctx, x):
            >>>         ctx.set_materialize_grads(False)
            >>>         ctx.save_for_backward(x)
            >>>         return x.clone(), x.clone()
            >>>
            >>>     @staticmethod
            >>>     @once_differentiable
            >>>     def backward(ctx, g1, g2):
            >>>         x, = ctx.saved_tensors
            >>>         grad_input = torch.zeros_like(x)
            >>>         if g1 is not None:  # We must check for None now
            >>>             grad_input += g1
            >>>         if g2 is not None:
            >>>             grad_input += g2
            >>>         return grad_input
            >>>
            >>> a = torch.tensor(1., requires_grad=True)
            >>> b, _ = Func.apply(a)  # induces g2 to be undefined

        N)materialize_grads)r   r   r   r   r   set_materialize_grads   s    *z!FunctionCtx.set_materialize_gradsN)__name__
__module____qualname__r   r   r   r   r   r   r   boolr!   r   r   r   r   r      s   .3# r   c                   @   s   e Zd Zedd ZdS )
_HookMixinc                 C   s*   | d u rt  } t| }|| |j< | |fS N)r   hooksRemovableHandleid)backward_hookshookhandler   r   r   _register_hook   s
    

z_HookMixin._register_hookN)r"   r#   r$   staticmethodr.   r   r   r   r   r&      s   r&   c                   @   s   e Zd Zdd Zdd ZdS )BackwardCFunctionc                 G   sN   | j j}| j j}|tjur,|tjur,td|tjur:|n|}|| g|R  S )NzsImplementing both 'backward' and 'vjp' for a custom Function is not allowed. You should only implement one of them.)_forward_clsbackwardvjpFunctionRuntimeError)r   r   backward_fnZvjp_fnZuser_fnr   r   r   apply   s    zBackwardCFunction.applyc                 G   s   | j j| g|R  S r'   )r1   jvpr   r   r   r   	apply_jvp   s    zBackwardCFunction.apply_jvpN)r"   r#   r$   r7   r9   r   r   r   r   r0      s   r0   c                       s    e Zd ZdZ fddZ  ZS )FunctionMetaa   Function metaclass.

    This metaclass sets up the following properties:
        _backward_cls: The Function class corresponding to the differentiated
            version of this function (which is generated on the fly by this
            metaclass).
    c                    s4   t |d tfd| i}|| _tt| ||| d S )NZBackwardr1   )typer0   Z_backward_clssuperr:   __init__)clsnamebasesattrsr6   	__class__r   r   r=     s    zFunctionMeta.__init__)r"   r#   r$   __doc__r=   __classcell__r   r   rB   r   r:     s   r:   c                   @   sl   e Zd ZdZdd Zdd ZdZeeeeeddd	Z	eeeed
ddZ
e
ZeeeedddZdS )r4   aZ  Base class to create custom `autograd.Function`

    To create a custom `autograd.Function`, subclass this class and implement
    the :meth:`forward` and :meth:`backward` static methods. Then, to use your custom
    op in the forward pass, call the class method ``apply``. Do not call
    :meth:`forward` directly.

    To ensure correctness and best performance, make sure you are calling the
    correct methods on ``ctx`` and validating your backward function using
    :func:`torch.autograd.gradcheck`.

    See :ref:`extending-autograd` for more details on how to use this class.

    Examples::

        >>> class Exp(Function):
        >>>     @staticmethod
        >>>     def forward(ctx, i):
        >>>         result = i.exp()
        >>>         ctx.save_for_backward(result)
        >>>         return result
        >>>
        >>>     @staticmethod
        >>>     def backward(ctx, grad_output):
        >>>         result, = ctx.saved_tensors
        >>>         return grad_output * result
        >>>
        >>> # Use it by calling the apply method:
        >>> output = Exp.apply(input)
    c                 O   s   | j }t| dt d S )Nz should not be instantiated. Methods on autograd functionsare all static, so you should invoke them on the class itself. Instantiating an autograd function will raise an error in a future version of PyTorch.)rC   r   r   DeprecationWarning)r   r   kwargsr>   r   r   r   r=   3  s    zFunction.__init__c                 O   s   t dd S )NzLegacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method. (Example: https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function))r5   r   r   rG   r   r   r   __call__:  s    zFunction.__call__F)ctxr   rG   returnc                 O   s   t ddS )a  Performs the operation.

        This function is to be overridden by all subclasses.

        It must accept a context ctx as the first argument, followed by any
        number of arguments (tensors or other types).

        The context can be used to store arbitrary data that can be then
        retrieved during the backward pass. Tensors should not be stored
        directly on `ctx` (though this is not currently enforced for
        backward compatibility). Instead, tensors should be saved either with
        :func:`ctx.save_for_backward` if they are intended to be used in
        ``backward`` (equivalently, ``vjp``) or :func:`ctx.save_for_forward`
        if they are intended to be used for in ``jvp``.
        zEYou must implement the forward function for custom autograd.Function.NNotImplementedError)rJ   r   rG   r   r   r   forwardC  s    zFunction.forward)rJ   grad_outputsrK   c                 G   s   t ddS )a  Defines a formula for differentiating the operation with backward mode
        automatic differentiation (alias to the vjp function).

        This function is to be overridden by all subclasses.

        It must accept a context :attr:`ctx` as the first argument, followed by
        as many outputs as the :func:`forward` returned (None will be passed in
        for non tensor outputs of the forward function),
        and it should return as many tensors, as there were inputs to
        :func:`forward`. Each argument is the gradient w.r.t the given output,
        and each returned value should be the gradient w.r.t. the
        corresponding input. If an input is not a Tensor or is a Tensor not
        requiring grads, you can just pass None as a gradient for that input.

        The context can be used to retrieve tensors saved during the forward
        pass. It also has an attribute :attr:`ctx.needs_input_grad` as a tuple
        of booleans representing whether each input needs gradient. E.g.,
        :func:`backward` will have ``ctx.needs_input_grad[0] = True`` if the
        first input to :func:`forward` needs gradient computated w.r.t. the
        output.
        zwYou must implement either the backward or vjp method for your custom autograd.Function to use it with backward mode AD.NrL   )rJ   rO   r   r   r   r2   W  s    zFunction.backward)rJ   grad_inputsrK   c                 G   s   t ddS )a  Defines a formula for differentiating the operation with forward mode
        automatic differentiation.
        This function is to be overridden by all subclasses.
        It must accept a context :attr:`ctx` as the first argument, followed by
        as many inputs as the :func:`forward` got (None will be passed in
        for non tensor inputs of the forward function),
        and it should return as many tensors as there were outputs to
        :func:`forward`. Each argument is the gradient w.r.t the given input,
        and each returned value should be the gradient w.r.t. the
        corresponding output. If an output is not a Tensor or the function is not
        differentiable with respect to that output, you can just pass None as a
        gradient for that input.

        You can use the :attr:`ctx` object to pass any value from the forward to this
        functions.
        z`You must implement the jvp function for custom autograd.Function to use it with forward mode AD.NrL   )rJ   rP   r   r   r   r8   u  s    zFunction.jvpN)r"   r#   r$   rD   r=   rI   is_traceabler/   r   rN   r2   r3   r8   r   r   r   r   r4     s   r4   c                    s   t   fdd}|S )Nc                    s   t    | g|R  }W d    n1 s.0    Y  t  sD|S tdd |D }|s^|S t|tsn|f}tdt|}dd  | fdd|D  S )Nc                 s   s    | ]}t |tjo|jV  qd S r'   )r   r   r   requires_grad).0argr   r   r   	<genexpr>  s   z7once_differentiable.<locals>.wrapper.<locals>.<genexpr>sR   trying to differentiate twice a function that was marked with @once_differentiablec                 S   s   | d ur|   } d| _| S )NT)detachrR   )varr   r   r   fake_requires_grad  s    z@once_differentiable.<locals>.wrapper.<locals>.fake_requires_gradc                    s   g | ]} |qS r   r   )rS   vrX   r   r   
<listcomp>      z8once_differentiable.<locals>.wrapper.<locals>.<listcomp>)	r   no_gradis_grad_enabledanyr   tupler   DelayedErrorlen)rJ   r   outputsrR   Zerr_fnfnrZ   r   wrapper  s"    
.
z$once_differentiable.<locals>.wrapper)	functoolswraps)re   rf   r   rd   r   once_differentiable  s    &ri   c                 C   s
   d| _ | S )a  Marks Function as traceable for the JIT.

    Traceable functions have additional restrictions - they can't pass any
    data-dependent values to backward (e.g. Prod passes the output, which makes
    it non-traceable), and their backward should be implemented entirely in terms
    of operations on autograd Tensors in all cases.

    DON'T USE THIS DECORATOR. IT IS FOR INTERNAL USE ONLY AND SHOULD BE HANDLED WITH
    CARE (or can give incorrect results otherwise).
    T)rQ   )Zfn_clsr   r   r   	traceable  s    rj   c                       s   e Zd Zd fdd	Z  ZS )InplaceFunctionFc                    s   t t|   || _d S r'   )r<   rk   r=   inplace)r   rl   rB   r   r   r=     s    zInplaceFunction.__init__)F)r"   r#   r$   r=   rE   r   r   rB   r   rk     s   rk   c                    s    fdd  S )Nc                    s    r S  d u rd S t  ttfr^fdd D }t drRt | S t |S t  tr| fdd D S tdt  rd d nd	 d S )
Nc                 3   s   | ]} |V  qd S r'   r   rS   x)_mapr   r   rU     r\   z,_nested_map.<locals>._map.<locals>.<genexpr>_fieldsc                    s   i | ]}| | qS r   r   rm   )ro   objr   r   
<dictcomp>  r\   z-_nested_map.<locals>._map.<locals>.<dictcomp>AAuto nesting doesn't know how to process an input object of type . Accepted types: , or lists/tuples of them )	r   listr`   hasattrr;   dict
ValueErrorr   typename)rq   Zmappedro   	conditioncondition_msgre   rq   r   ro     s(    

z_nested_map.<locals>._mapr   )r}   re   r~   r   r|   r   _nested_map  s    r   c                 C   s   t | dr|  S | S )N_jit_unwrap)rx   r   r   r   r   r   _jit_unwrap_structured  s    
r   Fc                    s    fdd  S )Nc                 3   s   d ur| } | r | V  n| d u r,d S t | ttfrZ| D ]} |D ]
}|V  qJq>n`t | tr|  D ]} |D ]
}|V  qxqln2r| V  n&tdt|  rd d nd d S )Nrs   rt   ru   rv   )r   rw   r`   ry   valuesrz   r   r{   )rq   orW   _iterallow_unknownr}   r~   
conversionr   r   r     s2    
z_iter_filter.<locals>._iterr   )r}   r   r~   r   r   r   r   _iter_filter  s    r   c                    s    fdd  | |d S )Nc                    s   g }t |dr|| S t|ttfs:| d | dd  fS |D ]0}|d u rV|| q> | |\}} || q>t||| fS )N	_jit_wrapr      )rx   r   r   rw   r`   appendr;   )inputprotoreseZres_eunflatten_helperr   r   r     s    

z$_unflatten.<locals>.unflatten_helperr   r   )r   r   r   r   r   
_unflatten  s    r   c                 C   s   | d u pt | tjjS r'   )r   r   _CValuer   r   r   r   <lambda>  r\   r   zjit's Values or None)r~   c                 C   s   t | tjS r'   r   r   r   rn   r   r   r   r     r\   ZTensors)r~   r   c                 C   s   t | tjS r'   r   r   r   r   r   r      r\   TzTensors (permissive))r   r~   c                 C   s   | d u pt | tjS r'   r   r   r   r   r   r   #  r\   zTensors or Nonec                 C   s   t | tjS r'   r   r   r   r   r   r   %  r\   c                 C   s   | j S r'   )datar   r   r   r   r   %  r\   c                       s   e Zd Z fddZ fddZeedddZeZeedd	d
ZeddddZ	e
 fddZeeddddZeeddddZeddddZeddddZ  ZS )NestedIOFunctionc                    s8   || _ tt|}tt| j| }| j}t|| j}|S r'   )_nested_inputr`   _iter_tensorsr<   r   _do_forward_nested_outputr   )r   r   Z
flat_inputZflat_outputZnested_outputnested_tensorsrB   r   r   r   -  s    zNestedIOFunction._do_forwardc                    s(   || _ tt| ||}|s$| `| `|S r'   )retain_variablesr<   r   _do_backwardr   _to_save_nested)r   	gradientsr   resultrB   r   r   r   5  s    zNestedIOFunction._do_backward)r   rK   c                 G   s"   t || j}| j| }tt|S r'   )r   r   backward_extendedr`   _iter_None_tensors)r   r   Znested_gradientsr   r   r   r   r2   =  s    
zNestedIOFunction.backward)r   rK   c                 G   s*   t | j}| j| }| `|| _tt|S r'   )_map_tensor_datar   forward_extendedr   r`   r   )r   r   r   r   r   r   r   rN   D  s
    

zNestedIOFunction.forwardNc                 G   s   t t|| _|| _d S r'   )r`   r   r
   r   r   r   r   r   r   K  s    z"NestedIOFunction.save_for_backwardc                    s   t t| j}t|| jS r'   )r<   r   saved_tensorsr   r   )r   Zflat_tensorsrB   r   r   r   O  s    zNestedIOFunction.saved_tensors)r   rG   rK   c                 O   s   t t||f| _d S r'   )r`   r   r   rH   r   r   r   r   T  s    zNestedIOFunction.mark_dirtyc                 O   s   t t||f| _d S r'   )r`   r   r   rH   r   r   r   r   W  s    z(NestedIOFunction.mark_non_differentiable)r   rK   c                 G   s   t d S r'   rL   )r   r   r   r   r   r   Z  s    z!NestedIOFunction.forward_extended)grad_outputrK   c                 G   s   t d S r'   rL   )r   r   r   r   r   r   ]  s    z"NestedIOFunction.backward_extended)r"   r#   r$   r   r   r   r2   rI   rN   r   propertyr   r   r   r   r   rE   r   r   rB   r   r   )  s   r   )N)FNN)'r   torch._Cr   r   torch.utils.hooksutilsr(   Z
torch._sixr   rg   r   collectionsr   typingr   r   r   objectr   Z_ContextMethodMixinr&   _FunctionBaser0   r;   r:   r4   ri   rj   rk   r   r   r   r   Z_iter_jit_valuesr   Z_iter_tensors_permissiver   r   r   r   r   r   r   <module>   sR    Zv,
  

