
    i
                         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Z	dedeeef   d	eed
      defdZ
dded	eed
      defdZy)    )OptionalUnion)Tensor)Literalimgreturnc                 d   | j                   dk7  rt        d| j                         | dddddf   | dddddf   z
  }| dddddf   | dddddf   z
  }|j                         j	                  g d      }|j                         j	                  g d      }||z   }|| 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scores         u/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/image/tv.py_total_variation_updater      s    
xx1}NsyykZ[[QR
Oc#ssA+..EQ
Oc#q#2#+..E99;??9%D99;??9%D4KE#))A,    r   num_elements	reduction)meanr   nonec                     |dk(  r| j                         |z  S |dk(  r| j                         S ||dk(  r| S t        d      )z$Compute final total variation score.r   r   r   zHExpected argument `reduction` to either be 'sum', 'mean', 'none' or None)r   
ValueError)r   r   r   s      r   _total_variation_computer"   !   sO     Fyy{\))Eyy{I/
_
``r   c                 8    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   s       r   total_variationr$   .   s"    < 2#6E<#E<CCr   N)r   )typingr   r   torchr   typing_extensionsr   tupleintr   r"   r$    r   r   <module>r+      s    #  %
 
E&#+,> 

a
a!&sF{!3
a@HQfIg@h
a
aD DHW=R5S,T Dag Dr   