a
    d                     @   s   d dl mZmZmZ d dlmZ d dlmZ eeeef dddZ	eeeef eed  edd	d
Z
deeed  edddZdS )    )OptionalTuple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   Zdiff1Zdiff2Zres1Zres2score r   i/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/image/tv.py_total_variation_update   s    
,,r   )meanr   none)r   num_elements	reductionr   c                 C   sD   |dkr|   | S |dkr$|   S |du s4|dkr8| S tddS )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   )r   r   r   c                 C   s   t | \}}t|||S )a[  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:
        >>> import torch
        >>> from torchmetrics.functional.image 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   r   r   Ztorchr   Ztyping_extensionsr   intr   r   r   r   r   r   r   <module>   s   