§
    ŠŠtjB(  ã                   ó  — d Z ddlmZmZ ddlmZ g d¢Z G d„ d¦  «        Z e¦   «         Z e¦   «         Z	e 
                    ej        ¦  «        e	 
                    ej        ¦  «        d„ ¦   «         ¦   «         Ze 
                    ej        ¦  «        d„ ¦   «         Ze	 
                    ej        ¦  «        d	„ ¦   «         Ze 
                    ej        ¦  «        e 
                    ej        ¦  «        e	 
                    ej        ¦  «        e	 
                    ej        ¦  «        d
„ ¦   «         ¦   «         ¦   «         ¦   «         Ze 
                    ej        ¦  «        e 
                    ej        ¦  «        e	 
                    ej        ¦  «        e	 
                    ej        ¦  «        d„ ¦   «         ¦   «         ¦   «         ¦   «         Ze 
                    ej        ¦  «        e	 
                    ej        ¦  «        d„ ¦   «         ¦   «         Ze 
                    ej        ¦  «        e 
                    ej        ¦  «        e	 
                    ej        ¦  «        e	 
                    ej        ¦  «        d„ ¦   «         ¦   «         ¦   «         ¦   «         Ze 
                    ej        ¦  «        d„ ¦   «         Ze	 
                    ej        ¦  «        d„ ¦   «         Ze	 
                    ej        ¦  «        d„ ¦   «         Ze	 
                    ej         ¦  «        e	 
                    ej!        ¦  «        d„ ¦   «         ¦   «         Z"e 
                    ej#        ¦  «        e	 
                    ej#        ¦  «        d„ ¦   «         ¦   «         Z$e 
                    ej%        ¦  «        d„ ¦   «         Z&e	 
                    ej%        ¦  «        d„ ¦   «         Z'e 
                    ej(        ¦  «        d„ ¦   «         Z)e	 
                    ej(        ¦  «        d„ ¦   «         Z*dS )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.
é    )ÚconstraintsÚ
transforms)Ú_Number)ÚConstraintRegistryÚ	biject_toÚtransform_toc                   ó0   ‡ — e Zd ZdZˆ fd„Zdd„Zd„ Zˆ xZS )r   z5
    Registry to link constraints to transforms.
    c                 óV   •— i | _         t          ¦   «                              ¦   «          d S ©N)Ú	_registryÚsuperÚ__init__)ÚselfÚ	__class__s    €úe/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/distributions/constraint_registry.pyr   zConstraintRegistry.__init__U   s&   ø€ ØˆŒÝ‰Œ×ÒÑÔÐÐÐó    Nc                 ó  ‡ ‡— |€ˆˆ fd„S t          ‰t          j        ¦  «        rt          ‰¦  «        Št          ‰t          ¦  «        rt	          ‰t          j        ¦  «        st          d‰› �¦  «        ‚|‰ j        ‰<   |S )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.
        Nc                 ó0   •— ‰                      ‰| ¦  «        S r   )Úregister)ÚfactoryÚ
constraintr   s    €€r   ú<lambda>z-ConstraintRegistry.register.<locals>.<lambda>l   s   ø€  4§=¢=°¸WÑ#EÔ#E€ r   zLExpected constraint to be either a Constraint subclass or instance, but got )Ú
isinstancer   Ú
ConstraintÚtypeÚ
issubclassÚ	TypeErrorr   ©r   r   r   s   `` r   r   zConstraintRegistry.registerY   sŸ   øø€ ð$ ˆ?ØEÐEÐEÐEÐEÐEõ �j¥+Ô"8Ñ9Ô9ð 	*Ý˜jÑ)Ô)ˆJå˜*¥dÑ+Ô+ð 	µ:Ø�Ô.ñ4
ô 4
ð 	õ ØkÐ_iÐkÐkñô ð ð &-ˆŒ�zÑ"Øˆr   c                 ó¸   — 	 | j         t          |¦  «                 }n4# t          $ r' t          dt          |¦  «        j        › d�¦  «        d‚w xY w ||¦  «        S )ah  
        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__zConstraintRegistry.__call__|   sx   € ð(	Ø”n¥T¨*Ñ%5Ô%5Ô6ˆGˆGøÝð 	ð 	ð 	Ý%ØK¥D¨Ñ$4Ô$4Ô$=ÐKÐKÐKñô àðð	øøøð ˆw�zÑ"Ô"Ð"s	   ‚ �1Ar   )r"   Ú
__module__Ú__qualname__Ú__doc__r   r   r#   Ú__classcell__)r   s   @r   r   r   P   sf   ø€ € € € € ðð ðð ð ð ð ð!ð !ð !ð !ðF#ð #ð #ð #ð #ð #ð #r   r   c                 ó   — t           j        S r   )r   Úidentity_transform©r   s    r   Ú_transform_to_realr+   ¢   s   € õ Ô(Ð(r   c                 ó^   — t          | j        ¦  «        }t          j        || j        ¦  «        S r   )r   Úbase_constraintr   ÚIndependentTransformÚreinterpreted_batch_ndims©r   Úbase_transforms     r   Ú_biject_to_independentr2   ¨   s/   € å˜zÔ9Ñ:Ô:€NÝÔ*Ø˜
Ô<ñô ð r   c                 ó^   — t          | j        ¦  «        }t          j        || j        ¦  «        S r   )r   r-   r   r.   r/   r0   s     r   Ú_transform_to_independentr4   °   s/   € å! *Ô"<Ñ=Ô=€NÝÔ*Ø˜
Ô<ñô ð r   c                 ó(   — t          j        ¦   «         S r   )r   ÚExpTransformr*   s    r   Ú_transform_to_positiver7   ¸   s   € õ
 Ô"Ñ$Ô$Ð$r   c                 ó€   — t          j        t          j        ¦   «         t          j        | j        d¦  «        g¦  «        S )Né   )r   ÚComposeTransformr6   ÚAffineTransformÚlower_boundr*   s    r   Ú_transform_to_greater_thanr=   À   s=   € õ
 Ô&åÔ#Ñ%Ô%ÝÔ& zÔ'=¸qÑAÔAð	
ñô ð r   c                 ó€   — t          j        t          j        ¦   «         t          j        | j        d¦  «        g¦  «        S )Néÿÿÿÿ)r   r:   r6   r;   Úupper_boundr*   s    r   Ú_transform_to_less_thanrA   Í   s=   € õ Ô&åÔ#Ñ%Ô%ÝÔ& zÔ'=¸rÑBÔBð	
ñô ð r   c                 ód  — t          | j        t          ¦  «        o
| j        dk    }t          | j        t          ¦  «        o
| j        dk    }|r|rt	          j        ¦   «         S | j        }| j        | j        z
  }t	          j        t	          j        ¦   «         t	          j        ||¦  «        g¦  «        S )Nr   r9   )r   r<   r   r@   r   ÚSigmoidTransformr:   r;   )r   Ú
lower_is_0Ú
upper_is_1ÚlocÚscales        r   Ú_transform_to_intervalrH   Ø   s·   € õ 	�:Ô)­7Ñ3Ô3ÐS¸
Ô8NÐRSÒ8Sð õ 	�:Ô)­7Ñ3Ô3ÐS¸
Ô8NÐRSÒ8Sð ð ð -�jð -ÝÔ*Ñ,Ô,Ð,à
Ô
 €CØÔ" ZÔ%;Ñ;€EÝÔ&Ý	Ô	$Ñ	&Ô	&­
Ô(BÀ3ÈÑ(NÔ(NÐOñô ð r   c                 ó(   — t          j        ¦   «         S r   )r   ÚStickBreakingTransformr*   s    r   Ú_biject_to_simplexrK   î   ó   € åÔ,Ñ.Ô.Ð.r   c                 ó(   — t          j        ¦   «         S r   )r   ÚSoftmaxTransformr*   s    r   Ú_transform_to_simplexrO   ó   s   € åÔ&Ñ(Ô(Ð(r   c                 ó(   — t          j        ¦   «         S r   )r   ÚLowerCholeskyTransformr*   s    r   Ú_transform_to_lower_choleskyrR   ù   rL   r   c                 ó(   — t          j        ¦   «         S r   )r   ÚPositiveDefiniteTransformr*   s    r   Ú_transform_to_positive_definiterU   þ   s   € õ Ô/Ñ1Ô1Ð1r   c                 ó(   — t          j        ¦   «         S r   )r   ÚCorrCholeskyTransformr*   s    r   Ú_transform_to_corr_choleskyrX     s   € õ Ô+Ñ-Ô-Ð-r   c                 ó`   — t          j        d„ | j        D ¦   «         | j        | j        ¦  «        S )Nc                 ó,   — g | ]}t          |¦  «        ‘ŒS © ©r   ©Ú.0Úcs     r   ú
<listcomp>z"_biject_to_cat.<locals>.<listcomp>  ó   € Ð/Ð/Ð/˜!��1‰ŒÐ/Ð/Ð/r   ©r   ÚCatTransformÚcseqÚdimÚlengthsr*   s    r   Ú_biject_to_catrg   
  s3   € åÔ"Ø/Ð/˜zœÐ/Ñ/Ô/°´ÀÔASñô ð r   c                 ó`   — t          j        d„ | j        D ¦   «         | j        | j        ¦  «        S )Nc                 ó,   — g | ]}t          |¦  «        ‘ŒS r[   ©r   r]   s     r   r`   z%_transform_to_cat.<locals>.<listcomp>  ó   € Ð2Ð2Ð2˜Q��a‰ŒÐ2Ð2Ð2r   rb   r*   s    r   Ú_transform_to_catrl     s3   € åÔ"Ø2Ð2 *¤/Ð2Ñ2Ô2°J´NÀJÔDVñô ð r   c                 óT   — t          j        d„ | j        D ¦   «         | j        ¦  «        S )Nc                 ó,   — g | ]}t          |¦  «        ‘ŒS r[   r\   r]   s     r   r`   z$_biject_to_stack.<locals>.<listcomp>  ra   r   ©r   ÚStackTransformrd   re   r*   s    r   Ú_biject_to_stackrq     s.   € åÔ$Ø/Ð/˜zœÐ/Ñ/Ô/°´ñô ð r   c                 óT   — t          j        d„ | j        D ¦   «         | j        ¦  «        S )Nc                 ó,   — g | ]}t          |¦  «        ‘ŒS r[   rj   r]   s     r   r`   z'_transform_to_stack.<locals>.<listcomp>"  rk   r   ro   r*   s    r   Ú_transform_to_stackrt     s.   € åÔ$Ø2Ð2 *¤/Ð2Ñ2Ô2°J´Nñô ð r   N)+r&   Útorch.distributionsr   r   Útorch.typesr   Ú__all__r   r   r   r   Úrealr+   Úindependentr2   r4   ÚpositiveÚnonnegativer7   Úgreater_thanÚgreater_than_eqr=   Ú	less_thanrA   ÚintervalÚhalf_open_intervalrH   ÚsimplexrK   rO   Úlower_choleskyrR   Úpositive_definiteÚpositive_semidefiniterU   Úcorr_choleskyrX   Úcatrg   rl   Ústackrq   rt   r[   r   r   ú<module>rˆ      sâ  ððAð AðF 8Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø Ð Ð Ð Ð Ð ðð ð €ðF#ð F#ð F#ð F#ð F#ñ F#ô F#ð F#ðR ÐÑ Ô €	Ø!Ð!Ñ#Ô#€ð ×Ò�KÔ$Ñ%Ô%Ø×Ò�{Ô'Ñ(Ô(ð)ð )ñ )Ô(ñ &Ô%ð)ð ×Ò�KÔ+Ñ,Ô,ðð ñ -Ô,ðð ×Ò�{Ô.Ñ/Ô/ðð ñ 0Ô/ðð ×Ò�KÔ(Ñ)Ô)Ø
×Ò�KÔ+Ñ,Ô,Ø×Ò�{Ô+Ñ,Ô,Ø×Ò�{Ô.Ñ/Ô/ð%ð %ñ 0Ô/ñ -Ô,ñ -Ô,ñ *Ô)ð%ð ×Ò�KÔ,Ñ-Ô-Ø
×Ò�KÔ/Ñ0Ô0Ø×Ò�{Ô/Ñ0Ô0Ø×Ò�{Ô2Ñ3Ô3ðð ñ 4Ô3ñ 1Ô0ñ 1Ô0ñ .Ô-ðð ×Ò�KÔ)Ñ*Ô*Ø×Ò�{Ô,Ñ-Ô-ðð ñ .Ô-ñ +Ô*ðð ×Ò�KÔ(Ñ)Ô)Ø
×Ò�KÔ2Ñ3Ô3Ø×Ò�{Ô+Ñ,Ô,Ø×Ò�{Ô5Ñ6Ô6ðð ñ 7Ô6ñ -Ô,ñ 4Ô3ñ *Ô)ðð$ ×Ò�KÔ'Ñ(Ô(ð/ð /ñ )Ô(ð/ð ×Ò�{Ô*Ñ+Ô+ð)ð )ñ ,Ô+ð)ð
 ×Ò�{Ô1Ñ2Ô2ð/ð /ñ 3Ô2ð/ð ×Ò�{Ô4Ñ5Ô5Ø×Ò�{Ô8Ñ9Ô9ð2ð 2ñ :Ô9ñ 6Ô5ð2ð ×Ò�KÔ-Ñ.Ô.Ø×Ò�{Ô0Ñ1Ô1ð.ð .ñ 2Ô1ñ /Ô.ð.ð ×Ò�K”OÑ$Ô$ðð ñ %Ô$ðð ×Ò�{”Ñ'Ô'ðð ñ (Ô'ðð ×Ò�KÔ%Ñ&Ô&ðð ñ 'Ô&ðð ×Ò�{Ô(Ñ)Ô)ðð ñ *Ô)ðð ð r   