o
    * i                      @  s   d dl mZ d dlZd dlZd dlmZ d dlZd dlZd dl	m
Z
 d dlmZ er>d dlmZ d dlmZ d dlmZmZ G d	d
 d
eZdS )    )annotationsN)TYPE_CHECKING)	framework)TransformedDistribution)Sequence)Tensor)	TransformUniformc                      s   e Zd ZU dZded< ded< ded< ded< d# fddZed$ddZed$ddZed$ddZ	d%ddZ
d%ddZd%ddZd$ddZg fd&dd Zg fd&d!d"Z  ZS )'Gumbela  The Gumbel distribution with location `loc` and `scale` parameters.

    Mathematical details

    The probability density function (pdf) is

    .. math::

        pdf(x; mu, sigma) = exp(-(x - mu) / sigma - exp(-(x - mu) / sigma)) / sigma


    In the above equation:

    * :math:`loc = \mu`: is the mean.
    * :math:`scale = \sigma`: is the std.

    Args:
        loc(int|float|tensor): The mean of gumbel distribution.The data type is int, float, tensor.
        scale(int|float|tensor): The std of gumbel distribution.The data type is int, float, tensor.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.distribution.gumbel import Gumbel

            >>> # Gumbel distributed with loc=0, scale=1
            >>> dist = Gumbel(paddle.full([1], 0.0), paddle.full([1], 1.0))

            >>> # doctest: +SKIP
            >>> print(dist.sample([2]))
            Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0.40484068],
            [3.19400501]])

            >>> print(dist.rsample([2]))
            Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[-0.95093185],
            [ 0.32422572]])

            >>> # doctest: -SKIP
            >>> value = paddle.full([1], 0.5)
            >>> print(dist.prob(value))
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.33070430])

            >>> print(dist.log_prob(value))
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [-1.10653067])

            >>> print(dist.cdf(value))
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.54523921])

            >>> print(dist.entropy())
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [1.57721567])
    r   locscaler	   	base_distztuple[Transform, ...]
transformsfloat | TensorreturnNonec              	     s  t |tjtjtjjfstdt	| t |tjtjtjjfs*tdt	| t |tjr7tj
d|d}t |tjrDtj
d|d}|j|jkrVt||g\| _| _n||| _| _tjdd}tjt| jt|jt| jtd|j | _d| _t | j| j d S )Nz5Expected type of loc is Real|Variable|Value, but got z7Expected type of scale is Real|Variable|Value, but got  )shape
fill_valueZfloat32)dtype   )
isinstancenumbersRealr   VariablepaddleZpirValue	TypeErrortypefullr   Zbroadcast_tensorsr   r   npfinfodistributionr	   Z	full_likefloatZtinyZepsr   r   super__init__)selfr   r   r!   	__class__r   f/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/distribution/gumbel.pyr%   c   s4   zGumbel.__init__c                 C  s   | j | jtj  S )u`  Mean of distribution

        The mean is

        .. math::

            mean = \mu + \sigma * γ

        In the above equation:

        * :math:`loc = \mu`: is the location parameter.
        * :math:`scale = \sigma`: is the scale parameter.
        * :math:`γ`: is the euler's constant.

        Returns:
            Tensor: mean value.

        )r   r   r    euler_gammar&   r   r   r)   mean   s   zGumbel.meanc                 C  s6   t j| jjtjtj | jjd}t | jd| d S )a  Variance of distribution.

        The variance is

        .. math::

            variance = \sigma^2 * \pi^2 / 6

        In the above equation:

        * :math:`scale = \sigma`: is the scale parameter.

        Returns:
            Tensor: The variance value.

        r   r   r         )	r   r   r   r   mathpir   r   pow)r&   tempr   r   r)   variance   s   
zGumbel.variancec                 C  s   t | jS )a  Standard deviation of distribution

        The standard deviation is

        .. math::

            stddev = \sqrt{\sigma^2 * \pi^2 / 6}

        In the above equation:
        * :math:`scale = \sigma`: is the scale parameter.

        Returns:
            Tensor: std value
        )r   sqrtr4   r+   r   r   r)   stddev   s   zGumbel.stddevvaluec                 C  sF   | j || j j | j| j j }t|t| | j|j S )zProbability density/mass function

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor: probability.The data type is same with value.

        )r   astyper   r   r   exp)r&   r7   yr   r   r)   prob   s   
"zGumbel.probc                 C  s   t | |S )zLog probability density/mass function.

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor: log probability.The data type is same with value.

        )r   logr;   r&   r7   r   r   r)   log_prob   s   
zGumbel.log_probc                 C  s0   t t || j|j  | j|j  S )zCumulative distribution function.
        Args:
            value (Tensor): value to be evaluated.

        Returns:
            Tensor: cumulative probability of value.

        )r   r9   r   r8   r   r   r=   r   r   r)   cdf   s   	z
Gumbel.cdfc                 C  s   t | jd tj S )z`Entropy of Gumbel distribution.

        Returns:
            Entropy of distribution.

        r   )r   r<   r   r    r*   r+   r   r   r)   entropy   s   zGumbel.entropyr   Sequence[int]c                 C  s6   t   | |W  d   S 1 sw   Y  dS )zSample from ``Gumbel``.

        Args:
            shape (Sequence[int], optional): The sample shape. Defaults to [].

        Returns:
            Tensor: A tensor with prepended dimensions shape.The data type is float32.

        N)r   Zno_gradrsample)r&   r   r   r   r)   sample   s   

$zGumbel.samplec                 C  sn   t j }t jt j| jjd| jjdt 	| j }t j| j| j }|
||
|| j|S )a   reparameterized sample
        Args:
            shape (Sequence[int], optional): 1D `int32`. Shape of the generated samples. Defaults to [].

        Returns:
            Tensor: A tensor with prepended dimensions shape.The data type is float32.

        r   r-   )r   r"   ZExpTransformZAffineTransformr   r   r   r   r   Z	ones_likeforwardZinverse_baserC   )r&   r   Z	exp_transZaffine_trans_1Zaffine_trans_2r   r   r)   rB   
  s"   
	
zGumbel.rsample)r   r   r   r   r   r   )r   r   )r7   r   r   r   )r   rA   r   r   )__name__
__module____qualname____doc____annotations__r%   propertyr,   r4   r6   r;   r>   r?   r@   rC   rB   __classcell__r   r   r'   r)   r
   "   s&   
 ;#



	r
   )
__future__r   r0   r   typingr   numpyr    r   Zpaddle.baser   Z,paddle.distribution.transformed_distributionr   collections.abcr   r   Zpaddle.distributionr   r	   r
   r   r   r   r)   <module>   s   