o
    .wi8                     @   sB  U d dl mZ d dl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s`dgZdZ e!e"d< ererd 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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 jtdd tjtdd dS )zDownload intensive operations.T)resume_downloadN)r   from_pretrainedr   r    r   r   S/home/ubuntu/sommelier/.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_dict2   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 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   ay  `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`` (: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.
        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])}

    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| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _|| _|| _|| _|| _|| _|| _|| _|rG|| _d| _n"tsMtdddlm} |d u r_t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   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$   )dist_reduce_fxr,   r-   r.   r   )super__init__r   r2   r3   r4   r5   r7   r8   r9   embedding_devicer;   r<   r=   r>   r?   r@   rA   rB   rC   	tokenizerr6   r   ModuleNotFoundErrortransformersr   r   r   	add_state)selfr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   r   	__class__r   r   rH      sL   
zBERTScore.__init__predstargetc                 C   s   t |ts	t|}t |tst|}t|| j| j| jd| jd\}}t|| j| j| j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)rC   sort_according_lengthown_tokenizerr    r!   N)
isinstancelistr   rJ   r;   rC   r6   r+   appendr,   r-   r.   )rN   rQ   rR   
preds_dict_target_dictr   r   r   update   s0   



	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n4d	d
| jd| jd| jd| jd| jd| jd| jd| jd| jd| jd| jd| jS d
| jd| jd| jd| jd| jd| jd| jd| jd| jd| jd| jd| jS )zCalculate BERT scores.r"   rQ   rR   r2   r3   r4   r5   r6   Nr7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   r   )r   r+   r,   r-   r.   r
   r2   r3   r4   r5   r6   rJ   r7   r8   r9   rI   r;   r<   r=   r>   r?   r@   rA   rB   )rN   rQ   rR   r   r   r   compute   s   	
	
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>:  s     z"BERTScore.plot.<locals>.<dictcomp>)r\   items_plot)rN   r]   r^   r   r   r   plot  s   -r   )NNFNNNFFNr/   r0   r   Fr1   FNNF)NN)!__name__
__module____qualname____doc__r&   bool__annotations__r'   r(   r)   floatr*   r   r   r   strintr	   r   r   dictr   r#   r:   rH   r   r[   rV   r\   r   r   rg   __classcell__r   r   rO   r   r   7   s   
 E	
.B$!!)r   N)*collections.abcr   typingr   r   r   r   r   r#   r   torch.nnr	   !torchmetrics.functional.text.bertr
   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   ro   rm   rL   r   r   r   rq   r%   r   r   r   r   r   <module>   s2   
&