o
    yi                     @   s|   d dl 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d
ed defddZdded
ed defddZ	dS )    )Tuple)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 )	z5Computes 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   T/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/image/tv.py_total_variation_update   s   
,,r   r   num_elements	reduction)meanr   noneNc                 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 )aU  Computes 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:
        >>> import torch
        >>> from torchmetrics.functional import total_variation
        >>> _ = torch.manual_seed(42)
        >>> img = torch.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   torchr   typing_extensionsr   intr   r   r   r   r   r   r   <module>   s   
 