§
    ’©ºj¸  ã                   óª   — d dl mZ d dlZd dlmZ dededdfd„Zddededee         deeef         fd	„Zd
ededefd„Zddededee         defd„Z	dS )é    )ÚOptionalN)ÚTensorÚpredsÚtargetÚreturnc                 óF  — t          | j        ¦  «        dk    r%t          dt          | j        ¦  «        › d�¦  «        ‚t          |j        ¦  «        dk    r%t          dt          |j        ¦  «        › d�¦  «        ‚| j        dd…         |j        k    r(t          d| j        dd…         › d|j        › d�¦  «        ‚|                      ¦   «         st	          d	| j        › d�¦  «        ‚|j        t          j        k    r%t	          d
t          j        › d|j        › d�¦  «        ‚dS )a3  Check shape and type consistency of input vectors.

    Args:
        preds:
            Logits or a unnormalized score assigned to each token in a sequence with shape [batch_size, seq_len,
            vocab_size]. Scores will be normalized internally using softmax.
        target:
            Ground truth values with a shape [batch_size, seq_len].

    Raises:
        ValueError:
            If ``preds`` tensor has no 3 dimensions.
        ValueError:
            If ``target`` tensor has no 2 dimensions.
        ValueError:
            If the first two dimensions of ``preds`` and ``target`` do not equal.
        TypeError:
            If ``preds`` dtype is not one of ``(torch.float16, torch.float32, torch.float64)``
        TypeError:
            If ``target`` is not of a type LongTensor (torch.int64)

    é   zbInput tensor `preds` is expected to have 3 dimensions, [batch_size, seq_len, vocab_size], but got ú.é   zWInput tensor `target` is expected to have 2 dimensions, [batch_size, seq_len], but got NzvInput tensors `preds` and `target` are expected to have equaling first two dimensions, [batch_size, seq_len], but got z and zFInput tensor `preds` is expected to be of floating point type but got z2Input tensor `target` is expected to be of a type z	 but got )ÚlenÚshapeÚ
ValueErrorÚis_floating_pointÚ	TypeErrorÚdtypeÚtorchÚint64)r   r   s     úw/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/text/perplexity.pyÚ!_check_shape_and_type_consistencyr      sk  € õ. ˆ5Œ;ÑÔ˜1ÒÐÝð,Ý˜EœKÑ(Ô(ð,ð ,ð ,ñ
ô 
ð 	
õ ˆ6Œ<ÑÔ˜AÒÐÝð-Ý˜FœLÑ)Ô)ð-ð -ð -ñ
ô 
ð 	
ð „{�2�A�2„˜&œ,Ò&Ð&ÝðUØ/4¬{¸2¸A¸2¬ðUð UØEKÄ\ðUð Uð Uñ
ô 
ð 	
ð ×"Ò"Ñ$Ô$ð qÝÐoÐafÔalÐoÐoÐoÑpÔpÐpØ„|•u”{Ò"Ð"ÝÐrÍUÌ[ÐrÐrÐciÔcoÐrÐrÐrÑsÔsÐsð #Ð"ó    Úignore_indexc                 ó’  — t          | |¦  «         t          j        j                             |                      d| j        d         ¦  «        d¬¦  «        }|                     d¦  «        }|�I|                     |¦  «        }|                     ||k    t          j	        d|j
        ¬¦  «        ¦  «        }n t          j        |t          j        ¬¦  «        }|t          j        |                     ¦   «         ¦  «        |f         |         }|                     ¦   «                              ¦   «          }|                     ¦   «         }||fS )a]  Compute intermediate statistics for Perplexity.

    Args:
        preds:
            Logits or a unnormalized score assigned to each token in a sequence with shape [batch_size, seq_len,
            vocab_size]. Scores will be normalized internally using softmax.
        target:
            Ground truth values with a shape [batch_size, seq_len].
        ignore_index:
            Integer specifying a target class to ignore. If given, this class index does not contribute
            to the returned score.

    Returns:
        Log probabilities, summed over all samples
        Number of samples

    éÿÿÿÿé   )ÚdimNr   )Údevice)r   )r   r   ÚnnÚ
functionalÚsoftmaxÚreshaper   ÚneÚwhereÚtensorr   Ú	ones_likeÚboolÚarangeÚnumelÚlogÚsum)r   r   r   ÚprobsÚmaskÚtotal_log_probsÚcounts          r   Ú_perplexity_updater.   A   s  € õ$ & e¨VÑ4Ô4Ð4åŒHÔ×'Ò'¨¯ª°b¸%¼+Àb¼/Ñ(JÔ(JÐPQÐ'ÑRÔR€EØ�^Š^˜BÑÔ€FàÐØ�yŠy˜Ñ&Ô&ˆØ—’˜f¨Ò4µe´lÀ1ÈVÌ]Ð6[Ñ6[Ô6[Ñ\Ô\ˆˆåŒ˜v­U¬ZÐ8Ñ8Ô8ˆà•%”,˜vŸ|š|™~œ~Ñ.Ô.°Ð6Ô7¸Ô=€EØ—y’y‘{”{—’Ñ(Ô(Ð(€OØ�HŠH‰JŒJ€Eà˜EÐ!Ð!r   Útotalr-   c                 ó0   — t          j        | |z  ¦  «        S )z£Compute the Perplexity.

    Args:
        total: Log probabilities, summed over all samples
        count: Number of samples
    Returns:
        Perplexity

    )r   Úexp)r/   r-   s     r   Ú_perplexity_computer2   e   s   € õ Œ9�U˜U‘]Ñ#Ô#Ð#r   c                 óJ   — t          | ||¦  «        \  }}t          ||¦  «        S )aË  Perplexity measures how well a language model predicts a text sample.

    This metric is calculated as the average number of bits per word a model needs to represent the sample.

    Args:
        preds:
            Logits or a unnormalized score assigned to each token in a sequence with shape [batch_size, seq_len,
            vocab_size], which is the output of a language model. Scores will be normalized internally using softmax.
        target:
            Ground truth values with a shape [batch_size, seq_len].
        ignore_index:
            Integer specifying a target class to ignore. If given, this class index does not contribute
            to the returned score.

    Returns:
        Perplexity value

    Examples:
        >>> from torch import rand, randint
        >>> preds = rand(2, 8, 5)
        >>> target = randint(5, (2, 8))
        >>> target[0, 6:] = -100
        >>> perplexity(preds, target, ignore_index=-100)
        tensor(5.8540)

    )r.   r2   )r   r   r   r/   r-   s        r   Ú
perplexityr4   r   s)   € õ6 & e¨V°\ÑBÔB�L€Eˆ5Ý˜u eÑ,Ô,Ð,r   )N)
Útypingr   r   r   r   ÚintÚtupler.   r2   r4   © r   r   ú<module>r9      s  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð ð)t¨Vð )t¸Vð )tÈð )tð )tð )tð )tðX!"ð !"˜fð !"¨fð !"ÀHÈSÄMð !"Ð]bÐciÐkqÐcqÔ]rð !"ð !"ð !"ð !"ðH
$˜vð 
$¨fð 
$¸ð 
$ð 
$ð 
$ð 
$ð-ð -�fð - fð -¸HÀS¼Mð -ÐU[ð -ð -ð -ð -ð -ð -r   