
    IЦiP                     d    S SK Jr  S SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  S/r " S S\5      rg)	    )DictN)constraints)Distribution)_sum_rightmost)_sizeIndependentc                   t  ^  \ rS rSr% Sr0 r\\\R                  4   \
S'    SU 4S jjrSU 4S jjr\S 5       r\S 5       r\R                   S 5       r\S	 5       r\S
 5       r\S 5       r\R,                  " 5       4S jr\R,                  " 5       4S\S\R2                  4S jjrS rS rSS jrS rSrU =r $ )r      aI  
Reinterprets some of the batch dims of a distribution as event dims.

This is mainly useful for changing the shape of the result of
:meth:`log_prob`. For example to create a diagonal Normal distribution with
the same shape as a Multivariate Normal distribution (so they are
interchangeable), you can::

    >>> from torch.distributions.multivariate_normal import MultivariateNormal
    >>> from torch.distributions.normal import Normal
    >>> loc = torch.zeros(3)
    >>> scale = torch.ones(3)
    >>> mvn = MultivariateNormal(loc, scale_tril=torch.diag(scale))
    >>> [mvn.batch_shape, mvn.event_shape]
    [torch.Size([]), torch.Size([3])]
    >>> normal = Normal(loc, scale)
    >>> [normal.batch_shape, normal.event_shape]
    [torch.Size([3]), torch.Size([])]
    >>> diagn = Independent(normal, 1)
    >>> [diagn.batch_shape, diagn.event_shape]
    [torch.Size([]), torch.Size([3])]

Args:
    base_distribution (torch.distributions.distribution.Distribution): a
        base distribution
    reinterpreted_batch_ndims (int): the number of batch dims to
        reinterpret as event dims
arg_constraintsc                 Z  > U[        UR                  5      :  a$  [        SU S[        UR                  5       35      eUR                  UR                  -   nU[        UR                  5      -   nUS [        U5      U-
   nU[        U5      U-
  S  nXl        X l        [        TU ]  XgUS9  g )NzQExpected reinterpreted_batch_ndims <= len(base_distribution.batch_shape), actual z vs validate_args)lenbatch_shape
ValueErrorevent_shape	base_distreinterpreted_batch_ndimssuper__init__)	selfbase_distributionr   r   shape	event_dimr   r   	__class__s	           ^/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/distributions/independent.pyr   Independent.__init__-   s     %s+<+H+H'II34D=N=Z=Z9[8\^  "--0A0M0MM-4E4Q4Q0RR	4c%j945CJ245*)B&O    c                 N  > U R                  [        U5      n[        R                  " U5      nU R                  R                  XR                  S U R                   -   5      Ul        U R                  Ul        [        [        U]'  XR                  SS9  U R                  Ul
        U$ )NFr   )_get_checked_instancer   torchSizer   expandr   r   r   r   _validate_args)r   r   	_instancenewr   s       r   r#   Independent.expand=   s    ((i@jj---**+KT-K-KLL
 )-(F(F%k3()) 	) 	
 "00
r   c                 .    U R                   R                  $ N)r   has_rsampler   s    r   r*   Independent.has_rsampleJ   s    ~~)))r   c                 P    U R                   S:  a  gU R                  R                  $ )Nr   F)r   r   has_enumerate_supportr+   s    r   r.   !Independent.has_enumerate_supportN   s#    ))A-~~333r   c                     U R                   R                  nU R                  (       a   [        R                  " XR                  5      nU$ r)   )r   supportr   r   independent)r   results     r   r1   Independent.supportT   s5    '')) ,,V5S5STFr   c                 .    U R                   R                  $ r)   )r   meanr+   s    r   r6   Independent.mean[       ~~"""r   c                 .    U R                   R                  $ r)   )r   moder+   s    r   r:   Independent.mode_   r8   r   c                 .    U R                   R                  $ r)   )r   variancer+   s    r   r=   Independent.variancec   s    ~~&&&r   c                 8    U R                   R                  U5      $ r)   )r   sampler   sample_shapes     r   r@   Independent.sampleg   s    ~~$$\22r   rB   returnc                 8    U R                   R                  U5      $ r)   )r   rsamplerA   s     r   rF   Independent.rsamplej   s    ~~%%l33r   c                 b    U R                   R                  U5      n[        X R                  5      $ r)   )r   log_probr   r   )r   valuerI   s      r   rI   Independent.log_probm   s'    >>**51h(F(FGGr   c                 `    U R                   R                  5       n[        XR                  5      $ r)   )r   entropyr   r   )r   rM   s     r   rM   Independent.entropyq   s%    ..((*g'E'EFFr   c                 j    U R                   S:  a  [        S5      eU R                  R                  US9$ )Nr   z5Enumeration over cartesian product is not implemented)r#   )r   NotImplementedErrorr   enumerate_support)r   r#   s     r   rQ   Independent.enumerate_supportu   s:    ))A-%G  ~~//v/>>r   c                 j    U R                   R                  SU R                   SU R                   S3-   $ )N(z, ))r   __name__r   r   r+   s    r   __repr__Independent.__repr__|   s8    NN##$..!D$B$B#C1EF	
r   )r   r   r)   )T)!rV   
__module____qualname____firstlineno____doc__r   r   strr   
Constraint__annotations__r   r#   propertyr*   r.   dependent_propertyr1   r6   r:   r=   r!   r"   r@   r   TensorrF   rI   rM   rQ   rW   __static_attributes____classcell__)r   s   @r   r   r      s   8 :<OT#{5556; KOP  * * 4 4
 ## $ # # # # ' ' #(**, 3 -2JJL 4E 4U\\ 4HG?
 
r   )typingr   r!   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   torch.typesr   __all__r    r   r   <module>rl      s.      + 9 4  /r
, r
r   