
    IЦiY(                        S r SSKrSSKJrJr  / SQr " S S5      r\" 5       r\" 5       r\R                  \R                  5      \R                  \R                  5      S 5       5       r\R                  \R                  5      S 5       r\R                  \R                  5      S	 5       r\R                  \R                  5      \R                  \R                   5      \R                  \R                  5      \R                  \R                   5      S
 5       5       5       5       r\R                  \R$                  5      \R                  \R&                  5      \R                  \R$                  5      \R                  \R&                  5      S 5       5       5       5       r\R                  \R*                  5      \R                  \R*                  5      S 5       5       r\R                  \R.                  5      \R                  \R0                  5      \R                  \R.                  5      \R                  \R0                  5      S 5       5       5       5       r\R                  \R4                  5      S 5       r\R                  \R4                  5      S 5       r\R                  \R:                  5      S 5       r\R                  \R>                  5      \R                  \R@                  5      S 5       5       r!\R                  \RD                  5      \R                  \RD                  5      S 5       5       r#\R                  \RH                  5      S 5       r%\R                  \RH                  5      S 5       r&\R                  \RN                  5      S 5       r(\R                  \RN                  5      S 5       r)g)aF  
PyTorch provides two global :class:`ConstraintRegistry` objects that link
:class:`~torch.distributions.constraints.Constraint` objects to
:class:`~torch.distributions.transforms.Transform` objects. These objects both
input constraints and return transforms, but they have different guarantees on
bijectivity.

1. ``biject_to(constraint)`` looks up a bijective
   :class:`~torch.distributions.transforms.Transform` from ``constraints.real``
   to the given ``constraint``. The returned transform is guaranteed to have
   ``.bijective = True`` and should implement ``.log_abs_det_jacobian()``.
2. ``transform_to(constraint)`` looks up a not-necessarily bijective
   :class:`~torch.distributions.transforms.Transform` from ``constraints.real``
   to the given ``constraint``. The returned transform is not guaranteed to
   implement ``.log_abs_det_jacobian()``.

The ``transform_to()`` registry is useful for performing unconstrained
optimization on constrained parameters of probability distributions, which are
indicated by each distribution's ``.arg_constraints`` dict. These transforms often
overparameterize a space in order to avoid rotation; they are thus more
suitable for coordinate-wise optimization algorithms like Adam::

    loc = torch.zeros(100, requires_grad=True)
    unconstrained = torch.zeros(100, requires_grad=True)
    scale = transform_to(Normal.arg_constraints['scale'])(unconstrained)
    loss = -Normal(loc, scale).log_prob(data).sum()

The ``biject_to()`` registry is useful for Hamiltonian Monte Carlo, where
samples from a probability distribution with constrained ``.support`` are
propagated in an unconstrained space, and algorithms are typically rotation
invariant.::

    dist = Exponential(rate)
    unconstrained = torch.zeros(100, requires_grad=True)
    sample = biject_to(dist.support)(unconstrained)
    potential_energy = -dist.log_prob(sample).sum()

.. note::

    An example where ``transform_to`` and ``biject_to`` differ is
    ``constraints.simplex``: ``transform_to(constraints.simplex)`` returns a
    :class:`~torch.distributions.transforms.SoftmaxTransform` that simply
    exponentiates and normalizes its inputs; this is a cheap and mostly
    coordinate-wise operation appropriate for algorithms like SVI. In
    contrast, ``biject_to(constraints.simplex)`` returns a
    :class:`~torch.distributions.transforms.StickBreakingTransform` that
    bijects its input down to a one-fewer-dimensional space; this a more
    expensive less numerically stable transform but is needed for algorithms
    like HMC.

The ``biject_to`` and ``transform_to`` objects can be extended by user-defined
constraints and transforms using their ``.register()`` method either as a
function on singleton constraints::

    transform_to.register(my_constraint, my_transform)

or as a decorator on parameterized constraints::

    @transform_to.register(MyConstraintClass)
    def my_factory(constraint):
        assert isinstance(constraint, MyConstraintClass)
        return MyTransform(constraint.param1, constraint.param2)

You can create your own registry by creating a new :class:`ConstraintRegistry`
object.
    N)constraints
transforms)ConstraintRegistry	biject_totransform_toc                   <   ^  \ rS rSrSrU 4S jrSS jrS rSrU =r	$ )r   Q   z-
Registry to link constraints to transforms.
c                 0   > 0 U l         [        TU ]	  5         g N)	_registrysuper__init__)self	__class__s    f/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/distributions/constraint_registry.pyr   ConstraintRegistry.__init__V   s        c                   ^ ^ Uc  UU 4S j$ [        T[        R                  5      (       a  [        T5      m[        T[        5      (       a  [	        T[        R                  5      (       d  [        ST 35      eUT R                  T'   U$ )a  
Registers a :class:`~torch.distributions.constraints.Constraint`
subclass in this registry. Usage::

    @my_registry.register(MyConstraintClass)
    def construct_transform(constraint):
        assert isinstance(constraint, MyConstraint)
        return MyTransform(constraint.arg_constraints)

Args:
    constraint (subclass of :class:`~torch.distributions.constraints.Constraint`):
        A subclass of :class:`~torch.distributions.constraints.Constraint`, or
        a singleton object of the desired class.
    factory (Callable): A callable that inputs a constraint object and returns
        a  :class:`~torch.distributions.transforms.Transform` object.
c                 (   > TR                  TU 5      $ r   )register)factory
constraintr   s    r   <lambda>-ConstraintRegistry.register.<locals>.<lambda>m   s    4==W#Er   zLExpected constraint to be either a Constraint subclass or instance, but got )
isinstancer   
Constrainttype
issubclass	TypeErrorr   r   r   r   s   `` r   r   ConstraintRegistry.registerZ   s    $ ?EE j+"8"899j)J*d++:..4
 4
 ^_i^jk  &-z"r   c                      U R                   [        U5         nU" U5      $ ! [         a$    [        S[        U5      R                   S35      Sef = f)a	  
Looks up a transform to constrained space, given a constraint object.
Usage::

    constraint = Normal.arg_constraints['scale']
    scale = transform_to(constraint)(torch.zeros(1))  # constrained
    u = transform_to(constraint).inv(scale)           # unconstrained

Args:
    constraint (:class:`~torch.distributions.constraints.Constraint`):
        A constraint object.

Returns:
    A :class:`~torch.distributions.transforms.Transform` object.

Raises:
    `NotImplementedError` if no transform has been registered.
zCannot transform z constraintsN)r   r   KeyErrorNotImplementedError__name__r    s      r   __call__ConstraintRegistry.__call__}   s`    (	nnT*%56G
 z""	  	%#D$4$=$=#>lK	s	   " .A)r   r   )
r%   
__module____qualname____firstlineno____doc__r   r   r&   __static_attributes____classcell__)r   s   @r   r   r   Q   s    !F# #r   r   c                 "    [         R                  $ r   )r   identity_transformr   s    r   _transform_to_realr1      s     (((r   c                 l    [        U R                  5      n[        R                  " XR                  5      $ r   )r   base_constraintr   IndependentTransformreinterpreted_batch_ndimsr   base_transforms     r   _biject_to_independentr8      s.    z99:N**<< r   c                 l    [        U R                  5      n[        R                  " XR                  5      $ r   )r   r3   r   r4   r5   r6   s     r   _transform_to_independentr:      s.    !*"<"<=N**<< r   c                 ,    [         R                  " 5       $ r   )r   ExpTransformr0   s    r   _transform_to_positiver=      s    
 ""$$r   c                     [         R                  " [         R                  " 5       [         R                  " U R                  S5      /5      $ )N   )r   ComposeTransformr<   AffineTransformlower_boundr0   s    r   _transform_to_greater_thanrC      s>    
 &&##%&&z'='=qA	
 r   c                     [         R                  " [         R                  " 5       [         R                  " U R                  S5      /5      $ )N)r   r@   r<   rA   upper_boundr0   s    r   _transform_to_less_thanrG      s>     &&##%&&z'='=rB	
 r   c                    [        U R                  [        R                  5      =(       a    U R                  S:H  n[        U R                  [        R                  5      =(       a    U R                  S:H  nU(       a  U(       a  [
        R                  " 5       $ U R                  nU R                  U R                  -
  n[
        R                  " [
        R                  " 5       [
        R                  " X45      /5      $ )Nr   r?   )	r   rB   numbersNumberrF   r   SigmoidTransformr@   rA   )r   
lower_is_0
upper_is_1locscales        r   _transform_to_intervalrP      s     	:))7>>: 	(""a' 
 	:))7>>: 	(""a'  j**,,

 
 C""Z%;%;;E&&		$	$	&
(B(B3(NO r   c                 ,    [         R                  " 5       $ r   )r   StickBreakingTransformr0   s    r   _biject_to_simplexrS          ,,..r   c                 ,    [         R                  " 5       $ r   )r   SoftmaxTransformr0   s    r   _transform_to_simplexrW      s    &&((r   c                 ,    [         R                  " 5       $ r   )r   LowerCholeskyTransformr0   s    r   _transform_to_lower_choleskyrZ      rT   r   c                 ,    [         R                  " 5       $ r   )r   PositiveDefiniteTransformr0   s    r   _transform_to_positive_definiter]     s     //11r   c                 ,    [         R                  " 5       $ r   )r   CorrCholeskyTransformr0   s    r   _transform_to_corr_choleskyr`     s     ++--r   c                     [         R                  " U R                   Vs/ s H  n[        U5      PM     snU R                  U R
                  5      $ s  snf r   )r   CatTransformcseqr   dimlengthsr   cs     r   _biject_to_catrh     s@    "")/!1/ASAS /   Ac                     [         R                  " U R                   Vs/ s H  n[        U5      PM     snU R                  U R
                  5      $ s  snf r   )r   rb   rc   r   rd   re   rf   s     r   _transform_to_catrk     s@    """,//2/Qa/2JNNJDVDV 2ri   c                     [         R                  " U R                   Vs/ s H  n[        U5      PM     snU R                  5      $ s  snf r   )r   StackTransformrc   r   rd   rf   s     r   _biject_to_stackrn     s8    $$)/!1/ /   Ac                     [         R                  " U R                   Vs/ s H  n[        U5      PM     snU R                  5      $ s  snf r   )r   rm   rc   r   rd   rf   s     r   _transform_to_stackrq   "  s8    $$",//2/Qa/2JNN 2ro   )*r+   rI   torch.distributionsr   r   __all__r   r   r   r   realr1   independentr8   r:   positivenonnegativer=   greater_thangreater_than_eqrC   	less_thanrG   intervalhalf_open_intervalrP   simplexrS   rW   lower_choleskyrZ   positive_definitepositive_semidefiniter]   corr_choleskyr`   catrh   rk   stackrn   rq    r   r   <module>r      s  AF  7F# F#R  	!# K$$%{''() ) &) K++, - {../ 0 K(()
K++,{++,{../% 0 - - *% K,,-
K//0{//0{223 4 1 1 . K))*{,,- . + K(()
K223{++,{556 7 - 4 *( K''(/ )/ {**+) ,)
 {112/ 3/ {445{8892 : 62 K--.{001. 2 /. KOO$ % {' ( K%%& ' {(() *r   