o
    !i=(                     @   s  d Z ddlZddlmZmZ g dZG dd dZe Ze Ze	ej
e	ej
dd Ze	ejd	d
 Ze	ejdd Ze	eje	eje	eje	ejdd Ze	eje	eje	eje	ejdd Ze	eje	ejdd Ze	eje	eje	eje	ejdd Ze	ejdd Ze	ejdd Ze	ejdd Ze	eje	ej dd Z!e	ej"e	ej"dd Z#e	ej$dd  Z%e	ej$d!d" Z&e	ej'd#d$ Z(e	ej'd%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.
    N)constraints
transforms)ConstraintRegistry	biject_totransform_toc                       s2   e Zd ZdZ fddZd	ddZdd Z  ZS )
r   z5
    Registry to link constraints to transforms.
    c                    s   i | _ t   d S N)	_registrysuper__init__)self	__class__ v/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torch/distributions/constraint_registry.pyr
   T   s   zConstraintRegistry.__init__Nc                    s\   |du r fd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                    s     | S r   )register)factory
constraintr   r   r   <lambda>k   s    z-ConstraintRegistry.register.<locals>.<lambda>zLExpected constraint to be either a Constraint subclass or instance, but got )
isinstancer   
Constrainttype
issubclass	TypeErrorr   r   r   r   r   r   r   r   X   s   
zConstraintRegistry.registerc                 C   s@   z| j t| }W ||S  ty   tdt|j ddw )aq  
        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   r   r   r   __call__{   s   zConstraintRegistry.__call__r   )r   
__module____qualname____doc__r
   r   r   __classcell__r   r   r   r   r   O   s
    
#r   c                 C   s   t jS r   )r   Zidentity_transformr   r   r   r   _transform_to_real   s   r$   c                 C      t | j}t|| jS r   )r   base_constraintr   IndependentTransformreinterpreted_batch_ndimsr   Zbase_transformr   r   r   _biject_to_independent      
r*   c                 C   r%   r   )r   r&   r   r'   r(   r)   r   r   r   _transform_to_independent   r+   r,   c                 C      t  S r   )r   ExpTransformr#   r   r   r   _transform_to_positive   s   r/   c                 C      t t  t | jdgS )N   )r   ComposeTransformr.   AffineTransformlower_boundr#   r   r   r   _transform_to_greater_than   s
   r5   c                 C   r0   )N)r   r2   r.   r3   upper_boundr#   r   r   r   _transform_to_less_than   s
   r8   c                 C   sl   t | jtjo| jdk}t | jtjo| jdk}|r |r t S | j}| j| j }tt t||gS )Nr   r1   )	r   r4   numbersNumberr7   r   ZSigmoidTransformr2   r3   )r   Z
lower_is_0Z
upper_is_1locscaler   r   r   _transform_to_interval   s   r=   c                 C   r-   r   )r   ZStickBreakingTransformr#   r   r   r   _biject_to_simplex      r>   c                 C   r-   r   )r   ZSoftmaxTransformr#   r   r   r   _transform_to_simplex   r?   r@   c                 C   r-   r   )r   ZLowerCholeskyTransformr#   r   r   r   _transform_to_lower_cholesky   r?   rA   c                 C   r-   r   )r   ZPositiveDefiniteTransformr#   r   r   r   _transform_to_positive_definite      rB   c                 C   r-   r   )r   ZCorrCholeskyTransformr#   r   r   r   _transform_to_corr_cholesky  rC   rD   c                 C      t dd | jD | j| jS )Nc                 S      g | ]}t |qS r   r   .0cr   r   r   
<listcomp>      z"_biject_to_cat.<locals>.<listcomp>r   ZCatTransformcseqdimlengthsr#   r   r   r   _biject_to_cat     rQ   c                 C   rE   )Nc                 S   rF   r   r   rH   r   r   r   rK     rL   z%_transform_to_cat.<locals>.<listcomp>rM   r#   r   r   r   _transform_to_cat  rR   rT   c                 C      t dd | jD | jS )Nc                 S   rF   r   rG   rH   r   r   r   rK     rL   z$_biject_to_stack.<locals>.<listcomp>r   ZStackTransformrN   rO   r#   r   r   r   _biject_to_stack     rW   c                 C   rU   )Nc                 S   rF   r   rS   rH   r   r   r   rK   #  rL   z'_transform_to_stack.<locals>.<listcomp>rV   r#   r   r   r   _transform_to_stack   rX   rY   )*r!   r9   Ztorch.distributionsr   r   __all__r   r   r   r   realr$   Zindependentr*   r,   ZpositiveZnonnegativer/   greater_thanZgreater_than_eqr5   	less_thanr8   intervalZhalf_open_intervalr=   Zsimplexr>   r@   Zlower_choleskyrA   Zpositive_definiteZpositive_semidefiniterB   Zcorr_choleskyrD   catrQ   rT   stackrW   rY   r   r   r   r   <module>   sh    CI














	

	



















