Ë
    f^(hš!  ã                   óP   — d dl Z d dl mZ d dlmZmZ d dlmZ dgZ G d„ de«      Zy)é    N)ÚTensor)ÚCategoricalÚconstraints)ÚDistributionÚMixtureSameFamilyc                   ó4  ‡ — e Zd ZU dZi Zeeej                  f   e	d<   dZ
	 ddede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j4                  «       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Úreturnc                 óÒ  •— || _         || _        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__)Úselfr
   r   r   ÚmdbsÚcdbsÚsize1Úsize2ÚkmÚkcr   Ú	__class__s              €úe/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/distributions/mixture_same_family.pyr!   zMixtureSameFamily.__init__:   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*   r0   zMixtureSameFamily.expandk   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*   r7   zMixtureSameFamily.support|   s   € ð ×+Ñ+×3Ñ3Ð3r+   c                 ó   — | j                   S r6   )r   r8   s    r*   r
   z&MixtureSameFamily.mixture_distribution‚   s   € à×)Ñ)Ð)r+   c                 ó   — | j                   S r6   )r   r8   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*   rB   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-   rA   r   Úvariancer   rB   Ú_padÚpow)r"   r@   Úmean_cond_varÚvar_cond_means       r*   rD   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<   )rE   r   Úcdfr
   r@   r-   rA   )r"   ÚxÚcdf_xÚmix_probs       r*   rJ   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<   )
r1   Ú_validate_samplerE   r   Úlog_probr-   Úlog_softmaxr
   r   Ú	logsumexp)r"   rK   Ú
log_prob_xÚlog_mix_probs       r*   rP   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*   rW   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"   rK   s     r*   rE   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   rX   r-   r.   r   )r"   rK   Ú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+   r6   ) Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   ÚdictÚstrr   Ú
ConstraintÚ__annotations__Úhas_rsampler   r   r!   r0   Údependent_propertyr7   Úpropertyr
   r   r   rB   rD   rJ   rP   r-   r.   rW   rE   r?   ro   Ú__classcell__)r)   s   @r*   r   r      s  ø… ñ(ðT :<€O�T˜#˜{×5Ñ5Ð5Ñ6Ó;Ø€Kð ñ	/
à)ð/
ð !-ð/
ð
 
õ/
õbð" ×#Ñ#ñ4ó $ð4ð
 ð* kò *ó ð*ð ð,¨ò ,ó ð,ð ð
�fò 
ó ð
ð ð
-˜&ò 
-ó ð
-ò3òBð #- %§*¡*£,ó /ò23òö=r+   )	r-   r   Útorch.distributionsr   r   Ú torch.distributions.distributionr   Ú__all__r   © r+   r*   ú<module>r€      s+   ðó Ý ß 8Ý 9ð Ð
€ôP=˜õ P=r+   