o
    pi                     @  s   d dl mZ d dlZd dlmZ d dlmZmZmZ er4d dlm	Z	 d dl
mZ d dlmZ d dlmZ G d	d
 d
ejZdddZdS )    )annotationsN)TYPE_CHECKING)distributionindependent	transform)Sequence)Tensor)Distribution)	Transformc                      sZ   e Zd ZU dZded< ded< d fdd	Zg fdddZg fdddZdddZ  Z	S )TransformedDistributiona  
    Applies a sequence of Transforms to a base distribution.

    Args:
        base (Distribution): The base distribution.
        transforms (Sequence[Transform]): A sequence of ``Transform`` .

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(2023)
            >>> from paddle.distribution import transformed_distribution

            >>> d = transformed_distribution.TransformedDistribution(
            ...     paddle.distribution.Normal(0., 1.),
            ...     [paddle.distribution.AffineTransform(paddle.to_tensor(1.), paddle.to_tensor(2.))]
            ... )

            >>> # doctest: +SKIP('random sample')
            >>> print(d.sample([10]))
            Tensor(shape=[10], dtype=float32, place=Place(cpu), stop_gradient=True,
                [ 3.22699189,  1.12264419,  0.50283587,  1.83812487, -2.00740123,
                -2.70338631,  1.26663208,  4.47909021, -0.11529565,  4.32719326])
            >>> print(d.log_prob(paddle.to_tensor(0.5)))
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                -1.64333570)
            >>> # doctest: -SKIP
    r	   baseSequence[Transform]
transformsreturnNonec                   s\  t |tjstdt| dt |tjs tdt| dtdd |D s-tdt	|}|j
|j }|| _|| _|sKt |j
|j d S t|j
|j |jjk rftd|jj dt| d|jjt|jkr|t||jjt|j }||j
|j }|jjtt|j|jj d	 }t |d t||  |t|| d   d S )
Nz1Expected type of 'base' is Distribution, but got .zGExpected type of 'transforms' is Sequence[Transform] or Chain, but got c                 s  s    | ]	}t |tjV  qd S )N)
isinstancer   r
   ).0t r   s/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/distribution/transformed_distribution.py	<genexpr>K   s    z3TransformedDistribution.__init__.<locals>.<genexpr>z1All element of transforms must be Transform type.z.'base' needs to have shape with size at least z
, bug got r   )r   r   r	   	TypeErrortypetypingr   allr   ZChainTransformZbatch_shapeevent_shape_base_transformssuper__init__len_domain
event_rank
ValueErrorr   ZIndependentZforward_shape	_codomainmax)selfr   r   chainZ
base_shapeZtransformed_shapeZtransformed_event_rank	__class__r   r   r    @   sN   

z TransformedDistribution.__init__shapeSequence[int]r   c                 C  &   | j |}| jD ]}||}q	|S )zSample from ``TransformedDistribution``.

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

        Returns:
            [Tensor]: The sample result.
        )r   sampler   forwardr'   r+   xr   r   r   r   r.   m      	
zTransformedDistribution.samplec                 C  r-   )zReparameterized sample from ``TransformedDistribution``.

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

        Returns:
            [Tensor]: The sample result.
        )r   rsampler   r/   r0   r   r   r   r3   {   r2   zTransformedDistribution.rsamplevaluec                 C  s   d}|}t | j}t| jD ]!}||}||jj|jj 7 }|t|	|||jj  }|}q|t| j
||t | j
j 7 }|S )zThe log probability evaluated at value.

        Args:
            value (Tensor): The value to be evaluated.

        Returns:
            Tensor: The log probability.
        g        )r!   r   reversedr   Zinverser"   r#   r%   _sum_rightmostZforward_log_det_jacobianr   log_prob)r'   r4   r7   yr#   r   r1   r   r   r   r7      s   	

z TransformedDistribution.log_prob)r   r	   r   r   r   r   )r+   r,   r   r   )r4   r   r   r   )
__name__
__module____qualname____doc____annotations__r    r.   r3   r7   __classcell__r   r   r)   r   r      s   
 -r   r4   r   nintr   c                 C  s"   |dkr|  tt| dS | S )Nr   )sumlistrange)r4   r?   r   r   r   r6      s   "r6   )r4   r   r?   r@   r   r   )
__future__r   r   r   Zpaddle.distributionr   r   r   collections.abcr   Zpaddler   Z paddle.distribution.distributionr	   Zpaddle.distribution.transformr
   r   r6   r   r   r   r   <module>   s    