a
    d 
                     @   s   d Z ddlZddlm  mZ ejejejdddZejejejdddZeje	ejdd	d
Z
dejeje	dddZdejeje	dddZdS )zALosses based on the divergence between probability distributions.    N)pqreturnc                 C   sX   | j \}}}}tj||| ||  | || || dd}|d||}|S )Nnone)	reduction)shapeFZkl_divZreshapelogsumview)r   r   batchZchansheightwidthZunsummed_klZ	kl_values r   a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/losses/divergence.py
_kl_div_2d   s    *r   c                 C   s(   d| |  }dt | | dt ||  S )Ng      ?)r   )r   r   mr   r   r   
_js_div_2d   s    r   )lossesr   r   c                 C   s(   |dkr| S |dkrt | S t | S )Nr   mean)torchr   r   )r   r   r   r   r   _reduce_loss   s    r   r   inputtargetr   c                 C   s   t t|| |S )a  Calculate the Jensen-Shannon divergence loss between heatmaps.

    Args:
        input: the input tensor with shape :math:`(B, N, H, W)`.
        target: the target tensor with shape :math:`(B, N, H, W)`.
        reduction: Specifies the reduction to apply to the
          output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction
          will be applied, ``'mean'``: the sum of the output will be divided by
          the number of elements in the output, ``'sum'``: the output will be
          summed.

    Examples:
        >>> input = torch.full((1, 1, 2, 4), 0.125)
        >>> loss = js_div_loss_2d(input, input)
        >>> loss.item()
        0.0
    )r   r   r   r   r   r   js_div_loss_2d    s    r   c                 C   s   t t|| |S )a  Calculate the Kullback-Leibler divergence loss between heatmaps.

    Args:
        input: the input tensor with shape :math:`(B, N, H, W)`.
        target: the target tensor with shape :math:`(B, N, H, W)`.
        reduction: Specifies the reduction to apply to the
          output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction
          will be applied, ``'mean'``: the sum of the output will be divided by
          the number of elements in the output, ``'sum'``: the output will be
          summed.

    Examples:
        >>> input = torch.full((1, 1, 2, 4), 0.125)
        >>> loss = kl_div_loss_2d(input, input)
        >>> loss.item()
        0.0
    )r   r   r   r   r   r   kl_div_loss_2d5   s    r   )r   )r   )__doc__r   Ztorch.nn.functionalnnZ
functionalr	   ZTensorr   r   strr   r   r   r   r   r   r   <module>   s   
	