§
    ’©ºj(  ã                   óÖ   — d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 deded	e
d
eeef         fd„Z	 ddedeeef         ded         d
efd„Z	 ddeded	e
ded         d
ef
d„ZdS )é    )ÚUnionN)ÚTensor)ÚLiteral)Ú_check_same_shape)Ú_safe_xlogyÚpÚqÚlog_probÚreturnc                 óÈ  — t          | |¦  «         | j        dk    s|j        dk    r t          d| j        › d|j        › d�¦  «        ‚| j        d         }|r/t	          j        |                      ¦   «         | |z
  z  d¬¦  «        }n[| |                      dd¬	¦  «        z  } ||                     dd¬	¦  «        z  }t          | | |z  ¦  «                             d¬¦  «        }||fS )
a—  Update and returns KL divergence scores for each observation and the total number of observations.

    Args:
        p: data distribution with shape ``[N, d]``
        q: prior or approximate distribution with shape ``[N, d]``
        log_prob: bool indicating if input is log-probabilities or probabilities. If given as probabilities,
            will normalize to make sure the distributes sum to 1

    é   z4Expected both p and q distribution to be 2D but got z and z respectivelyr   éÿÿÿÿ)ÚaxisT)r   Úkeepdim)r   ÚndimÚ
ValueErrorÚshapeÚtorchÚsumÚexpr   )r   r	   r
   ÚtotalÚmeasuress        ú€/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/regression/kl_divergence.pyÚ_kld_updater      sï   € õ �a˜ÑÔÐØ„v�‚{€{�a”f ’k�kÝÐrÐPQÔPVÐrÐrÐ]^Ô]cÐrÐrÐrÑsÔsÐsàŒG�AŒJ€EØð 6Ý”9˜QŸUšU™WœW¨¨A©Ñ.°RÐ8Ñ8Ô8ˆˆà�—’˜2 t�Ñ,Ô,Ñ,ˆØ�—’˜2 t�Ñ,Ô,Ñ,ˆÝ˜q ! a¡%Ñ(Ô(×,Ò,°"Ð,Ñ5Ô5ˆà�Uˆ?Ðó    Úmeanr   r   Ú	reduction)r   r   ÚnoneNc                 óŽ   — |dk    r|                       ¦   «         S |dk    r|                       ¦   «         |z  S |�|dk    r| S | |z  S )aµ  Compute the KL divergenece based on the type of reduction.

    Args:
        measures: Tensor of KL divergence scores for each observation
        total: Number of observations
        reduction:
            Determines how to reduce over the ``N``/batch dimension:

            - ``'mean'`` [default]: Averages score across samples
            - ``'sum'``: Sum score across samples
            - ``'none'`` or ``None``: Returns score per sample

    Example:
        >>> p = torch.tensor([[0.36, 0.48, 0.16]])
        >>> q = torch.tensor([[1/3, 1/3, 1/3]])
        >>> measures, total = _kld_update(p, q, log_prob=False)
        >>> _kld_compute(measures, total)
        tensor(0.0853)

    r   r   Nr   )r   )r   r   r   s      r   Ú_kld_computer    2   s[   € ð. �EÒÐØ�|Š|‰~Œ~ÐØ�FÒÐØ�|Š|‰~Œ~ Ñ%Ð%ØÐ˜I¨Ò/Ð/ØˆØ�eÑÐr   Fc                 óL   — t          | ||¦  «        \  }}t          |||¦  «        S )a©  Compute `KL divergence`_.

    .. math::
        D_{KL}(P||Q) = \sum_{x\in\mathcal{X}} P(x) \log\frac{P(x)}{Q{x}}

    Where :math:`P` and :math:`Q` are probability distributions where :math:`P` usually represents a distribution
    over data and :math:`Q` is often a prior or approximation of :math:`P`. It should be noted that the KL divergence
    is a non-symmetrical metric i.e. :math:`D_{KL}(P||Q) \neq D_{KL}(Q||P)`.

    Args:
        p: data distribution with shape ``[N, d]``
        q: prior or approximate distribution with shape ``[N, d]``
        log_prob: bool indicating if input is log-probabilities or probabilities. If given as probabilities,
            will normalize to make sure the distributes sum to 1
        reduction:
            Determines how to reduce over the ``N``/batch dimension:

            - ``'mean'`` [default]: Averages score across samples
            - ``'sum'``: Sum score across samples
            - ``'none'`` or ``None``: Returns score per sample

    Example:
        >>> from torch import tensor
        >>> p = tensor([[0.36, 0.48, 0.16]])
        >>> q = tensor([[1/3, 1/3, 1/3]])
        >>> kl_divergence(p, q)
        tensor(0.0853)

    )r   r    )r   r	   r
   r   r   r   s         r   Úkl_divergencer"   R   s,   € õ@ " ! Q¨Ñ1Ô1�O€HˆeÝ˜ %¨Ñ3Ô3Ð3r   )r   )Fr   )Útypingr   r   r   Útyping_extensionsr   Útorchmetrics.utilities.checksr   Útorchmetrics.utilities.computer   ÚboolÚtupleÚintr   r    r"   © r   r   ú<module>r+      sK  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6ð�6ð ˜fð °ð ¸¸vÀs¸{Ô9Kð ð ð ð ð4 djðð ØðØ" 3¨ ;Ô/ðØ<CÐD_Ô<`ðàðð ð ð ðB ekð!4ð !4Øð!4Øð!4Ø$(ð!4Ø=DÐE`Ô=að!4àð!4ð !4ð !4ð !4ð !4ð !4r   