§
    ’©ºj,A  ã                   ó„  — 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sdgZ#dZ$e%e&d<   ererd dl'm(Z(m)Z) 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„Z, G 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_TYPEzBERTScore.plotzroberta-largeÚ_DEFAULT_MODEL)Ú	AutoModelÚAutoTokenizerÚreturnc                  ór   — t          j        t          d¬¦  «         t          j        t          d¬¦  «         dS )zDownload intensive operations.T)Úresume_downloadN)r   Úfrom_pretrainedr   r   © ó    úf/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/text/bert.pyÚ_download_model_for_bert_scorer$   +   s4   € åÔ%¥nÀdÐKÑKÔKÐKÝÔ!¥.À$ÐGÑGÔGÐGÐGÐGr"   Ú	BERTScoreÚ	input_idsÚattention_maskc                 óT   — t          j        | ¦  «        t          j        |¦  «        dœS )z]Create an input dictionary of ``input_ids`` and ``attention_mask`` for BERTScore calculation.©r&   r'   )ÚtorchÚcatr)   s     r#   Ú_get_input_dictr,   6   s$   € åœ 9Ñ-Ô-ÅÄÈ>ÑAZÔAZÐ[Ð[Ð[r"   c            )       óÄ  ‡ — 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<   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d0d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(„Zd)eeee         f         d*eeee         eee                  f         d'dfd+„Zd'eeeeee
         ef         f         fd,„Z	 d1d-eeeee         f                  d.ee         d'efd/„Zˆ xZS )2r%   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                 óÔ  •—  t          ¦   «         j        di |¤Ž |pt          | _        || _        || _        || _        || _        || _        || _	        |	| _
        |
| _        || _        || _        || _        || _        || _        || _        || _        || _        d | _        |r|| _        d| _        nWt.          st1          d¦  «        ‚ddlm} |€t7          dt          ›d�¦  «          |j        | j        ¦  «        | _        d| _        |                      dg d	¬
¦  «         |                      dg d	¬
¦  «         |                      dg d	¬
¦  «         |                      dg 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 - ú.Fr3   r+   )Údist_reduce_fxr4   r5   r6   r!   )ÚsuperÚ__init__r   r:   r;   r<   r=   r?   r@   rA   Úembedding_devicerC   rD   rE   rF   rG   rH   rI   rJ   rK   Úref_group_boundariesÚ	tokenizerr>   r   ÚModuleNotFoundErrorÚtransformersr   r   r    Ú	add_state)Úselfr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r   Ú	__class__s                        €r#   rQ   zBERTScore.__init__ž   sº  ø€ ð, 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ø"4Ð"F½ˆÔØ$ˆŒØ$ˆŒØˆŒ
Ø.ˆÔØˆŒØˆŒØ &ˆÔØ$ˆŒØ$ˆŒØ&ˆÔØ&ˆÔØˆŒ	Ø%:ˆÔ"Ø*ˆÔØ(ˆÔØ$ˆŒØEIˆÔ!àð 	(Ø+ˆDŒNØ"&ˆDÔÐå2ð Ý)ðpñô ð ð 3Ð2Ð2Ð2Ð2Ð2à!Ð)ÝðXåDRðXð Xð Xñô ð ð
 ;˜]Ô:¸4Ô;RÑSÔSˆDŒNØ"'ˆDÔà�ŠÐ(¨"¸UˆÑCÔCÐCØ�ŠÐ-¨rÀ%ˆÑHÔHÐHØ�ŠÐ)¨2¸eˆÑDÔDÐDØ�ŠÐ.°À5ˆÑIÔIÐIÐIÐIr"   ÚpredsÚtargetc                 óR  — t          |t          ¦  «        r|g}t          |t          ¦  «        r|g}t          |t          ¦  «        st          |¦  «        }t          |t          ¦  «        st          |¦  «        }t          |¦  «        t          |¦  «        k    r/t	          dt          |¦  «        › dt          |¦  «        › �¦  «        ‚t          |t          ¦  «        rTt          |¦  «        dk    rAt          |t          ¦  «        r,t          |¦  «        dk    rt          ||¦  «        \  }}| _        t          || j        | j	        | j
        d| j        ¬¦  «        \  }}t          t          t          t                   |¦  «        | j        | j	        | j
        d| j        ¬¦  «        \  }}| 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)rK   Úsort_according_lengthÚown_tokenizerr&   r'   N)Ú
isinstanceÚstrÚlistÚlenÚ
ValueErrorr   rS   r   rT   rC   rK   r>   r
   r   r3   Úappendr4   r5   r6   )rX   rZ   r[   Ú
preds_dictÚ_Útarget_dicts         r#   ÚupdatezBERTScore.updateá   s  € õ �e�SÑ!Ô!ð 	Ø�GˆEÝ�f�cÑ"Ô"ð 	Ø�XˆFÝ˜%¥Ñ&Ô&ð 	 Ý˜‘K”KˆEÝ˜&¥$Ñ'Ô'ð 	"Ý˜&‘\”\ˆFåˆu‰:Œ:�˜V™œÒ$Ð$Ýð2Ý�u‘:”:ð2ð 2Ý$'¨¡K¤Kð2ð 2ñô ð õ
 �e�TÑ"Ô"ð 	f¥s¨5¡z¤z°A¢~ ~½*ÀVÍTÑ:RÔ:R ~ÕWZÐ[aÑWbÔWbÐefÒWfÐWfÝ7VÐW\Ð^dÑ7eÔ7eÑ4ˆE�6˜4Ô4å(ØØŒNØŒOØ”Ø"'ØÔ-ð
ñ 
ô 
‰ˆ
�Aõ *Ý••c”˜FÑ#Ô#ØŒNØŒOØ”Ø"'ØÔ-ð
ñ 
ô 
‰ˆ�Qð 	Ô×#Ò# J¨{Ô$;Ñ<Ô<Ð<ØÔ!×(Ò(¨Ð4DÔ)EÑFÔFÐFØÔ×$Ò$ [°Ô%=Ñ>Ô>Ð>ØÔ"×)Ò)¨+Ð6FÔ*GÑHÔHÐHÐHÐHr"   c                 ó  — 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
        r| 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        �‡t3          |d         t4          ¦  «        rlt3          |d         t4          ¦  «        rQt3          |d         t4          ¦  «        r6t7          |d         |d         |d         | j        ¦  «        \  |d<   |d<   |d<   |S )zCalculate BERT scores.r)   rZ   r[   r:   r;   r<   r=   r>   Nr?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   Ú	precisionÚrecallÚf1r!   )r   r3   r4   r5   r6   r   r:   r;   r<   r=   r>   rT   r?   r@   rA   rR   rC   rD   rE   rF   rG   rH   rI   rJ   rS   r_   r   r   )rX   rZ   r[   Úoutput_dicts       r#   ÚcomputezBERTScore.compute  s*  € õ & dÔ&:Ñ;Ô;Ý*¨4Ô+DÑEÔEð
ð 
ˆõ
 & dÔ&;Ñ<Ô<Ý*¨4Ô+EÑFÔFð
ð 
ˆõ
 !ð 
ð 
ð 
Ø�%ð
à�6ð
ð  $Ô6Ð6ð
ð ”�ð	
ð
 ”�ð
ð ”*�*ð
ð .2Ô-@ÐJ˜4œ>˜>Àdøð
ð !Ô0Ð0ð
ð ”L�Lð
ð ”�ð
ð Ô(Ð(ð
ð ”�ð
ð ”�ð
ð Ô(Ð(ð
ð Ô(Ð(ð
ð  ”�ð!
ð" #'Ô"<Ð"<ð#
ð$ Ô,Ð,ð%
ð& Ô*Ð*ð'
ˆð. Ô%Ð1Ý˜; {Ô3µVÑ<Ô<ð 2å˜; xÔ0µ&Ñ9Ô9ð 2õ ˜; tÔ,­fÑ5Ô5ð 2õ
 RrØ˜KÔ(¨+°hÔ*?ÀÈTÔARÐTXÔTmñRô RÑNˆK˜Ñ$ k°(Ñ&;¸[ÈÑ=Nð Ðr"   ÚvalÚaxc                 ó–   — |€2|                       ¦   «         }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                 ób   — i | ],\  }}|t          j        |¦  «                             ¦   «         “Œ-S r!   )r*   ÚtensorÚmean)Ú.0ÚkÚvs      r#   ú
<dictcomp>z"BERTScore.plot.<locals>.<dictcomp>m  s2   € ÐEÐEÐE±°°A�1•e”l 1‘o”o×*Ò*Ñ,Ô,ÐEÐEÐEr"   )rn   ÚitemsÚ_plot)rX   ro   rp   s      r#   ÚplotzBERTScore.plot>  sF   € ðZ ˆ;Ø—,’,‘.”.ˆCØEÐE¸¿º¹¼ÐEÑEÔEˆCØ�zŠz˜#˜rÑ"Ô"Ð"r"   )NNFNNNFFNr7   r8   r   Fr9   FNNF)NN) Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   ÚboolÚ__annotations__r/   r0   r1   Úfloatr2   r   r   r   r`   Úintr   r   r   Údictr	   r*   rB   rQ   r   rh   rn   r   r   r{   Ú__classcell__)rY   s   @r#   r%   r%   ;   sK  ø€ € € € € € ðUð Uðn $Ð�tÐ#Ð#Ñ#Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!à˜&”\Ð!Ð!Ñ!Ø˜vœ,Ð&Ð&Ñ&Ø˜6”lÐ"Ð"Ñ"Ø œ<Ð'Ð'Ñ'ð -1Ø$(Ø Ø"&Ø(,ØSWØØØ59ØØØØ!ØØ&+Ø'+Ø&*Ø ð'AJð AJà$ SœMðAJð ˜S”MðAJð ð	AJð
 ˜ÔðAJð ! œðAJð " (¨F°D¸¸f¸Ô4EÐ+FÈÐ+NÔ"OÔPðAJð ðAJð ðAJð ˜˜s E¤LÐ0Ô1Ô2ðAJð ðAJð ðAJð ðAJð ðAJð ðAJð   $ð!AJð"   ”}ð#AJð$ ˜s”mð%AJð& ð'AJð( ð)AJð* 
ð+AJð AJð AJð AJð AJð AJðF.IØ˜3 ¨¤Ð-Ô.ð.IØ8=¸cÀ8ÈCÄ=ÐRZÐ[cÐdgÔ[hÔRiÐ>iÔ8jð.Ià	ð.Ið .Ið .Ið .Ið`+˜˜c 5¨°°e´¸cÐ)AÔ#BÐBÔCð +ð +ð +ð +ð\ _cð0#ð 0#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð0#ØIQÐRZÔI[ð0#à	ð0#ð 0#ð 0#ð 0#ð 0#ð 0#ð 0#ð 0#r"   )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`   r�   rV   r   r   r$   r„   r,   r%   r!   r"   r#   ú<module>r’      sX  ðð %Ð $Ð $Ð $Ð $Ð $Ð $Ø DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ Dà €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð
 RÐ QÐ QÐ QÐ QÐ QØ &Ð &Ð &Ð &Ð &Ð &Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø WÐ WÐ WÐ WÐ WÐ WÐ WÐ WØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø aÐ aÐ aÐ aÐ aÐ aÐ aÐ aØ @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð *Ø(Ð)Ðð &€�Ð %Ð %Ñ %àð 7Ð9ð 7Ø5Ð5Ð5Ð5Ð5Ð5Ð5Ð5ðHð Hð Hð Hð
 %Ð$Ð%CÑDÔDð ;Ø'Ð)9Ð:Ðøà#Ð%5Ð6Ðð\˜t Fœ|ð \¸TÀ&¼\ð \ÈdÐSVÐX^ÐS^ÔN_ð \ð \ð \ð \ð
s#ð s#ð s#ð s#ð s#�ñ s#ô s#ð s#ð s#ð s#r"   