+
    åûj¶  ã                  ó˜   € R t ^ RIHt ^ RIt^ RIt^ RIHu Ht ^ RI	H
t
Ht R R ltR R ltR R	 ltR
 R ltRR R lltRR R lltR# )zALosses based on the divergence between probability distributions.)ÚannotationsN)ÚKORNIA_CHECKÚKORNIA_CHECK_SHAPEc               ó$   € V ^8„  d   QhRRRRRR/# ©é   Úpútorch.TensorÚqÚreturn© )Úformats   "ÚTC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\losses\divergence.pyÚ__annotate__r      s!   € ÷ 2ñ 2�,ð 2 <ð 2°Lñ 2ó    c                ó  € V P                   w  r#rEV P                  W#,          WE,          4      p VP                  W#,          WE,          4      pV ^ 8H  V^ 8¬  ,          \        P                  ! V4      ,          pV P	                  VR4      p VP	                  VR4      p\        P
                  P                  4       '       d   VP                  4       pM†VP                  \        P                  \        P                  39   dH   VP                  P                  R8X  d-   \        P                  ! R4      '       d   VP                  4       pVP                  4       p\        P                   ! WpRR7      pVP#                  R4      P%                  W#4      # )é    ç      ð?ÚcpuÚnone)Ú	reductionéÿÿÿÿ)ÚshapeÚreshapeÚtorchÚisfiniteÚmasked_fillÚjitÚis_scriptingÚlogÚdtypeÚfloat16Úbfloat16ÚdeviceÚtypeÚis_autocast_enabledÚfloatÚFÚkl_divÚsumÚview)	r   r
   ÚbatchÚchansÚheightÚwidthÚemptyÚlog_qÚunsummed_kls	   &&       r   Ú
_kl_div_2dr2      s  € ð
 #$§'¡'Ñ€E�&Ø	�	‰	�%•- ¥Ó0€AØ	�	‰	�%•- ¥Ó0€AØ�!‰V˜˜Q™Õ¤%§.¢.°Ó"3Õ3€EØ	�‰�e˜SÓ!€AØ	�‰�e˜SÓ!€Aô ‡y�y×Ñ×Òà—‘“‰à�7‰7”u—}‘}¤e§n¡nÐ5Ô5¸!¿(¹(¿-¹-È5Ô:PÔUZ×UnÒUnÐot×UuÒUuØ—‘“	ˆAØ—‘“ˆÜ—(’(˜5¨vÔ6€Kà�?‰?˜2Ó×#Ñ# EÓ1Ð1r   c               ó$   € V ^8„  d   QhRRRRRR/# r   r   )r   s   "r   r   r   7   s!   € ÷ :ñ :�,ð : <ð :°Lñ :r   c                óz  € \         P                  ! V P                  VP                  4      pV P                  \         P                  \         P                  39   d   V P                  4       p VP                  \         P                  \         P                  39   d   VP                  4       pW,           P                  V ^ 8H  V^ 8H  ,          R4      P                  4       p. pW3W33 F§  w  rVV^ 8H  V^ 8¬  ,          \         P                  ! V4      ,          pVP                  VR4      P                  4       V,
          \        P                  ! R4      ,           P                  VR4      pVP                  WX,          P                  R4      4       K©  	  RV^ ,          V^,          ,           ,          P                  V4      # )r   r   g       @g        g      à?)éþÿÿÿr   )r   Úpromote_typesr    r!   r"   r&   r   r   r   ÚmathÚappendr)   Úto)	r   r
   Úoutput_dtypeÚlog_sumÚtermsÚaÚbr/   Ú	log_ratios	   &&       r   Ú
_js_div_2dr@   7   sB  € ä×&Ò& q§w¡w°·±Ó8€Là‡w�w”5—=‘=¤%§.¡.Ð1Ô1Ø�G‰G‹IˆØ‡w�w”5—=‘=¤%§.¡.Ð1Ô1Ø�G‰G‹Iˆð �u×!Ñ! 1¨¡6¨a°1©fÕ"5°sÓ;×?Ñ?ÓA€GØ€EØ�˜!˜Ó ‰ˆà�a‘˜A ™FÕ#¤e§n¢n°QÓ&7Õ7ˆØ—]‘] 5¨#Ó.×2Ñ2Ó4°wÕ>ÄÇÂÈ#ÃÕN×[Ñ[Ð\aÐcfÓgˆ	Ø�‰�a•m×(Ñ(¨Ó2Ö3ñ	 !ð
 �5˜•8˜e A�hÕ&Õ'×+Ñ+¨LÓ9Ð9r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úlossesr	   r   Ústrr   r   )r   s   "r   r   r   L   s&   € ÷ Fñ F˜ð F°#ð F¸,ñ Fr   c                ó¤   € VR 8X  d   V # VR8X  d   \         P                  ! V 4      # VR8X  d   \         P                  ! V 4      # \        RV 24      h)r   Úmeanr)   zInvalid reduction mode: )r   rE   r)   ÚNotImplementedError)rB   r   s   &&r   Ú_reduce_lossrG   L   sP   € Ø�FÔØˆØ�FÔÜ�zŠz˜&Ó!Ð!Ø�EÔÜ�yŠy˜Ó Ð Ü
Ð 8¸¸ÐDÓ
EÐEr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úpredr	   Útargetr   ÚNoner   )r   s   "r   r   r   V   s!   € ÷ ñ ˜,ð °ð Àñ r   c                óœ   € \        V . RO4       \        V P                  VP                  8H  RV P                   RVP                   24       R# )ÚBz.pred and target shapes must be the same. Got: z and N)rM   ÚNÚHÚW)r   r   r   )rI   rJ   s   &&r   Ú_check_heatmapsrQ   V   sF   € Ü�tÒ1Ô2ÜØ�
‰
�f—l‘lÑ"Ð&TÐUY×U_ÑU_ÐT`Ð`eÐfl×frÑfrÐesÐ$tör   c               ó(   € V ^8„  d   QhRRRRRRRR/# ©r   rI   r	   rJ   r   rC   r   r   )r   s   "r   r   r   ]   s)   € ÷ (=ñ (=˜ð (=¨|ð (=Èð (=ÐYeñ (=r   c                óB   € \        W4       \        \        W4      V4      # )aõ  Compute the Jensen-Shannon divergence between heatmaps.

``loss = js_div_loss_2d(pred, target)`` is the symmetric divergence of each of the ``B N`` pairs of ``(H, W)``
probability heatmaps.

.. math::

    \text{JSD}(P, Q) = \frac{1}{2} \mathrm{KL}(P \,\|\, M) + \frac{1}{2} \mathrm{KL}(Q \,\|\, M), \quad
    M = \frac{P + Q}{2}

Convention:
    - The divergence is in nats and at most :math:`\ln 2`. The inputs and the reductions are those of
      :func:`~kornia.losses.kl_div_loss_2d`; see its Convention block.
    - A cell that is zero in both inputs contributes zero, including to the gradients. Both inputs must have
      the same shape; unsupported reductions raise ``NotImplementedError``.
    - The mixture is evaluated in log space so halving a tiny probability cannot round it to zero.
      Float16 and bfloat16 inputs are evaluated in float32. The returned loss retains the promoted input dtype.

Args:
    pred: predicted heatmaps with shape ``(B, N, H, W)``.
    target: target heatmaps with the same shape.
    reduction: ``'mean'`` (default) averages the ``B N`` divergences, ``'sum'`` adds them, ``'none'`` returns them
      with shape ``(B, N)``.

Returns:
    the loss, a scalar or, for ``reduction='none'``, with shape ``(B, N)``.

See Also:
    - :func:`~kornia.losses.kl_div_loss_2d`: not symmetric, and ``inf`` where ``pred`` is zero and ``target`` is
      positive.

Examples:
    >>> pred = torch.full((1, 1, 2, 4), 0.125)
    >>> loss = js_div_loss_2d(pred, pred)
    >>> loss.item()
    0.0

)rQ   rG   r@   ©rI   rJ   r   s   &&&r   Újs_div_loss_2drV   ]   s   € ôN �DÔ!Üœ
 6Ó0°)Ó<Ð<r   c               ó(   € V ^8„  d   QhRRRRRRRR/# rS   r   )r   s   "r   r   r   ˆ   s)   € ÷ ,=ñ ,=˜ð ,=¨|ð ,=Èð ,=ÐYeñ ,=r   c                óB   € \        W4       \        \        W4      V4      # )aë  Compute the Kullback-Leibler divergence KL(target || pred) between heatmaps.

``loss = kl_div_loss_2d(pred, target)`` is the divergence of each of the ``B N`` pairs of ``(H, W)`` probability
heatmaps, in nats.

.. math::

    \mathrm{KL}(\text{target} \,\|\, \text{pred}) = \sum_{H \times W} \text{target}
    \left(\log \text{target} - \log \text{pred}\right)

Convention:
    - The order is that of torch's ``F.kl_div(pred.log(), target)``.
      :ref:`Losses and metrics <losses-metrics-conventions>` maps it onto torch, scipy and torchmetrics.
    - The inputs are probabilities, each :math:`(b, n)` slice a distribution over :math:`H \times W`. The log is
      taken inside, so log-probabilities give NaN, and nothing is normalised. A zero in ``pred`` where ``target``
      is positive gives ``inf``.
    - The default ``'mean'`` averages over the :math:`B N` distributions, not over every element as torch's does.
    - Both inputs must have the same shape. Unsupported reductions, including torch's ``'batchmean'``, raise
      ``NotImplementedError``. A zero in ``target`` with a finite nonnegative ``pred`` contributes zero to the
      value and gradients, including when both entries are zero.
    - Under CPU autocast, the logarithm of a float16/bfloat16 ``pred`` is taken in float32, as CUDA and MPS
      autocast already do; ``F.kl_div`` alone would promote to float32 only after that logarithm had been rounded.

Args:
    pred: predicted heatmaps with shape ``(B, N, H, W)``.
    target: target heatmaps with the same shape.
    reduction: ``'mean'`` (default) averages the ``B N`` divergences, ``'sum'`` adds them, ``'none'`` returns them
      with shape ``(B, N)``.

Returns:
    the loss, a scalar or, for ``reduction='none'``, with shape ``(B, N)``.

See Also:
    - :func:`~kornia.losses.js_div_loss_2d`: symmetric and at most :math:`\ln 2`, finite where ``pred`` is zero.

Examples:
    >>> pred = torch.full((1, 1, 2, 4), 0.125)
    >>> loss = kl_div_loss_2d(pred, pred)
    >>> loss.item()
    0.0

)rQ   rG   r2   rU   s   &&&r   Úkl_div_loss_2drY   ˆ   s   € ôV �DÔ!Üœ
 6Ó0°)Ó<Ð<r   )rE   )Ú__doc__Ú
__future__r   r7   r   Útorch.nn.functionalÚnnÚ
functionalr'   Úkornia.core.checkr   r   r2   r@   rG   rQ   rV   rY   r   r   r   Ú<module>r`      sB   ðñ$ Iå "ã ã ß Ð ç >õ2õ2:õ*Fõ÷(=÷V,=ñ ,=r   