a
    d6                     @   sD  U d dl mZmZmZmZmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZmZ d dlmZmZ esdgZdZ e!e"d< erd dl#m$Z$m%Z% ddddZ&eree&sddgZnddgZee	 ee	 ee!e	f dddZ'G dd deZ(dS )    )AnyCallableDictListOptionalSequenceUnionN)Tensor)Module)
bert_score)_preprocess_text)Metric)rank_zero_warn)_SKIP_SLOW_DOCTEST_try_proceed_with_timeout)dim_zero_cat)_MATPLOTLIB_AVAILABLE_TRANSFORMERS_GREATER_EQUAL_4_4)_AX_TYPE_PLOT_OUT_TYPEBERTScore.plotzroberta-large_DEFAULT_MODEL)	AutoModelAutoTokenizerreturnc                   C   s   t t tt dS )zDownload intensive operations.N)r   from_pretrainedr   r    r   r   _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/text/bert.py_download_model&   s    
r   	BERTScore)	input_idsattention_maskr   c                 C   s   t | t |dS )z]Create an input dictionary of ``input_ids`` and ``attention_mask`` for BERTScore calculation.r!   r"   )torchcatr#   r   r   r   _get_input_dict1   s    r&   c                       sn  e Zd ZU dZdZeed< dZeed< dZeed< dZ	e
ed< d	Ze
ed
< ee ed< ee ed< ee ed< ee ed< d ee ee eee ee eeeeeef gef  eeeeeejf  eeeeeeee ee edd fddZeeee f eeee f ddddZeeeeee
 ef f dddZd!eeeee f  ee edddZ  ZS )"r    a  `Bert_score Evaluating Text Generation`_ for measuring text similarity.

    BERT leverages the pre-trained contextual embeddings from BERT and matches words in candidate and reference
    sentences by cosine similarity. It has been shown to correlate with human judgment on sentence-level and
    system-level evaluation. Moreover, BERTScore computes precision, recall, and F1 measure, which can be useful for
    evaluating different language generation tasks. This implemenation follows the original implementation from
    `BERT_score`_.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~List`): An iterable of predicted sentences
    - ``target`` (:class:`~List`): An iterable of reference sentences

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``score`` (:class:`~Dict`): A dictionary containing the keys ``precision``, ``recall`` and ``f1`` with
      corresponding values

    Args:
        preds: An iterable of predicted sentences.
        target: An iterable of target sentences.
        model_type: A name or a model path used to load ``transformers`` pretrained model.
        num_layers: A layer of representation to use.
        all_layers:
            An indication of whether the representation from all model's layers should be used.
            If ``all_layers=True``, the argument ``num_layers`` is ignored.
        model:  A user's own model. Must be of `torch.nn.Module` instance.
        user_tokenizer:
            A user's own tokenizer used with the own model. This must be an instance with the ``__call__`` method.
            This method must take an iterable of sentences (`List[str]`) and must return a python dictionary
            containing `"input_ids"` and `"attention_mask"` represented by :class:`~torch.Tensor`.
            It is up to the user's model of whether `"input_ids"` is a :class:`~torch.Tensor` of input ids or embedding
            vectors. This tokenizer must prepend an equivalent of ``[CLS]`` token and append an equivalent of ``[SEP]``
            token as ``transformers`` tokenizer does.
        user_forward_fn:
            A user's own forward function used in a combination with ``user_model``. This function must take
            ``user_model`` and a python dictionary of containing ``"input_ids"`` and ``"attention_mask"`` represented
            by :class:`~torch.Tensor` as an input and return the model's output represented by the single
            :class:`~torch.Tensor`.
        verbose: An indication of whether a progress bar to be displayed during the embeddings' calculation.
        idf: An indication whether normalization using inverse document frequencies should be used.
        device: A device to be used for calculation.
        max_length: A maximum length of input sequences. Sequences longer than ``max_length`` are to be trimmed.
        batch_size: A batch size used for model processing.
        num_threads: A number of threads to use for a dataloader.
        return_hash: An indication of whether the correspodning ``hash_code`` should be returned.
        lang: A language of input sentences.
        rescale_with_baseline:
            An indication of whether bertscore should be rescaled with a pre-computed baseline.
            When a pretrained model from ``transformers`` model is used, the corresponding baseline is downloaded
            from the original ``bert-score`` package from `BERT_score`_ if available.
            In other cases, please specify a path to the baseline csv/tsv file, which must follow the formatting
            of the files from `BERT_score`_.
        baseline_path: A path to the user's own local csv/tsv file with the baseline scale.
        baseline_url: A url path to the user's own  csv/tsv file with the baseline scale.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from pprint import pprint
        >>> from torchmetrics.text.bert import BERTScore
        >>> preds = ["hello there", "general kenobi"]
        >>> target = ["hello there", "master kenobi"]
        >>> bertscore = BERTScore()
        >>> pprint(bertscore(preds, target))
        {'f1': tensor([1.0000, 0.9961]), 'precision': tensor([1.0000, 0.9961]), 'recall': tensor([1.0000, 0.9961])}

    Fis_differentiableThigher_is_betterfull_state_updateg        plot_lower_boundg      ?plot_upper_boundpreds_input_idspreds_attention_masktarget_input_idstarget_attention_maskN   @   r   en)model_name_or_path
num_layers
all_layersmodeluser_tokenizeruser_forward_fnverboseidfdevice
max_length
batch_sizenum_threadsreturn_hashlangrescale_with_baselinebaseline_pathbaseline_urlkwargsr   c                    s  t  jf i | |pt| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _|| _|| _|| _|| _|| _|| _|r|| _d| _n8tstd|d u rtdtd t| j| _d| _| jdg dd | jd	g dd | jd
g dd | jdg dd d S )NTz`BERTScore` metric with default tokenizers requires `transformers` package be installed. Either install with `pip install transformers>=4.4` or `pip install torchmetrics[text]`.zThe argument `model_name_or_path` was not specified while it is required when the default `transformers` model is used. It will use the default recommended model - .Fr,   r%   )Zdist_reduce_fxr-   r.   r/   )super__init__r   r3   r4   r5   r6   r8   r9   r:   embedding_devicer<   r=   r>   r?   r@   rA   rB   rC   	tokenizerr7   r   ModuleNotFoundErrorr   r   r   Z	add_state)selfr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   	__class__r   r   rG      sH    
zBERTScore.__init__)predstargetr   c                 C   s   t |tst|}t |ts$t|}t|| j| jdd| jd\}}t|| j| jdd| jd\}}| j|d  | j|d  | j	|d  | j
|d  dS )zStore predictions/references for computing BERT scores.

        It is necessary to store sentences in a tokenized form to ensure the DDP mode working.

        F)Z
truncationZsort_according_lengthZown_tokenizerr!   r"   N)
isinstancelistr   rI   r<   r7   r,   appendr-   r.   r/   )rK   rN   rO   Z
preds_dict_target_dictr   r   r   update   s0    



	zBERTScore.updater   c                 C   s   t | jt | jd}t | jt | jd}t||| j| j| j| j	| j
rN| jnd| j| j| j| j| j| j| j| j| j| j| j| jdS )zCalculate BERT scores.r#   N)rN   rO   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   )r   r,   r-   r.   r/   r   r3   r4   r5   r6   r7   rI   r8   r9   r:   rH   r<   r=   r>   r?   r@   rA   rB   rC   )rK   rN   rO   r   r   r   compute   s6    zBERTScore.compute)valaxr   c                 C   s.   |du r"|   }dd | D }| ||S )a,  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> from torchmetrics.text.bert import BERTScore
            >>> preds = ["hello there", "general kenobi"]
            >>> target = ["hello there", "master kenobi"]
            >>> metric = BERTScore()
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> from torch import tensor
            >>> from torchmetrics.text.bert import BERTScore
            >>> preds = ["hello there", "general kenobi"]
            >>> target = ["hello there", "master kenobi"]
            >>> metric = BERTScore()
            >>> values = []
            >>> for _ in range(10):
            ...     val = metric(preds, target)
            ...     val = {k: tensor(v).mean() for k,v in val.items()}  # convert into single value per key
            ...     values.append(val)
            >>> fig_, ax_ = metric.plot(values)

        Nc                 S   s    i | ]\}}|t | qS r   )r$   Ztensormean).0kvr   r   r   
<dictcomp>4      z"BERTScore.plot.<locals>.<dictcomp>)rV   itemsZ_plot)rK   rW   rX   r   r   r   plot  s    -r   )NNFNNNFFNr0   r1   r   Fr2   FNN)NN) __name__
__module____qualname____doc__r'   bool__annotations__r(   r)   r*   floatr+   r   r	   r   strintr
   r   r   r   r   r$   r;   rG   r   rU   rV   r   r   r`   __classcell__r   r   rL   r   r    6   sn   
D                 >*!$! ))typingr   r   r   r   r   r   r   r$   r	   Ztorch.nnr
   Z!torchmetrics.functional.text.bertr   Z4torchmetrics.functional.text.helper_embedding_metricr   Ztorchmetrics.metricr   Ztorchmetrics.utilitiesr   Ztorchmetrics.utilities.checksr   r   Ztorchmetrics.utilities.datar   Ztorchmetrics.utilities.importsr   r   Ztorchmetrics.utilities.plotr   r   Z__doctest_skip__r   rh   rf   Ztransformersr   r   r   r&   r    r   r   r   r   <module>   s,   &
"