a
    d}                     @   s   d dl mZmZ d dlZd dlmZ d dlmZ ejej	fZ
eeddddZdeeee eeef dd	d
ZeeedddZdeeee edddZdS )    )OptionalTupleN)Tensor)
functional)predstargetreturnc                 C   s   t | jdkr$tdt | j dt |jdkrHtdt |j d| jdd |jkr~td| jdd  d|j d| jtvrtd	t d
| j d|jtjkrt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 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   p/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/text/perplexity.py!_check_shape_and_type_consistency   s4    
r   )r   r   ignore_indexr   c                 C   s   t | | tj| d| jd dd}|d}|dur`||}|||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:
            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

       )ZdimNr   )device)r   )r   FZsoftmaxZreshaper   newherer   Ztensorr   Z	ones_likeboolZdiagonallogsum)r   r   r   ZprobsmaskZtotal_log_probscountr   r   r   _perplexity_updateF   s    


r#   )totalr"   r   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_computej   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:
        >>> import torch
        >>> gen = torch.manual_seed(42)
        >>> preds = torch.rand(2, 8, 5, generator=gen)
        >>> target = torch.randint(5, (2, 8), generator=gen)
        >>> target[0, 6:] = -100
        >>> perplexity(preds, target, ignore_index=-100)
        tensor(5.8540)

    )r#   r&   )r   r   r   r$   r"   r   r   r   
perplexityw   s    r'   )N)N)typingr   r   r   r   Ztorch.nnr   r   float32float64r   r   intr#   r&   r'   r   r   r   r   <module>   s   ."$