Ë
    P/æj)!  ã                   óP   — d dl mZ d dlZd dlmZmZ d dlmZ dgZ G d„ de«      Z	y)é    )ÚDictN)ÚCategoricalÚconstraints)ÚDistributionÚMixtureSameFamilyc                   ó  ‡ — e Zd ZU dZi Zeeej                  f   e	d<   dZ
	 dˆ fd„	Zdˆ fd„	Zej                  d„ «       Zed„ «       Zed„ «       Zed	„ «       Ze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_constraintsFc                 óÒ  •— || _         || _        t        | j                   t        «      st	        d«      ‚t        | j                  t
        «      st	        d«      ‚| j                   j                  }| j                  j                  d d }t        t        |«      t        |«      «      D ](  \  }}|dk7  sŒ|dk7  sŒ||k7  sŒt	        d|› d|› d�«      ‚ | j                   j                  j                  d   }| j                  j                  d   }	|�|	�||	k7  rt	        d|› d	|	› d�«      ‚|| _        | j                  j                  }
t        |
«      | _        t        ‰| �A  ||
|¬
«       y )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_shapeÚvalidate_args)Ú_mixture_distributionÚ_component_distributionÚ
isinstancer   Ú
ValueErrorr   r   ÚzipÚreversedÚlogitsÚshapeÚ_num_componentr   ÚlenÚ_event_ndimsÚsuperÚ__init__)ÚselfÚmixture_distributionÚcomponent_distributionr   ÚmdbsÚcdbsÚsize1Úsize2ÚkmÚkcr   Ú	__class__s              €új/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/distributions/mixture_same_family.pyr   zMixtureSameFamily.__init__9   sŠ  ø€ ð &:ˆÔ"Ø'=ˆÔ$ä˜$×4Ñ4´kÔBÜð?óð ô
 ˜$×6Ñ6¼ÔEÜð?óð ð ×)Ñ)×5Ñ5ˆØ×+Ñ+×7Ñ7¸¸Ð<ˆÜ¤¨£´¸³Ó?ò 	‰LˆE�5Ø˜‹z˜e q›j¨U°e«^Ü Ø:¸4¸&ð A$à$( 6¨ð,óð ð	ð ×'Ñ'×.Ñ.×4Ñ4°RÑ8ˆØ×)Ñ)×5Ñ5°bÑ9ˆØˆ>˜b˜n°°r²ÜØ4°R°Dð 9à�D˜ðóð ð
 !ˆÔà×2Ñ2×>Ñ>ˆÜ Ó,ˆÔÜ‰ÑØ¨+À]ð 	õ 	
ó    c                 ó¼  •— t        j                  |«      }|| j                  fz   }| j                  t        |«      }| j
                  j                  |«      |_        | 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)   r/   zMixtureSameFamily.expandg   sÍ   ø€ Ü—j‘j Ó-ˆØ&¨$×*=Ñ*=Ð)?Ñ?ÐØ×(Ñ(Ô):¸IÓFˆØ&*×&BÑ&B×&IÑ&IØó'
ˆÔ#ð %)×$>Ñ$>×$EÑ$EÀkÓ$RˆÔ!Ø!×0Ñ0ˆÔØ×,Ñ,ˆÔØ×1Ñ1×=Ñ=ˆÜÔ Ñ.Ø#°ÈEð 	/ô 	
ð "×0Ñ0ˆÔØˆ
r*   c                 ó.   — | j                   j                  S ©N)r   Úsupport©r   s    r)   r6   zMixtureSameFamily.supportx   s   € ð ×+Ñ+×3Ñ3Ð3r*   c                 ó   — | j                   S r5   )r   r7   s    r)   r    z&MixtureSameFamily.mixture_distribution~   s   € à×)Ñ)Ð)r*   c                 ó   — | j                   S r5   )r   r7   s    r)   r!   z(MixtureSameFamily.component_distribution‚   s   € à×+Ñ+Ð+r*   c                 óÂ   — | j                  | j                  j                  «      }t        j                  || j
                  j                  z  d| j                  z
  ¬«      S ©Nr   ©Údim)Ú_pad_mixture_dimensionsr    Úprobsr,   Úsumr!   Úmeanr   )r   r?   s     r)   rA   zMixtureSameFamily.mean†   sP   € à×,Ñ,¨T×-FÑ-F×-LÑ-LÓMˆÜ�y‰yØ�D×/Ñ/×4Ñ4Ñ4¸"¸t×?PÑ?PÑ:Pô
ð 	
r*   c                 ó˜  — | j                  | j                  j                  «      }t        j                  || j
                  j                  z  d| j                  z
  ¬«      }t        j                  || j
                  j                  | j                  | j                  «      z
  j                  d«      z  d| j                  z
  ¬«      }||z   S )Nr   r<   g       @)r>   r    r?   r,   r@   r!   Úvariancer   rA   Ú_padÚpow)r   r?   Úmean_cond_varÚvar_cond_means       r)   rC   zMixtureSameFamily.variance�   s¬   € ð ×,Ñ,¨T×-FÑ-F×-LÑ-LÓMˆÜŸ	™	Ø�D×/Ñ/×8Ñ8Ñ8¸bÀ4×CTÑCTÑ>Tô
ˆô Ÿ	™	Ø�T×0Ñ0×5Ñ5¸¿	¹	À$Ç)Á)Ó8LÑL×QÑQÐRUÓVÑVØ�T×&Ñ&Ñ&ô
ˆð ˜}Ñ,Ð,r*   c                 óº   — | j                  |«      }| j                  j                  |«      }| j                  j                  }t        j                  ||z  d¬«      S r;   )rD   r!   Úcdfr    r?   r,   r@   )r   ÚxÚcdf_xÚmix_probs       r)   rI   zMixtureSameFamily.cdfš   sL   € Ø�I‰I�a‹LˆØ×+Ñ+×/Ñ/°Ó2ˆØ×,Ñ,×2Ñ2ˆä�y‰y˜ Ñ)¨rÔ2Ð2r*   c                 ó  — | j                   r| j                  |«       | j                  |«      }| j                  j	                  |«      }t        j                  | j                  j                  d¬«      }t        j                  ||z   d¬«      S r;   )
r0   Ú_validate_samplerD   r!   Úlog_probr,   Úlog_softmaxr    r   Ú	logsumexp)r   rJ   Ú
log_prob_xÚlog_mix_probs       r)   rO   zMixtureSameFamily.log_prob¡   su   € Ø×ÒØ×!Ñ! !Ô$Ø�I‰I�a‹LˆØ×0Ñ0×9Ñ9¸!Ó<ˆ
Ü×(Ñ(Ø×%Ñ%×,Ñ,°"ô
ˆô �‰˜z¨LÑ8¸bÔAÐAr*   c           
      ó�  — t        j                  «       5  t        |«      }t        | j                  «      }||z   }| j                  }| j
                  j                  |«      }|j                  }| j                  j                  |«      }|j                  |t        j                  dgt        |«      dz   z  «      z   «      }	|	j                  t        j                  dgt        |«      z  «      t        j                  dg«      z   |z   «      }	t        j                  |||	«      }
|
j                  |«      cd d d «       S # 1 sw Y   y xY w)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)   rV   zMixtureSameFamily.sample«   s  € Ü�]‰]‹_ñ 	/Ü˜\Ó*ˆJÜ˜D×,Ñ,Ó-ˆIØ# iÑ/ˆJØ×!Ñ!ˆBð ×2Ñ2×9Ñ9¸,ÓGˆJØ"×(Ñ(ˆIð  ×6Ñ6×=Ñ=¸lÓKˆLð &×-Ñ-ØœEŸJ™J¨ s¬c°"«g¸©kÑ':Ó;Ñ;óˆLð (×.Ñ.Ü—
‘
˜A˜3¤ Y£Ñ/Ó0´5·:±:¸q¸c³?ÑBÀRÑGóˆLô —l‘l <°¸\ÓJˆGØ—?‘? :Ó.÷-	/÷ 	/ò 	/ús   •DD<Ä<Ec                 ó>   — |j                  d| j                  z
  «      S )Nr   )Ú	unsqueezer   )r   rJ   s     r)   rD   zMixtureSameFamily._padÄ   s   € Ø�{‰{˜2 × 1Ñ 1Ñ1Ó2Ð2r*   c                 óN  — t        | j                  «      }t        | j                  j                  «      }|dk(  rdn||z
  }|j                  }|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   rW   r,   r-   r   )r   rJ   Údist_batch_ndimsÚcat_batch_ndimsÚ	pad_ndimsÚxss         r)   r>   z)MixtureSameFamily._pad_mixture_dimensionsÇ   s©   € Ü˜t×/Ñ/Ó0ÐÜ˜d×7Ñ7×CÑCÓDˆØ(¨AÒ-‘AÐ3CÀoÑ3Uˆ	Ø�W‰WˆØ�I‰IØˆs�ˆGÜ�j‰j˜ a S™Ó)ñ*à��ˆgñô �j‰j˜×*Ñ*¨a¨SÑ0Ó1ñ2ó
ˆð ˆr*   c                 óJ   — d| j                   › d| j                  › �}d|z   dz   S )Nz
  z,
  zMixtureSameFamily(r   )r    r!   )r   Úargs_strings     r)   Ú__repr__zMixtureSameFamily.__repr__Ô   s7   € à�4×,Ñ,Ð-¨U°4×3NÑ3NÐ2OÐPð 	ð )¨;Ñ6¸Ñ<Ð<r*   r5   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   Ústrr   Ú
ConstraintÚ__annotations__Úhas_rsampler   r/   Údependent_propertyr6   Úpropertyr    r!   rA   rC   rI   rO   r,   r-   rV   rD   r>   rn   Ú__classcell__)r(   s   @r)   r   r      sÒ   ø… ñ(ðR :<€O�T˜#˜{×5Ñ5Ð5Ñ6Ó;Ø€Kð KOõ,
õ\ð" ×#Ñ#ñ4ó $ð4ð
 ñ*ó ð*ð ñ,ó ð,ð ñ
ó ð
ð ñ
-ó ð
-ò3òBð #- %§*¡*£,ó /ò23òö=r*   )
Útypingr   r,   Útorch.distributionsr   r   Ú torch.distributions.distributionr   Ú__all__r   © r*   r)   ú<module>r      s)   ðå ã ß 8Ý 9ð Ð
€ôL=˜õ L=r*   