o
    .wi                     @   s   d dl mZ d dlZd dlmZ dededdfddZddeded	ee deeef fd
dZdededefddZddeded	ee defddZ	dS )    )OptionalN)Tensorpredstargetreturnc                 C   s   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|  sLt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    r   d/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/text/perplexity.py!_check_shape_and_type_consistency   s2   r   ignore_indexc                 C   s   t | | tjjj| d| jd dd}|d}|dur2||}|||ktj	d|j
d}ntj|tjd}|t| |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countr   r   r   _perplexity_updateA   s   
 

r+   totalr*   c                 C   s   t | | S )zCompute the Perplexity.

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

    )r   exp)r,   r*   r   r   r   _perplexity_computee   s   
r.   c                 C   s   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+   r.   )r   r   r   r,   r*   r   r   r   
perplexityr   s   
r/   )N)
typingr   r   r   r   inttupler+   r.   r/   r   r   r   r   <module>   s   (,$$