o
    .wi
                     @   s   d dl mZmZ d dlmZ d dlmZ dedeeef fddZ	ded	eeef d
eed  defddZ
dded
eed  defddZdS )    )OptionalUnion)Tensor)Literalimgreturnc                 C   s   | j dkrtd| j | dddddf | dddddf  }| dddddf | dddddf  }| g d}| g d}|| }|| jd fS )	z4Compute total variation statistics on current batch.   z1Expected input `img` to be an 4D tensor, but got .   N)r	         r   )ndimRuntimeErrorshapeabssum)r   diff1diff2res1res2score r   ]/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/image/tv.py_total_variation_update   s   
,,r   r   num_elements	reduction)meanr   nonec                 C   s@   |dkr
|   | S |dkr|   S |du s|dkr| S td)z$Compute final total variation score.r   r   Nr   zHExpected argument `reduction` to either be 'sum', 'mean', 'none' or None)r   
ValueError)r   r   r   r   r   r   _total_variation_compute!   s   r   r   c                 C   s   t | \}}t|||S )a9  Compute total variation loss.

    Args:
        img: A `Tensor` of shape `(N, C, H, W)` consisting of images
        reduction: a method to reduce metric score over samples.

            - ``'mean'``: takes the mean over samples
            - ``'sum'``: takes the sum over samples
            - ``None`` or ``'none'``: return the score per sample

    Returns:
        A loss scalar value containing the total variation

    Raises:
        ValueError:
            If ``reduction`` is not one of ``'sum'``, ``'mean'``, ``'none'`` or ``None``
        RuntimeError:
            If ``img`` is not 4D tensor

    Example:
        >>> from torch import rand
        >>> from torchmetrics.functional.image import total_variation
        >>> img = rand(5, 3, 28, 28)
        >>> total_variation(img)
        tensor(7546.8018)

    )r   r   )r   r   r   r   r   r   r   total_variation.   s   r    N)r   )typingr   r   torchr   typing_extensionsr   tupleintr   r   r    r   r   r   r   <module>   s   


$