§
    ’©ºjÅ*  ã                   óà   — d dl Z 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mZmZmZmZmZ d dl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esddgZ G d„ de¦  «        ZdS )é    N)ÚSequence)ÚAnyÚClassVarÚListÚOptionalÚUnion)ÚTensor)Ú_load_tokenizer_and_model)Ú$_ALLOWED_INFORMATION_MEASURE_LITERALÚ_get_dataloaderÚ_get_special_tokens_mapÚ_infolm_computeÚ_infolm_updateÚ_InformationMeasure)ÚMetric)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLEÚ_TRANSFORMERS_GREATER_EQUAL_4_4)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzInfoLM.plotÚInfoLMc                   óJ  ‡ — e Zd ZU dZdZee         ed<   ee         ed<   ee         ed<   ee         ed<   ddddddddddœ	Ze	ed	<   	 	 	 	 	 	 	 	 	 	 	 	 d'de
eej        f         dedededee         dee         dee
eej        f                  dee         dededededeeef         ddfˆ fd„Zedefd„¦   «         Zd e
eee         f         d!e
eee         f         ddfd"„Zde
eeeef         f         fd#„Z	 d(d$ee
eee         f                  d%ee         defd&„Z ˆ xZ!S ))r   uÆ  Calculate `InfoLM`_.

    InfoLM measures a distance/divergence between predicted and reference sentence discrete distribution using one of
    the following information measures:

        - `KL divergence`_
        - `alpha divergence`_
        - `beta divergence`_
        - `AB divergence`_
        - `RÃ©nyi divergence`_
        - L1 distance
        - L2 distance
        - L-infinity distance
        - `Fisher-Rao distance`_

    `InfoLM`_ is a family of untrained embedding-based metrics which addresses some famous flaws of standard
    string-based metrics thanks to the usage of pre-trained masked language models. This family of metrics is mainly
    designed for summarization and data-to-text tasks.

    The implementation of this metric is fully based HuggingFace ``transformers``' package.

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

    - ``preds`` (:class:`~Sequence`): An iterable of hypothesis corpus
    - ``target`` (:class:`~Sequence`): An iterable of reference corpus

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

    -  ``infolm`` (:class:`~torch.Tensor`): If `return_sentence_level_score=True` return a tuple with a tensor
       with the corpus-level InfoLM score and a list of sentence-level InfoLM scores, else return a corpus-level
       InfoLM score

    Args:
        model_name_or_path:
            A name or a model path used to load ``transformers`` pretrained model.
            By default the `"bert-base-uncased"` model is used.
        temperature:
            A temperature for calibrating language modelling. For more information, please reference `InfoLM`_ paper.
        information_measure:
            A name of information measure to be used. Please use one of: ['kl_divergence', 'alpha_divergence',
            'beta_divergence', 'ab_divergence', 'renyi_divergence', 'l1_distance', 'l2_distance', 'l_infinity_distance',
            'fisher_rao_distance']
        idf:
            An indication of whether normalization using inverse document frequencies should be used.
        alpha:
            Alpha parameter of the divergence used for alpha, AB and RÃ©nyi divergence measures.
        beta:
            Beta parameter of the divergence used for beta and AB divergence measures.
        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.
        verbose:
            An indication of whether a progress bar to be displayed during the embeddings calculation.
        return_sentence_level_score:
            An indication whether a sentence-level InfoLM score to be returned.

    Example:
        >>> from torchmetrics.text.infolm import InfoLM
        >>> preds = ['he read the book because he was interested in world history']
        >>> target = ['he was interested in world history because he read the book']
        >>> infolm = InfoLM('google/bert_uncased_L-2_H-128_A-2', idf=False)
        >>> infolm(preds, target)
        tensor(-0.1784)

    FÚpreds_input_idsÚpreds_attention_maskÚtarget_input_idsÚtarget_attention_maskT)	Úkl_divergenceÚalpha_divergenceÚbeta_divergenceÚab_divergenceÚrenyi_divergenceÚl1_distanceÚl2_distanceÚl_infinity_distanceÚfisher_rao_distanceÚ%_information_measure_higher_is_betterúbert-base-uncasedç      Ð?r   Né@   r   Úmodel_name_or_pathÚtemperatureÚinformation_measureÚidfÚalphaÚbetaÚdeviceÚ
max_lengthÚ
batch_sizeÚnum_threadsÚverboseÚreturn_sentence_level_scoreÚkwargsÚreturnc                 ó„  •—  t          ¦   «         j        di |¤Ž || _        || _        || _        || _        || _        || _        t          j	        |pd¦  «        | _
        |	| _        |
| _        || _        || _        t          ||¦  «        \  | _        | _        t%          |||¦  «        | _        |p| j        j        j        | _        t-          | j        ¦  «        | _        |                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         d S )	NÚcpur   Úcat)Údist_reduce_fxr   r   r   © )ÚsuperÚ__init__r*   r+   r,   r-   r.   r/   Útorchr0   Ú_devicer2   r3   r4   r5   r
   Ú	tokenizerÚmodelr   Úinformation_measure_clsÚconfigr1   r   Úspecial_tokens_mapÚ	add_state)Úselfr*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   Ú	__class__s                 €úh/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/text/infolm.pyr>   zInfoLM.__init__†   sC  ø€ ð  	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ø"4ˆÔØ&ˆÔØ#6ˆÔ ØˆŒØˆŒ
ØˆŒ	Ý”| F O¨eÑ4Ô4ˆŒØ$ˆŒØ&ˆÔØˆŒØ+FˆÔ(å%>Ð?QÐSYÑ%ZÔ%ZÑ"ˆŒ˜œ
Ý':Ð;NÐPUÐW[Ñ'\Ô'\ˆÔ$Ø$ÐD¨¬
Ô(9Ô(DˆŒÝ"9¸$¼.Ñ"IÔ"IˆÔà�ŠÐ(¨"¸UˆÑCÔCÐCØ�ŠÐ-¨rÀ%ˆÑHÔHÐHØ�ŠÐ)¨2¸eˆÑDÔDÐDØ�ŠÐ.°À5ˆÑIÔIÐIÐIÐIó    c                 ó&   — | j         | j                 S )z¹Returns a bool indicating whether a higher value of the information measure is better.

        Done this way as depends on if the information measure is positive or negative.

        )r&   r,   )rG   s    rI   Úhigher_is_betterzInfoLM.higher_is_better­   s   € ð Ô9¸$Ô:RÔSÐSrJ   ÚpredsÚtargetc                 ó  — t          ||| j        | j        ¦  «        \  }}}}| j                             |¦  «         | j                             |¦  «         | j                             |¦  «         | j                             |¦  «         dS )z*Update state with predictions and targets.N)r   rA   r1   r   Úappendr   r   r   )rG   rM   rN   r   r   r   r   s          rI   ÚupdatezInfoLM.update¶   s“   € åYgØ�6˜4œ>¨4¬?ñZ
ô Z
ÑVˆÐ-Ð/?ÐAVð 	Ô×#Ò# OÑ4Ô4Ð4ØÔ!×(Ò(Ð)=Ñ>Ô>Ð>ØÔ×$Ò$Ð%5Ñ6Ô6Ð6ØÔ"×)Ò)Ð*?Ñ@Ô@Ð@Ð@Ð@rJ   c           
      óè  — t          t          | j        ¦  «        t          | j        ¦  «        | j        | j        | j        ¬¦  «        }t          t          | j        ¦  «        t          | j        ¦  «        | j        | j        | j        ¬¦  «        }t          | j
        ||| j        | j        | j        | j        | j        ¦  «        }| j        r|                     ¦   «         |fS |                     ¦   «         S )zLCalculate selected information measure using the pre-trained language model.)Ú	input_idsÚattention_maskr-   r2   Únum_workers)r   r   r   r   r-   r2   r3   r   r   r   rB   r+   rC   rE   r4   r5   Úmean)rG   Úpreds_dataloaderÚtarget_dataloaderÚinfo_lm_scores       rI   ÚcomputezInfoLM.computeÀ   sô   € å*Ý" 4Ô#7Ñ8Ô8Ý'¨Ô(AÑBÔBØ”Ø”ØÔ(ð
ñ 
ô 
Ðõ ,Ý" 4Ô#8Ñ9Ô9Ý'¨Ô(BÑCÔCØ”Ø”ØÔ(ð
ñ 
ô 
Ðõ (ØŒJØØØÔØŒHØÔ(ØÔ#ØŒLñ	
ô 	
ˆð Ô+ð 	7Ø ×%Ò%Ñ'Ô'¨Ð6Ð6à×!Ò!Ñ#Ô#Ð#rJ   ÚvalÚaxc                 ó.   — |                       ||¦  «        S )a1  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.infolm import InfoLM
            >>> metric = InfoLM('google/bert_uncased_L-2_H-128_A-2', idf=False)
            >>> preds = ['he read the book because he was interested in world history']
            >>> target = ['he was interested in world history because he read the book']
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> from torchmetrics.text.infolm import InfoLM
            >>> metric = InfoLM('google/bert_uncased_L-2_H-128_A-2', idf=False)
            >>> preds = ["this is the prediction", "there is an other sample"]
            >>> target = ["this is the reference", "there is another one"]
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(preds, target))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)rG   r[   r\   s      rI   ÚplotzInfoLM.plotá   s   € ðT �zŠz˜#˜rÑ"Ô"Ð"rJ   )r'   r(   r   TNNNNr)   r   TF)NN)"Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úis_differentiabler   r	   Ú__annotations__r&   r   r   ÚstrÚosÚPathLikeÚfloatr   Úboolr   r?   r0   ÚintÚdictr   r>   ÚpropertyrL   r   rQ   ÚtuplerZ   r   r   r_   Ú__classcell__)rH   s   @rI   r   r   *   sÈ  ø€ € € € € € ðEð EðN ÐØ˜&”\Ð!Ð!Ñ!Ø˜vœ,Ð&Ð&Ñ&Ø˜6”lÐ"Ð"Ñ"Ø œ<Ð'Ð'Ñ'ð Ø à ØØ!ØØØ$Ø$ð7ð 7Ð)¨8ð ð ñ ð  7JØ!ØDSØØ!%Ø $Ø59Ø$(ØØØØ,1ð%Jð %Jà! # r¤{Ð"2Ô3ð%Jð ð%Jð Bð	%Jð
 ð%Jð ˜Œð%Jð �uŒoð%Jð ˜˜s E¤LÐ0Ô1Ô2ð%Jð ˜S”Mð%Jð ð%Jð ð%Jð ð%Jð &*ð%Jð �s˜C�x”.ð%Jð 
ð%Jð %Jð %Jð %Jð %Jð %JðN ðT $ð Tð Tð Tñ „XðTðA˜E # x°¤}Ð"4Ô5ð A¸uÀSÈ(ÐSVÌ-ÐEWÔ?Xð AÐ]að Að Að Að Að$˜˜v u¨V°V¨^Ô'<Ð<Ô=ð $ð $ð $ð $ðD _cð*#ð *#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð*#ØIQÐRZÔI[ð*#à	ð*#ð *#ð *#ð *#ð *#ð *#ð *#ð *#rJ   ) rg   Úcollections.abcr   Útypingr   r   r   r   r   r?   r	   Ú4torchmetrics.functional.text.helper_embedding_metricr
   Ú#torchmetrics.functional.text.infolmr   r   r   r   r   r   Útorchmetrics.metricr   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   r   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r<   rJ   rI   ú<module>ry      s€  ðð 
€	€	€	Ø $Ð $Ð $Ð $Ð $Ð $Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7à €€€Ø Ð Ð Ð Ð Ð à ZÐ ZÐ ZÐ ZÐ ZÐ Zðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð 'Ð &Ð &Ð &Ð &Ð &Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø aÐ aÐ aÐ aÐ aÐ aÐ aÐ aØ @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð 'Ø%�Ðà&ð 1Ø  -Ð0Ðða#ð a#ð a#ð a#ð a#ˆVñ a#ô a#ð a#ð a#ð a#rJ   