U
    <c                     @   s   d Z ddlmZmZ ddlmZmZ ddlmZm	Z	 ddl
mZ ddd	gZG d
d deZe	dedZdeeeedddZdeeeddd	ZdS )z<
Weight Normalization from https://arxiv.org/abs/1602.07868
    )	ParameterUninitializedParameter)_weight_normnorm_except_dim)AnyTypeVar   )Module
WeightNormweight_normremove_weight_normc                   @   sx   e Zd ZU eed< eed< eeddddZeeddd	Z	e
eed dd
dZeddddZeeddddZdS )r
   namedimN)r   r   returnc                 C   s   |d krd}|| _ || _d S )N)r   r   )selfr   r    r   >/tmp/pip-unpacked-wheel-gikjz4vx/torch/nn/utils/weight_norm.py__init__   s    zWeightNorm.__init__)moduler   c                 C   s.   t || jd }t || jd }t||| jS N_g_v)getattrr   r   r   )r   r   gvr   r   r   compute_weight   s    zWeightNorm.compute_weightc                 C   s   | j  D ]*\}}t|tr
|j|kr
td|q
|d krBd}t||}t| |}t|trht	d| j
|= | |d tt|d|j | |d t|j t| |||  | | |S )Nz>Cannot register two weight_norm hooks on the same parameter {}r   zThe module passed to `WeightNorm` can't have uninitialized parameters. Make sure to run the dummy forward before applying weight normalizationr   r   r   )_forward_pre_hooksitems
isinstancer
   r   RuntimeErrorformatr   r   
ValueError_parametersZregister_parameterr   r   datasetattrr   Zregister_forward_pre_hook)r   r   r   khookfnweightr   r   r   apply   s&    



zWeightNorm.applyc                 C   sJ   |  |}t|| j |j| jd = |j| jd = t|| jt|j d S r   )r   delattrr   r#   r%   r   r$   )r   r   r)   r   r   r   remove9   s
    
zWeightNorm.remove)r   inputsr   c                 C   s   t || j| | d S )N)r%   r   r   )r   r   r-   r   r   r   __call__@   s    zWeightNorm.__call__)__name__
__module____qualname__str__annotations__intr   r	   r   r   staticmethodr*   r,   r.   r   r   r   r   r
      s   
T_module)boundr)   )r   r   r   r   c                 C   s   t | || | S )a[  Applies weight normalization to a parameter in the given module.

    .. math::
         \mathbf{w} = g \dfrac{\mathbf{v}}{\|\mathbf{v}\|}

    Weight normalization is a reparameterization that decouples the magnitude
    of a weight tensor from its direction. This replaces the parameter specified
    by :attr:`name` (e.g. ``'weight'``) with two parameters: one specifying the magnitude
    (e.g. ``'weight_g'``) and one specifying the direction (e.g. ``'weight_v'``).
    Weight normalization is implemented via a hook that recomputes the weight
    tensor from the magnitude and direction before every :meth:`~Module.forward`
    call.

    By default, with ``dim=0``, the norm is computed independently per output
    channel/plane. To compute a norm over the entire weight tensor, use
    ``dim=None``.

    See https://arxiv.org/abs/1602.07868

    Args:
        module (Module): containing module
        name (str, optional): name of weight parameter
        dim (int, optional): dimension over which to compute the norm

    Returns:
        The original module with the weight norm hook

    Example::

        >>> m = weight_norm(nn.Linear(20, 40), name='weight')
        >>> m
        Linear(in_features=20, out_features=40, bias=True)
        >>> m.weight_g.size()
        torch.Size([40, 1])
        >>> m.weight_v.size()
        torch.Size([40, 20])

    )r
   r*   )r   r   r   r   r   r   r   F   s    ')r   r   r   c                 C   sV   | j  D ]6\}}t|tr
|j|kr
||  | j |= |   S q
td|| dS )a  Removes the weight normalization reparameterization from a module.

    Args:
        module (Module): containing module
        name (str, optional): name of weight parameter

    Example:
        >>> m = weight_norm(nn.Linear(20, 40))
        >>> remove_weight_norm(m)
    z#weight_norm of '{}' not found in {}N)r   r   r   r
   r   r,   r"   r!   )r   r   r&   r'   r   r   r   r   q   s    

 N)r)   r   )r)   )__doc__Ztorch.nn.parameterr   r   Ztorchr   r   typingr   r   modulesr	   __all__objectr
   r6   r2   r4   r   r   r   r   r   r   <module>   s   
9+