o
    oi                     @   sb   d dl mZmZmZmZ d dlmZmZ d dlm	Z	 ddgZ
G dd de	ZG dd dejZdS )	    )AnyCallableOptionalTuple)Tensornn)FunctionSTEFunctionStraightThroughEstimatorc                   @   sd   e Zd ZdZeddedededeedef  def
d	d
Z	ededede
eedf fddZdS )r	   a  Straight-Through Estimation (STE) function.

    STE bridges the gradients between the input tensor and output tensor as if the function
    was an identity function. Meanwhile, advanced gradient functions are also supported. e.g.
    the output gradients can be mapped into [-1, 1] with ``F.hardtanh`` function.

    Args:
        grad_fn: function to restrain the gradient received. If None, no mapping will performed.

    Example:
        Let the gradients of ``torch.sign`` estimated from STE.
        >>> input = torch.randn(4, requires_grad = True)
        >>> output = torch.sign(input)
        >>> loss = output.mean()
        >>> loss.backward()
        >>> input.grad
        tensor([0., 0., 0., 0.])

        >>> with torch.no_grad():
        ...     output = torch.sign(input)
        >>> out_est = STEFunction.apply(input, output)
        >>> loss = out_est.mean()
        >>> loss.backward()
        >>> input.grad
        tensor([0.2500, 0.2500, 0.2500, 0.2500])

    Nctxinputoutputgrad_fn.returnc                 C   s   |j | _|j | _|| _|S N)shapein_shape	out_shaper   )r   r   r   r    r   M/home/ubuntu/.local/lib/python3.10/site-packages/kornia/grad_estimator/ste.pyforward7   s   zSTEFunction.forwardgrad_outputc                 C   sJ   | j d u r|| j|| jd fS |  || j|  || jd fS r   )r   sum_to_sizer   r   )r   r   r   r   r   backward>   s   
zSTEFunction.backwardr   )__name__
__module____qualname____doc__staticmethodr   r   r   r   r   r   r   r   r   r   r   r	      s    .&c                       s^   e Zd ZdZddejdeedef  ddf fddZ	de
fd	d
ZdedefddZ  ZS )r
   a  Straight-Through Estimation (STE) module.

    STE wraps the ``STEFunction`` to aid the back propagation of non-differentiable modules.
    It may also use to avoid gradient computation for differentiable operations. By default,
    STE bridges the gradients between the input tensor and output tensor as if the function
    was an identity function. Meanwhile, advanced gradient functions are also supported. e.g.
    the output gradients can be mapped into [-1, 1] with ``F.hardtanh`` function.

    Args:
        target_fn: the target function to wrap with.
        grad_fn: function to restrain the gradient received. If None, no mapping will performed.

    Example:
        ``RandomPosterize`` is a non-differentiable operation. Let STE estimate the gradients.
        >>> import kornia.augmentation as K
        >>> import torch.nn.functional as F
        >>> input = torch.randn(1, 1, 4, 4, requires_grad = True)
        >>> estimator = StraightThroughEstimator(K.RandomPosterize(3, p=1.), grad_fn=F.hardtanh)
        >>> out = estimator(input)
        >>> out.mean().backward()
        >>> input.grad
        tensor([[[[0.0625, 0.0625, 0.0625, 0.0625],
                  [0.0625, 0.0625, 0.0625, 0.0625],
                  [0.0625, 0.0625, 0.0625, 0.0625],
                  [0.0625, 0.0625, 0.0625, 0.0625]]]])

        This can be used to chain up the gradients within a ``Sequential`` block.
        >>> import kornia.augmentation as K
        >>> input = torch.randn(1, 1, 4, 4, requires_grad = True)
        >>> aug = K.ImageSequential(
        ...     K.RandomAffine((77, 77)),
        ...     StraightThroughEstimator(K.RandomPosterize(3, p=1.), grad_fn=None),
        ...     K.RandomRotation((15, 15)),
        ... )
        >>> aug(input).mean().backward()
        >>> input.grad
        tensor([[[[0.0422, 0.0626, 0.0566, 0.0422],
                  [0.0566, 0.0626, 0.0626, 0.0626],
                  [0.0626, 0.0626, 0.0626, 0.0566],
                  [0.0422, 0.0566, 0.0626, 0.0422]]]])

    N	target_fnr   .r   c                    s   t    || _|| _d S r   )super__init__r   r   )selfr   r   	__class__r   r   r!   |   s   

z!StraightThroughEstimator.__init__c                 C   s   | j j d| j d| j dS )Nz(target_fn=z
, grad_fn=))r$   r   r   r   )r"   r   r   r   __repr__   s   z!StraightThroughEstimator.__repr__r   c                 C   s0   |  |}t|tstdt||| j}|S )NzdOnly Tensor is supported at the moment. Feel free to contribute to https://github.com/kornia/kornia.)r   
isinstancer   NotImplementedErrorr	   applyr   )r"   r   outr   r   r   r   r      s   

z StraightThroughEstimator.forwardr   )r   r   r   r   r   Moduler   r   r   r!   strr&   r   r   __classcell__r   r   r#   r   r
   P   s
    *+N)typingr   r   r   r   torchr   r   torch.autogradr   __all__r	   r+   r
   r   r   r   r   <module>   s   6