
    &Vji'                         d dl Z d dlmZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZmZ d dlmZ d	gZd
 Zd Zd Z G d d	e          ZdS )    N)Optional)Tensor)constraints)Distribution)_batch_mahalanobis	_batch_mv)_standard_normallazy_property)_sizeLowRankMultivariateNormalc                 T   |                      d          }| j        |                    d          z  }t          j        ||                                           }|                    d||z            dddd|dz   fxx         dz  cc<   t          j                            |          S )z
    Computes Cholesky of :math:`I + W.T @ inv(D) @ W` for a batch of matrices :math:`W`
    and a batch of vectors :math:`D`.
    N   )	sizemT	unsqueezetorchmatmul
contiguousviewlinalgcholesky)WDmWt_DinvKs        i/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/distributions/lowrank_multivariate_normal.py_batch_capacitance_trilr       s    
 	
r

AdQ[[__$GWa  ++--AFF2q1uaaaAEk"""a'"""<  ###    c                     d|                     dd                                                              d          z  |                                                    d          z   S )z
    Uses "matrix determinant lemma"::
        log|W @ W.T + D| = log|C| + log|D|,
    where :math:`C` is the capacitance matrix :math:`I + W.T @ inv(D) @ W`, to compute
    the log determinant.
       r   r   )dim1dim2)diagonallogsum)r   r   capacitance_trils      r   _batch_lowrank_logdetr*      sa     ((br(::>>@@DDRHHH15577;;
L L  r!   c                     | j         |                    d          z  }t          ||          }|                    d          |z                      d          }t          ||          }||z
  S )a  
    Uses "Woodbury matrix identity"::
        inv(W @ W.T + D) = inv(D) - inv(D) @ W @ inv(C) @ W.T @ inv(D),
    where :math:`C` is the capacitance matrix :math:`I + W.T @ inv(D) @ W`, to compute the squared
    Mahalanobis distance :math:`x.T @ inv(W @ W.T + D) @ x`.
    r   r#   r   )r   r   r   powr(   r   )r   r   xr)   r   	Wt_Dinv_xmahalanobis_term1mahalanobis_term2s           r   _batch_lowrank_mahalanobisr1   )   sh     dQ[[__$G'1%%IqA**2..*+;YGG000r!   c                       e Zd ZdZej         ej        ej        d           ej        ej        d          dZ	ej        Z
dZ	 ddeded	ed
ee         ddf
 fdZd f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edefd            Zedefd            Z ej                    fdedefdZd Zd Z xZS )r   a  
    Creates a multivariate normal distribution with covariance matrix having a low-rank form
    parameterized by :attr:`cov_factor` and :attr:`cov_diag`::

        covariance_matrix = cov_factor @ cov_factor.T + cov_diag

    Example:
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_LAPACK)
        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = LowRankMultivariateNormal(
        ...     torch.zeros(2), torch.tensor([[1.0], [0.0]]), torch.ones(2)
        ... )
        >>> m.sample()  # normally distributed with mean=`[0,0]`, cov_factor=`[[1],[0]]`, cov_diag=`[1,1]`
        tensor([-0.2102, -0.5429])

    Args:
        loc (Tensor): mean of the distribution with shape `batch_shape + event_shape`
        cov_factor (Tensor): factor part of low-rank form of covariance matrix with shape
            `batch_shape + event_shape + (rank,)`
        cov_diag (Tensor): diagonal part of low-rank form of covariance matrix with shape
            `batch_shape + event_shape`

    Note:
        The computation for determinant and inverse of covariance matrix is avoided when
        `cov_factor.shape[1] << cov_factor.shape[0]` thanks to `Woodbury matrix identity
        <https://en.wikipedia.org/wiki/Woodbury_matrix_identity>`_ and
        `matrix determinant lemma <https://en.wikipedia.org/wiki/Matrix_determinant_lemma>`_.
        Thanks to these formulas, we just need to compute the determinant and inverse of
        the small size "capacitance" matrix::

            capacitance = I + cov_factor.T @ inv(cov_diag) @ cov_factor
    r#   r   )loc
cov_factorcov_diagTNr3   r4   r5   validate_argsreturnc           	      X   |                                 dk     rt          d          |j        dd          }|                                 dk     rt          d          |j        dd         |k    rt          d|d          d	          |j        dd          |k    rt          d
|           |                    d          }|                    d          }	 t	          j        |||          \  }| _        }n:# t          $ r-}t          d|j         d|j         d|j                   |d }~ww xY w|d         | _        |d         | _	        | j        j        d d         }	|| _
        || _        t          ||          | _        t                                          |	||           d S )Nr   z%loc must be at least one-dimensional.r   r#   zScov_factor must be at least two-dimensional, with optional leading batch dimensionsr   z2cov_factor must be a batch of matrices with shape r   z x mz/cov_diag must be a batch of vectors with shape zIncompatible batch shapes: loc z, cov_factor z, cov_diag ).r   r6   )dim
ValueErrorshaper   r   broadcast_tensorsr4   RuntimeErrorr3   r5   _unbroadcasted_cov_factor_unbroadcasted_cov_diagr    _capacitance_trilsuper__init__)selfr3   r4   r5   r6   event_shapeloc_	cov_diag_ebatch_shape	__class__s             r   rC   z"LowRankMultivariateNormal.__init__a   s    7799q==DEEEin>>a9   BrE"k11Y[QR^YYY   >"##+--O+OO   }}R  &&r**		/4/Fj)0 0,D$/99  	 	 	w#)ww*JZwwgoguww 	 <!&)hnSbS))3&'/$!8X!N!NkOOOOOs   C; ;
D2(D--D2c                    |                      t          |          }t          j        |          }|| j        z   }| j                            |          |_        | j                            |          |_        | j                            || j        j	        dd          z             |_        | j
        |_
        | j        |_        | j        |_        t          t          |                              || j        d           | j        |_        |S )Nr   Fr9   )_get_checked_instancer   r   SizerE   r3   expandr5   r4   r<   r?   r@   rA   rB   rC   _validate_args)rD   rI   	_instancenew	loc_shaperJ   s        r   rN   z LowRankMultivariateNormal.expand   s    (()BINNj--$"22	(//),,}++I66//	DO<QRTRURU<V0VWW(,(F%&*&B# $ 6'--66) 	7 	
 	
 	
 "0
r!   c                     | j         S Nr3   rD   s    r   meanzLowRankMultivariateNormal.mean   	    xr!   c                     | j         S rT   rU   rV   s    r   modezLowRankMultivariateNormal.mode   rX   r!   c                     | j                             d                              d          | j        z                       | j        | j        z             S )Nr#   r   )r?   r,   r(   r@   rN   _batch_shape_event_shaperV   s    r   variancez"LowRankMultivariateNormal.variance   sK     *..q1155b99D<XX
&"T%66
7
7	8r!   c                    | j         d         }| j                                                            d          }| j        |z  }t          j        ||j                                                  }|	                    d||z            d d d d |dz   fxx         dz  cc<   |t
          j
                            |          z  }|                    | j        | j         z   | j         z             S )Nr   r   r   )r]   r@   sqrtr   r?   r   r   r   r   r   r   r   rN   r\   )rD   ncov_diag_sqrt_unsqueeze
Dinvsqrt_Wr   
scale_trils         r   rd   z$LowRankMultivariateNormal.scale_tril   s     a "&">"C"C"E"E"O"OPR"S"S36MM
LZ]33>>@@	r1q5!!!XXAX+&&&!+&&&,u|/D/DQ/G/GG
   11D4EE
 
 	
r!   c                     t          j        | j        | j        j                  t          j        | j                  z   }|                    | j        | j        z   | j        z             S rT   )	r   r   r?   r   
diag_embedr@   rN   r\   r]   )rD   covariance_matrixs     r   rg   z+LowRankMultivariateNormal.covariance_matrix   sh    !L*D,J,M
 
T9::; !'' 11D4EE
 
 	
r!   c                 ^   | j         j        | j                            d          z  }t          j                            | j        |d          }t	          j        | j        	                                          |j        |z  z
  }|
                    | j        | j        z   | j        z             S )Nr   F)upper)r?   r   r@   r   r   r   solve_triangularrA   rf   
reciprocalrN   r\   r]   )rD   r   Aprecision_matrixs       r   rm   z*LowRankMultivariateNormal.precision_matrix   s     *-*44R889 	 L))$*@'QV)WWT9DDFFGG!$QR(R 	  && 11D4EE
 
 	
r!   sample_shapec                 v   |                      |          }|d d         | j        j        dd          z   }t          || j        j        | j        j                  }t          || j        j        | j        j                  }| j        t          | j        |          z   | j	        
                                |z  z   S )Nr   )dtypedevice)_extended_shaper4   r<   r	   r3   rp   rq   r   r?   r@   r`   )rD   rn   r<   W_shapeeps_Weps_Ds         r   rsamplez!LowRankMultivariateNormal.rsample   s    $$\22*t4RSS99 txWWW dhnTX_UUUH6>>?*//11E9:	
r!   c                 @   | j         r|                     |           || j        z
  }t          | j        | j        || j                  }t          | j        | j        | j                  }d| j        d         t          j
        dt          j        z            z  |z   |z   z  S )Ng      r   r#   )rO   _validate_sampler3   r1   r?   r@   rA   r*   r]   mathr'   pi)rD   valuediffMlog_dets        r   log_probz"LowRankMultivariateNormal.log_prob   s     	)!!%(((tx&*("	
 
 (*("
 

 t(+dhq47{.C.CCgMPQQRRr!   c                    t          | j        | j        | j                  }d| j        d         dt          j        dt
          j        z            z   z  |z   z  }t          | j	                  dk    r|S |
                    | j	                  S )Ng      ?r   g      ?r#   )r*   r?   r@   rA   r]   ry   r'   rz   lenr\   rN   )rD   r~   Hs      r   entropyz!LowRankMultivariateNormal.entropy   s    '*("
 

 4$Q'3!dg+1F1F+FG'QRt !!Q&&H88D-...r!   rT   ) __name__
__module____qualname____doc__r   real_vectorindependentrealpositivearg_constraintssupporthas_rsampler   r   boolrC   rN   propertyrW   rZ   r
   r^   rd   rg   rm   r   rM   r   rv   r   r   __classcell__)rJ   s   @r   r   r   7   s=        D &-k-k.>BB+K+K,@!DD O
 %GK )-)P )P)P )P 	)P
  ~)P 
)P )P )P )P )P )PV       f    X f    X 8& 8 8 8 ]8
 
F 
 
 
 ]
  
6 
 
 
 ]
 
& 
 
 
 ]
  -7EJLL 	
 	
E 	
V 	
 	
 	
 	
S S S"
/ 
/ 
/ 
/ 
/ 
/ 
/r!   )ry   typingr   r   r   torch.distributionsr    torch.distributions.distributionr   'torch.distributions.multivariate_normalr   r   torch.distributions.utilsr	   r
   torch.typesr   __all__r    r*   r1   r    r!   r   <module>r      s                 + + + + + + 9 9 9 9 9 9 Q Q Q Q Q Q Q Q E E E E E E E E       '
'	$ 	$ 	$	 	 	1 1 1D/ D/ D/ D/ D/ D/ D/ D/ D/ D/r!   