
    zj!.                        d dl m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
mZ d dlmZ d dlmZmZ erd dlmZ d d	lmZ d d
lmZ d dlmZmZ  G d d          ZdS )    )annotationsN)TYPE_CHECKING)_C_ops)check_variable_and_dtypeconvert_dtype)Variable)in_dynamic_or_pir_modein_pir_mode)Sequence)	TypeGuard)Tensor)NestedNumericSequence
TensorLikec                      e Zd ZdZ	 d,d- fdZed.d	            Zed.d
            Zed/d            Zed/d            Z	g fd0dZ
g fd0dZd/dZd1dZd2dZd2dZd2dZd3dZd4dZd5d Zd6d"Z	 d7d8d(Z	 d7d9d*Zd5d+Z xZS ):Distributiona>  
    The abstract base class for probability distributions. Functions are
    implemented in specific distributions.

    Args:
        batch_shape(Sequence[int], optional):  independent, not identically
            distributed draws, aka a "collection" or "bunch" of distributions.
        event_shape(Sequence[int], optional): the shape of a single
            draw from the distribution; it may be dependent across dimensions.
            For scalar distributions, the event shape is []. For n-dimension
            multivariate distribution, the event shape is [n].
     batch_shapeSequence[int]event_shapereturnNonec                    t          |t                    r|nt          |          | _        t          |t                    r|nt          |          | _        t	                                                       d S N)
isinstancetuple_batch_shape_event_shapesuper__init__)selfr   r   	__class__s      p/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/distribution.pyr   zDistribution.__init__6   sw    
 +u--$KK{## 	 +u--$KK{## 	 	    c                    | j         S )zeReturns batch shape of distribution

        Returns:
            Sequence[int]: batch shape
        )r   r    s    r"   r   zDistribution.batch_shapeF          r#   c                    | j         S )zeReturns event shape of distribution

        Returns:
            Sequence[int]: event shape
        )r   r%   s    r"   r   zDistribution.event_shapeO   r&   r#   r   c                    t           )zMean of distributionNotImplementedErrorr%   s    r"   meanzDistribution.meanX   
     "!r#   c                    t           )zVariance of distributionr)   r%   s    r"   variancezDistribution.variance]   r,   r#   shapec                    t           )zSampling from the distribution.r)   r    r/   s     r"   samplezDistribution.sampleb       !!r#   c                    t           )zreparameterized sampler)   r1   s     r"   rsamplezDistribution.rsamplef   r3   r#   c                    t           )z The entropy of the distribution.r)   r%   s    r"   entropyzDistribution.entropyj   r3   r#   otherc                    t           )z7The KL-divergence between self distributions and other.r)   )r    r8   s     r"   kl_divergencezDistribution.kl_divergencen   r3   r#   valuec                P    |                      |                                          S )zProbability density/mass function evaluated at value.

        Args:
            value (Tensor): value which will be evaluated
        )log_probexpr    r;   s     r"   probzDistribution.probr   s"     }}U##'')))r#   c                    t           )z&Log probability density/mass function.r)   r?   s     r"   r=   zDistribution.log_probz   r3   r#   c                    t           )zProbability density/mass function.

        Note:

            This method will be deprecated in the future, please use `prob`
            instead.
        r)   r?   s     r"   probszDistribution.probs~   s
     "!r#   sample_shapeSequence[int] | Tensorc                t    t          |          t          | j                  z   t          | j                  z   S )zcompute shape of the sample

        Args:
            sample_shape (Sequence[int]|Tensor): sample shape

        Returns:
            Tensor: generated sample data shape
        )r   r   r   )r    rD   s     r"   _extend_shapezDistribution._extend_shape   s:     ,D%&&'D%&&'	
r#   args"TensorLike | NestedNumericSequenceTypeGuard[Tensor]c                    d}d}|D ]-}t          |t          t          j        j        f          rd}+d}.|r|rt          d          |S )z
        Argument validation for distribution args
        Args:
            value (float, list, numpy.ndarray, Tensor)
        Raises
            ValueError: if one argument is Tensor, all arguments should be Tensor
        FTz9if one argument is Tensor, all arguments should be Tensor)r   r   paddlepirValue
ValueError)r    rH   is_variable	is_numberargs        r"   _validate_argszDistribution._validate_args   ss     	 	! 	!C#&**:;<< !" 		 	9 	K   r#   tuple[Tensor, ...]c           
        g }g }d}|D ]}t          |t          t          t          t          j        t          t          j        j	        f          st          dt          |                     t          |t          j        j	                  r|                    |           t	          j        |          }|j        }t          |          dk    r<t          |          dk    rt!          j        d           |                    d          }||z   }|                    |            |j        }|D ]}t          |t          j        j	                  r|                    |           7t	          j        ||          \  }	}
t)                      rt          j        |	j                  }n t          j                            |          }t          j        |	|           |                    |           t          |          S )z
        Argument convert args to Tensor

        Args:
            value (float, list, numpy.ndarray, Tensor)
        Returns:
            Tensor of args.
        g        ZType of input args must be float, list, tuple, numpy.ndarray or Tensor, but received type float32float64zadata type of argument only support float32 and float64, your argument will be convert to float32.dtype)r   floatlistr   npndarrayr   rL   rM   rN   	TypeErrortypeappendarrayrZ   strwarningswarnastypebroadcast_arraysr
   zerosr/   tensorcreate_tensorassign)r    rH   
numpy_argsvariable_argstmprR   arg_np	arg_dtyperZ   arg_broadcasted_arg_variables               r"   
_to_tensorzDistribution._to_tensor   s    
 	& 	&CeRZ6:;KL    |quvyqzqz||   #vz/00 !!#&&&Xc]]FI9~~**y>>Y.. M{    y11,Cf%%%%	 	/ 	/C#vz/00 $$S)))!#!4S#!>!>OQ}} H%|O,ABB%}:::GGM/<888  ....]###r#   paramc                   t          j        |          r|                    |j                  S t	                      rxt                      rt          |dddgd           |j        |j        k    rDt          |j                  dv r.t          j	        d           t          j        ||j                  S |S t          |dddgd           |j        |j        k    r/t          j	        d           t          j        ||j                  S |S )a  
        Log_prob and probs methods have input ``value``, if value's dtype is different from param,
        convert value's dtype to be consistent with param's dtype.

        Args:
            param (Tensor): low and high in Uniform class, loc and scale in Normal class.
            value (Tensor): The input tensor.

        Returns:
            value (Tensor): Change value's dtype if value's dtype is different from param.
        r;   rW   rX   r=   )rW   rX   ztdtype of input 'value' needs to be the same as parameters of distribution class. dtype of 'value' will be converted.rY   )rL   
is_complexrf   rZ   r	   r
   r   r   rd   re   r   cast)r    ru   r;   s      r"   _check_values_dtype_in_probsz)Distribution._check_values_dtype_in_probs   s5    U## 	-<<,,,!## 	}} (7Y	$:J   {ek))mEK.H.H M / /  K   {5%+666L 	"		
 	
 	
 ;%+%%M G   ;uEK8888r#   FrC   float | Tensor	is_binaryboolc                    |r*t          j        |          t          j        |           z
  nt          j        |          S )a  
        Converts probabilities into logits. For the binary, probs denotes the
        probability of occurrence of the event indexed by `1`. For the
        multi-dimensional, values of last axis denote the probabilities of
        occurrence of each of the events.
        )rL   loglog1p)r    rC   r{   s      r"   _probs_to_logitszDistribution._probs_to_logits  s>     #VZuf!5!555E""	
r#   logitsc                    |r$t           j        j                            |          n%t           j        j                            |d          S )z
        Converts logits into probabilities. For the binary, each value denotes
        log odds, whereas for the multi-dimensional case, the values along the
        last dimension denote the log probabilities of the events.
        )axis)rL   nn
functionalsigmoidsoftmax)r    r   r{   s      r"   _logits_to_probszDistribution._logits_to_probs(  sD     ?FI ((000%--f2->>	
r#   c           
        |D ]d}t          |t          t          t          t          j        t          t          j        j	        f          st          dt          |                     et          d |D                       sit          j                    |D ]1}t          |t          t          j        j	        f          r	|j         n2fd|D             }t          j        |          S t          j        |          S )a  
        Returns a list where each arg is broadcasted. Scalar args are upcast to tensors
        having the same data type as the first Tensor passed to `args`.  If all the
        args are scalars, then they are upcasted to Tensors with paddle default data type.

        Args:
            value (float, list, numpy.ndarray, Tensor)

        Returns:
            Broadcasted Tensor of args.
        rV   c              3  b   K   | ]*}t          |t          t          j        j        f          V  +d S r   )r   r   rL   rM   rN   ).0rR   s     r"   	<genexpr>z.Distribution._broadcast_all.<locals>.<genexpr>L  sG       
 
>AJsXvz'7899
 
 
 
 
 
r#   c                    g | ]@}t          |t          t          j        j        f          r|nt          j        |           AS )rY   )r   r   rL   rM   rN   	to_tensor)r   rR   rZ   s     r"   
<listcomp>z/Distribution._broadcast_all.<locals>.<listcomp>T  s[         "#&*2B'CDD<CC)#U;;;	  r#   )r   r[   r\   r   r]   r^   r   rL   rM   rN   r_   r`   allget_default_dtyperZ   broadcast_tensors)r    rH   rR   new_argsrZ   s       @r"   _broadcast_allzDistribution._broadcast_all6  s8     	 	CeRZ6:;KL    |quvyqzqz||  	  
 
EI
 
 
 
 
 	6 ,..E  cHfj.>#?@@ IEE       H +H555'---r#   )r   r   )r   r   r   r   r   r   )r   r   )r   r   )r/   r   r   r   )r8   r   r   r   )r;   r   r   r   )rD   rE   r   r   )rH   rI   r   rJ   )rH   rI   r   rT   )ru   r   r;   r   r   r   )F)rC   rz   r{   r|   r   r   )r   rz   r{   r|   r   r   )__name__
__module____qualname____doc__r   propertyr   r   r+   r.   r2   r5   r7   r:   r@   r=   rC   rG   rS   rt   ry   r   r   r   __classcell__)r!   s   @r"   r   r   (   s         MO        ! ! ! X! ! ! ! X! " " " X" " " " X" -/ " " " " " .0 " " " " "" " " "" " " "* * * *" " " "" " " "
 
 
 
   29$ 9$ 9$ 9$v, , , ,^ 8=
 
 
 
 
  9>
 
 
 
 
'. '. '. '. '. '. '. '.r#   r   )
__future__r   rd   typingr   numpyr]   rL   r   paddle.base.data_feederr   r   paddle.base.frameworkr   paddle.frameworkr	   r
   collections.abcr   typing_extensionsr   r   paddle._typingr   r   r   r   r#   r"   <module>r      sB   # " " " " "                         K K K K K K K K * * * * * *       
  A((((((++++++@@@@@@@@u. u. u. u. u. u. u. u. u. u.r#   