
    /j"                     d    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 dgZ	 G d de          Z
dS )    N)Tensor)Categoricalconstraints)MixtureSameFamilyConstraint)DistributionMixtureSameFamilyc            	       J    e Zd ZU dZi Zeeej        f         e	d<   dZ
	 ddedededz  ddf fd	Zd fd
	Zej        d             Zedefd            Zedefd            Zedefd            Zedefd            Zd Zd Z ej                    fdZd Zd Zd Z xZ S )r   a  
    The `MixtureSameFamily` distribution implements a (batch of) mixture
    distribution where all component are from different parameterizations of
    the same distribution type. It is parameterized by a `Categorical`
    "selecting distribution" (over `k` component) and a component
    distribution, i.e., a `Distribution` with a rightmost batch shape
    (equal to `[k]`) which indexes each (batch of) component.

    Examples::

        >>> # xdoctest: +SKIP("undefined vars")
        >>> # Construct Gaussian Mixture Model in 1D consisting of 5 equally
        >>> # weighted normal distributions
        >>> mix = D.Categorical(torch.ones(5,))
        >>> comp = D.Normal(torch.randn(5,), torch.rand(5,))
        >>> gmm = MixtureSameFamily(mix, comp)

        >>> # Construct Gaussian Mixture Model in 2D consisting of 5 equally
        >>> # weighted bivariate normal distributions
        >>> mix = D.Categorical(torch.ones(5,))
        >>> comp = D.Independent(D.Normal(
        ...          torch.randn(5,2), torch.rand(5,2)), 1)
        >>> gmm = MixtureSameFamily(mix, comp)

        >>> # Construct a batch of 3 Gaussian Mixture Models in 2D each
        >>> # consisting of 5 random weighted bivariate normal distributions
        >>> mix = D.Categorical(torch.rand(3,5))
        >>> comp = D.Independent(D.Normal(
        ...         torch.randn(3,5,2), torch.rand(3,5,2)), 1)
        >>> gmm = MixtureSameFamily(mix, comp)

    Args:
        mixture_distribution: `torch.distributions.Categorical`-like
            instance. Manages the probability of selecting component.
            The number of categories must match the rightmost batch
            dimension of the `component_distribution`. Must have either
            scalar `batch_shape` or `batch_shape` matching
            `component_distribution.batch_shape[:-1]`
        component_distribution: `torch.distributions.Distribution`-like
            instance. Right-most batch dimension indexes component.
    arg_constraintsFNmixture_distributioncomponent_distributionvalidate_argsreturnc                    || _         || _        t          | j         t                    st	          d          t          | j        t
                    st	          d          | j         j        }| j        j        d d         }t          t          |          t          |                    D ]-\  }}|dk    r"|dk    r||k    rt	          d| d| d          .| j         j	        j
        d         }| j        j        d         }	||	||	k    rt	          d| d	|	 d          || _        | j        j        }
t          |
          | _        t                                          ||
|
           d S )NzU The Mixture distribution needs to be an  instance of torch.distributions.CategoricalzUThe Component distribution need to be an instance of torch.distributions.Distribution   z$`mixture_distribution.batch_shape` (z>) is not compatible with `component_distribution.batch_shape`()z"`mixture_distribution component` (z;) does not equal `component_distribution.batch_shape[-1]` (batch_shapeevent_shaper   )_mixture_distribution_component_distribution
isinstancer   
ValueErrorr   r   zipreversedlogitsshape_num_componentr   len_event_ndimssuper__init__)selfr   r   r   mdbscdbssize1size2kmkcr   	__class__s              l/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/distributions/mixture_same_family.pyr"   zMixtureSameFamily.__init__;   s    &:"'=$$4kBB 	?  
 $6EE 	?   )5+7<?? 	 	LE5zzeqjjUe^^ ,4 , ,$(, , ,   '.4R8)5b9>bnrR      
 !2>,,#'	 	 	
 	
 	
 	
 	
    c                    t          j        |          }|| j        fz   }|                     t          |          }| j                            |          |_        | j                            |          |_        | j        |_        | j        |_        |j        j	        }t          t          |                              ||d           | j        |_        |S )NFr   )torchSizer   _get_checked_instancer   r   expandr   r    r   r!   r"   _validate_args)r#   r   	_instancebatch_shape_compnewr   r*   s         r+   r1   zMixtureSameFamily.expando   s    j--&$*=)??(():IFF&*&B&I&I'
 '
# %)$>$E$Ek$R$R!!0,1=%%..#E 	/ 	
 	
 	
 "0
r,   c                 4    t          | j        j                  S N)r   r   supportr#   s    r+   r8   zMixtureSameFamily.support   s     +4+G+OPPPr,   c                     | j         S r7   )r   r9   s    r+   r   z&MixtureSameFamily.mixture_distribution   s    ))r,   c                     | j         S r7   )r   r9   s    r+   r   z(MixtureSameFamily.component_distribution   s    ++r,   c                     |                      | j        j                  }t          j        || j        j        z  d| j        z
            S Nr   dim)_pad_mixture_dimensionsr   probsr.   sumr   meanr    )r#   rA   s     r+   rC   zMixtureSameFamily.mean   sL    ,,T-F-LMMyD/44"t?P:P
 
 
 	
r,   c                 R   |                      | j        j                  }t          j        || j        j        z  d| j        z
            }t          j        || j        j        | 	                    | j                  z
  
                    d          z  d| j        z
            }||z   S )Nr   r>   g       @)r@   r   rA   r.   rB   r   variancer    rC   _padpow)r#   rA   mean_cond_varvar_cond_means       r+   rE   zMixtureSameFamily.variance   s     ,,T-F-LMM	D/88b4CT>T
 
 
 	T05		$)8L8LLQQRUVVVT&&
 
 
 },,r,   c                     |                      |          }| j                            |          }| j        j        }t          j        ||z  d          S r=   )rF   r   cdfr   rA   r.   rB   )r#   xcdf_xmix_probs       r+   rK   zMixtureSameFamily.cdf   sL    IIaLL+//22,2y)r2222r,   c                 
   | j         r|                     |           |                     |          }| j                            |          }t          j        | j        j        d          }t          j	        ||z   d          S r=   )
r2   _validate_samplerF   r   log_probr.   log_softmaxr   r   	logsumexp)r#   rL   
log_prob_xlog_mix_probs       r+   rQ   zMixtureSameFamily.log_prob   s     	%!!!$$$IIaLL099!<<
(%,"
 
 
 zL8bAAAAr,   c           
         t          j                    5  t          |          }t          | j                  }||z   }| j        }| j                            |          }|j        }| j                            |          }|	                    |t          j
        dgt          |          dz   z            z             }	|	                    t          j
        dgt          |          z            t          j
        dg          z   |z             }	t          j        |||	          }
|
                    |          cd d d            S # 1 swxY w Y   d S )Nr   )r.   no_gradr   r   r   r   sampler   r   reshaper/   repeatgathersqueeze)r#   sample_shape
sample_len	batch_len
gather_dimes
mix_sample	mix_shapecomp_samplesmix_sample_rsampless              r+   rX   zMixtureSameFamily.sample   s|   ]__ 	/ 	/\**JD,--I#i/J!B 299,GGJ"(I  6==lKKL &--EJsc"ggk':;;; L (..
A3Y/005:qc??BRG L l<\JJG??:..-	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s   D$EE	E	c                 <    |                     d| j        z
            S )Nr   )	unsqueezer    )r#   rL   s     r+   rF   zMixtureSameFamily._pad   s    {{2 11222r,   c                 F   t          | j                  }t          | j        j                  }|dk    rdn||z
  }|j        }|                    |d d         t          j        |dgz            z   |dd          z   t          j        | j        dgz            z             }|S )Nr   r   r   )r   r   r   r   rY   r.   r/   r    )r#   rL   dist_batch_ndimscat_batch_ndims	pad_ndimsxss         r+   r@   z)MixtureSameFamily._pad_mixture_dimensions   s    t/00d7CDD(A--AA3Co3U	WIIssGjaS))*g j*aS0112
 
 r,   c                 6    d| j          d| j         }d|z   dz   S )Nz
  z,
  zMixtureSameFamily(r   )r   r   )r#   args_strings     r+   __repr__zMixtureSameFamily.__repr__   s2    P4,PP43NPP 	 );6<<r,   r7   )!__name__
__module____qualname____doc__r
   dictstrr   
Constraint__annotations__has_rsampler   r   boolr"   r1   dependent_propertyr8   propertyr   r   r   rC   rE   rK   rQ   r.   r/   rX   rF   r@   rp   __classcell__)r*   s   @r+   r   r      s        ( (T :<OT#{556;;;K &*	2
 2
)2
 !-2
 d{	2

 
2
 2
 2
 2
 2
 2
h     " #Q Q $#Q *k * * * X* , , , , X, 
f 
 
 
 X
 
-& 
- 
- 
- X
-3 3 3B B B #-%*,, / / / /23 3 3  = = = = = = =r,   )r.   r   torch.distributionsr   r   torch.distributions.constraintsr    torch.distributions.distributionr   __all__r    r,   r+   <module>r      s           8 8 8 8 8 8 8 8 G G G G G G 9 9 9 9 9 9 
R= R= R= R= R= R= R= R= R= R=r,   