+
    ÿäûjm<  ã                  ób   € ^ RI Ht ^ RIt^ RIt^ RIHtHtHt ^ RIH	t	 ^ RI
Ht  ! R R4      tR# )é    )ÚannotationsN)ÚKORNIA_CHECKÚKORNIA_CHECK_SAME_SIZEÚKORNIA_CHECK_SHAPE)Ú_autocast_disabled)Úeuclidean_distancec                  óœ   € ] tR t^tRtRR R llt]R R l4       t]R R l4       t]	R	 R
 l4       t
R R ltR R ltR R ltR R ltRtR# )ÚKMeansa}
  Cluster ``(N, D)`` points into ``num_clusters`` groups by k-means with Euclidean distance.

``kmeans = KMeans(num_clusters, cluster_centers)`` stores the settings, and ``kmeans.fit(X)`` alternates
nearest-centre assignment and centre means on the ``(N, D)`` points ``X``. ``labels = kmeans.predict(x)`` returns
the ``int64`` index of the nearest centre for each row of ``(M, D)`` points. After :meth:`fit`,
:attr:`cluster_centers` is ``(num_clusters, D)`` and :attr:`cluster_assignments` holds the ``(N,)`` labels of ``X``.

Convention:
    - :meth:`fit` stops after the first update whose squared total centre shift,
      :math:`(\sum_k \lVert c_k^{\text{new}} - c_k \rVert)^2`, is at most ``tolerance``, or after ``max_iterations``
      updates. ``max_iterations=0`` sets no limit, so ``tolerance`` must not be ``None`` then.
    - ``seed`` calls ``torch.manual_seed(seed)``, which reseeds the global generator. The initial centres and the
      points that empty clusters move to are drawn from it; there is no ``generator`` argument. A second
      :meth:`fit` on the same data therefore starts from other centres unless the global generator is reseeded
      first.
    - A point at the same distance from two centres goes to the one with the lower index. A cluster that gets no
      point moves to a random data point.
    - Distances and means are computed in the promoted dtype of the data and the centres, at least ``float32``
      (``int64`` for integer data and centres). The centres keep the dtype of the data, so an integer mean is
      truncated toward zero.

Args:
    num_clusters: number of clusters the data has to be assigned to, a positive integer. Required, also with
        ``cluster_centers``; without them, :meth:`fit` needs at least ``num_clusters`` samples.
    cluster_centers: the ``(num_clusters, D)`` starting centres, or ``None`` to sample them from the data in
        each :meth:`fit`. Pass ``None`` explicitly; the argument has no default.
    tolerance: the bound on the squared total centre shift that stops :meth:`fit`, or ``None`` to stop only after
        ``max_iterations`` updates.
    max_iterations: the maximum number of updates; 0 sets no limit.
    seed: if given, passed to ``torch.manual_seed`` when the object is created.

Raises:
    BaseError: if ``num_clusters`` is not a positive integer, ``max_iterations`` or ``tolerance`` is negative,
        ``tolerance=None`` comes with ``max_iterations=0``, or ``cluster_centers`` is not ``(num_clusters, D)``.

Example:
    >>> kmeans = kornia.contrib.KMeans(3, None, 10e-4, 100, 0)
    >>> kmeans.fit(torch.rand((1000, 5)))
    >>> predictions = kmeans.predict(torch.rand((10, 5)))

Nc               ó0   € V ^8„  d   QhRRRRRRRRRR	R
R/# )é   Únum_clustersÚintÚcluster_centersztorch.Tensor | NoneÚ	toleranceÚfloatÚmax_iterationsÚseedz
int | NoneÚreturnÚNone© )Úformats   "ÚQC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\contrib\kmeans.pyÚ__annotate__ÚKMeans.__annotate__J   sF   € ÷ ,$ñ ,$àð,$ð -ð,$ð ð	,$ð
 ð,$ð ð,$ð 
ñ,$ó    c                	ó
  €  \         P                  ! V4      p\        V\        4      ;'       g@    \        V\
        P                  4      ;'       d    VP                  \
        P                  8H  p\        V'       * ;'       d    V^ 8„  RV: 24       Tp\        V^ 8¬  RV 24       \        VRJ ;'       g    V^ 8¬  RV 24       \        VRJ;'       g    V^ 8„  R4       VeF   \        VRR.4       \        VP                  ^ ,          V8H  RVP                  ^ ,           R	V 24       Wn        W n        W n        W0n        W@n        RV n        RV n        Ve   \
        P$                  ! V4       R# R#   \         d    ^ p ELii ; i)
r   z)num_clusters must be a positive int, got z!max_iterations must be >= 0, got Nz$tolerance must be None or >= 0, got zXtolerance=None needs max_iterations > 0: with max_iterations=0 the fit would never stop.ÚCÚDzcluster_centers has z rows but num_clusters=)ÚoperatorÚindexÚ	TypeErrorÚ
isinstanceÚboolÚtorchÚTensorÚdtyper   r   Úshaper   Ú_initial_cluster_centersÚ_cluster_centersr   r   Ú_final_cluster_assignmentsÚ_final_cluster_centersÚmanual_seed)Úselfr   r   r   r   r   ÚcountÚis_bools   &&&&&&  r   Ú__init__ÚKMeans.__init__J   s‰  € ð	Ü—N’N <Ó0ˆEô ˜\¬4Ó0÷ 
ð 
Ü�|¤U§\¡\Ó2×WÐW°|×7IÑ7IÌUÏZÉZÑ7Wð 	ô 	˜”[×.Ð. U¨Q¡YÐ2[Ð\hÑ[kÐ0lÔmØˆÜ�^ qÑ(Ð,MÈnÐM]Ð*^Ô_Ü�Y $Ð&×8Ð8¨)°q©.Ð<`ÐajÐ`kÐ:lÔmÜØ˜TÐ!×7Ð7 ^°aÑ%7Øfô	
ð Ò&Ü˜°°c°
Ô;ÜØ×%Ñ% aÕ(¨LÑ8Ø& ×'<Ñ'<¸QÕ'?Ð&@Ð@WÐXdÐWeÐfôð
 )ÔØ(7Ô%Ø /ÔØ"ŒØ,Ôà?CˆÔ'Ø;?ˆÔ#àÒÜ×Ò˜dÖ#ñ øô? ô 	Ø‹Eð	ús   ‚E1 Å1FÆFc               ó   € V ^8„  d   QhRR/# ©r   r   útorch.Tensorr   )r   s   "r   r   r   y   s   € ÷ ?ñ ? ñ ?r   c                óð   € \        V P                  \        P                  4      '       d   V P                  # \        V P                  \        P                  4      '       d   V P                  # \        R4      h)a¬  Return the current cluster centers.

Returns:
    A tensor with shape ``(C, D)``:
    - ``C`` is the number of clusters.
    - ``D`` is the feature dimension of each sample.

    If :meth:`fit` has already been called, this returns the learned
    final centers. Otherwise, it returns the initialization provided
    during construction.

Raises:
    TypeError: If no initial centers were provided and ``fit`` has not been run.
ú#Model has not been fit to a dataset)r"   r+   r$   r%   r)   r!   ©r-   s   &r   r   ÚKMeans.cluster_centersx   sV   € ô  �d×1Ñ1´5·<±<×@Ò@Ø×.Ñ.Ð.Ü�d×+Ñ+¬U¯\©\×:Ò:Ø×(Ñ(Ð(ÜÐ=Ó>Ð>r   c               ó   € V ^8„  d   QhRR/# r3   r   )r   s   "r   r   r   �   s   € ÷ ?ñ ? \ñ ?r   c                ó„   € \        V P                  \        P                  4      '       d   V P                  # \	        R4      h)aš  Return cluster labels assigned during the most recent ``fit`` call.

Returns:
    A 1D tensor with shape ``(N,)``, where ``N`` is the number of
    samples given to :meth:`fit`. Each value is the cluster index
    nearest to the corresponding sample in the final centers, matching
    :meth:`predict` on the training data even when fitting stops early.

Raises:
    TypeError: If ``fit`` has not been run yet.
r6   )r"   r*   r$   r%   r!   r7   s   &r   Úcluster_assignmentsÚKMeans.cluster_assignmentsŽ   s3   € ô �d×5Ñ5´u·|±|×DÒDØ×2Ñ2Ð2ÜÐ=Ó>Ð>r   c               ó    € V ^8„  d   QhRRRR/# )r   Útensorsr4   r   ztorch.dtyper   )r   s   "r   r   r       s   € ÷ ñ ˜lð ¨{ñ r   c                 ó:  € V ^ ,          P                   pV R,           F#  p\        P                  ! WP                   4      pK%  	  V\        P                  \        P                  39   d   \        P
                  # VP                  '       g   \        P                  # V# )a%  Dtype for distances and sums: the promoted dtype.

Half types lift to float32. When every tensor is integer the work dtype is int64, which is exact (float32
would merge integers above 2**24, and uint8 arithmetic wraps) while the squared distance stays below 2**63
(a distance of about 3.0e9).
:é   NN)r&   r$   Úpromote_typesÚfloat16Úbfloat16Úfloat32Úis_floating_pointÚint64)r>   r&   Úts   *  r   Ú_work_dtypeÚKMeans._work_dtypeŸ   sq   € ð ˜•
× Ñ ˆØ˜—�ˆAÜ×'Ò'¨¯w©wÓ7ŠEñ à”U—]‘]¤E§N¡NÐ3Ô3Ü—=‘=Ð Ø×&×&Ð&Ü—;‘;ÐØˆr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   ÚXr4   r   r   r   r   )r   s   "r   r   r   °   s"   € ÷ ñ ¨\ð Èð ÐQ]ñ r   c                ót   € \        V4      p\        P                  ! W1P                  R7      pVRV pW,          # )zßChooses num_cluster points from X as the initial cluster centers.

Args:
    X: 2D input torch.Tensor to be clustered
    num_clusters: number of desired cluster centers

Returns:
    2D torch.Tensor with num_cluster rows

©ÚdeviceN)Úlenr$   ÚrandpermrN   )r-   rK   r   Únum_samplesÚpermÚidxs   &&&   r   Ú_initialise_cluster_centersÚ"KMeans._initialise_cluster_centers°   s2   € ô ˜q›6ˆÜ�~Š~˜k·(±(Ô;ˆØ�=�LÐ!ˆØ�vˆr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Údata1r4   Údata2r   r   )r   s   "r   r   r   À   s"   € ÷ (ñ (°,ð (À|ð (ÐXdñ (r   c                óú   € VR,          pVR,          pVP                   P                  '       gD   VP                   P                  '       g(   W4,
          P                  ^4      P                  RR7      # \	        W44      # )a8  Compute pairwise distance between 2 sets of vectors.

Floating-point vectors give the Euclidean distance; integer vectors give the exact squared distance in int64
(same argmin).

Args:
    data1: 2D torch.Tensor of shape N, D
    data2: 2D torch.Tensor of shape C, D

Returns:
    2D torch.Tensor of shape N, C

©Údim)ºNNNN.)N.éÿÿÿÿ)r&   rE   ÚpowÚsumr   )r-   rW   rX   ÚAÚBs   &&&  r   Ú_pairwise_euclidean_distanceÚ#KMeans._pairwise_euclidean_distanceÀ   sb   € ð �,Õˆà�)ÕˆØ�w‰w×(×(Ð(°·±×1J×1JÐ1Jð •E—;‘;˜q“>×%Ñ%¨"Ð%Ó-Ð-Ü! !Ó'Ð'r   c               ó    € V ^8„  d   QhRRRR/# )r   rK   r4   r   r   r   )r   s   "r   r   r   Ø   s   € ÷ [:ñ [:�\ð [: dñ [:r   c           	     óÒ  a a€ \        SRR.4       S P                  fE   \        \        S4      S P                  8¬  VV 3R l4       S P                  SS P                  4      pM%\        SS P                  ^RR7       S P                  pS P                  SV4      pSP                  V4      pRp^ p S P                  WBP                  V4      4      pVP                  R4      pVP                  4       p\        P                  P                  P                  VS P                  R7      p	V	P!                  ^ 4      p
VP"                  '       dX   \%        SP&                  P(                  4      ;_uu_ 4        V	P                  V4      P+                  4       V,          pRRR4       MgVR,          P-                  RVP.                  ^,          4      pVP1                  S P                  VP.                  ^,          4      P3                  ^ WÄ4      pV
^ 8H  P5                  ^4      pV
P7                  ^R7      P5                  ^4      P                  V4      pVP"                  '       d   XV,          pM\        P8                  ! XVR	R
7      pV\        P:                  ! \        S4      S P                  3SP&                  R7      ,          p\        P<                  ! VVV4      P                  SP>                  4      pVP                  V4      VP                  V4      ,
          p\        P                   ! \        P@                  ! \        P                   ! V^,          ^R7      4      4      pV^,           pS PB                  e   V^,          S PB                  8:  d   M)S PD                  ^ 8w  g   EKò  VS PD                  8¼  g   EK   VS n#        S PI                  S4      S n%        R#   + '       g   i     ELß; i)z´Cluster the rows of ``X`` and store the final centres and assignments.

Args:
    X: the ``(N, D)`` points to cluster, with the ``D`` of ``cluster_centers`` when those are given.

ÚNr   Nc                 ó:   <€ R SP                    R\        S 4       2# )znum_clusters=z+ needs at least as many samples, but X has )r   rO   )rK   r-   s   €€r   Ú<lambda>ÚKMeans.fit.<locals>.<lambda>æ   s"   ø€ ˜-¨×(9Ñ(9Ð':Ð:eÔfiÐjkÓflÐemÑnr   z;X and cluster_centers must have the same number of columns.©r[   Úmsg)Únum_classes)ÚminÚtrunc)Úrounding_moderM   rZ   r]   )r\   N)&r   r(   r   rO   r   rT   r   rH   Útorb   ÚargminÚcloner$   ÚnnÚ
functionalÚone_hotr_   rE   r   rN   ÚtyperG   Úexpandr'   Ú	new_zerosÚscatter_add_Ú	unsqueezeÚclampÚdivÚrandintÚwherer&   Úsqrtr   r   r+   Úpredictr*   )r-   rK   Úcurrent_centersÚ
work_dtypeÚX_workÚprevious_centersÚ	iterationÚdistanceÚcluster_assignmentru   Úcluster_countsÚcluster_sumsr    Ú
empty_maskÚsafe_countsÚmeansÚrandom_pointsÚshiftÚcenter_shifts   ff                 r   ÚfitÚ
KMeans.fitØ   s  ù€ ô 	˜1˜s C˜jÔ)à×(Ñ(Ò0äÜ�A“˜$×+Ñ+Ñ+Ýnôð #×>Ñ>¸qÀ$×BSÑBSÓT‰Oô #ØØ×-Ñ-ØØQõ	ð #×;Ñ;ˆOð ×%Ñ% a¨Ó9ˆ
Ø—‘�jÓ!ˆà04ÐØˆ	àà%)×%FÑ%FÀv×OaÑOaÐblÓOmÓ%nˆHà!)§¡°Ó!4Ðà.×4Ñ4Ó6Ðô —h‘h×)Ñ)×1Ñ1Ð2DÐRV×RcÑRcÐ1ÓdˆGØ$Ÿ[™[¨›^ˆNØ×+×+Ð+Ü'¨¯©¯©×6Õ6Ø#*§:¡:¨jÓ#9×#;Ñ#;Ó#=ÀÕ#F�L÷ 7Ð6ð +¨7Õ3×:Ñ:¸2¸v¿|¹|ÈA½ÓO�Ø%×/Ñ/°×0AÑ0AÀ6Ç<Á<ÐPQÅ?ÓS×`Ñ`ÐabÐdiÓr�ð )¨AÑ-×8Ñ8¸Ó;ˆJØ(×.Ñ.°1Ð.Ó5×?Ñ?ÀÓB×EÑEÀjÓQˆKØ×+×+Ð+Ø$ {Õ2‘ô Ÿ	š	 ,°È7ÔS�Ø"¤5§=¢=´°Q³¸$×:KÑ:KÐ9MÐVW×V^ÑV^Ô#_Õ`ˆMÜ#Ÿkšk¨*°mÀUÓK×NÑNÈqÏwÉwÓWˆOð $×&Ñ& zÓ2Ð5E×5HÑ5HÈÓ5TÕTˆEÜ Ÿ9š9¤U§Z¢Z´·	²	¸%À½(ÈÔ0JÓ%KÓLˆLà! A�ˆIà�~‰~Ò)¨l¸A­oÀÇÁÔ.OØà×"Ñ" a×'¨I¸×9LÑ9L×,LØà&5ˆÔ#à*.¯,©,°q«/ˆÖ'÷I 7×6Ð6ús   Å0'OÏO&	c               ó    € V ^8„  d   QhRRRR/# )r   Úxr4   r   r   )r   s   "r   r   r   5  s   € ÷ #ñ #˜ð #¨,ñ #r   c                ó  € \        VRR.4       \        WP                  ^RR7       V P                  WP                  4      pV P	                  VP                  V4      V P                  P                  V4      4      pVP                  R4      # )zÍReturn the index of the nearest cluster centre for each row of ``x``.

Args:
    x: the ``(M, D)`` points, with the ``D`` of :attr:`cluster_centers`.

Returns:
    The ``(M,)`` ``int64`` cluster indices.

rf   r   z;x and cluster_centers must have the same number of columns.rj   r]   )r   r   r   rH   rb   rp   rq   )r-   r“   r‚   r†   s   &&  r   r€   ÚKMeans.predict5  s|   € ô 	˜1˜s C˜jÔ)äØ×#Ñ#¨Ð0mõ	
ð ×%Ñ% a×)=Ñ)=Ó>ˆ
Ø×4Ñ4°Q·T±T¸*Ó5EÀt×G[ÑG[×G^ÑG^Ð_iÓGjÓkˆØ�‰˜rÓ"Ð"r   )r)   r*   r+   r(   r   r   r   )gü©ñÒMbP?r   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r0   Úpropertyr   r;   ÚstaticmethodrH   rT   rb   r�   r€   Ú__static_attributes__r   r   r   r
   r
      se   † ñ(÷T,$ð\ ô?ó ð?ð* ô?ó ð?ð  ôó ðõ õ (õ0[:÷z#ñ #r   r
   )Ú
__future__r   r   r$   Úkornia.core.checkr   r   r   Úkornia.core.utilsr   Úkornia.geometry.linalgr   r
   r   r   r   Ú<module>r¢      s(   ðõ( #ã ã ç VÑ VÝ 0Ý 5÷h#ó h#r   