o
    yiG                     @   s   d dl mZmZ d dlZd dlm  mZ d dlmZ ej	ej
f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 )    )OptionalTupleN)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| jtvrPtd	t d
| j d|jtjkrctdtj d
|j ddS )a  Check shape and type consistency of input vectors.

    Args:
        preds:
            Probabilities assigned to each token in a sequence with shape [batch_size, seq_len, vocab_size].
        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 z8Input tensor `preds` is expected to be of a type one of z	 but got z2Input tensor `target` is expected to be of a type )lenshape
ValueErrordtype_TORCH_FLOAT_OR_DOUBLE	TypeErrortorchint64)r   r    r   [/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/text/perplexity.py!_check_shape_and_type_consistency   s6   
r   ignore_indexc                 C   s   t | | tj| d| jd dd}|d}|dur0||}|||ktjd|j	d}ntj
|tjd}|dd|f  | }|   }| }||fS )a  Compute intermediate statistics for Perplexity.

    Args:
        preds:
            Probabilities assigned to each token in a sequence with shape [batch_size, seq_len, vocab_size].
        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   Fsoftmaxreshaper   newherer   tensorr   	ones_likebooldiagonallogsum)r   r   r   probsmasktotal_log_probscountr   r   r   _perplexity_updateD   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_computef   s   	r-   c                 C   s   t | ||\}}t||S )a  Perplexity measures how well a language model predicts a text sample. It's calculated as the average number
    of bits per word a model needs to represent the sample.

    Args:
        preds:
            Log probabilities assigned to each token in a sequence with shape [batch_size, seq_len, vocab_size].
        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:
        >>> import torch
        >>> preds = torch.rand(2, 8, 5, generator=torch.manual_seed(22))
        >>> target = torch.randint(5, (2, 8), generator=torch.manual_seed(22))
        >>> target[0, 6:] = -100
        >>> perplexity(preds, target, ignore_index=-100)
        tensor(5.2545)
    )r*   r-   )r   r   r   r+   r)   r   r   r   
perplexityr   s   
r.   )N)typingr   r   r   torch.nn.functionalnn
functionalr   r   float32float64r   r   intr*   r-   r.   r   r   r   r   <module>   s   (,"$