o
    yi5                     @   sh   d dl mZ d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ G dd	 d	e
ZdS )
    )AnyN)Tensor)Literal)_kld_compute_kld_update)Metric)dim_zero_catc                	       s   e Zd ZU dZdZeed< dZeed< dZeed< e	ed< 		dd	ed
e
d deddf fddZde	de	ddfddZde	fddZ  ZS )KLDivergencea  Computes the `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-symetrical metric i.e. :math:`D_{KL}(P||Q) \neq D_{KL}(Q||P)`.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``p`` (:class:`~torch.Tensor`): a data distribution with shape ``(N, d)``
    - ``q`` (:class:`~torch.Tensor`): prior or approximate distribution with shape ``(N, d)``

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``kl_divergence`` (:class:`~torch.Tensor`): A tensor with the KL divergence

    Args:
        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

        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        TypeError:
            If ``log_prob`` is not an ``bool``.
        ValueError:
            If ``reduction`` is not one of ``'mean'``, ``'sum'``, ``'none'`` or ``None``.

    .. note::
        Half precision is only support on GPU for this metric

    Example:
        >>> import torch
        >>> from torchmetrics.functional import kl_divergence
        >>> p = torch.tensor([[0.36, 0.48, 0.16]])
        >>> q = torch.tensor([[1/3, 1/3, 1/3]])
        >>> kl_divergence(p, q)
        tensor(0.0853)
    Tis_differentiableFhigher_is_betterfull_state_updatetotalmeanlog_prob	reductionr   sumnoneNkwargsreturnNc                    s   t  jdi | t|tstd| || _g d}||vr*td| d| || _| jdv r>| jdt	
ddd	 n| jdg d
d	 | jdt	
ddd	 d S )Nz0Expected argument `log_prob` to be bool but got r   z+Expected argument `reduction` to be one of z	 but got )r   r   measuresg        r   )dist_reduce_fxcatr   r    )super__init__
isinstancebool	TypeErrorr   
ValueErrorr   	add_statetorchtensor)selfr   r   r   allowed_reduction	__class__r   Y/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/regression/kl_divergence.pyr   N   s   

zKLDivergence.__init__pqc                 C   sZ   t ||| j\}}| jd u s| jdkr| j| d S |  j| 7  _|  j|7  _d S Nr   )r   r   r   r   appendr   r   )r#   r(   r)   r   r   r   r   r'   updated   s
   zKLDivergence.updatec                 C   s4   | j d u s
| j dkrt| jn| j}t|| j| j S r*   )r   r   r   r   r   )r#   r   r   r   r'   computel   s   $zKLDivergence.compute)Fr   )__name__
__module____qualname____doc__r
   r   __annotations__r   r   r   r   r   r   r,   r-   __classcell__r   r   r%   r'   r	      s&   
 /r	   )typingr   r!   r   typing_extensionsr   0torchmetrics.functional.regression.kl_divergencer   r   torchmetrics.metricr   torchmetrics.utilities.datar   r	   r   r   r   r'   <module>   s   