Ë
    ýÿæiU2  ã                   ód  — 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,                  j/                  d	d
«      «      Z eej,                  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(y)-é    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                 óZ   — | j                  «       |j                  «       cxk(  xr dk(  S c S )Nr   )Únumel©r   r   s     úr/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/utilities/checks.pyÚ_check_for_empty_tensorsr       s$   € Ø�;‰;‹=˜FŸL™L›NÖ/¨aÑ/Ð/Ñ/Ð/ó    c                 ó‚   — | j                   |j                   k7  r&t        d| j                   › d|j                   › d�«      ‚y)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   $   sH   € à‡{�{�f—l‘lÒ"ÜØSÐTY×T_ÑT_ÐS`Ð`eÐfl×frÑfrÐesÐstÐuó
ð 	
ð #r   Úallow_non_binary_targetc                 ó¼   — | j                   |j                   k7  rt        d«      ‚| j                  «       r| j                  «       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&   ,   sM   € ð, ‡{�{�f—l‘lÒ"ÜÐIÓJÐJà�;‰;Œ= §
¡
¤ÜÐXÓYÐYä7¸¸vÐ_vÔwÐwr   ÚindexesÚignore_indexc                 ó°  — | j                   |j                   k7  s|j                   |j                   k7  rt        d«      ‚| j                  t        j                  urt        d«      ‚|�||k7  }| |   ||   ||   }}} | j                  «       r| j                  «       st        d«      ‚t        |||¬«      \  }}| j	                  «       j                  «       ||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 integerszH`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±lÒ'BÜÐTÓUÐUà‡}�}œEŸJ™JÑ&ÜÐFÓGÐGð ÐØ  LÑ0ˆØ!(¨Ñ!9¸5ÀÑ;QÐSYÐZiÑSj˜�ˆà�=‰=Œ? '§,¡,¤.ÜØVó
ð 	
ô AØˆvÐ/Fô�M€Eˆ6ð �<‰<‹>×!Ñ!Ó# U¨FÐ2Ð2r   c                 ó  — |j                   t        j                  t        j                  t        j                  fvr t        j
                  |«      st        d«      ‚| j                  «       st        d«      ‚|s1|j                  «       dkD  s|j                  «       dk  rt        d«      ‚|j                  «       r|j                  «       n|j                  «       }| j                  «       } | j                  «       |j                  «       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™J¬¯
©
´E·I±IÐ>Ñ>Äu×G^ÑG^Ð_eÔGfÜÐTÓUÐUà×"Ñ"Ô$ÜÐ=Ó>Ð>á"¨¯
©
«°qÒ(8¸F¿J¹J»LÈ1Ò<LÜÐ@ÓAÐAà%×7Ñ7Ô9ˆV�\‰\Œ^¸v¿{¹{»}€FØ�K‰K‹M€Eà�=‰=‹?˜FŸN™NÓ,Ð,Ð,r   Úres1Úres2Úatolc                 ó,  ‡ ‡— 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        ||«      –— Œ y ­w©N©Ú_allclose_recursive)Ú.0Úr1Úr2s      r   Ú	<genexpr>z&_allclose_recursive.<locals>.<genexpr>¤   s   è ø€ ÐM¹_±6°2°rÔ& r¨2×.¹_ùs   ‚c              3   óB   •K  — | ]  }t        ‰|   ‰|   «      –— Œ y ­wr=   r>   )r@   Úkr8   r9   s     €€r   rC   z&_allclose_recursive.<locals>.<genexpr>¦   s"   øè ø€ ÐGÁ$¸QÔ& t¨A¡w°°Q±×8Á$ùs   ƒ)	Ú
isinstancer   r+   ÚallcloseÚstrr   ÚallÚzipr   )r8   r9   r:   s   `` r   r?   r?   œ   s|   ù€ ô �$œÔÜ�~‰~˜d D¨tÔ4Ð4Ü�$œÔØ�t‰|ÐÜ�$œÔ!ÜÑM¼SÀÀt¼_ÓMÓMÐMÜ�$œÔ ÜÔGÁ$ÓGÓGÐGØ�4‰<Ðr   )é
   éd   iè  é   Úmetric_classÚ	init_argsÚ
input_argsÚnum_update_to_compareÚrepsc                 ó4  — |xs i }|xs i } G d„ d| «      } G d„ d| «      } |di |¤Ž} |di |¤Ž}d}		 t        |d   «      D ]  }
|	t         |di |¤Ž |di |¤Ž«      z  }	Œ 	 |j                  «       }	 |j                  «       }|	t        |«      z  }	|	st	        d«       y	t        j                  d
t        |«      |«      }t        ||g«      D ]o  \  }}t        |«      D ]\  \  }}t        |«      D ]I  }t        «       }t        |«      D ]
  }
 |di |¤Ž}
Œ t        «       }||z
  ||||f<   |j                  «        ŒK Œ^ Œq t        j                  |d«      }t        j                  |d«      }t        t        |«      «      D ]G  }t	        d||   › d|d|f   › d|d|f   d›�«       t	        d||   › d|d|f   d›d|d|f   d›�«       ŒI |d   |d   k  j                  «       }t	        d| › d�«       y	# t        $ r d}	Y �ŒŸw xY w# t        $ r d}	Y �Œ�w xY w)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y)ú4check_forward_full_state_property.<locals>.FullStateTN©Ú__name__Ú
__module__Ú__qualname__Úfull_state_update© r   r   Ú	FullStaterU   â   s   „ Ø Ñr   r\   c                   ó   — e Zd ZdZy)ú4check_forward_full_state_property.<locals>.PartStateFNrV   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   ra   )r   ra   z'Recommended setting `full_state_update=Ú`r[   )Úranger?   r   ÚcomputeÚprintr+   ÚzerosÚlenÚ	enumerater   ÚresetÚmeanÚstdÚitem)rN   rO   rP   rQ   rR   r\   r_   Ú	fullstateÚ	partstateÚequalÚ_r8   r9   ÚresÚiÚmetricÚjÚtÚrÚstartÚendrj   rk   Úfasters                           r   Ú!check_forward_full_state_propertyrz   ª   s‚  € ðj ’˜R€IØÒ!˜r€Jô!�Lô !ô"�Lô "ñ Ñ&˜IÑ&€IÙÑ&˜IÑ&€Ià€EðÜÐ,¨QÑ/Ö0ˆAØÔ/±	Ñ0G¸JÑ0GÉÑI`ÐU_ÑI`ÓaÑa‰Eñ 1ð ×ÑÓ€DðØ× Ñ Ó"ˆð Ô'¨¨dÓ3Ñ3€EáÜÐ<Ô=Øô �+‰+�aœÐ2Ó3°TÓ
:€CÜ 	¨9Ð5Ö6‰	ˆˆ6ÜÐ3Ö4‰DˆAˆqÜ˜4–[�Ü$›�Ü˜qž�AÙÑ, Ñ,‘Að "ä"“n�Ø" U™{��A�q˜!�G‘Ø—‘•ñ !ñ 5ð 7ô �:‰:�c˜2Ó€DÜ
�)‰)�C˜Ó
€Cä”3Ð,Ó-Ö.ˆÜ�Ð 5°aÑ 8Ð9¸ÀtÈAÈqÈDÁzÀlÐRTÐUXÐYZÐ\]ÐY]ÑU^Ð_cÐTdÐeÔfÜÐ"Ð#8¸Ñ#;Ð"<¸MÈ$ÈqÐRSÈtÉ*ÐUYÐIZÐZ\Ð]`ÐabÐdeÐaeÑ]fÐgkÐ\lÐmÕnð /ð �5‰k˜D ™KÑ'×-Ñ-Ó/€FÜ	Ð3¸°J°<¸qÐ
AÔBØ
øôE ò Ø‹ðûô
 ò Ø‹ðús#   ¶.G6 Á6H Ç6HÈHÈHÈHÚmethod_nameÚinstanceÚparentc                 ó0  — t        || d«      }|€yt        |d«      r|j                  }t        |t        «      r|j
                  }nt        |t        «      r|j                  }|€yt        || d«      }|€t        d«      ‚|j                  |j                  k7  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   rF   r   Ú_mock_wrapsr   Úfuncr"   Ú__code__)r{   r|   r}   Úinstance_attrÚparent_attrs        r   Úis_overriddenr‡     s˜   € ä˜H k°4Ó8€MØÐØäˆ}˜mÔ,Ø%×1Ñ1ˆä�-¤Ô&à%×1Ñ1‰ä	�M¤7Ô	+Ø%×*Ñ*ˆØÐØä˜& +¨tÓ4€KØÐÜÐ>Ó?Ð?à×!Ñ! [×%9Ñ%9Ñ9Ð9r   ÚfnÚtimeoutc                 ó¢  — t        j                  «       j                  ryt        j                  | ¬«      }t	        d| j
                  › d|› d�t        j                  ¬«       |j                  «        |j                  |«       |j                  «       syt	        d| j
                  › d|› d	�t        j                  ¬«       |j                  «        y
)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

    T)r   zTrying to run `z` for zs...)Úfilerb   z` did not complete with z%, killing process and returning FalseF)ÚmultiprocessingÚcurrent_processÚdaemonÚProcessre   rW   ÚsysÚstderrrw   ÚjoinÚis_aliveÚkill)rˆ   r‰   Úprocs      r   Ú_try_proceed_with_timeoutr–   -  sŸ   € ô ×&Ñ&Ó(×/Ò/àä×"Ñ"¨"Ô-€Dä	ˆO˜BŸK™K˜=¨¨w¨i°tÐ
<Ä3Ç:Á:ÕNØ‡J�J„Là‡I�IˆgÔà�=‰=Œ?Øä	ˆAˆb�k‰kˆ]Ð2°7°)Ð;`Ð
aÔhk×hrÑhrÕsð
 	‡I�I„KØr   )F)FN)g�íµ ÷Æ°>))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?   ÚdictrH   rz   Úobjectr‡   r–   r[   r   r   Ú<module>r¥      sD  ðó Û 	Û 
ß -Ý Ý ß 9Ó 9Ý ã Ý å &á §
¡
§¡Ð/IÈ3Ó OÓPÐ Ù˜"Ÿ*™*Ÿ.™.Ð)<¸aÓ@ÓAÐ ð0 Fð 0°Fð 0¸tó 0ð
˜Vð 
¨Vð 
¸ó 
ð %*ñxØðxàðxð "ðxð ˆ6�6ˆ>Ñó	xðF %*Ø"&ñ.3Øð.3àð.3ð ð.3ð "ð	.3ð
 ˜3‘-ð.3ð ˆ6�6˜6Ð!Ñ"ó.3ðh %*ñ-Øð-àð-ð "ð-ð ˆ6�6ˆ>Ñó	-ñ@˜cð ¨ð °Eð ÀTó ð ð +/Ø+/Ú+:ØñfØðfà˜˜S #˜X™Ñ'ðfð ˜˜c 3˜h™Ñ(ðfð $ C™=ð	fð
 ðfð 
òfó ðfðR:˜sð :¨fð :¸fð :Èó :ð2 <Uñ " (ð "°Sð "ÐY]ô "r   