+
    åûjq{  ã                  ód  € ^ RI Ht ^ RIHtHt ^ RIt^ RIHt ^ RIHtH	t	H
t
 ^ RIHtHtHt ^ RIHt ^ RIHtHt RtRtR	 R
 ltR R ltR R lt ! R R]P2                  P4                  4      tR R ltRR R llt ! R R]P<                  4      tRR R llt  ! R R]P<                  4      t!R# )é    )Úannotations)ÚAnyÚOptionalN)Únn)ÚKORNIA_CHECKÚKORNIA_CHECK_IS_TENSORÚKORNIA_CHECK_SHAPE)Úis_autocast_enabledÚis_compilingÚis_exporting)Úmask_ignore_pixels)Ú_check_class_labelsÚone_hotc               ó    € V ^8„  d   QhRRRR/# )é   Úpredútorch.TensorÚreturnÚbool© )Úformats   "ÚOC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\losses\focal.pyÚ__annotate__r   *   s   € ÷ ñ  <ð °Dñ ó    c                ó`  € V P                   P                  R8H  ;'       Ed    V P                  \        P                  \        P
                  39   ;'       dÙ    \        4       '       * ;'       dÂ    \        P                  P                  4       '       * ;'       d—    V P                  4       \        8¬  ;'       dx    \        P                  P                  4       '       * ;'       dM    \        4       '       * ;'       d6    \        P                  P                  P!                  V 4      P"                  RJ # )zmSelect large native-precision CPU logits, whose full-size log-softmax buffers are fresh memory on every call.ÚcpuN)ÚdeviceÚtypeÚdtypeÚtorchÚfloat32Úfloat64r   ÚjitÚ
is_tracingÚnumelÚ_CPU_MIN_NUMELÚ_CÚ _are_functorch_transforms_activer
   ÚautogradÚ
forward_adÚunpack_dualÚtangent)r   s   &r   Ú_focal_loss_cpu_eligibler-   *   sü   € ð 	�‰×Ñ˜EÑ!÷ 
	Hñ 
	HØ�J‰Jœ5Ÿ=™=¬%¯-©-Ð8Ñ8÷
	Hð 
	Hô “Ô÷	
	Hð 
	Hô —	‘	×$Ñ$Ó&Ô&÷
	Hð 
	Hð �J‰J‹LœNÑ*÷
	Hð 
	Hô —‘×9Ñ9Ó;Ô;÷
	Hð 
	Hô $Ó%Ô%÷
	Hð 
	Hô �N‰N×%Ñ%×1Ñ1°$Ó7×?Ñ?À4ÐGðr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r   r   ÚtensorsúOptional[torch.Tensor]r   zlist[tuple[Any, ...]]r   )r   s   "r   r   r   ?   s(   € ÷ cñ c˜ð cÐ0Fð cÐK`ñ cr   c                ó,  a€ V P                  4       \        ^V P                  ^ ,          4      ,          V P                  4       ,          p\        ^\        \        ^V4      ,          4      pW0P                  ^ ,          8¼  d   V .VO5.# V .VO Uu. uF  qDe   VP                  V4      MRNK  	  pp\        \        V^ ,          4      4       Uau. uF5  o\        ;QJ d    . V3R lV 4       F  NK  	  5M! V3R lV 4       4      NK7  	  up# u upi u upi )zlSplit ``pred`` and ``tensors`` (``None`` stays ``None``) into the same batch chunks of about 2 MB of logits.Nc              3  ó@   <"  € T F  qe
   VS,          MR x € K  	  R # 5i©Nr   )Ú.0ÚsÚis   & €r   Ú	<genexpr>Ú _batch_chunks.<locals>.<genexpr>F   s   øé € ÐB¹6°aš-�!�A–$¨TÔ1»6ùs   ƒ)	r%   ÚmaxÚshapeÚelement_sizeÚ_CPU_CHUNK_BYTESÚsplitÚrangeÚlenÚtuple)r   r/   Úsample_bytesÚstepÚtÚsplitsr6   s   &*    `r   Ú_batch_chunksrE   ?   sà   ø€ à—:‘:“<¤3 q¨$¯*©*°Q­-Ó#8Õ8¸4×;LÑ;LÓ;NÕN€LÜˆqÔ"¤c¨!¨\Ó&:Õ:Ó;€DØ�z‰z˜!�}ÔØÐ ˜Ñ Ð!Ð!ØAEÐ@PÈÑ@PÓQÑ@P¸1š}ˆa�g‰g�dŒm°$Ò6Ñ@P€FÐQÜLQÔRUÐV\Ð]^ÕV_ÓR`ÔLaÔbÑLaÀq�EŒEÔB¹6ÓB�E‰EÔB¹6ÓBÖBÑLaÑbÐbùò RùÚbs   ÂDÃDÃ+Dc          
     ó,   € V ^8„  d   QhRRRRRRRRRR	/# )
r   r   r   ÚtargetÚvalidr0   Úinplacer   r   z/tuple[torch.Tensor, torch.Tensor, torch.Tensor]r   )r   s   "r   r   r   I   s4   € ÷ 8ñ 8Ø
ð8Ø ,ð8Ø5Kð8ØVZð8à4ñ8r   c                ó¸  € Vf   T M\         P                  ! W R4      pVP                  ^RR7      pVP                  ^V4      pV'       dL   Ve   VP	                  V4      MWE,
          P                  4       P                  ^RR7      P                  4       pM5WE,
          P                  4       P                  ^RR7      P                  4       pWe,
          V,
          WW3# )zÞ``log_softmax(where(valid, pred, 0), 1).gather(1, target)``, and the maximum and log-sum of each position.

The log-probability is ``(x - max) - log(sum(exp(x - max)))``, in that order, as in ``log_softmax``'s CPU kernel.
ç        T)Úkeepdim)
r    ÚwhereÚamaxÚgatherÚsub_Úexp_ÚsumÚlog_ÚexpÚlog)r   rG   rH   rI   ÚxÚx_maxÚx_targetÚlog_sums   &&&&    r   Ú_target_log_softmax_valuesrZ   I   s³   € ð ’‰¤5§;¢;¨u¸CÓ#@€Aà�F‰F�1˜dˆFÓ#€EØ�x‰x˜˜6Ó"€Hßà$)Ò$5�1—6‘6˜%”=¸1½9×JÑJÓL×PÑPÐQRÐ\`ÐPÓa×fÑfÓh‰à•9—/‘/Ó#×'Ñ'¨°4Ð'Ó8×<Ñ<Ó>ˆØÕ Õ'¨Ð7Ð7r   c                  óF   € ] tR t^\tRt]R R l4       t]R R l4       tRtR# )Ú_TargetLogSoftmaxaÇ  Target-class log-probability ``log_softmax(where(valid, pred, 0), 1).gather(1, target)`` of CPU logits.

``log_softmax`` saves a full-size output for its backward, and the ``gather`` and ``masked_fill`` backwards each
allocate another full-size gradient: on a large CPU batch, fresh memory that is faulted in on every call. This
saves ``pred`` itself and the maximum and log-sum of each position, and the backward writes
:math:`(\text{onehot}(t) - \text{softmax}) \cdot g` straight into the one gradient buffer, a batch chunk at a time.
The logits are masked as ``log_softmax``'s input was, so NaN and infinity positions are the same, and the gradient
at a position that is not ``valid`` is exactly 0, as ``masked_fill``'s backward made it. ``torch.compile`` and
functorch get the same arithmetic out of place and unchunked; a double backward differentiates a softmax
recomputed from ``pred``. Forward-mode AD never reaches it (see :func:`_focal_loss_cpu_eligible`).
c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   Úctxr   r   r   rG   rH   r0   r   r   )r   s   "r   r   Ú_TargetLogSoftmax.__annotate__j   s1   € ÷ ñ �Sð  ð °lð ÐKað Ðfrñ r   c                	ó�  a
€ \        4       '       g%   \        P                  P                  4       '       d   \	        WVR R7      w  rEpMd\        WV4       UUU	u. uF  w  rxp	\	        WxV	RR7      NK  	  up	ppo
\        S
4      ^8X  d   S
^ ,          w  rEpMV
3R l\        ^4       4       w  rEpV P                  WW5V4       V# u up	ppi )F)rI   Tc              3  ó†   <"  € T F1  p\         P                  ! S Uu. uF  q"V,          NK  	  up4      x € K3  	  R # u upi 5ir3   )r    Úcat)r4   r6   ÚpartÚpartss   &  €r   r7   Ú,_TargetLogSoftmax.forward.<locals>.<genexpr>t   s6   øé € Ð+eÑ\dÐWX¬E¯IªIÉ5Ó6QÉ5À4¸A·w°wÉ5Ñ6Q×,RÐ,RÓ\dùÒ6Qùs   ƒAœ<
­A)	r   r    r'   r(   rZ   rE   r?   r>   Úsave_for_backward)r^   r   rG   rH   Úlog_probrW   rY   ÚprC   Úvrd   s   &&&&      @r   ÚforwardÚ_TargetLogSoftmax.forwardi   s³   ø€ ä�>Š>œUŸX™X×FÑF×HÒHÜ'AÀ$ÐPUÐ_dÔ'eÑ$ˆH™Wô R_Ð_cÐmrÔQsõÙQsÁgÀaÈAÔ*¨1°¸D×AÑQsóˆEô �5‹z˜QŒØ+0°­8Ñ(�¡ä+eÔ\aÐbcÔ\dÓ+eÑ(� Ø×Ñ˜d¨E¸'ÔBØˆùôs   ÁCc               ó$   € V ^8„  d   QhRRRRRR/# )r   r^   r   Úgrad_outputr   r   ztuple[torch.Tensor, None, None]r   )r   s   "r   r   r_   y   s"   € ÷  ñ  �cð  ¨ð  Ð9Xñ  r   c           
     	ó|  € V P                   w  r#rEp\        P                  ! 4       '       dj   Vf   TM\        P                  ! WBR4      pVP	                  ^4      V) ,          P                  ^W14      pVf   VR R 3# \        P                  ! WHR4      R R 3# \        4       '       g‰   \        P                  P                  4       '       ge   \        P                  P                  P                  V4      '       g6   \        P                  P                  P                  V4      P                  eY   W%,
          V,
          P                  4       V) ,          P                  ^W14      pVf   VR R 3# \        P                  ! WHR4      R R 3# \        P                   ! V\        P"                  R7      pV) p	\%        W(W4WVW4       Fy  w  r«rÍrïpp\        P&                  ! W®VR7       VP)                  V4      P+                  4       P-                  V4      P/                  ^VV4       Vf   Kf  VP1                  V( R4       K{  	  VR R 3# )NrK   )Úmemory_format)Úout)Úsaved_tensorsr    Úis_grad_enabledrM   ÚsoftmaxÚscatter_addr   r'   r(   Ú
_functorchÚis_legacy_batchedtensorr)   r*   r+   r,   rT   Ú
empty_likeÚcontiguous_formatrE   ÚsubrP   rQ   Úmul_Úscatter_add_Úmasked_fill_)r^   rm   r   rG   rH   rW   rY   rV   ÚgradÚneg_grad_outputrh   ÚgrC   ri   ÚmÚlsÚgoÚneg_gos   &&                r   ÚbackwardÚ_TargetLogSoftmax.backwardx   sÕ  € à.1×.?Ñ.?Ñ+ˆ�e GÜ× Ò ×"Ò"ð š‘¬5¯;ª;°uÀCÓ+HˆAØ—I‘I˜a“L K <Õ/×<Ñ<¸QÀÓTˆDØ!šM�DÈtÐUYÐYÐY¬u¯{ª{¸5ÈÓ/LÈtÐUYÐYÐYô �NŠNÜ�x‰x×8Ñ8×:Ò:Ü�x‰x×"Ñ"×:Ñ:¸;×GÒGÜ�~‰~×(Ñ(×4Ñ4°[ÓA×IÑIÒUà•l gÕ-×2Ñ2Ó4¸°|ÕC×PÑPÐQRÐTZÓhˆDØ!šM�DÈtÐUYÐYÐY¬u¯{ª{¸5ÈÓ/LÈtÐUYÐYÐYä×Ò ´E×4KÑ4KÔLˆØ&˜,ˆÜ-:Ø˜ u°{ö.
Ñ)ˆA�!˜˜r 6ô �IŠI�a Õ"Ø�F‰F�2‹J�O‰OÓ×"Ñ" 6Ó*×7Ñ7¸¸1¸bÔAØŒ}Ø—‘ ˜r 3Ö'ñ.
ð �T˜4ÐÐr   r   N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ústaticmethodrj   r„   Ú__static_attributes__r   r   r   r\   r\   \   s/   † ñ
ð ôó ðð ô ó ô r   r\   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   r   r   rG   Útarget_maskr0   Úreducedr   r   r   )r   s   "r   r   r   ˜   s4   € ÷ ñ Ø
ðØ ,ðØ;QðØ\`ðàñr   c                ól  € \         P                  P                  4       '       g?   V'       d7   \        V 4      '       d&   \        P                  WP                  ^4      V4      # Ve   V P                  V( R4      p V P                  ^4      pV'       d"   VP                  ^VP                  ^4      4      pV# )ziLog-softmax over dim 1 of ``pred``, positions outside ``target_mask`` set to 0; at ``target`` if reduced.rK   )
r    r#   Úis_scriptingr-   r\   ÚapplyÚ	unsqueezeÚmasked_fillÚlog_softmaxrO   )r   rG   rŽ   r�   Úlog_pred_softs   &&&& r   Ú_masked_log_softmaxr—   ˜   s“   € ô �9‰9×!Ñ!×#Ò#ßÔ/°×5Ò5Ü$×*Ñ*¨4×1AÑ1AÀ!Ó1DÀkÓRÐRØÒà×Ñ  ¨cÓ2ˆØ×$Ñ$ QÓ'€MßØ%×,Ñ,¨Q°×0@Ñ0@ÀÓ0CÓDˆØÐr   c               ó8   € V ^8„  d   QhRRRRRRRRRR	R
RRRRR/# )r   r   r   rG   ÚalphaúOptional[float]ÚgammaÚfloatÚ	reductionÚstrÚweightr0   Úignore_indexúOptional[int]r   r   )r   s   "r   r   r   ¨   sb   € ÷ iñ iØ
ðiàðið ðið ð	ið
 ðið #ðið  ðið ñir   c                ód  € \        V . RO4       V P                  ^ ,          3V P                  R,          ,           p\        V P                  ^ ,          VP                  ^ ,          8H  ;'       d(    VP                  R,          V P                  R,          8H  RV RVP                   24       \        V P                  VP                  8H  RV P                   RVP                   24       \	        W4      w  rVR9   ;'       dt    VR	J ;'       dh    ^ Tu;8*  ;'       d    \        R
4      8  Mu ;'       dA    VR	J ;'       g5    \        V\        \
        34      ;'       d    ^ Tu;8*  ;'       d    ^8*  Mu p	R	p
V	'       d   \        VP                  \        P                  8H  RVP                   24       \        V P                  ^,          ^8¬  RV P                   24       \        WP                  ^,          4      pM3\        WP                  ^,          V P                  V P                  R7      p
Ve   VP                  ^4      pV
e	   W¨,          p
\        WW‰4      pRVP                  4       ,
          pV^ 8H  p\        P                   ! WÜP#                  4       P%                  V4      VP'                  VR4      P%                  V4      4      pV
e(   VP'                  V
^ 8H  R4      pV) V,          V
,          pM!Ve   VP'                  V( R4      pV) V,          pV P                  ^,          pR.^.\)        V P                  R,          4      ,          ,           pVe¾   V	'       dS   \        P                   ! VP                  ^4      ^ 8H  VP+                  R^V,
          4      VP+                  RV4      4      pMZ\        P,                  ! ^V,
          .V.V^,
          ,          ,           VP                  VP                  R7      P/                  V4      pVV,          pVe¸   \1        VR4       \        VP                  ^ ,          V8H  ;'       d    VP3                  4       V8H  RVP                   RV P                   24       \        VP                  V P                  8H  RVP                   RV P                   24       VP/                  V4      pW_,          pVR8X  d   TpV# VR8X  d¡   V	'       d€   VP                  \        P4                  \        P6                  39   d   \        P8                  MVP                  pVP;                  VR7      V,          P=                  VP                  4      pV# \        P:                  ! V4      p V# VR8X  d   \        P>                  ! V4      pV# \A        RV 24      h)a»  Compute the multi-class focal loss of ``(B, C, *)`` logits against ``(B, *)`` class indices, using softmax.

``loss = focal_loss(pred, target, alpha)`` scales the cross entropy :math:`-\log p_t` of each pixel's target class
by :math:`\alpha_t (1 - p_t)^{\gamma}` :cite:`lin2018focal`, so that pixels the softmax already classifies well
count less. :math:`p_t` is the softmax probability of the target class and :math:`\alpha_t` the class weight set by
``alpha``.

.. math::

    \text{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma} \, \text{log}(p_t)

Convention:
    - ``pred`` holds logits ``(B, C, *)`` and the softmax over dim 1 is taken inside. ``target`` holds int64
      class indices ``(B, *)`` in ``[0, C)``; its batch and spatial sizes must equal those of ``pred``.
    - Class 0 is the background: ``alpha`` weights it by :math:`1 - \alpha` and classes :math:`1, \dots, C - 1`
      by :math:`\alpha`, so a relabelling that moves class 0 changes the loss. ``alpha=None`` weights every class
      by 1. With ``C = 2`` the target slice equals :func:`~kornia.losses.binary_focal_loss_with_logits` of the
      logit difference ``pred[:, 1:] - pred[:, :1]`` and the target ``target[:, None]``, with the same ``alpha``
      and ``gamma``: class 1 is the positive class.
    - The ``'none'`` map holds the focal term above in the target-class slice and 0 in the other slices, also
      where their log-probability overflows to ``-inf``. ``'mean'`` divides its sum by every element of that map,
      ``C`` times the pixel count, so with ``gamma=0`` and ``alpha=None`` it is the mean cross entropy divided by
      ``C``.
    - ``weight`` multiplies the slice of class ``c`` by ``weight[c]``; ``'mean'`` is not normalised by the
      weights.
    - A pixel labelled ``ignore_index`` (default ``-100``) is 0 in every slice and still counts in the ``'mean'``
      denominator, where :func:`~kornia.losses.dice_loss` and :func:`~kornia.losses.tversky_loss` drop it.
      :ref:`Losses and metrics <losses-metrics-conventions>` ports
      :func:`~torch.nn.functional.cross_entropy` to this loss.
    - ``alpha`` outside ``[0, 1]`` and a negative ``gamma`` are not validated.
    - On the CPU, ``'mean'`` and ``'sum'`` without ``weight``, with a finite ``gamma >= 0`` and ``alpha`` ``None``
      or a number in ``[0, 1]``, of float32 or float64 logits with at least :math:`2^{19}` values compute the
      target-class log-probability with a custom autograd function, a batch chunk at a time, instead of a
      full-size ``log_softmax``, unless ``torch.export`` or ``torch.jit.trace`` captures the call, a functorch
      transform or forward-mode AD runs, or autocast is on. It agrees with ``log_softmax`` to rounding, with the
      same NaN and infinity positions and a zero gradient at ignored pixels, and supports double backward.

Args:
    pred: logits ``(B, C, *)``.
    target: int64 class indices ``(B, *)`` in ``[0, C)``, or ``ignore_index``.
    alpha: weight :math:`\alpha \in [0, 1]` of classes :math:`1, \dots, C - 1`; ``None`` weights every class by 1.
    gamma: focusing exponent :math:`\gamma \ge 0`.
    reduction: ``'none'`` (default) keeps the ``(B, C, *)`` map, ``'mean'`` averages it, ``'sum'`` adds it.
    weight: class weights ``(C,)``.
    ignore_index: label whose pixels contribute 0 but stay in the ``'mean'`` denominator.

Returns:
    the loss, a ``(B, C, *)`` map for ``reduction='none'`` and a scalar otherwise.

See Also:
    - :class:`~kornia.losses.FocalLoss`: the module form.
    - :func:`~kornia.losses.binary_focal_loss_with_logits`: a sigmoid per element in place of the softmax over
      the classes.
    - :func:`~kornia.losses.dice_loss`: an overlap loss; ignored pixels leave the average.

Example:
    >>> C = 5  # num_classes
    >>> pred = torch.randn(1, C, 3, 5, requires_grad=True)
    >>> target = torch.randint(C, (1, 3, 5))
    >>> kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'}
    >>> output = focal_loss(pred, target, **kwargs)
    >>> output.backward()

ºr   NN:é   NNúExpected target size ú, got ú1pred and target must be in the same device. Got: ú and ÚmeanrR   NÚinfz*labels must be of dtype torch.int64. Got: z6The number of classes must be >= 1. Got pred of shape )Únum_classesr   r   ç      ð?rK   )r   r   ú$weight must be torch.Tensor or None.úBweight must hold one value per class of pred: got weight of shape ú for pred of shape ú1weight and pred must be in the same device. Got: Únone)r   úInvalid reduction mode: ©ÚBÚCÚ*)r©   rR   éÿÿÿÿr   )!r	   r:   r   r   r   rœ   Ú
isinstanceÚintr   r    Úint64r   r   r“   r—   rT   rM   ÚdetachÚpowr”   r?   Únew_fullÚtensorÚviewr   r%   Úfloat16Úbfloat16r!   r©   ÚtorR   ÚNotImplementedError)r   rG   r™   r›   r�   rŸ   r    Úout_sizerŽ   r�   Útarget_one_hotr–   ÚbaseÚ	saturatedÚfocal_weightÚloss_tmpÚnum_of_classesÚbroadcast_dimsÚ	alpha_facÚlossÚ	acc_dtypes   &&&&&&&              r   Ú
focal_lossrÏ   ¨   sÝ  € ôR �tš_Ô-Ø—
‘
˜1•Ð $§*¡*¨R¥.Õ0€HÜØ	�‰�A�˜&Ÿ,™, q�/Ñ	)×	PÐ	P¨f¯l©l¸2Õ.>À$Ç*Á*ÈRÅ.Ñ.PØ
 ˜z¨°·±¨~Ð>ôô Ø�‰�v—}‘}Ñ$Ø
;¸D¿K¹K¸=ÈÈfÏmÉmÈ_Ð]ôô
 -¨VÓBÑ€Fð 	�_Ñ$÷ 	Uð 	UØ�dˆN÷	Uð 	Uà�×%Ô%œ˜u›Ô%÷	Uð 	Uð �dˆ]×SÐSœz¨%´#´u°Ó>×RÐRÀ1ÈÇ?Ä?ÐQRÄ?ð	 ð .2€NßÜ�V—\‘\¤U§[¡[Ñ0Ð4^Ð_e×_kÑ_kÐ^lÐ2mÔnÜ�T—Z‘Z •] aÑ'Ð+aÐbf×blÑblÐamÐ)nÔoä$ V¯Z©Z¸­]Ó;‰ä  ·Z±ZÀµ]È4Ï;É;Ð^b×^hÑ^hÔiˆàÒØ!×+Ñ+¨AÓ.ˆØÒ%Ø+Õ9ˆNä'¨°kÓK€Mð �×"Ñ"Ó$Õ$€DØ˜‘	€IÜ—;’;˜y¯+©+«-×*;Ñ*;¸EÓ*BÀD×DTÑDTÐU^Ð`cÓDd×DhÑDhÐinÓDoÓp€LàÒ!Ø%×1Ñ1°.ÀAÑ2EÀsÓKˆØ �= =Õ0°>ÕA‰àÒ"Ø)×5Ñ5°{°lÀCÓHˆMØ �= =Õ0ˆà—Z‘Z •]€NØ�T˜Q˜C¤# d§j¡j°¥nÓ"5Õ5Õ5€NØÒßäŸšØ× Ñ  Ó# qÑ(¨(×*;Ñ*;¸BÀÀEÅ	Ó*JÈH×L]ÑL]Ð^`ÐbgÓLhó‰Iô ŸšØ�U•�˜u˜g¨¸!Õ);Õ<Õ<ÀHÇNÁNÐ[c×[jÑ[jôç‰d�>Ó"ð ð ˜xÕ'ˆàÒÜ˜vÐ'MÔNÜØ�\‰\˜!�_ Ñ.×SÐS°6·<±<³>À^Ñ3SØPÐQW×Q]ÑQ]ÐP^Ð^qØ�z‰zˆlðô	
ô
 	Ø�M‰M˜TŸ[™[Ñ(Ø?ÀÇÁ¸ÈeÐTX×T_ÑT_ÐS`Ðaô	
ð
 —‘˜^Ó,ˆØÕ$ˆà�FÔØˆð €Kð 
�fÔ	ßà)1¯©¼E¿M¹MÌ5Ï>É>Ð;ZÔ)ZœŸšÐ`h×`nÑ`nˆIØ—M‘M¨	�MÓ2°^ÕC×GÑGÈÏÉÓWˆDð €Kô —:’:˜hÓ'‰Dð
 €Kð	 
�eÔ	Ü�yŠy˜Ó"ˆð €Kô "Ð$<¸Y¸KÐ"HÓIÐIr   c                  óB   a € ] tR tRtRtRR V 3R llltR R ltRtV ;t# )	Ú	FocalLossiT  a¦  Compute the multi-class focal loss of ``(B, C, *)`` logits against ``(B, *)`` class indices (module form).

``criterion = FocalLoss(alpha, gamma, reduction, weight, ignore_index)`` stores its arguments, and
``loss = criterion(pred, target)`` is :func:`~kornia.losses.focal_loss`. A plain tensor ``weight`` is stored as a
non-persistent buffer, so ``criterion.to(device)`` moves it and ``state_dict`` omits it.

Convention:
    See the Convention block of :func:`~kornia.losses.focal_loss`.

Args:
    alpha: weight :math:`\alpha \in [0, 1]` of classes :math:`1, \dots, C - 1`; ``None`` weights every class by 1.
    gamma: focusing exponent :math:`\gamma \ge 0`.
    reduction: ``'none'`` (default), ``'mean'`` or ``'sum'``, as in :func:`~kornia.losses.focal_loss`.
    weight: class weights ``(C,)``.
    ignore_index: label whose pixels contribute 0 but stay in the ``'mean'`` denominator.

Shape:
    - pred: logits ``(B, C, *)``.
    - target: int64 class indices ``(B, *)`` in ``[0, C)``, or ``ignore_index``.
    - Output: a ``(B, C, *)`` map for ``reduction='none'``, a scalar otherwise.

See Also:
    - :func:`~kornia.losses.focal_loss`: the one-call form.
    - :class:`~kornia.losses.BinaryFocalLossWithLogits`: a sigmoid per element in place of the softmax over the
      classes.
    - :class:`~kornia.losses.DiceLoss`: an overlap loss; ignored pixels leave the average.

Example:
    >>> C = 5  # num_classes
    >>> pred = torch.randn(1, C, 3, 5, requires_grad=True)
    >>> target = torch.randint(C, (1, 3, 5))
    >>> kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'}
    >>> criterion = FocalLoss(**kwargs)
    >>> output = criterion(pred, target)
    >>> output.backward()

c               ó0   € V ^8„  d   QhRRRRRRRRR	R
RR/# )r   r™   rš   r›   rœ   r�   rž   rŸ   r0   r    r¡   r   ÚNoner   )r   s   "r   r   ÚFocalLoss.__annotate__{  sF   € ÷ 8ñ 8àð8ð ð8ð ð	8ð
 'ð8ð $ð8ð 
ñ8r   c                	óÊ   <€ \         SV `  4        Wn        W n        W0n        \        V\        P                  4      '       d   W@n        MV P                  R VRR7       WPn
        R# )rŸ   F©Ú
persistentN)ÚsuperÚ__init__r™   r›   r�   r¸   r   Ú	ParameterrŸ   Úregister_bufferr    )Úselfr™   r›   r�   rŸ   r    Ú	__class__s   &&&&&&€r   rÙ   ÚFocalLoss.__init__{  sP   ø€ ô 	‰ÑÔØ&+Œ
Ø!Œ
Ø'ŒÜ�fœbŸl™l×+Ò+Ø �Kà× Ñ  ¨6¸eÐ ÔDØ+7Ör   c               ó$   € V ^8„  d   QhRRRRRR/# ©r   r   r   rG   r   r   )r   s   "r   r   rÔ   �  s&   € ÷ pñ p˜Lð p°,ð pÀ<ñ pr   c           	     ó†   € \        WV P                  V P                  V P                  V P                  V P
                  4      # )aØ  Compute multi-class focal loss from class logits.

Args:
    pred: Logit tensor with shape ``(B, C, *)``, where :math:`B`
        is batch size, :math:`C` is number of classes, and ``*`` is
        any number of spatial dimensions.
    target: Integer class-label tensor with shape ``(B, *)``.

Returns:
    Focal-loss tensor reduced according to ``self.reduction``. See :func:`~kornia.losses.focal_loss` for
    the class weighting by ``self.alpha`` and ``self.weight``.
)rÏ   r™   r›   r�   rŸ   r    ©rÜ   r   rG   s   &&&r   rj   ÚFocalLoss.forward�  s3   € ô ˜$¨¯
©
°D·J±JÀÇÁÐPT×P[ÑP[Ð]a×]nÑ]nÓoÐor   )r™   r›   r    r�   rŸ   ©ç       @r±   Néœÿÿÿ©	r†   r‡   rˆ   r‰   rŠ   rÙ   rj   rŒ   Ú__classcell__©rÝ   s   @r   rÑ   rÑ   T  s   ø† ñ$÷L8ò 8÷$pó pr   rÑ   c               ó<   € V ^8„  d   QhRRRRRRRRRR	R
RRRRRRR/	# )r   r   r   rG   r™   rš   r›   rœ   r�   rž   Ú
pos_weightr0   rŸ   r    r¡   r   r   )r   s   "r   r   r   �  sm   € ÷ Dñ DØ
ðDàðDð ðDð ð	Dð
 ðDð 'ðDð #ðDð  ðDð ñDr   c                ó  € \        V . RO4       \        V P                  VP                  8H  RV P                   RVP                   24       \        V P                  VP                  8H  RV P                   RVP                   24       \	        W4      w  rVe   V P                  V( R4      p \        P                  P                  V 4      p	\        P                  P                  V ) 4      p
Ve   W¨,          p
W˜,          p	W:,          P                  4       ) V,          V	,          pW9,          P                  4       ) RV,
          ,          V
,          pVe   W+,          pRV,
          V,          pV P                  ^,          pR.^.\        V P                  R,          4      ,          ,           pVe¸   \        VR4       \        VP                  ^ ,          V8H  ;'       d    VP                  4       V8H  R	VP                   R
V P                   24       \        VP                  V P                  8H  RVP                   RV P                   24       VP                  V4      pW[,          pW¼,           pVe¸   \        VR4       \        VP                  ^ ,          V8H  ;'       d    VP                  4       V8H  RVP                   R
V P                   24       \        VP                  V P                  8H  RVP                   RV P                   24       VP                  V4      pWo,          pVR8X  d   TpV# VR8X  d   \        P                  ! V4      pV# VR8X  d   \        P                   ! V4      pV# \#        RV 24      h)aw  Compute the binary focal loss of ``(B, C, *)`` logits against same-shape targets in [0, 1], using sigmoid.

``loss = binary_focal_loss_with_logits(pred, target)`` scales the binary cross entropy of each logit by
:math:`\alpha_t (1 - p_t)^{\gamma}` :cite:`lin2018focal`, so that elements the sigmoid already classifies well
count less. For a target of 0 or 1, :math:`p_t` is the sigmoid probability of the target value, and
:math:`\alpha_t` is ``alpha`` for a target of 1 and ``1 - alpha`` for a target of 0.

.. math::

    \text{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma} \, \text{log}(p_t)

Convention:
    - Every element of ``pred`` is an independent binary logit: ``pred`` is ``(B, C, *)`` with at least two
      dimensions and ``target`` has its shape, with values in ``[0, 1]``. ``'mean'`` and ``'sum'`` reduce the
      loss map as in :func:`~kornia.losses.focal_loss`.
    - ``alpha`` defaults to ``0.25``. A fractional target :math:`t` weights the two terms by :math:`t` and
      :math:`1 - t`:
      :math:`t \alpha (1 - p)^\gamma (-\log p) + (1 - t) (1 - \alpha) p^\gamma (-\log (1 - p))`, where :math:`p`
      is the sigmoid of the logit.
    - ``pos_weight`` and ``weight`` are ``(C,)`` vectors applied along dim 1: ``pos_weight[c]`` scales the
      positive term of channel ``c`` and ``weight[c]`` its whole loss.
    - A target entry equal to ``ignore_index`` (default ``-100``) contributes 0 with zero logit gradient,
      including for non-finite ignored logits, and still counts in the ``'mean'`` denominator,
      as in :func:`~kornia.losses.focal_loss`.
    - :ref:`Losses and metrics <losses-metrics-conventions>` maps this loss onto torchvision's
      ``sigmoid_focal_loss`` and torch's ``pos_weight``.

Args:
    pred: logits ``(B, C, *)``.
    target: targets in ``[0, 1]`` with the shape of ``pred``, or ``ignore_index``.
    alpha: weight :math:`\alpha \in [0, 1]` of the positive term; ``None`` weights both terms by 1.
    gamma: focusing exponent :math:`\gamma \ge 0`.
    reduction: ``'none'`` (default) keeps the map of ``pred``'s shape, ``'mean'`` averages it, ``'sum'`` adds it.
    pos_weight: weight of the positive term of each channel ``(C,)``.
    weight: channel weights ``(C,)``.
    ignore_index: target value whose entries contribute 0 but stay in the ``'mean'`` denominator.

Returns:
    the loss, a map with the shape of ``pred`` for ``reduction='none'`` and a scalar otherwise.

See Also:
    - :class:`~kornia.losses.BinaryFocalLossWithLogits`: the module form.
    - :func:`~kornia.losses.focal_loss`: a softmax over the classes against class indices, for exclusive classes.

Examples:
    >>> C = 3  # num_classes
    >>> pred = torch.randn(1, C, 5, requires_grad=True)
    >>> target = torch.randint(2, (1, C, 5))
    >>> kwargs = {"alpha": 0.25, "gamma": 2.0, "reduction": 'mean'}
    >>> output = binary_focal_loss_with_logits(pred, target, **kwargs)
    >>> output.backward()

r¥   r¦   r§   r¨   rK   r¬   r£   z(pos_weight must be torch.Tensor or None.zJpos_weight must hold one value per class of pred: got pos_weight of shape r¯   z5pos_weight and pred must be in the same device. Got: r­   r®   r°   r±   r©   rR   r²   r³   r·   )r	   r   r:   r   r   r”   r   Ú
functionalÚ
logsigmoidrT   r?   r   r%   r¿   r    r©   rR   rÃ   )r   rG   r™   r›   r�   rë   rŸ   r    rŽ   Úlog_probs_posÚlog_probs_negÚpos_termÚneg_termrÊ   rË   rÉ   rÍ   s   &&&&&&&&         r   Úbinary_focal_loss_with_logitsró   �  s7  € ô~ �tš_Ô-Ü�—‘˜vŸ|™|Ñ+Ð/DÀTÇZÁZÀLÐPVÐW]×WcÑWcÐVdÐ-eÔfÜØ�‰�v—}‘}Ñ$Ø
;¸D¿K¹K¸=ÈÈfÏmÉmÈ_Ð]ôô
 -¨VÓBÑ€FàÒð ×Ñ  ¨cÓ2ˆä"$§-¡-×":Ñ":¸4Ó"@€MÜ"$§-¡-×":Ñ":¸D¸5Ó"A€MàÒà%Õ3ˆØ%Õ3ˆà$Õ4×9Ñ9Ó;Ð;¸fÕDÀ}ÕT€HØ$Õ4×9Ñ9Ó;Ð;¸sÀV½|ÕLÈ}Õ\€HØÒØÕ#ˆØ˜%•K 8Õ+ˆà—Z‘Z •]€NØ�T˜Q˜C¤# d§j¡j°¥nÓ"5Õ5Õ5€NØÒÜ˜zÐ+UÔVÜØ×Ñ˜aÕ  NÑ2×[Ð[°z×7GÑ7GÓ7IÈ^Ñ7[ØXÐYc×YiÑYiÐXjð kØŸ
™
�|ð%ô	
ô
 	Ø×Ñ §¡Ñ,ØCÀJ×DUÑDUÐCVÐV[Ð\`×\gÑ\gÐ[hÐiô	
ð
  —_‘_ ^Ó4ˆ
ØÕ(ˆà%Õ0€HØÒÜ˜vÐ'MÔNÜØ�\‰\˜!�_ Ñ.×SÐS°6·<±<³>À^Ñ3SØPÐQW×Q]ÑQ]ÐP^Ð^qØ�z‰zˆlðô	
ô
 	Ø�M‰M˜TŸ[™[Ñ(Ø?ÀÇÁ¸ÈeÐTX×T_ÑT_ÐS`Ðaô	
ð
 —‘˜^Ó,ˆØÕ$ˆà�FÔØˆð €Kð 
�fÔ	Ü�zŠz˜(Ó#ˆð
 €Kð	 
�eÔ	Ü�yŠy˜Ó"ˆð €Kô "Ð$<¸Y¸KÐ"HÓIÐIr   c                  óB   a € ] tR tRtRtRR V 3R llltR R ltRtV ;t# )	ÚBinaryFocalLossWithLogitsi$  aX  Compute the binary focal loss of ``(B, C, *)`` logits against same-shape targets in [0, 1] (module form).

``criterion = BinaryFocalLossWithLogits(alpha, gamma, reduction, pos_weight, weight, ignore_index)`` stores its
arguments, and ``loss = criterion(pred, target)`` is :func:`~kornia.losses.binary_focal_loss_with_logits`. A plain
tensor ``pos_weight`` or ``weight`` is stored as a non-persistent buffer, so ``criterion.to(device)`` moves it and
``state_dict`` omits it.

Convention:
    See the Convention block of :func:`~kornia.losses.binary_focal_loss_with_logits`. Unlike the function,
    ``alpha`` has no default here.

Args:
    alpha: weight :math:`\alpha \in [0, 1]` of the positive term; ``None`` weights both terms by 1.
    gamma: focusing exponent :math:`\gamma \ge 0`.
    reduction: ``'none'`` (default), ``'mean'`` or ``'sum'``, as in
        :func:`~kornia.losses.binary_focal_loss_with_logits`.
    pos_weight: weight of the positive term of each channel ``(C,)``.
    weight: channel weights ``(C,)``.
    ignore_index: target value whose entries contribute 0 but stay in the ``'mean'`` denominator.

Shape:
    - pred: logits ``(B, C, *)``.
    - target: targets in ``[0, 1]`` with the shape of ``pred``, or ``ignore_index``.
    - Output: a map with the shape of ``pred`` for ``reduction='none'``, a scalar otherwise.

See Also:
    - :func:`~kornia.losses.binary_focal_loss_with_logits`: the one-call form.
    - :class:`~kornia.losses.FocalLoss`: a softmax over the classes against class indices, for exclusive classes.

Examples:
    >>> C = 3  # num_classes
    >>> pred = torch.randn(1, C, 5, requires_grad=True)
    >>> target = torch.randint(2, (1, C, 5))
    >>> kwargs = {"alpha": 0.25, "gamma": 2.0, "reduction": 'mean'}
    >>> criterion = BinaryFocalLossWithLogits(**kwargs)
    >>> output = criterion(pred, target)
    >>> output.backward()

c               ó4   € V ^8„  d   QhRRRRRRRRR	RR
RRR/# )r   r™   rš   r›   rœ   r�   rž   rë   r0   rŸ   r    r¡   r   rÓ   r   )r   s   "r   r   Ú&BinaryFocalLossWithLogits.__annotate__M  sP   € ÷ 8ñ 8àð8ð ð8ð ð	8ð
 +ð8ð 'ð8ð $ð8ð 
ñ8r   c                	ó@  <€ \         SV `  4        Wn        W n        W0n        \        V\        P                  4      '       d   W@n        MV P                  R VRR7       \        V\        P                  4      '       d   WPn
        MV P                  RVRR7       W`n        R# )rë   FrÖ   rŸ   N)rØ   rÙ   r™   r›   r�   r¸   r   rÚ   rë   rÛ   rŸ   r    )rÜ   r™   r›   r�   rë   rŸ   r    rÝ   s   &&&&&&&€r   rÙ   Ú"BinaryFocalLossWithLogits.__init__M  s{   ø€ ô 	‰ÑÔØ&+Œ
Ø!Œ
Ø'ŒÜ�j¤"§,¡,×/Ò/Ø(�Oà× Ñ  ¨zÀeÐ ÔLÜ�fœbŸl™l×+Ò+Ø �Kà× Ñ  ¨6¸eÐ ÔDØ+7Ör   c               ó$   € V ^8„  d   QhRRRRRR/# rà   r   )r   s   "r   r   r÷   d  s!   € ÷ 
ñ 
˜Lð 
°,ð 
À<ñ 
r   c           
     óœ   € \        WV P                  V P                  V P                  V P                  V P
                  V P                  4      # )a”  Compute binary focal loss from logits.

Args:
    pred: Logit tensor with shape ``(B, C, *)``, at least two dimensions.
    target: Binary target tensor with the same shape as ``pred``.

Returns:
    Binary focal-loss tensor reduced according to ``self.reduction``.
    Positive terms may be reweighted by ``self.pos_weight``, and entries
    matching ``self.ignore_index`` contribute 0 when configured.
)ró   r™   r›   r�   rë   rŸ   r    râ   s   &&&r   rj   Ú!BinaryFocalLossWithLogits.forwardd  s>   € ô -Ø˜$Ÿ*™* d§j¡j°$·.±.À$Ç/Á/ÐSW×S^ÑS^Ð`d×`qÑ`qó
ð 	
r   )r™   r›   r    rë   r�   rŸ   )rå   r±   NNræ   rç   ré   s   @r   rõ   rõ   $  s   ø† ñ&÷P8ò 8÷.
ó 
r   rõ   i    i   rä   )g      Ð?rå   r±   NNræ   )"Ú
__future__r   Útypingr   r   r    r   Úkornia.core.checkr   r   r	   Úkornia.core.utilsr
   r   r   Úkornia.losses._utilsr   Úkornia.losses.one_hotr   r   r<   r&   r-   rE   rZ   r)   ÚFunctionr\   r—   rÏ   ÚModulerÑ   ró   rõ   r   r   r   Ú<module>r     s•   ðõ$ #ç  ã Ý ç VÑ Vß MÑ MÝ 3ß >ð #Ð ð €õõ*cõ8ô&9 ˜Ÿ™×/Ñ/ô 9 õx÷ iôXFp�—	‘	ô Fp÷RDôNN
 §	¡	ö N
r   