§
    ’©ºj¸1  ã                   óª  — 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	 d dl
mZmZmZmZ d dlmZ d dlZd dlmZ d dlmZ  eej                             d	d
¦  «        ¦  «        Z eej                             dd ¦  «        ¦  «        Zdededefd„Zdededdfd„Z	 d,dedededeeef         fd„Z	 	 d-dededededee         deeeef         fd„Z	 d,dedededeeef         fd„Z d.dedede!defd„Z"eddg d¢dfded ee#e$ef                  d!ee#e$ef                  d"ee         d#eddfd$„¦   «         Z%d%e$d&e&d'e&defd(„Z'efd)ed*edefd+„Z(dS )/é    N)ÚMappingÚSequence)Úpartial)Úperf_counter)ÚAnyÚCallableÚOptionalÚno_type_check)ÚMock)ÚTensor)ÚMetricÚDOCTEST_DOWNLOAD_TIMEOUTéx   ÚSKIP_SLOW_DOCTESTÚpredsÚtargetÚreturnc                 ój   — |                       ¦   «         |                      ¦   «         cxk    odk    nc S )Nr   )Únumel©r   r   s     úm/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/utilities/checks.pyÚ_check_for_empty_tensorsr       s4   € Ø�;Š;‰=Œ=˜FŸLšL™NœNÐ/Ð/Ò/Ð/¨aÒ/Ð/Ð/Ð/Ð/ó    c                 óf   — | j         |j         k    r t          d| j         › d|j         › d�¦  «        ‚dS )zHCheck that predictions and target have the same shape, else raise error.zEPredictions and targets are expected to have the same shape, but got z and ú.N)ÚshapeÚRuntimeErrorr   s     r   Ú_check_same_shaper   $   sI   € à„{�f”lÒ"Ð"ÝØuÐTYÔT_ÐuÐuÐflÔfrÐuÐuÐuñ
ô 
ð 	
ð #Ð"r   FÚallow_non_binary_targetc                 óÒ   — | j         |j         k    rt          d¦  «        ‚|                      ¦   «         r|                      ¦   «         st          d¦  «        ‚t	          | ||¬¦  «        S )a9  Check ``preds`` and ``target`` tensors are of the same shape and of the correct data type.

    Args:
        preds: either tensor with scores/logits
        target: tensor with ground true labels
        allow_non_binary_target: whether to allow target to contain non-binary values

    Raises:
        ValueError:
            If ``preds`` and ``target`` don't have the same shape, if they are empty
            or not of the correct ``dtypes``.

    Returns:
        preds: as torch.float32
        target: as torch.long if not floating point else torch.float32

    z.`preds` and `target` must be of the same shapez=`preds` and `target` must be non-empty and non-scalar tensors©r   )r   Ú
ValueErrorr   ÚsizeÚ,_check_retrieval_target_and_prediction_types©r   r   r   s      r   Ú"_check_retrieval_functional_inputsr&   ,   sh   € ð, „{�f”lÒ"Ð"ÝÐIÑJÔJÐJà�;Š;‰=Œ=ð Z §
¢
¡¤ð ZÝÐXÑYÔYÐYå7¸¸vÐ_vÐwÑwÔwÐwr   ÚindexesÚignore_indexc                 óÎ  — | j         |j         k    s|j         |j         k    rt          d¦  «        ‚| j        t          j        urt          d¦  «        ‚|�||k    }| |         ||         ||         }}} |                      ¦   «         r|                      ¦   «         st          d¦  «        ‚t          |||¬¦  «        \  }}|                      ¦   «                              ¦   «         ||fS )a²  Check ``indexes``, ``preds`` and ``target`` tensors are of the same shape and of the correct data type.

    Args:
        indexes: tensor with queries indexes
        preds: tensor with scores/logits
        target: tensor with ground true labels
        allow_non_binary_target: whether to allow target to contain non-binary values
        ignore_index: ignore predictions where targets are equal to this number

    Raises:
        ValueError:
            If ``preds`` and ``target`` don't have the same shape, if they are empty or not of the correct ``dtypes``.

    Returns:
        indexes: as ``torch.long``
        preds: as ``torch.float32``
        target: as ``torch.long``

    z9`indexes`, `preds` and `target` must be of the same shapez+`indexes` must be a tensor of long integersNzH`indexes`, `preds` and `target` must be non-empty and non-scalar tensorsr!   )	r   r"   ÚdtypeÚtorchÚlongr   r#   r$   Úflatten)r'   r   r   r   r(   Úvalid_positionss         r   Ú_check_retrieval_inputsr/   K   sø   € ð4 „}˜œÒ#Ð# u¤{°f´lÒ'BÐ'BÝÐTÑUÔUÐUà„}�EœJÐ&Ð&ÝÐFÑGÔGÐGð ÐØ  LÒ0ˆØ!(¨Ô!9¸5ÀÔ;QÐSYÐZiÔSj˜�ˆà�=Š=‰?Œ?ð 
 '§,¢,¡.¤.ð 
ÝØVñ
ô 
ð 	
õ AØˆvÐ/Fðñ ô �M€Eˆ6ð �<Š<‰>Œ>×!Ò!Ñ#Ô# U¨FÐ2Ð2r   c                 óT  — |j         t          j        t          j        t          j        fvr#t          j        |¦  «        st          d¦  «        ‚|                      ¦   «         st          d¦  «        ‚|s?|                     ¦   «         dk    s|                     ¦   «         dk     rt          d¦  «        ‚|                     ¦   «         r| 	                    ¦   «         n|                     ¦   «         }|  	                    ¦   «         } |  
                    ¦   «         | 
                    ¦   «         fS )a¸  Check ``preds`` and ``target`` tensors are of the same shape and of the correct data type.

    Args:
        preds: either tensor with scores/logits
        target: tensor with ground true labels
        allow_non_binary_target: whether to allow target to contain non-binary values

    Raises:
        ValueError:
            If ``preds`` and ``target`` don't have the same shape, if they are empty or not of the correct ``dtypes``.

    z9`target` must be a tensor of booleans, integers or floatsz"`preds` must be a tensor of floatsé   r   z%`target` must contain `binary` values)r*   r+   Úboolr,   ÚintÚis_floating_pointr"   ÚmaxÚminÚfloatr-   r%   s      r   r$   r$   |   sñ   € ð" „|�EœJ­¬
µE´IÐ>Ð>Ð>ÅuÔG^Ð_eÑGfÔGfÐ>ÝÐTÑUÔUÐUà×"Ò"Ñ$Ô$ð ?ÝÐ=Ñ>Ô>Ð>à"ð B¨¯
ª
©¬°qÒ(8Ð(8¸F¿JºJ¹L¼LÈ1Ò<LÐ<LÝÐ@ÑAÔAÐAà%×7Ò7Ñ9Ô9ÐLˆV�\Š\‰^Œ^ˆ^¸v¿{º{¹}¼}€FØ�KŠK‰MŒM€Eà�=Š=‰?Œ?˜FŸNšNÑ,Ô,Ð,Ð,r   ç�íµ ÷Æ°>Úres1Úres2Úatolc                 óz  ‡ ‡— t          ‰ t          ¦  «        rt          j        ‰ ‰|¬¦  «        S t          ‰ t          ¦  «        r‰ ‰k    S t          ‰ t
          ¦  «        r't          d„ t          ‰ ‰¦  «        D ¦   «         ¦  «        S t          ‰ t          ¦  «        rt          ˆ ˆfd„‰ D ¦   «         ¦  «        S ‰ ‰k    S )zFRecursively asserting that two results are within a certain tolerance.)r;   c              3   ó<   K  — | ]\  }}t          ||¦  «        V — Œd S ©N©Ú_allclose_recursive)Ú.0Úr1Úr2s      r   ú	<genexpr>z&_allclose_recursive.<locals>.<genexpr>¤   s1   è è € ÐMÐM±6°2°rÕ& r¨2Ñ.Ô.ÐMÐMÐMÐMÐMÐMr   c              3   óP   •K  — | ] }t          ‰|         ‰|         ¦  «        V — Œ!d S r>   r?   )rA   Úkr9   r:   s     €€r   rD   z&_allclose_recursive.<locals>.<genexpr>¦   s6   øè è € ÐGÐG¸QÕ& t¨A¤w°°Q´Ñ8Ô8ÐGÐGÐGÐGÐGÐGr   )	Ú
isinstancer   r+   ÚallcloseÚstrr   ÚallÚzipr   )r9   r:   r;   s   `` r   r@   r@   œ   sÆ   øø€ õ �$�ÑÔð 5ÝŒ~˜d D¨tÐ4Ñ4Ô4Ð4Ý�$�ÑÔð Ø�tŠ|ÐÝ�$�Ñ!Ô!ð NÝÐMÐM½SÀÀt¹_¼_ÐMÑMÔMÑMÔMÐMÝ�$�Ñ Ô ð HÝÐGÐGÐGÐGÐGÀ$ÐGÑGÔGÑGÔGÐGØ�4Š<Ðr   )é
   éd   iè  é   Úmetric_classÚ	init_argsÚ
input_argsÚnum_update_to_compareÚrepsc                 óø  — |pi }|pi } G d„ d| ¦  «        } G d„ d| ¦  «        } |di |¤Ž} |di |¤Ž}d}		 t          |d         ¦  «        D ]!}
|	t           |di |¤Ž |di |¤Ž¦  «        z  }	Œ"n# t          $ r d}	Y nw xY w|                     ¦   «         }	 |                     ¦   «         }n# t          $ r d}	Y nw xY w|	t          ||¦  «        z  }	|	st	          d¦  «         d	S t          j        d
t          |¦  «        |¦  «        }t          ||g¦  «        D ]�\  }}t          |¦  «        D ]l\  }}t          |¦  «        D ]W}t          ¦   «         }t          |¦  «        D ]
}
 |di |¤Ž}
Œt          ¦   «         }||z
  ||||f<   | 
                    ¦   «          ŒXŒmŒ‚t          j        |d¦  «        }t          j        |d¦  «        }t          t          |¦  «        ¦  «        D ]a}t	          d||         › d|d|f         › d|d|f         d›�¦  «         t	          d||         › d|d|f         d›d|d|f         d›�¦  «         Œb|d         |d         k                          ¦   «         }t	          d| › d�¦  «         d	S )aÝ  Check if the new ``full_state_update`` property works as intended.

    This function checks if the property can safely be set to ``False`` which will for most metrics results in a
    speedup when using ``forward``.

    Args:
        metric_class: metric class object that should be checked
        init_args: dict containing arguments for initializing the metric class
        input_args: dict containing arguments to pass to ``forward``
        num_update_to_compare: if we successfully detect that the flag is safe to set to ``False``
            we will run some speedup test. This arg should be a list of integers for how many
            steps to compare over.
        reps: number of repetitions of speedup test

    Example (states in ``update`` are independent, save to set ``full_state_update=False``)
        >>> from torchmetrics.classification import MulticlassConfusionMatrix
        >>> check_forward_full_state_property(  # doctest: +SKIP
        ...     MulticlassConfusionMatrix,
        ...     init_args = {'num_classes': 3},
        ...     input_args = {'preds': torch.randint(3, (100,)), 'target': torch.randint(3, (100,))},
        ... )
        Full state for 10 steps took: ...
        Partial state for 10 steps took: ...
        Full state for 100 steps took: ...
        Partial state for 100 steps took: ...
        Full state for 1000 steps took: ...
        Partial state for 1000 steps took: ...
        Recommended setting `full_state_update=False`

    Example (states in ``update`` are dependent meaning that ``full_state_update=True``):
        >>> from torchmetrics.classification import MulticlassConfusionMatrix
        >>> class MyMetric(MulticlassConfusionMatrix):
        ...     def update(self, preds, target):
        ...         super().update(preds, target)
        ...         # by construction make future states dependent on prior states
        ...         if self.confmat.sum() > 20:
        ...             self.reset()
        >>> check_forward_full_state_property(
        ...     MyMetric,
        ...     init_args = {'num_classes': 3},
        ...     input_args = {'preds': torch.randint(3, (10,)), 'target': torch.randint(3, (10,))},
        ... )
        Recommended setting `full_state_update=True`

    c                   ó   — e Zd ZdZdS )ú4check_forward_full_state_property.<locals>.FullStateTN©Ú__name__Ú
__module__Ú__qualname__Úfull_state_update© r   r   Ú	FullStaterV   â   s   € € € € € Ø ÐÐÐr   r]   c                   ó   — e Zd ZdZdS )ú4check_forward_full_state_property.<locals>.PartStateFNrW   r\   r   r   Ú	PartStater_   å   s   € € € € € Ø!ÐÐÐr   r`   Tr   Fz,Recommended setting `full_state_update=True`Né   éÿÿÿÿzFull state for z steps took: z+-z0.3fzPartial state for r1   )r1   rb   )r   rb   z'Recommended setting `full_state_update=ú`r\   )Úranger@   r   ÚcomputeÚprintr+   ÚzerosÚlenÚ	enumerater   ÚresetÚmeanÚstdÚitem)rO   rP   rQ   rR   rS   r]   r`   Ú	fullstateÚ	partstateÚequalÚ_r9   r:   ÚresÚiÚmetricÚjÚtÚrÚstartÚendrk   rl   Úfasters                           r   Ú!check_forward_full_state_propertyr{   ª   s�  € ðj �˜R€IØÐ!˜r€Jð!ð !ð !ð !ð !�Lñ !ô !ð !ð"ð "ð "ð "ð "�Lñ "ô "ð "ð �	Ð&Ð&˜IÐ&Ð&€IØ�	Ð&Ð&˜IÐ&Ð&€Ià€EðÝÐ,¨QÔ/Ñ0Ô0ð 	bð 	bˆAØÕ/°	°	Ð0GÐ0G¸JÐ0GÐ0GÈÈÐI`ÐI`ÐU_ÐI`ÐI`ÑaÔaÑaˆEˆEð	bøåð ð ð Øˆˆˆðøøøà×ÒÑÔ€DðØ× Ò Ñ"Ô"ˆˆøÝð ð ð ØˆˆˆðøøøàÕ'¨¨dÑ3Ô3Ñ3€Eàð ÝÐ<Ñ=Ô=Ð=Øˆõ Œ+�a�Ð2Ñ3Ô3°TÑ
:Ô
:€CÝ 	¨9Ð5Ñ6Ô6ð ð ‰	ˆˆ6ÝÐ3Ñ4Ô4ð 	ð 	‰DˆAˆqÝ˜4‘[”[ð ð �Ý$™œ�Ý˜q™œð -ð -�AØ˜Ð,Ð, Ð,Ð,�A�AÝ"‘n”n�Ø" U™{��A�q˜!�G‘Ø—’‘”��ðð	õ Œ:�c˜2ÑÔ€DÝ
Œ)�C˜Ñ
Ô
€Cå•3Ð,Ñ-Ô-Ñ.Ô.ð oð oˆÝÐeÐ 5°aÔ 8ÐeÐeÀtÈAÈqÈDÄzÐeÐeÐUXÐYZÐ\]ÐY]ÔU^ÐeÐeÐeÑfÔfÐfÝÐmÐ#8¸Ô#;ÐmÐmÈ$ÈqÐRSÈtÌ*ÐmÐmÐmÐ]`ÐabÐdeÐaeÔ]fÐmÐmÐmÑnÔnÐnÐnà�5Œk˜D œKÒ'×-Ò-Ñ/Ô/€FÝ	Ð
A¸°JÐ
AÐ
AÐ
AÑBÔBÐBØ
€Fs#   ¸7A0 Á0A?Á>A?ÂB, Â,B;Â:B;Úmethod_nameÚinstanceÚparentc                 ó8  — t          || d¦  «        }|€dS t          |d¦  «        r|j        }t          |t          ¦  «        r|j        }nt          |t          ¦  «        r|j        }|€dS t          || d¦  «        }|€t          d¦  «        ‚|j	        |j	        k    S )zRCheck if a method has been overridden by an instance compared to its parent class.NFÚ__wrapped__z#The parent should define the method)
ÚgetattrÚhasattrr€   rG   r   Ú_mock_wrapsr   Úfuncr"   Ú__code__)r|   r}   r~   Úinstance_attrÚparent_attrs        r   Úis_overriddenrˆ     s°   € å˜H k°4Ñ8Ô8€MØÐØˆuåˆ}˜mÑ,Ô,ð 2Ø%Ô1ˆå�-¥Ñ&Ô&ð +à%Ô1ˆˆå	�M¥7Ñ	+Ô	+ð +Ø%Ô*ˆØÐØˆuå˜& +¨tÑ4Ô4€KØÐÝÐ>Ñ?Ô?Ð?àÔ! [Ô%9Ò9Ð9r   ÚfnÚtimeoutc                 ór  — t          j        | ¬¦  «        }t          d| j        › d|› d�t          j        ¬¦  «         |                     ¦   «          |                     |¦  «         |                     ¦   «         sdS t          d| j        › d|› d	�t          j        ¬¦  «         | 	                    ¦   «          d
S )aY  Check if a certain function is taking too long to execute.

    Function will only be executed if running inside a doctest context. Currently, does not support Windows.

    Args:
        fn: function to check
        timeout: timeout for function

    Returns:
        Bool indicating if the function finished within the specified timeout

    )r   zTrying to run `z` for zs...)ÚfileTrc   z` did not complete with z%, killing process and returning FalseF)
ÚmultiprocessingÚProcessrf   rX   ÚsysÚstderrrx   ÚjoinÚis_aliveÚkill)r‰   rŠ   Úprocs      r   Ú_try_proceed_with_timeoutr•   -  s·   € õ Ô"¨"Ð-Ñ-Ô-€Då	Ð
<˜BœKÐ
<Ð
<¨wÐ
<Ð
<Ð
<Å3Ä:ÐNÑNÔNÐNØ‡J‚J�L„L€Là‡I‚IˆgÑÔÐà�=Š=‰?Œ?ð Øˆtå	Ð
aˆbŒkÐ
aÐ
a°7Ð
aÐ
aÐ
aÕhkÔhrÐsÑsÔsÐsð
 	‡I‚I�K„K€KØˆ5r   )F)FN)r8   ))r�   Úosr�   Úcollections.abcr   r   Ú	functoolsr   Útimer   Útypingr   r   r	   r
   Úunittest.mockr   r+   r   Útorchmetrics.metricr   r3   ÚenvironÚgetÚ_DOCTEST_DOWNLOAD_TIMEOUTr2   Ú_SKIP_SLOW_DOCTESTr   r   Útupler&   r/   r$   r7   r@   ÚdictrI   r{   Úobjectrˆ   r•   r\   r   r   ú<module>r¤      s   ðð Ð Ð Ð Ø 	€	€	€	Ø 
€
€
€
Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð à &Ð &Ð &Ð &Ð &Ð &à˜C ¤
§¢Ð/IÈ3Ñ OÔ OÑPÔPÐ Ø�T˜"œ*Ÿ.š.Ð)<¸aÑ@Ô@ÑAÔAÐ ð0 Fð 0°Fð 0¸tð 0ð 0ð 0ð 0ð
˜Vð 
¨Vð 
¸ð 
ð 
ð 
ð 
ð %*ðxð xØðxàðxð "ðxð ˆ6�6ˆ>Ôð	xð xð xð xðF %*Ø"&ð.3ð .3Øð.3àð.3ð ð.3ð "ð	.3ð
 ˜3”-ð.3ð ˆ6�6˜6Ð!Ô"ð.3ð .3ð .3ð .3ðh %*ð-ð -Øð-àð-ð "ð-ð ˆ6�6ˆ>Ôð	-ð -ð -ð -ð@ð ˜cð ¨ð °Eð ÀTð ð ð ð ð ð +/Ø+/Ø+:¨?¨?Øðfð fØðfà˜˜S #˜XœÔ'ðfð ˜˜c 3˜hœÔ(ðfð $ Cœ=ð	fð
 ðfð 
ðfð fð fñ „ðfðR:˜sð :¨fð :¸fð :Èð :ð :ð :ð :ð2 <Uð ð  (ð °Sð ÐY]ð ð ð ð ð ð r   