o
    ªôj,A  ã                   @   sR  U d dl mZ 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mZ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shdgZ#dZ$e%e&d< erˆerˆd dl'm(Z(m)Z) ddd„Z*ee*ƒs‡ddgZ#nddgZ#dee dee de+e%ef fdd„Z,G dd„ deƒZ-dS )é    )ÚSequence)ÚAnyÚCallableÚListÚOptionalÚTupleÚUnionÚcastN)ÚTensor)ÚModule)Ú _postprocess_multiple_referencesÚ_preprocess_multiple_referencesÚ
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 jtdd� tjtdd� dS )zDownload intensive operations.T)Úresume_downloadN)r   Úfrom_pretrainedr   r   © r    r    úi/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchmetrics/text/bert.pyÚ_download_model_for_bert_score+   s   r"   Ú	BERTScoreÚ	input_idsÚattention_maskc                 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_dict6   s   r)   c                )       sÎ  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< 																		d4dee dee dedee dee deeeeeef gef  dededeeeejf  dededed ed!ed"ed#ee d$ee d%ed&ed'df(‡ fd(d)„Zd*eeee f d+eeee eee  f d'dfd,d-„Zd'eeeeee
 ef f fd.d/„Z	d5d0eeeee f  d1ee d'efd2d3„Z‡  ZS )6r#   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 implementation follows the original implementation from
    `BERT_score`_.

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

    - ``preds``: Predicted sentence(s). Can be one of:

        * A single predicted sentence as a string (``str``)
        * A sequence of predicted sentences (``Sequence[str]``)

    - ``target``: Target/reference sentence(s). Can be one of:

        * A single reference sentence as a string (``str``)
        * A sequence of reference sentences (``Sequence[str]``)
        * A sequence of sequences of reference sentences for multi-reference evaluation (``Sequence[Sequence[str]]``)

    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 (Union[str, Sequence[str]]): A single predicted sentence or a sequence of predicted sentences.
        target (Union[str, Sequence[str], Sequence[Sequence[str]]]): A single target sentence, a sequence of target
            sentences, or a sequence of sequences of target sentences for multiple references per prediction.
        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.
        truncation: An indication of whether the input sequences should be truncated to the ``max_length``.
        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])}

    Example:
        >>> from pprint import pprint
        >>> from torchmetrics.text.bert import BERTScore
        >>> preds = ["hello there", "general kenobi"]
        >>> target = [["hello there", "master kenobi"], ["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Ú
truncationÚkwargsr   c                    s  t ƒ jdi |¤Ž |pt| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _|| _|| _|| _|| _|| _|| _|| _d | _|rJ|| _d| _n"tsPtdƒ‚ddlm} |d u rbtdt›d�ƒ | | 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]`.r   )r   z¤The 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(   )Údist_reduce_fxr0   r1   r2   r    )ÚsuperÚ__init__r   r6   r7   r8   r9   r;   r<   r=   Úembedding_devicer?   r@   rA   rB   rC   rD   rE   rF   rG   Úref_group_boundariesÚ	tokenizerr:   r   ÚModuleNotFoundErrorÚtransformersr   r   r   Ú	add_state)Úselfr6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   r   ©Ú	__class__r    r!   rL   ž   sN   
ÿþÿzBERTScore.__init__ÚpredsÚtargetc                 C   s<  t |tƒr|g}t |tƒr|g}t |tƒst|ƒ}t |tƒs"t|ƒ}t|ƒt|ƒkr8tdt|ƒ› dt|ƒ› �ƒ‚t |tƒrWt|ƒdkrWt |tƒrWt|ƒdkrWt||ƒ\}}| _t|| j| j	| j
d| jd�\}}tttt |ƒ| j| j	| j
d| jd�\}}| j |d ¡ | j |d ¡ | j |d ¡ | j |d ¡ dS )	z¡Store predictions/references for computing BERT scores.

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

        zLExpected number of predicted and reference sentences to be the same, but gotz and r   F)rG   Úsort_according_lengthÚown_tokenizerr$   r%   N)Ú
isinstanceÚstrÚlistÚlenÚ
ValueErrorr   rN   r   rO   r?   rG   r:   r	   r   r/   Úappendr0   r1   r2   )rS   rV   rW   Ú
preds_dictÚ_Útarget_dictr    r    r!   Úupdateá   sL   



ÿÿÿ,
ú
ú	zBERTScore.updatec                 C   s:  t | jƒt | jƒdœ}t | jƒt | jƒdœ}tdi d|“d|“d| j“d| j“d| j“d| j	“d| j
r6| jnd	“d
| j“d| j“d| j“d| j“d| j“d| j“d| j“d| j“d| j“d| j“d| j“d| j“Ž}| jd	ur›t|d tƒr›t|d tƒr›t|d tƒr›t|d |d |d | jƒ\|d< |d< |d< |S )zCalculate BERT scores.r&   rV   rW   r6   r7   r8   r9   r:   Nr;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   Ú	precisionÚrecallÚf1r    )r   r/   r0   r1   r2   r   r6   r7   r8   r9   r:   rO   r;   r<   r=   rM   r?   r@   rA   rB   rC   rD   rE   rF   rN   rZ   r
   r   )rS   rV   rW   Úoutput_dictr    r    r!   Úcompute  sr   þþÿþýüûú€ùø	÷
öõôóòñðïîí
ÿþýÿzBERTScore.computeÚvalÚaxc                 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'   ÚtensorÚmean)Ú.0ÚkÚvr    r    r!   Ú
<dictcomp>m  s     z"BERTScore.plot.<locals>.<dictcomp>)rh   ÚitemsÚ_plot)rS   ri   rj   r    r    r!   Úplot>  s   -r   )NNFNNNFFNr3   r4   r   Fr5   FNNF)NN) Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   ÚboolÚ__annotations__r+   r,   r-   Úfloatr.   r   r
   r   r[   Úintr   r   r   Údictr   r'   r>   rL   r   rc   rh   r   r   rs   Ú__classcell__r    r    rT   r!   r#   ;   s¬   
 Wíþýüûúùø	÷
öõôóòñðïîíìëCÿÿ
þ$0.ÿÿÿþ)r   N).Úcollections.abcr   Útypingr   r   r   r   r   r   r	   r'   r
   Útorch.nnr   Ú!torchmetrics.functional.text.bertr   r   r   Ú4torchmetrics.functional.text.helper_embedding_metricr   Útorchmetrics.metricr   Útorchmetrics.utilitiesr   Útorchmetrics.utilities.checksr   r   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   r   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r[   ry   rQ   r   r   r"   r|   r)   r#   r    r    r    r!   Ú<module>   s2   $
€&