a
    8SicW                     @   s   d dl mZ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 d dlmZ dd	d
Zdd ZG dd deZG dd deZdddeeee eedddZG dd deZd eeeeee edddZdS )!    )EnumautoN)Tensor   )parametrize)Module)
functional)Optionalc                 C   sV   |  d|  d }}tj|| j| jd}d| t| jj }tj| j|  ||dS )Ndtypedeviceg      $@)atol)	sizetorcheyer   r   finfoepsallclosemH)Qr   nkId r   [/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/nn/utils/parametrizations.py_is_orthogonal   s    r   c                 C   s<   t | \}}t j||}||jddd d9 }|S )z Assume that A is a tall matrix.
    Compute the Q factor s.t. A = QR (A may be complex) and diag(R) is real and non-negative
    r
   r   dim1dim2)r   geqrflinalghouseholder_productdiagonalsgn	unsqueeze)AXtaur   r   r   r   _make_orthogonal   s    r*   c                   @   s   e Zd Ze Ze Ze ZdS )	_OrthMapsN)__name__
__module____qualname__r   
matrix_expcayleyhouseholderr   r   r   r   r+      s   r+   c                       sf   e Zd ZU eed< ddedd fddZejejdd	d
Zej	
 ejejdddZ  ZS )_OrthogonalbaseTuse_trivializationN)orthogonal_mapreturnc                   sF   t    | r$|tjkr$td|j| _|| _|rB| dd  d S )NzAThe householder parametrization does not support complex tensors.r3   )	super__init__
is_complexr+   r1   
ValueErrorshaper6   register_buffer)selfweightr6   r5   	__class__r   r   r9   (   s    
z_Orthogonal.__init__)r(   r7   c           	      C   s  | d| d }}||k }|r2|j}|| }}| jtjksL| jtjkr| }||krtj||	||| j
g |jd d ddR  gdd}||j }| jtjkrt|}nH| jtjkrtj||j|jd}tjtj||ddtj||dd}||krl|dd |f }nN|jdd	}d
d|| jdd  }tj||}||jddd d }t| dr| j| }|r|j}|S )Nr
   r   dimr   g      )alphag      ?.)r$   g       @g      ?r   r3   )r   mTr6   r+   r/   r0   trilr   cat	new_zerosexpandr<   r   r   r   r   r"   solveaddsumr#   r$   intr&   hasattrr3   )	r>   r(   r   r   
transposedr'   r   r   r)   r   r   r   forwardC   s4    
<
&

z_Orthogonal.forward)r   r7   c           
      C   sj  |j | j kr&td| j  d|j  d|}|d|d }}||k }|r\|j}|| }}t| ds| jtjks~| jtjkrt	dt
|\}}|jddd  |jddd|d	k  d9  < |r|jS |S ||krt|st|}n| }nHt
j| d d ||| f |j|jd
}t
j||gdd}t|}|| _t
|}	|	jdddd |	S d S )Nz0Expected a matrix or batch of matrices of shape z. Got a tensor of shape .r
   r   r3   ztIt is not possible to assign to the matrix exponential or the Cayley parametrizations when use_trivialization=False.r   g        r   rB   g      )r<   r;   r   rE   rN   r6   r+   r0   r/   NotImplementedErrorr   r!   r$   sign_r   r*   clonerandnr   r   rG   r3   
zeros_likefill_)
r>   r   ZQ_initr   r   	transposer'   r)   NZneg_Idr   r   r   right_inversek   s8    




.
z_Orthogonal.right_inverse)r,   r-   r.   r   __annotations__r+   r9   r   rP   autogradno_gradrZ   __classcell__r   r   r@   r   r2   %   s   
(r2   r?   Tr4   )modulenamer6   r5   r7   c                C   s   t | |d}t|ts&td| ||jdk rBtd|j d|du rn|d|dksf| rjdnd	}t t|d}|du rtd
| t	|||d}t
j| ||dd | S )a  Applies an orthogonal or unitary parametrization to a matrix or a batch of matrices.

    Letting :math:`\mathbb{K}` be :math:`\mathbb{R}` or :math:`\mathbb{C}`, the parametrized
    matrix :math:`Q \in \mathbb{K}^{m \times n}` is **orthogonal** as

    .. math::

        \begin{align*}
            Q^{\text{H}}Q &= \mathrm{I}_n \mathrlap{\qquad \text{if }m \geq n}\\
            QQ^{\text{H}} &= \mathrm{I}_m \mathrlap{\qquad \text{if }m < n}
        \end{align*}

    where :math:`Q^{\text{H}}` is the conjugate transpose when :math:`Q` is complex
    and the transpose when :math:`Q` is real-valued, and
    :math:`\mathrm{I}_n` is the `n`-dimensional identity matrix.
    In plain words, :math:`Q` will have orthonormal columns whenever :math:`m \geq n`
    and orthonormal rows otherwise.

    If the tensor has more than two dimensions, we consider it as a batch of matrices of shape `(..., m, n)`.

    The matrix :math:`Q` may be parametrized via three different ``orthogonal_map`` in terms of the original tensor:

    - ``"matrix_exp"``/``"cayley"``:
      the :func:`~torch.matrix_exp` :math:`Q = \exp(A)` and the `Cayley map`_
      :math:`Q = (\mathrm{I}_n + A/2)(\mathrm{I}_n - A/2)^{-1}` are applied to a skew-symmetric
      :math:`A` to give an orthogonal matrix.
    - ``"householder"``: computes a product of Householder reflectors
      (:func:`~torch.linalg.householder_product`).

    ``"matrix_exp"``/``"cayley"`` often make the parametrized weight converge faster than
    ``"householder"``, but they are slower to compute for very thin or very wide matrices.

    If ``use_trivialization=True`` (default), the parametrization implements the "Dynamic Trivialization Framework",
    where an extra matrix :math:`B \in \mathbb{K}^{n \times n}` is stored under
    ``module.parametrizations.weight[0].base``. This helps the
    convergence of the parametrized layer at the expense of some extra memory use.
    See `Trivializations for Gradient-Based Optimization on Manifolds`_ .

    Initial value of :math:`Q`:
    If the original tensor is not parametrized and ``use_trivialization=True`` (default), the initial value
    of :math:`Q` is that of the original tensor if it is orthogonal (or unitary in the complex case)
    and it is orthogonalized via the QR decomposition otherwise (see :func:`torch.linalg.qr`).
    Same happens when it is not parametrized and ``orthogonal_map="householder"`` even when ``use_trivialization=False``.
    Otherwise, the initial value is the result of the composition of all the registered
    parametrizations applied to the original tensor.

    .. note::
        This function is implemented using the parametrization functionality
        in :func:`~torch.nn.utils.parametrize.register_parametrization`.


    .. _`Cayley map`: https://en.wikipedia.org/wiki/Cayley_transform#Matrix_map
    .. _`Trivializations for Gradient-Based Optimization on Manifolds`: https://arxiv.org/abs/1909.09501

    Args:
        module (nn.Module): module on which to register the parametrization.
        name (str, optional): name of the tensor to make orthogonal. Default: ``"weight"``.
        orthogonal_map (str, optional): One of the following: ``"matrix_exp"``, ``"cayley"``, ``"householder"``.
            Default: ``"matrix_exp"`` if the matrix is square or complex, ``"householder"`` otherwise.
        use_trivialization (bool, optional): whether to use the dynamic trivialization framework.
            Default: ``True``.

    Returns:
        The original module with an orthogonal parametrization registered to the specified
        weight

    Example::

        >>> orth_linear = orthogonal(nn.Linear(20, 40))
        >>> orth_linear
        ParametrizedLinear(
        in_features=20, out_features=40, bias=True
        (parametrizations): ModuleDict(
            (weight): ParametrizationList(
            (0): _Orthogonal()
            )
        )
        )
        >>> Q = orth_linear.weight
        >>> torch.dist(Q.T @ Q, torch.eye(20))
        tensor(4.9332e-07)
    Nz5Module '{}' has no parameter ot buffer with name '{}'r   z8Expected a matrix or batch of matrices. Got a tensor of z dimensions.r
   r   r/   r1   zLorthogonal_map has to be one of "matrix_exp", "cayley", "householder". Got: r4   T)unsafe)getattr
isinstancer   r;   formatndimr   r:   r+   r2   r   register_parametrization)r_   r`   r6   r5   r?   Z	orth_enumZorthr   r   r   
orthogonal   s,    W



$rg   c                       s   e Zd Zdejeeedd fddZejejdd	d
Zej	
 ejeddddZejejdddZejejdddZ  ZS )_SpectralNorm   r   -q=N)r?   n_power_iterationsrC   r   r7   c                    s   t    |j}||ks"|| k rBtd| d|d  d| d|dkrXtd||dkrd|n|| | _|| _|dkr|| _| 	|}|
 \}}||dd}	||dd}
| dtj|	d| jd	 | d
tj|
d| jd	 | |d d S )Nz5Dimension out of range (expected to be in range of [-z, ri   z
] but got )r   zIExpected n_power_iterations to be positive, but got n_power_iterations={}_urC   r   _v   )r8   r9   re   
IndexErrorr;   rd   rC   r   rk   _reshape_weight_to_matrixr   	new_emptynormal_r=   F	normalize_power_method)r>   r?   rk   rC   r   re   
weight_mathwuvr@   r   r   r9     s2    


z_SpectralNorm.__init__)r?   r7   c                    sL   |j dksJ  jdkrB|j jg fddt| D R  }|dS )Nri   r   c                 3   s   | ]}| j kr|V  qd S NrB   ).0dr>   r   r   	<genexpr>D      z:_SpectralNorm._reshape_weight_to_matrix.<locals>.<genexpr>)re   rC   permuterangeflatten)r>   r?   r   r   r   rr   >  s    
*z'_SpectralNorm._reshape_weight_to_matrix)rx   rk   r7   c                 C   sh   |j dksJ t|D ]L}tjt|| jd| j| jd| _tjt|	 | jd| j| jd| _qd S )Nri   r   )rC   r   out)
re   r   ru   rv   r   mvro   r   rm   t)r>   rx   rk   _r   r   r   rw   H  s    "

z_SpectralNorm._power_methodc                 C   sz   |j dkrtj|d| jdS | |}| jr:| || j | jj	t
jd}| jj	t
jd}t
|t
||}|| S d S )Nri   r   rn   )memory_format)re   ru   rv   r   rr   trainingrw   rk   rm   rT   r   contiguous_formatro   dotr   )r>   r?   rx   r{   r|   sigmar   r   r   rP   u  s    

z_SpectralNorm.forward)valuer7   c                 C   s   |S r}   r   )r>   r   r   r   r   rZ     s    z_SpectralNorm.right_inverse)ri   r   rj   )r,   r-   r.   r   r   rM   floatr9   rr   r\   r]   rw   rP   rZ   r^   r   r   r@   r   rh     s      !
,rh   ri   rj   )r_   r`   rk   r   rC   r7   c              	   C   sp   t | |d}t|ts&td| ||du rTt| tjjtjjtjj	frPd}nd}t
| |t|||| | S )a  Applies spectral normalization to a parameter in the given module.

    .. math::
        \mathbf{W}_{SN} = \dfrac{\mathbf{W}}{\sigma(\mathbf{W})},
        \sigma(\mathbf{W}) = \max_{\mathbf{h}: \mathbf{h} \ne 0} \dfrac{\|\mathbf{W} \mathbf{h}\|_2}{\|\mathbf{h}\|_2}

    When applied on a vector, it simplifies to

    .. math::
        \mathbf{x}_{SN} = \dfrac{\mathbf{x}}{\|\mathbf{x}\|_2}

    Spectral normalization stabilizes the training of discriminators (critics)
    in Generative Adversarial Networks (GANs) by reducing the Lipschitz constant
    of the model. :math:`\sigma` is approximated performing one iteration of the
    `power method`_ every time the weight is accessed. If the dimension of the
    weight tensor is greater than 2, it is reshaped to 2D in power iteration
    method to get spectral norm.


    See `Spectral Normalization for Generative Adversarial Networks`_ .

    .. _`power method`: https://en.wikipedia.org/wiki/Power_iteration
    .. _`Spectral Normalization for Generative Adversarial Networks`: https://arxiv.org/abs/1802.05957

    .. note::
        This function is implemented using the parametrization functionality
        in :func:`~torch.nn.utils.parametrize.register_parametrization`. It is a
        reimplementation of :func:`torch.nn.utils.spectral_norm`.

    .. note::
        When this constraint is registered, the singular vectors associated to the largest
        singular value are estimated rather than sampled at random. These are then updated
        performing :attr:`n_power_iterations` of the `power method`_ whenever the tensor
        is accessed with the module on `training` mode.

    .. note::
        If the `_SpectralNorm` module, i.e., `module.parametrization.weight[idx]`,
        is in training mode on removal, it will perform another power iteration.
        If you'd like to avoid this iteration, set the module to eval mode
        before its removal.

    Args:
        module (nn.Module): containing module
        name (str, optional): name of weight parameter. Default: ``"weight"``.
        n_power_iterations (int, optional): number of power iterations to
            calculate spectral norm. Default: ``1``.
        eps (float, optional): epsilon for numerical stability in
            calculating norms. Default: ``1e-12``.
        dim (int, optional): dimension corresponding to number of outputs.
            Default: ``0``, except for modules that are instances of
            ConvTranspose{1,2,3}d, when it is ``1``

    Returns:
        The original module with a new parametrization registered to the specified
        weight

    Example::

        >>> snm = spectral_norm(nn.Linear(20, 40))
        >>> snm
        ParametrizedLinear(
        in_features=20, out_features=40, bias=True
        (parametrizations): ModuleDict(
            (weight): ParametrizationList(
            (0): _SpectralNorm()
            )
        )
        )
        >>> torch.linalg.matrix_norm(snm.weight, 2)
        tensor(1.0000, grad_fn=<CopyBackwards>)
    Nz5Module '{}' has no parameter or buffer with name '{}'ri   r   )rb   rc   r   r;   rd   r   nnConvTranspose1dConvTranspose2dConvTranspose3dr   rf   rh   )r_   r`   rk   r   rC   r?   r   r   r   spectral_norm  s    L


r   )N)r?   N)r?   ri   rj   N)enumr   r   r   r   utilsr   modulesr    r   ru   typingr	   r   r*   r+   r2   strboolrg   rh   rM   r   r   r   r   r   r   <module>   s>   
   qq    