o
    * i                     @  sV   d dl mZ d dlmZ d dlmZ er d dlmZ d dlm	Z	 G dd dej
ZdS )	    )annotations)TYPE_CHECKING)distribution)Sequence)Tensorc                      st   e Zd ZdZd fdd	ZedddZedddZg fdddZd ddZ	d ddZ
dddZd!ddZ  ZS )"Independenta  
    Reinterprets some of the batch dimensions of a distribution as event dimensions.

    This is mainly useful for changing the shape of the result of
    :meth:`log_prob`.

    Args:
        base (Distribution): The base distribution.
        reinterpreted_batch_rank (int): The number of batch dimensions to
            reinterpret as event dimensions.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> from paddle.distribution import independent

            >>> beta = paddle.distribution.Beta(paddle.to_tensor([0.5, 0.5]), paddle.to_tensor([0.5, 0.5]))
            >>> print(beta.batch_shape, beta.event_shape)
            (2,) ()
            >>> print(beta.log_prob(paddle.to_tensor(0.2)))
            Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                    [-0.22843921, -0.22843921])
            >>> reinterpreted_beta = independent.Independent(beta, 1)
            >>> print(reinterpreted_beta.batch_shape, reinterpreted_beta.event_shape)
            () (2,)
            >>> print(reinterpreted_beta.log_prob(paddle.to_tensor([0.2,  0.2])))
            Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                    -0.45687842)
    basedistribution.Distributionreinterpreted_batch_rankintreturnNonec                   s   t |tjstdt| d|  k rt|jks*n tdt|j d| || _|| _	|j|j
 }t j|d t|j|  |t|j| d  d d S )Nz1Expected type of 'base' is Distribution, but got r   z)Expected 0 < reinterpreted_batch_rank <= z
, but got )batch_shapeevent_shape)
isinstancer   Distribution	TypeErrortypelenr   
ValueError_base_reinterpreted_batch_rankr   super__init__)selfr   r
   shape	__class__ k/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/distribution/independent.pyr   <   s&   
zIndependent.__init__r   c                 C     | j jS N)r   meanr   r   r   r   r"   T      zIndependent.meanc                 C  r    r!   )r   variancer#   r   r   r   r%   X   r$   zIndependent.variancer   Sequence[int]c                 C  s   | j |S r!   )r   sample)r   r   r   r   r   r'   \   s   zIndependent.samplevaluec                 C  s   |  | j|| jS r!   )_sum_rightmostr   log_probr   r   r(   r   r   r   r*   _   s   zIndependent.log_probc                 C  s   |  | S r!   )r*   expr+   r   r   r   probd   s   zIndependent.probc                 C  s   |  | j | jS r!   )r)   r   entropyr   r#   r   r   r   r.   g   s   zIndependent.entropync                 C  s"   |dkr| tt| dS |S )Nr   )sumlistrange)r   r(   r/   r   r   r   r)   l   s   "zIndependent._sum_rightmost)r   r	   r
   r   r   r   )r   r   )r   r&   r   r   )r(   r   r   r   )r(   r   r/   r   r   r   )__name__
__module____qualname____doc__r   propertyr"   r%   r'   r*   r-   r.   r)   __classcell__r   r   r   r   r      s     


r   N)
__future__r   typingr   Zpaddle.distributionr   collections.abcr   Zpaddler   r   r   r   r   r   r   <module>   s   