§
    ’©ºjzh  ã            0       óÌ  — d dl Z d dlZd dlZd dlmZmZ 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 d dlmZmZmZmZmZmZmZ d d	lmZ d d
l m!Z!m"Z" d dl#m$Z$m%Z% eded         fd„¦   «         Z&dZ'e%rd dl(m)Z)m*Z* dCd„Z+e!r e"e+¦  «        sdgZ,ndgZ,	 	 	 	 	 	 dDdede-dedeee.ej/        f                  dee-         de0de0de0dee
ee1e.ef         gef                  deeef         fd„Z2dede.dedefd „Z3d!ed"ed#ed$edeeeef         f
d%„Z4dEd&ee.         dee-         de0de.fd'„Z5d(e.defd)„Z6d*e.defd+„Z7	 	 	 	 dFd-e.d&ee.         d(ee.         d*ee.         dee         f
d.„Z8	 	 dGd/ed0ed1ed2edee-         de0deeeef         fd3„Z9d4ee.         d5eee.ee.         f                  deee.         ee.         eeee-e-f                           f         fd6„Z:d/ed0ed1ed7eee-e-f                  deeeef         f
d8„Z;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dHd4ee.ee.         e1e.ef         f         d5ee.ee.         eee.                  e1e.ef         f         d&ee.         dee-         de0dee         d;e	dee
ee1e.ef         gef                  de0de0deee.ej/        f                  d<e-d=e-d>e-d?e0d-e.d@e0d(ee.         d*ee.         dAe0de1e.eeee<         e.f         f         f*dB„Z=dS )Ié    N)ÚIteratorÚSequence)Úcontextmanager)ÚAnyÚCallableÚListÚOptionalÚTupleÚUnionÚcast)ÚTensor)ÚModule)Ú
DataLoader)ÚTextDatasetÚTokenizedDatasetÚ_check_shape_of_model_outputÚ_get_progress_barÚ_input_data_collatorÚ_output_data_collatorÚ*_process_attention_mask_for_special_tokens)Úrank_zero_warn)Ú_SKIP_SLOW_DOCTESTÚ_try_proceed_with_timeout)Ú_TQDM_AVAILABLEÚ_TRANSFORMERS_GREATER_EQUAL_4_4Úreturnc               #   ó   K  — t          j        d¦  «        } |                      ¦   «         }	 |                      t           j        ¦  «         dV — |                      |¦  «         dS # |                      |¦  «         w xY w)z]Ignore irrelevant fine-tuning warning from transformers when loading the model for BertScore.ztransformers.modeling_utilsN)ÚloggingÚ	getLoggerÚgetEffectiveLevelÚsetLevelÚERROR)ÚloggerÚoriginal_levels     úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/text/bert.pyÚ_ignore_log_warningr&   (   s|   è è € õ ÔÐ<Ñ=Ô=€FØ×-Ò-Ñ/Ô/€Nð(Ø�Š�œÑ&Ô&Ð&Øˆˆˆà�Š˜Ñ'Ô'Ð'Ð'Ð'øˆ�Š˜Ñ'Ô'Ð'Ð'øøøs   ¬#A& Á&A=zroberta-large)Ú	AutoModelÚAutoTokenizerc                  ó¸   — t          ¦   «         5  t          j        t          ¦  «         t	          j        t          ¦  «         ddd¦  «         dS # 1 swxY w Y   dS )zDownload intensive operations.N)r&   r(   Úfrom_pretrainedÚ_DEFAULT_MODELr'   © ó    r%   Ú_download_model_for_bert_scorer.   :   s”   € å Ñ"Ô"ð 	6ð 	6ÝÔ)­.Ñ9Ô9Ð9ÝÔ%¥nÑ5Ô5Ð5ð	6ð 	6ð 	6ñ 	6ô 	6ð 	6ð 	6ð 	6ð 	6ð 	6ð 	6ð 	6øøøð 	6ð 	6ð 	6ð 	6ð 	6ð 	6s   �3AÁAÁAÚ
bert_scoreFÚ
dataloaderÚ
target_lenÚmodelÚdeviceÚ
num_layersÚ
all_layersÚidfÚverboseÚuser_forward_fnc	                 óf  — g }	g }
t          | |¦  «        D �]ð}t          j        ¦   «         5  t          ||¦  «        }|sf|s, ||d         |d         d¬¦  «        }|j        |�|nd         }n" |||¦  «        }t          ||d         ¦  «         |                     d¦  «        }nP|rt          d¦  «        ‚ ||d         |d         d¬¦  «        }t          j        d	„ |j        D ¦   «         d¬
¦  «        }ddd¦  «         n# 1 swxY w Y   || 	                    d¬
¦  «                             d¦  «        z  }t          ||d         |¦  «        \  }}t          |¦  «        }t          j        d||¦  «        }|	                     |                     ¦   «         ¦  «         |r|d         |z  n|                     |j        ¦  «        }||                     dd¬¦  «        z  }|
                     |                     ¦   «         ¦  «         �Œòt          j        |	¦  «        }t          j        |
¦  «        }||fS )a<  Calculate sentence embeddings and the inverse-document-frequency scaling factor.

    Args:
        dataloader: dataloader instance.
        target_len: A length of the longest sequence in the data. Used for padding the model output.
        model: BERT model.
        device: A device to be used for calculation.
        num_layers: The layer of representation to use.
        all_layers: An indication whether representation from all model layers should be used for BERTScore.
        idf: An Indication whether normalization using inverse document frequencies should be used.
        verbose: An indication of whether a progress bar to be displayed during the embeddings' calculation.
        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`.

    Return:
        A tuple of :class:`~torch.Tensor`s containing the model's embeddings and the normalized tokens IDF.
        When ``idf = False``, tokens IDF is not calculated, and a matrix of mean weights is returned instead.
        For a single sentence, ``mean_weight = 1/seq_len``, where ``seq_len`` is a sum over the corresponding
        ``attention_mask``.

    Raises:
        ValueError:
            If ``all_layers = True`` and a model, which is not from the ``transformers`` package, is used.

    Ú	input_idsÚattention_maskT)Úoutput_hidden_statesNéÿÿÿÿé   zQThe option `all_layers=True` can be used only with default `transformers` models.c                 ó8   — g | ]}|                      d ¦  «        ‘ŒS )r>   )Ú	unsqueeze)Ú.0Úos     r%   ú
<listcomp>z1_get_embeddings_and_idf_scale.<locals>.<listcomp>�   s"   € Ð KÐ KÐ K°A §¢¨Q¡¤Ð KÐ KÐ Kr-   ©Údimzblsd, bs -> blsdÚinput_ids_idf)Úkeepdim)r   ÚtorchÚno_gradr   Úhidden_statesr   r@   Ú
ValueErrorÚcatÚnormr   r   ÚeinsumÚappendÚcpuÚtypeÚdtypeÚsum)r0   r1   r2   r3   r4   r5   r6   r7   r8   Úembeddings_listÚidf_scale_listÚbatchÚoutr;   Úprocessed_attention_maskrF   Ú
embeddingsÚ	idf_scales                     r%   Ú_get_embeddings_and_idf_scaler[   F   sº  € ðN %'€OØ#%€NÝ" :¨wÑ7Ô7ð  3ñ  3ˆÝŒ]‰_Œ_ð 	Tð 	TÝ(¨°Ñ7Ô7ˆEàð TØ&ð JØ˜%  kÔ 2°EÐ:JÔ4KÐbfÐgÑgÔg�CØÔ+¸*Ð:P¨J¨JÐVXÔY�C�Cà)˜/¨%°Ñ7Ô7�CÝ0°°e¸KÔ6HÑIÔIÐIØ—m’m AÑ&Ô&��à"ð Ý$Økñô ð ð �e˜E +Ô.°Ð6FÔ0GÐ^bÐcÑcÔc�Ý”iÐ KÐ K¸Ô9JÐ KÑ KÔ KÐQRÐSÑSÔS�ð#	Tð 	Tð 	Tñ 	Tô 	Tð 	Tð 	Tð 	Tð 	Tð 	Tð 	Tøøøð 	Tð 	Tð 	Tð 	Tð& 	ˆs�xŠx˜BˆxÑÔ×)Ò)¨"Ñ-Ô-Ñ-ˆÝ3°C¸Ð?OÔ9PÐR\Ñ]Ô]Ñˆˆ^Ý#MÈnÑ#]Ô#]Ð åŒlÐ-¨sÐ4LÑMÔMˆØ×Ò˜sŸwšw™yœyÑ)Ô)Ð)ð BEÐrˆE�/Ô"Ð%=Ñ=Ð=ÐJb×JgÒJgÐhkÔhqÑJrÔJrð 	ð 	˜×*Ò*¨2°tÐ*Ñ<Ô<Ñ<ˆØ×Ò˜m×/Ò/Ñ1Ô1Ñ2Ô2Ð2Ñ2å”˜?Ñ+Ô+€JÝ”	˜.Ñ)Ô)€Ià�yÐ Ð s   «C	D Ä D	ÄD	Úcos_simÚmetricrZ   c                 óî   — |dk    rdnd}|                       |¬¦  «        j        }t          j        d||¦  «                             d¦  «        }|                     dd¦  «                             ¦   «         S )	zOCalculate precision or recall, transpose it and scale it with idf_scale factor.Ú	precisioné   é   rD   zbls, bs -> blsr=   r   r>   )ÚmaxÚvaluesrH   rN   rS   Ú	transposeÚsqueeze)r\   r]   rZ   rE   Úress        r%   Ú_get_scaled_precision_or_recallrg   —   sm   € à˜Ò$Ð$ˆ!ˆ!¨!€CØ
�+Š+˜#ˆ+Ñ
Ô
Ô
%€CÝ
Œ,Ð'¨¨iÑ
8Ô
8×
<Ò
<¸RÑ
@Ô
@€Cà�=Š=˜˜AÑÔ×&Ò&Ñ(Ô(Ð(r-   Úpreds_embeddingsÚtarget_embeddingsÚpreds_idf_scaleÚtarget_idf_scalec                 óè   — t          j        d| |¦  «        }t          |d|¦  «        }t          |d|¦  «        }d|z  |z  ||z   z  }|                     t          j        |¦  «        d¦  «        }|||fS )a¿  Calculate precision, recall and F1 score over candidate and reference sentences.

    Args:
        preds_embeddings: Embeddings of candidate sentences.
        target_embeddings: Embeddings of reference sentences.
        preds_idf_scale: An IDF scale factor for candidate sentences.
        target_idf_scale: An IDF scale factor for reference sentences.

    Return:
        Tensors containing precision, recall and F1 score, respectively.

    zblpd, blrd -> blprr_   Úrecallra   ç        )rH   rN   rg   Úmasked_fillÚisnan)rh   ri   rj   rk   r\   r_   rm   Úf1_scores           r%   Ú_get_precision_recall_f1rr       s�   € õ  ŒlÐ/Ð1AÐCTÑUÔU€Gå/°¸ÀoÑVÔV€IÝ,¨W°hÐ@PÑQÔQ€Fà�9‰}˜vÑ%¨°VÑ);Ñ<€HØ×#Ò#¥E¤K°Ñ$9Ô$9¸3Ñ?Ô?€Hà�f˜hÐ&Ð&r-   Úmodel_name_or_pathc                 ó   — | › d|› |rdnd› �S )z,Compute `BERT_score`_ (copied and adjusted).Ú_LÚ_idfz_no-idfr,   )rs   r4   r6   s      r%   Ú	_get_hashrw   »   s$   € à ÐNÐN JÐN¸#Ð0L°°À9ÐNÐNÐNr-   Úbaseline_pathc                 óê   — t          | ¦  «        5 }t          j        |¦  «        }d„ t          |¦  «        D ¦   «         }ddd¦  «         n# 1 swxY w Y   t	          j        |¦  «        dd…dd…f         S )zqRead baseline from csv file from the local file.

    This method implemented to avoid `pandas` dependency.

    c                 ó8   — g | ]\  }}|d k    ¯d„ |D ¦   «         ‘ŒS )r   c                 ó,   — g | ]}t          |¦  «        ‘ŒS r,   ©Úfloat©rA   Úitems     r%   rC   z8_read_csv_from_local_file.<locals>.<listcomp>.<listcomp>È   s   € Ð6Ð6Ð6¨$�% ™+œ+Ð6Ð6Ð6r-   r,   ©rA   ÚidxÚrows      r%   rC   z-_read_csv_from_local_file.<locals>.<listcomp>È   s7   € ÐfÐfÐf¹8¸3ÀÐ^aÐdeÒ^eÐ^eÐ6Ð6°#Ð6Ñ6Ô6Ð^eÐ^eÐ^er-   Nr>   )ÚopenÚcsvÚreaderÚ	enumeraterH   Útensor)rx   ÚfnameÚcsv_fileÚbaseline_lists       r%   Ú_read_csv_from_local_filer‹   À   sÂ   € õ 
ˆmÑ	Ô	ð g Ý”:˜eÑ$Ô$ˆØfÐfÅyÐQYÑGZÔGZÐfÑfÔfˆðgð gð gñ gô gð gð gð gð gð gð gøøøð gð gð gð gõ Œ<˜Ñ&Ô& q q q¨!¨"¨" uÔ-Ð-s   �.A
Á
AÁAÚbaseline_urlc                 óæ   — t           j                             | ¦  «        5 }d„ t          |¦  «        D ¦   «         }t	          j        |¦  «        dd…dd…f         cddd¦  «         S # 1 swxY w Y   dS )ziRead baseline from csv file from URL.

    This method is implemented to avoid `pandas` dependency.

    c                 ó¨   — g | ]O\  }}|d k    ¯d„ |                      ¦   «                              d¦  «                             d¦  «        D ¦   «         ‘ŒPS )r   c                 ó,   — g | ]}t          |¦  «        ‘ŒS r,   r|   r~   s     r%   rC   z1_read_csv_from_url.<locals>.<listcomp>.<listcomp>Ô   s   € ÐLÐLÐL˜T�U�4‰[Œ[ÐLÐLÐLr-   zutf-8ú,)ÚstripÚdecodeÚsplitr€   s      r%   rC   z&_read_csv_from_url.<locals>.<listcomp>Ó   s`   € ð 
ð 
ð 
á��SØ�QŠwˆwð MÐL S§Y¢Y¡[¤[×%7Ò%7¸Ñ%@Ô%@×%FÒ%FÀsÑ%KÔ%KÐLÑLÔLàˆwˆwr-   Nr>   )ÚurllibÚrequestÚurlopenr†   rH   r‡   )rŒ   Úhttp_requestrŠ   s      r%   Ú_read_csv_from_urlr˜   Ì   sÁ   € õ 
Œ×	Ò	 Ñ	-Ô	-ð 2°ð
ð 
å% lÑ3Ô3ð
ñ 
ô 
ˆõ
 Œ|˜MÑ*Ô*¨1¨1¨1¨a¨b¨b¨5Ô1ð2ð 2ð 2ð 2ñ 2ô 2ð 2ð 2ð 2ð 2ð 2ð 2øøøð 2ð 2ð 2ð 2ð 2ð 2s    9A&Á&A*Á-A*ÚenÚlangc                 ó²   — |rt          |¦  «        }nD|rt          |¦  «        }n2| r|rd}|› d| › d|› d�}t          |¦  «        }nt          d¦  «         dS |S )z<Load a CSV file with the baseline values used for rescaling.zWhttps://raw.githubusercontent.com/Tiiiger/bert_score/master/bert_score/rescale_baselineú/z.tsvzFBaseline was not successfully loaded. No baseline is going to be used.N)r‹   r˜   r   )rš   rs   rx   rŒ   ÚbaselineÚurl_bases         r%   Ú_load_baselinerŸ   Û   sŽ   € ð ð Ý%>¸}Ñ%MÔ%MˆˆØ	ð 	Ý% lÑ3Ô3ˆˆà	ð Ð$ð ØlˆØ"ÐDÐD TÐDÐDÐ,>ÐDÐDÐDˆÝ% lÑ3Ô3ˆˆåÐ_Ñ`Ô`Ð`Øˆtà€Or-   r_   rm   rq   r�   c                 óÆ   — |€|du rd}t          j        | ||gd¬¦  «        }|r|                     d¦  «        n||         }||z
  d|z
  z  }|d         |d         |d         fS )	z<Rescale the computed metrics with the pre-computed baseline.NFr=   rD   r>   ).r   ).r>   ).ra   )rH   Ústackr@   )r_   rm   rq   r�   r4   r5   Úall_metricsÚbaseline_scales           r%   Ú_rescale_metrics_with_baseliner¤   ò   s†   € ð Ð˜j¨EÐ1Ð1Øˆ
Ý”+˜y¨&°(Ð;ÀÐDÑDÔD€KØ.8ÐR�X×'Ò'¨Ñ*Ô*Ð*¸hÀzÔ>R€NØ Ñ/°A¸Ñ4FÑG€Kà�vÔ ¨FÔ 3°[ÀÔ5HÐHÐHr-   ÚpredsÚtargetc                 ó0  — t          d„ | D ¦   «         ¦  «        st          d¦  «        ‚t          d„ |D ¦   «         ¦  «        }|�r0g }g }g }d}t          | |¦  «        D �]\  }}t	          |t
          t          f¦  «        r“|                     |gt          |¦  «        z  ¦  «         |                     t          t          t                   |¦  «        ¦  «         |                     ||t          |¦  «        z   f¦  «         |t          |¦  «        z  }Œµ|                     |¦  «         |                     t          t          |¦  «        ¦  «         |                     ||dz   f¦  «         |dz  }�Œ|||fS | t          t          t                   |¦  «        dfS )a®  Preprocesses predictions and targets when dealing with multiple references.

    This function handles the case where a single prediction might have multiple
    reference targets (represented as a list/tuple of strings).

    Args:
        preds: A list of predictions
        target: A list of targets, where each item could be a string or a list/tuple of strings

    Returns:
        Tuple: (preds, target, ref_group_boundaries)
            - preds: Flattened list of `str`
            - target: Flattened list of `str`
            - ref_group_boundaries: List of tuples (start, end) indicating the boundaries
              of reference groups in the flattened lists or `None`

    c              3   ó@   K  — | ]}t          |t          ¦  «        V — Œd S ©N)Ú
isinstanceÚstrr~   s     r%   ú	<genexpr>z2_preprocess_multiple_references.<locals>.<genexpr>  s,   è è € Ð7Ð7¨�z˜$¥Ñ$Ô$Ð7Ð7Ð7Ð7Ð7Ð7r-   úInvalid input provided.c              3   óN   K  — | ] }t          |t          t          f¦  «        V — Œ!d S r©   )rª   ÚlistÚtupler~   s     r%   r¬   z2_preprocess_multiple_references.<locals>.<genexpr>  s0   è è € ÐRÐRÀ4�z¨$µµu°Ñ>Ô>ÐRÐRÐRÐRÐRÐRr-   r   r>   N)ÚallrK   ÚanyÚziprª   r¯   r°   ÚextendÚlenr   r   r«   rO   )	r¥   r¦   Úhas_nested_sequencesÚref_group_boundariesÚ	new_predsÚ
new_targetÚcountÚpredÚ	ref_groups	            r%   Ú_preprocess_multiple_referencesr½     s›  € õ( Ð7Ð7°Ð7Ñ7Ô7Ñ7Ô7ð 4ÝÐ2Ñ3Ô3Ð3åÐRÐRÈ6ÐRÑRÔRÑRÔRÐàñ ;Ø68ÐØ!ˆ	Ø "ˆ
Øˆå" 5¨&Ñ1Ô1ð 
	ñ 
	‰OˆD�)Ý˜)¥d­E ]Ñ3Ô3ð 	Ø× Ò  $ ­#¨i©.¬.Ñ!8Ñ9Ô9Ð9Ø×!Ò!¥$¥t­C¤y°)Ñ"<Ô"<Ñ=Ô=Ð=Ø$×+Ò+¨U°E½CÀ	¹N¼NÑ4JÐ,KÑLÔLÐLØ�˜Y™œÑ'��à× Ò  Ñ&Ô&Ð&Ø×!Ò!¥$¥s¨IÑ"6Ô"6Ñ7Ô7Ð7Ø$×+Ò+¨U°E¸A±IÐ,>Ñ?Ô?Ð?Ø˜‘
�‘Ø˜*Ð&:Ð:Ð:Ø•$•t�C”y &Ñ)Ô)¨4Ð/Ð/r-   r·   c                 ó  — g g g }}}|D �]]\  }}|                       ¦   «         dk    r²|                     | dd…||…f                              d¬¦  «        d         ¦  «         |                     |dd…||…f                              d¬¦  «        d         ¦  «         |                     |dd…||…f                              d¬¦  «        d         ¦  «         ŒÐ|                     | ||…                              ¦   «         ¦  «         |                     |||…                              ¦   «         ¦  «         |                     |||…                              ¦   «         ¦  «         �Œ_|                       ¦   «         dk    rCt          j        |d¬¦  «        } t          j        |d¬¦  «        }t          j        |d¬¦  «        }n<t          j        |¦  «        } t          j        |¦  «        }t          j        |¦  «        }| ||fS )a)  Postprocesses metrics when dealing with multiple references.

    For each group of references that correspond to a single prediction,
    this function takes the maximum score among all references.

    Args:
        precision: Tensor of precision scores
        recall: Tensor of recall scores
        f1_score: Tensor of F1 scores
        ref_group_boundaries: List of tuples (start, end) indicating the boundaries
                              of reference groups

    Returns:
        tuple: (precision, recall, f1_score) with updated metrics

    r>   NrD   r   )rE   rO   rb   rH   r¡   )	r_   rm   rq   r·   Úmax_precisionÚ
max_recallÚmax_f1ÚstartÚends	            r%   Ú _postprocess_multiple_referencesrÄ   2  sð  € ð& )+¨B°˜v�:€Mà*ð 5ñ 5‰
ˆˆsØ�=Š=‰?Œ?˜QÒÐØ× Ò  ¨1¨1¨1¨e°C¨i¨<Ô!8×!<Ò!<ÀÐ!<Ñ!CÔ!CÀAÔ!FÑGÔGÐGØ×Ò˜f Q Q Q¨¨c¨	 \Ô2×6Ò6¸1Ð6Ñ=Ô=¸aÔ@ÑAÔAÐAØ�MŠM˜( 1 1 1 e¨C i <Ô0×4Ò4¸Ð4Ñ;Ô;¸AÔ>Ñ?Ô?Ð?Ð?à× Ò  ¨5°¨9Ô!5×!9Ò!9Ñ!;Ô!;Ñ<Ô<Ð<Ø×Ò˜f U¨3 YÔ/×3Ò3Ñ5Ô5Ñ6Ô6Ð6Ø�MŠM˜( 5¨ 9Ô-×1Ò1Ñ3Ô3Ñ4Ô4Ð4Ñ4à‡}‚}�„˜ÒÐÝ”K °1Ð5Ñ5Ô5ˆ	Ý”˜Z¨QÐ/Ñ/Ô/ˆÝ”;˜v¨1Ð-Ñ-Ô-ˆˆå”K Ñ.Ô.ˆ	Ý”˜ZÑ(Ô(ˆÝ”;˜vÑ&Ô&ˆà�f˜hÐ&Ð&r-   é   é@   Úuser_tokenizerÚ
max_lengthÚ
batch_sizeÚnum_threadsÚreturn_hashÚrescale_with_baselineÚ
truncationc                 ó

  — d}t          | t          ¦  «        r| g} t          |t          ¦  «        r|g}t          | t          t          f¦  «        st          | ¦  «        } t          |t          t          f¦  «        st          |¦  «        }t	          | ¦  «        t	          |¦  «        k    r/t          dt	          | ¦  «        › dt	          |¦  «        › �¦  «        ‚t          | t          ¦  «        rOt	          | ¦  «        dk    r<t          |t          ¦  «        r't	          |¦  «        dk    rt          | |¦  «        \  } }}t          |	t          ¦  «        st          d|	› d�¦  «        ‚|rt          st          d¦  «        ‚|€�t          st          d¦  «        ‚|€t          d	t          › d�¦  «         t          ¦   «         5  t          j        |pt          ¦  «        }t!          j        |pt          ¦  «        }ddd¦  «         n# 1 swxY w Y   n|}|                     ¦   «          |                     |
¦  «         	 t'          |j        d
¦  «        rTt          |j        j        t,          ¦  «        r5|r2||j        j        k    r"t          d|› d|› d|j        j        › �¦  «        ‚nt          d¦  «         n# t.          $ r t          d¦  «         Y nw xY wt1          d„ | |fD ¦   «         ¦  «        }t1          d„ | |fD ¦   «         ¦  «        }t1          d„ | |fD ¦   «         ¦  «        }|rBt          d¦  «         dgdgdgdœ}|r&|                     dt5          |||	¦  «        i¦  «         |S |rt7          ||||¦  «        nd}|r/t9          ||||	|¬¦  «        }t9          | |||	|j        |¬¦  «        }n8|r't=          di |¤d|	i¤Ž}t=          di | ¤|	|j        dœ¤Ž}nt          d¦  «        ‚t?          |||¬¦  «        }t?          |||¬¦  «        }tA          ||j!        ||
|||	||¦	  «	        \  }} tA          ||j!        ||
|||	||¦	  «	        \  }!}"|!|j"        j#                 }!||j"        j#                 }|"|j"        j#                 }"| |j"        j#                 } tI          |!||"| ¦  «        \  }#}$}%|�tK          |#|$|%|||¦  «        \  }#}$}%|�tM          |#|$|%|¦  «        \  }#}$}%|#|$|%dœ}|r&|                     dt5          |||	¦  «        i¦  «         |S )a2  `Bert_score Evaluating Text Generation`_ for text similirity matching.

    This metric 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`_.

    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_name_or_path: 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.
        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. his 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 of 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. It is used when the scores are rescaled with a baseline.
        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 maximum length.

    Returns:
        Python dictionary containing the keys ``precision``, ``recall`` and ``f1`` with corresponding values.

    Raises:
        ValueError:
            If ``len(preds) != len(target)``.
        ModuleNotFoundError:
            If `tqdm` package is required and not installed.
        ModuleNotFoundError:
            If ``transformers`` package is required and not installed.
        ValueError:
            If ``num_layer`` is larger than the number of the model layers.
        ValueError:
            If invalid input is provided.

    Example:
        >>> from pprint import pprint
        >>> from torchmetrics.functional.text.bert import bert_score
        >>> preds = ["hello there", "general kenobi"]
        >>> target = ["hello there", "master kenobi"]
        >>> pprint(bert_score(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.functional.text.bert import bert_score
        >>> preds = ["hello there", "general kenobi"]
        >>> target = [["hello there", "master kenobi"], ["hello there", "master kenobi"]]
        >>> pprint(bert_score(preds, target))
        {'f1': tensor([1.0000, 0.9961]), 'precision': tensor([1.0000, 0.9961]), 'recall': tensor([1.0000, 0.9961])}

    NzLExpected number of predicted and reference sentences to be the same, but gotz and r   z1Expected argument `idf` to be a boolean, but got ú.zcAn argument `verbose = True` requires `tqdm` package be installed. Install with `pip install tqdm`.z®`bert_score` metric with default models requires `transformers` package be installed. Either install with `pip install transformers>=4.4` or `pip install torchmetrics[text]`.z«The argument `model_name_or_path` was not specified while it is required when default `transformers` model are used.It is, therefore, used the default recommended model - Únum_hidden_layersznum_layers=z is forbidden for z. Please use num_layers <= zhModel config does not have `num_hidden_layers` as an integer attribute. Unable to validate `num_layers`.zXIt was not possible to retrieve the parameter `num_layers` from the model specification.c              3   óf   K  — | ],}t          |t          ¦  «        ot          |¦  «        d k    V — Œ-dS ©r   N)rª   r¯   rµ   ©rA   Útexts     r%   r¬   zbert_score.<locals>.<genexpr>  s;   è è € ÐaÐaÈ�: d­DÑ1Ô1ÐDµc¸$±i´iÀ1²nÐaÐaÐaÐaÐaÐar-   c              3   óœ   K  — | ]G}t          |t          ¦  «        o-t          |¦  «        d k    ot          |d          t          ¦  «        V — ŒHdS rÒ   )rª   r¯   rµ   r«   rÓ   s     r%   r¬   zbert_score.<locals>.<genexpr>  s_   è è € ð ð ØRV�
�4�ÑÔÐM¥3 t¡9¤9¨q¢=ÐMµZÀÀQÄÍÑ5MÔ5Mðð ð ð ð ð r-   c              3   óv   K  — | ]4}t          |t          ¦  «        ot          |d          t          ¦  «        V — Œ5dS )r:   N)rª   Údictr   rÓ   s     r%   r¬   zbert_score.<locals>.<genexpr>  sQ   è è € ð ð ØMQ�
�4�ÑÔÐH¥:¨d°;Ô.?ÅÑ#HÔ#Hðð ð ð ð ð r-   z%Predictions and references are empty.rn   )r_   rm   Úf1Úhash)r6   rÍ   )r6   Ú
tokens_idfrÍ   r6   )r6   rÚ   r­   )rÉ   Únum_workersr,   )'rª   r«   r¯   r×   rµ   rK   r½   Úboolr   ÚModuleNotFoundErrorr   r   r+   r&   r(   r*   r'   ÚevalÚtoÚhasattrÚconfigrÐ   ÚintÚAttributeErrorr±   Úupdaterw   rŸ   r   rÚ   r   r   r[   rÈ   ÚdatasetÚsorting_indicesrr   r¤   rÄ   )&r¥   r¦   rs   r4   r5   r2   rÇ   r8   r7   r6   r3   rÈ   rÉ   rÊ   rË   rš   rÌ   rx   rŒ   rÍ   r·   Ú	tokenizerÚ_are_empty_listsÚ_are_valid_listsÚ_are_valid_tensorsÚoutput_dictr�   Útarget_datasetÚpreds_datasetÚtarget_loaderÚpreds_loaderri   rk   rh   rj   r_   rm   rq   s&                                         r%   r/   r/   ]  sá  € ðN =AÐå�%�ÑÔð Ø�ˆÝ�&�#ÑÔð Ø�ˆÝ�e�d¥D˜\Ñ*Ô*ð Ý�U‘”ˆÝ�f�t¥T˜lÑ+Ô+ð Ý�f‘”ˆå
ˆ5�z„z•S˜‘[”[Ò Ð Ýð.Ý�5‰zŒzð.ð .Ý # F¡¤ð.ð .ñ
ô 
ð 	
õ
 �%�ÑÔð ]¥3 u¡:¤:°¢> >µjÀÍÑ6NÔ6N >ÕSVÐW]ÑS^ÔS^ÐabÒSbÐSbÝ.MÈeÐU[Ñ.\Ô.\Ñ+ˆˆvÐ+å�c�4Ñ Ô ð UÝÐSÈSÐSÐSÐSÑTÔTÐTàð 
�ð 
Ý!Øqñ
ô 
ð 	
ð €}Ý.ð 	Ý%ðlñô ð ð Ð%Ýð\åJXð\ð \ð \ñô ð õ
 !Ñ"Ô"ð 	Tð 	TÝ%Ô5Ð6HÐ6ZÍNÑ[Ô[ˆIÝÔ-Ð.@Ð.RÅNÑSÔSˆEð	Tð 	Tð 	Tñ 	Tô 	Tð 	Tð 	Tð 	Tð 	Tð 	Tð 	Tøøøð 	Tð 	Tð 	Tð 	Tøð #ˆ	Ø	‡J‚J�L„L€LØ	‡H‚HˆVÑÔÐðsÝ�5”<Ð!4Ñ5Ô5ð 
	½*ÀUÄ\ÔEcÕehÑ:iÔ:ið 
	Øð ˜j¨5¬<Ô+IÒIÐIÝ ðR *ð Rð RÐ@Rð Rð RØ16´Ô1OðRð Rñô ð øõ
 ð3ñô ð øøõ ð sð sð sÝÐqÑrÔrÐrÐrÐrðsøøøõ ÐaÐaÐRWÐY_ÐQ`ÐaÑaÔaÑaÔaÐÝð ð Ø[`ÐbhÐZiðñ ô ñ ô Ðõ ð ð ØV[Ð]cÐUdðñ ô ñ ô Ðð ð 	ÝÐ>Ñ?Ô?Ð?à˜Ø�eØ�%ðC
ð C
ˆð
 ð 	YØ×Ò ­	Ð2DÀjÐRUÑ(VÔ(VÐWÑXÔXÐXØÐð YnÐw�~˜dÐ$6¸À|ÑTÔTÐTÐsw€Hð ð 4Ý$ V¨Y¸
ÈÐXbÐcÑcÔcˆÝ#ØØØØØ%Ô0Ø!ð
ñ 
ô 
ˆˆð 
ð 4Ý)Ð<Ð<¨FÐ<Ð<¸Ð<Ð<Ð<ˆÝ(Ð`Ð`¨5Ð`°cÀnÔF_Ð`Ð`Ð`Ð`ˆˆåÐ2Ñ3Ô3Ð3å˜~¸*ÐR]Ð^Ñ^Ô^€MÝ˜m¸
ÐP[Ð\Ñ\Ô\€Lå*GØ�~Ô0°%¸ÀÈZÐY\Ð^eÐgvñ+ô +Ñ'ÐÐ'õ )FØ�mÔ.°°v¸zÈ:ÐWZÐ\cÐetñ)ô )Ñ%Ð�oð (¨Ô(<Ô(LÔMÐØ)¨-Ô*?Ô*OÔPÐà% lÔ&:Ô&JÔK€OØ'¨Ô(=Ô(MÔNÐå":ØÐ+¨_Ð>Nñ#ô #Ñ€Iˆv�xð ÐÝ&DØ�v˜x¨°:¸zñ'
ô '
Ñ#ˆ	�6˜8ð Ð'Ý&FØ�v˜xÐ)=ñ'
ô '
Ñ#ˆ	�6˜8ð
 ØØðð €Kð
 ð UØ×Ò˜F¥IÐ.@À*ÈcÑ$RÔ$RÐSÑTÔTÐTØÐs%   Æ<7G?Ç?HÈHÈ7A8J0 Ê0KËK)r   N)NNFFFN)NNF)r™   NNN)NF)NNFNNNFFNrÅ   rÆ   r   Fr™   FNNF)>r„   r   r”   Úcollections.abcr   r   Ú
contextlibr   Útypingr   r   r   r	   r
   r   r   rH   r   Útorch.nnr   Útorch.utils.datar   Ú4torchmetrics.functional.text.helper_embedding_metricr   r   r   r   r   r   r   Útorchmetrics.utilitiesr   Útorchmetrics.utilities.checksr   r   Útorchmetrics.utilities.importsr   r   r&   r+   Útransformersr'   r(   r.   Ú__doctest_skip__râ   r«   r3   rÜ   r×   r[   rg   rr   rw   r‹   r˜   rŸ   r¤   r½   rÄ   r}   r/   r,   r-   r%   ú<module>rû      sZ  ðð €
€
€
Ø €€€Ø €€€Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ø %Ð %Ð %Ð %Ð %Ð %Ø DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ Dà €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð 2Ð 1Ð 1Ð 1Ð 1Ð 1Ø WÐ WÐ WÐ WÐ WÐ WÐ WÐ WØ [Ð [Ð [Ð [Ð [Ð [Ð [Ð [ð ð(˜X dœ^ð (ð (ð (ñ „ð(ð !€à"ð &Ø5Ð5Ð5Ð5Ð5Ð5Ð5Ð5ð6ð 6ð 6ð 6ð ð *Ð";Ð";Ð<ZÑ"[Ô"[ð *Ø(˜>Ðøà$�~Ðð 26Ø $ØØØØOSðN!ð N!ØðN!àðN!ð ðN!ð �U˜3 ¤Ð,Ô-Ô.ð	N!ð
 ˜”ðN!ð ðN!ð 
ðN!ð ðN!ð ˜h¨°°S¸&°[Ô0AÐ'BÀFÐ'JÔKÔLðN!ð ˆ6�6ˆ>ÔðN!ð N!ð N!ð N!ðb)¨Vð )¸Sð )ÈVð )ÐX^ð )ð )ð )ð )ð'Øð'Ø17ð'ØJPð'Ødjð'à
ˆ6�6˜6Ð!Ô"ð'ð 'ð 'ð 'ð6Oð O (¨3¤-ð OÀHÈSÄMð OÐ_cð OÐpsð Oð Oð Oð Oð
	.¨Sð 	.°Vð 	.ð 	.ð 	.ð 	.ð2 Sð 2¨Vð 2ð 2ð 2ð 2ð  Ø(,Ø#'Ø"&ð	ð Ø
ðà  œðð ˜C”=ðð ˜3”-ð	ð
 ˆfÔðð ð ð ð8 !%ØðIð IØðIàðIð ðIð ð	Ið
 ˜”ðIð ðIð ˆ6�6˜6Ð!Ô"ðIð Ið Ið Ið$+0Ø�Œ9ð+0Ø" 5¨¨h°s¬mÐ);Ô#<Ô=ð+0à
ˆ4�Œ9�d˜3”i ¨$¨u°S¸#°X¬Ô*?Ô!@Ð@ÔAð+0ð +0ð +0ð +0ð\('Øð('Ø%ð('Ø17ð('ØOSÐTYÐZ]Ð_bÐZbÔTcÔOdð('à
ˆ6�6˜6Ð!Ô"ð('ð ('ð ('ð ('ð\ )-Ø $ØØ"ØØOSØØØ15ØØØØØØ"'Ø#'Ø"&Øð)qð qØ��h˜s”m T¨#¨v¨+Ô%6Ð6Ô7ðqà�#�x ”} h¨x¸¬}Ô&=¸tÀCÈÀKÔ?PÐPÔQðqð ! œðqð ˜”ð	qð
 ðqð �FÔðqð ðqð ˜h¨°°S¸&°[Ô0AÐ'BÀFÐ'JÔKÔLðqð ðqð 
ðqð �U˜3 ¤Ð,Ô-Ô.ðqð ðqð ðqð ðqð ðqð  ð!qð"  ð#qð$ ˜C”=ð%qð& ˜3”-ð'qð( ð)qð* 
ˆ#ˆu�V˜T %œ[¨#Ð-Ô.Ð
.Ô/ð+qð qð qð qð qð qr-   