
    sgA:                         d Z ddlmZmZmZ ddlZddlmc mZ	 ddl
mZ g dZ G d d      Z G d d	      Z G d
 d      Z ede      Z	 	 	 	 ddededededee   defdZddededefdZy)z=Spectral Normalization from https://arxiv.org/abs/1802.05957.    )AnyOptionalTypeVarN)Module)SpectralNorm SpectralNormLoadStateDictPreHookSpectralNormStateDictHookspectral_normremove_spectral_normc                      e Zd ZU dZeed<   eed<   eed<   eed<   eed<   	 	 	 	 ddededededd	f
d
Zde	j                  de	j                  fdZdedede	j                  fdZdedd	fdZdededd	fdZd Zededededededd fd       Zy	)r      _versionnamedimn_power_iterationsepsweightreturnNc                 b    || _         || _        |dk  rt        d|       || _        || _        y )Nr   zGExpected n_power_iterations to be positive, but got n_power_iterations=)r   r   
ValueErrorr   r   )selfr   r   r   r   s        O/var/www/html/venv/lib/python3.12/site-packages/torch/nn/utils/spectral_norm.py__init__zSpectralNorm.__init__"   sH     	"**<)=?  #5    c                    |}| j                   dk7  rR |j                  | j                   gt        |j                               D cg c]  }|| j                   k7  s| c} }|j                  d      }|j	                  |d      S c c}w )Nr   )r   permuterangesizereshape)r   r   
weight_matdheights        r   reshape_weight_to_matrixz%SpectralNorm.reshape_weight_to_matrix3   s}    
88q=+++',Z^^-='>P!!txx-APJ #!!&"-- Qs   B
B
moduledo_power_iterationc                 >   t        || j                  dz         }t        || j                  dz         }t        || j                  dz         }| j                  |      }|rt        j                         5  t        | j                        D ]~  }t        j                  t        j                  |j                         |      d| j                  |      }t        j                  t        j                  ||      d| j                  |      } | j                  dkD  r@|j                  t        j                        }|j                  t        j                        }d d d        t        j                  |t        j                  ||            }||z  }|S # 1 sw Y   :xY w)N_orig_u_vr   )r   r   out)memory_format)getattrr   r$   torchno_gradr   r   F	normalizemvtr   clonecontiguous_formatdot)	r   r%   r&   r   uvr!   _sigmas	            r   compute_weightzSpectralNorm.compute_weight=   sO   < W!45FDII,-FDII,-226:
 Gt667 YA 3aA EHHZ$;VWXAY **Q.e.E.EFAe.E.EFAG 		!UXXj!45%!G Gs   4C&FFc                    t        j                         5  | j                  |d      }d d d        t        || j                         t        || j                  dz          t        || j                  dz          t        || j                  dz          |j                  | j                  t         j                  j                  j                                      y # 1 sw Y   xY w)NFr&   r)   r*   r(   )	r.   r/   r;   delattrr   register_parameternn	Parameterdetach)r   r%   r   s      r   removezSpectralNorm.removes   s    ]]_ 	K((E(JF	K		"		D()		D()		G+,!!$))UXX-?-?-PQ	K 	Ks   CC#inputsc                 h    t        || j                  | j                  ||j                               y )Nr=   )setattrr   r;   training)r   r%   rD   s      r   __call__zSpectralNorm.__call__|   s+    II6??K	
r   c           
      l   t         j                  j                  |j                         j	                  |      j                         |j                         |j                  d      g      j                  d      }|j                  |t        j                  |t        j                  ||            z        S )Nr   )r.   linalg	multi_dotr3   mmpinverse	unsqueezesqueezemul_r6   r2   )r   r!   r7   target_sigmar8   s        r   _solve_v_and_rescalez!SpectralNorm._solve_v_and_rescale   s     LL""\\^z*335z||~q{{ST~V

'!* 	
 vvlUYYq%((:q2I%JJKKr   c                    | j                   j                         D ]0  }t        |t              s|j                  |k(  s$t        d|        t        ||||      }| j                  |   }|t        d| d      t        |t        j                  j                  j                        rt        d      t        j                         5  |j                  |      }|j                         \  }	}
t        j                   |j#                  |	      j%                  dd      d|j&                        }t        j                   |j#                  |
      j%                  dd      d|j&                        }d d d        t)        | |j                         | j+                  |j                  dz   |       t-        | |j                  |j.                         | j1                  |j                  d	z          | j1                  |j                  d
z          | j3                  |       | j5                  t7        |             | j9                  t;        |             |S # 1 sw Y   xY w)Nz>Cannot register two spectral_norm hooks on the same parameter z/`SpectralNorm` cannot be applied as parameter `z	` is NonezThe module passed to `SpectralNorm` can't have uninitialized parameters. Make sure to run the dummy forward before applying spectral normalizationr   r   )r   r   r(   r)   r*   )_forward_pre_hooksvalues
isinstancer   r   RuntimeError_parametersr   r.   r@   	parameterUninitializedParameterr/   r$   r   r0   r1   	new_emptynormal_r   r>   r?   rF   dataregister_bufferregister_forward_pre_hook_register_state_dict_hookr	   "_register_load_state_dict_pre_hookr   )r%   r   r   r   r   hookfnr   r!   hwr7   r8   s                r   applyzSpectralNorm.apply   s    --446 	D$-$))t2C"TUYTZ[ 	 $ 2C=##D)>A$yQ  fehh00GGH\ 
 ]]_ 	R44V<J??$DAqF,,Q/771=1"&&QAF,,Q/771=1"&&QA	R 	 !!"''G"3V< 	-rww~q1rww~q1((,(()B2)FG112RSU2VW	-	R 	Rs   	B'II)r   r   r   -q=)__name__
__module____qualname__r   int__annotations__strfloatr   r.   Tensorr$   r   boolr;   rC   r   rH   rR   staticmethodrf    r   r   r   r      s   
 Hc
 I	H	J "#   	
  
".u|| . .4V 4 4%,, 4lRV R R
v 
s 
t 
L ++!+7:+AD+KP+	+ +r   r   c                        e Zd ZddZ	 	 ddZy)r   Nc                     || _         y Nrc   r   rc   s     r   r   z)SpectralNormLoadStateDictPreHook.__init__   	    r   c                 N   | j                   }|j                  di       j                  |j                  dz   d       }	|	|	dk  r||j                  z   |	t        fddD              rvry d}
dD ]"  }|z   }|vsd}
|s|j	                  |       $ |
ry t        j                         5  d	z      }j                        }||z  j                         }|j                  |      }d
z      }|j                  |||      }|dz   <   d d d        y y # 1 sw Y   y xY w)Nr
   .versionr   c              3   ,   K   | ]  }|z   v   y wru   rr   ).0s
state_dict
weight_keys     r   	<genexpr>z<SpectralNormLoadStateDictPreHook.__call__.<locals>.<genexpr>   s     T
Q*4Ts   )r(   r)   r*   F)r(    r)   Tr(   r)   r*   )rc   getr   allappendr.   r/   popmeanr$   rR   )r   r~   prefixlocal_metadatastrictmissing_keysunexpected_keys
error_msgsrc   versionhas_missing_keyssuffixkeyweight_origr   r:   r!   r7   r8   r   s    `                 @r   rH   z)SpectralNormLoadStateDictPreHook.__call__   sU    WW $$_b9==GGj $
 ?gk"'')JT>STTj0
 $- 1 6)j('+$$++C01   2(g)=>#
3$v-33588E
zD01++J5A01
:,-2 2) *(2 2s   0A!DD$r   Nrh   ri   rj   r   rH   rr   r   r   r   r      s    )2 
)2r   r   c                       e Zd ZddZddZy)r	   Nc                     || _         y ru   rv   rw   s     r   r   z"SpectralNormStateDictHook.__init__   rx   r   c                     d|vri |d<   | j                   j                  dz   }||d   v rt        d|       | j                   j                  |d   |<   y )Nr
   rz   z-Unexpected key in metadata['spectral_norm']: )rc   r   rW   r   )r   r%   r~   r   r   r   s         r   rH   z"SpectralNormStateDictHook.__call__   s_    .0.0N?+ggllZ'.11!NseTUU/3ww/?/?',r   r   r   rr   r   r   r	   r	      s    @r   r	   T_module)boundr%   r   r   r   r   r   c                     |\t        | t        j                  j                  t        j                  j                  t        j                  j
                  f      rd}nd}t        j                  | ||||       | S )aA  Apply 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}

    Spectral normalization stabilizes the training of discriminators (critics)
    in Generative Adversarial Networks (GANs) by rescaling the weight tensor
    with spectral norm :math:`\sigma` of the weight matrix calculated using
    power iteration method. If the dimension of the weight tensor is greater
    than 2, it is reshaped to 2D in power iteration method to get spectral
    norm. This is implemented via a hook that calculates spectral norm and
    rescales weight before every :meth:`~Module.forward` call.

    See `Spectral Normalization for Generative Adversarial Networks`_ .

    .. _`Spectral Normalization for Generative Adversarial Networks`: https://arxiv.org/abs/1802.05957

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

    Returns:
        The original module with the spectral norm hook

    .. note::
        This function has been reimplemented as
        :func:`torch.nn.utils.parametrizations.spectral_norm` using the new
        parametrization functionality in
        :func:`torch.nn.utils.parametrize.register_parametrization`. Please use
        the newer version. This function will be deprecated in a future version
        of PyTorch.

    Example::

        >>> m = spectral_norm(nn.Linear(20, 40))
        >>> m
        Linear(in_features=20, out_features=40, bias=True)
        >>> m.weight_u.size()
        torch.Size([40])

    r   r   )rV   r.   r@   ConvTranspose1dConvTranspose2dConvTranspose3dr   rf   )r%   r   r   r   r   s        r   r
   r
   	  si    p {((((((
 CCvt%7cBMr   c                 Z   | j                   j                         D ]E  \  }}t        |t              s|j                  |k(  s'|j                  |        | j                   |=  n t        d| d|        | j                  j                         D ]>  \  }}t        |t              s|j                  j                  |k(  s1| j                  |=  n | j                  j                         D ]?  \  }}t        |t              s|j                  j                  |k(  s1| j                  |=  | S  | S )a  Remove the spectral normalization reparameterization from a module.

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

    Example:
        >>> m = spectral_norm(nn.Linear(40, 10))
        >>> remove_spectral_norm(m)
    zspectral_norm of 'z' not found in )rT   itemsrV   r   r   rC   r   _state_dict_hooksr	   rc   _load_state_dict_pre_hooksr   )r%   r   krb   s       r   r   r   Q  s    ,,224 M4dL)dii4.?KK))!,	M -dV?6(KLL++113 4d56477<<4;O((+
 44::< 4d<=$'',,RVBV11!4M
 Mr   )r   r   rg   N)r   )__doc__typingr   r   r   r.   torch.nn.functionalr@   
functionalr0   torch.nn.modulesr   __all__r   r   r	   r   rm   rk   rn   r
   r   rr   r   r   <module>r      s    C ) )    #e eT62 62v@ @ :V,
 EE
E E 
	E
 
#E EP  H r   