
    ёi!                    ~    S SK Jr  S SKrS SKJrJr  S SKJr  SSKJr  SSK	J
r
  SSKJr  S	S
KJr  / r " S S\5      rg)    )annotationsN)_C_opspir)unique_name   )	framework)check_variable_and_dtype)no_grad   )Initializerc                  B   ^  \ rS rSrSrSSU 4S jjjrSS	S jjrSrU =r$ )

Orthogonal   u5  The orthogonal initializer. The initialized tensor is (semi) orthogonal.

It's only applied to Tensor whose dimension is greater than or equal to 2.

For the Tensor whose dimension is greater than 2, the 0 dimension is seen as ``rows`` ,
and the >=1 dimension are flattened as ``cols`` .

Which can be describe as:

.. code-block:: text

    rows = shape[0]
    cols = shape[1]·shape[2]···shape[N]

    if rows < cols:
        The rows are orthogonal vectors
    elif rows > cols:
        The columns are orthogonal vectors
    else rows = cols:
        Both rows and columns are orthogonal vectors

Args:
    gain(float, optional): The multiplication coefficient for initialized tensor. Default: 1.0.
    name(str|None, optional): The default value is None. Normally there is no need for user to set this
        property. For more information, please refer to :ref:`api_guide_Name`.

Returns:
    A parameter initialized by orthogonal initialized.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.Orthogonal())
        >>> linear = paddle.nn.Linear(10, 15, weight_attr=weight_attr)
        >>> # linear.weight: X * X' = I
        >>> linear = paddle.nn.Linear(15, 10, weight_attr=weight_attr)
        >>> # linear.weight: X' * X = I
c                B   > Uc   S5       e[         TU ]  5         Xl        g )Nzgain should not be None)super__init___gain)selfgainname	__class__s      `/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/nn/initializer/orthogonal.pyr   Orthogonal.__init__F   s%    :!::
    c                   [        U[        R                  5      (       a  UR                  5       (       a   S5       eU R	                  U5      n[        U[        R
                  [        R                  R                  [        R                  R                  45      (       d   e[        U[        R                  [        R                  45      (       d   eUR                  R                  U l        UR                  n[!        U5      S:  d   S5       eUS   nSnUSS  H  nXV-  nM	     [#        XE5      [%        XE5      /n[        R&                  " 5       (       Ga'  [)        5          [        R*                  " 5       n[,        R.                  " USSU R                  UR0                  U5      n	[,        R2                  " U	S	5      u  p[,        R4                  " USS5      n[,        R6                  " U5      n[,        R8                  " X5      n
XE:  a  [,        R:                  " U
SS/5      n
[,        R<                  " XR                  5      n
[,        R>                  " XR@                  SS
5      nURC                  U5         SSS5        g[        RD                  " 5       (       Ga  [        R*                  " 5       n[,        R.                  " USSU R                  UR0                  U5      n	[,        R2                  " U	S	5      u  p[,        R4                  " USS5      n[,        R6                  " U5      n[,        R8                  " X5      n
XE:  a  [,        R:                  " U
SS/5      n
[,        R<                  " XR                  5      n
[,        R>                  " XR@                  SS
5      nU$ [G        USSS/S5        URI                  [J        RL                  " SRO                  SS/5      5      UR0                  SS
S9n	URQ                  S0 SU	0SSUU R                  UR0                  S.S
S9  URI                  [J        RL                  " SRO                  / SQ5      5      U	R0                  SS
S9n
URI                  [J        RL                  " SRO                  / SQ5      5      U	R0                  SS
S9nURQ                  SSU	/0U
US.SS	0S
S9  URI                  [J        RL                  " SRO                  SS/5      5      UR0                  SS
S9nURQ                  SSU0SU0SSS.S
S9  UnURQ                  SSU/0SU0S
S 9  URQ                  S!XS".SU
00 S
S9  URI                  [J        RL                  " SRO                  / S#Q5      5      U
R0                  SS
S9nXE:  a_  URI                  [J        RL                  " SRO                  S$S/5      5      U
R0                  SS
S9nURQ                  S%SU
0UUS&.S'SS/0S
S9  Un
URQ                  S(SU
0XS&.S)UR                  0S
S9  URQ                  S*SU
0SU0U R@                  SS+.S,9nU$ ! , (       d  f       GN= f)-aX  Initialize the input tensor with orthogonal initializer.

Args:
    var(Tensor): Tensor that needs to be initialized.
    block(Block|None, optional): The block in which initialization ops
           should be added. Used in static graph only, default None.

Returns:
    The last initialization op, it contain 8 ops in orthogonal initializer.
zGCurrently, orthogonal initializer not support lazy init for dist param.   zFOnly Tensor with 2 or more dimensions can be initialized by Orthogonalr   r   Ng              ?reducedTOutfloat32float64r   .gaussian_randomtmpF)r   dtypepersistablestop_gradient)meanstdshapeseedr%   )typeinputsoutputsattrsr'   )qrqr$   )r0   rr$   r0   X)QRmodediagdiag_v2)offsetpadding_valuesign)r,   r-   r.   r'   elementwise_mul)r3   Y)	transposer*   r$   r>   
transpose2)r   XShapeaxisreshape2r*   scale)rC   bias)r,   r-   r.   r/   ))
isinstancer   EagerParamBaseis_dist_check_blockVariablepaddler   ValuecoreParameterMetaBlockprogramrandom_seed_seedr*   lenmaxminin_dygraph_moder
   _current_expected_placer   gaussianr%   r0   r7   r;   multiplyr>   reshaperC   r   _share_underline_tensor_toin_pir_moder	   
create_varr   generatejoin	append_op)r   varblockr*   rowcoliflatten_shapeplace
normal_varr1   r2   r_diagr_signr$   x_shapeq_transposeops                     r   __call__Orthogonal.__call__K   s    sI4455#++--	
 V	
 

 !!%()$$fjj&6&68N8NO
 
 	
 
 %)//399!=>>>>]]..
		5zQ 	
T	
 AhqrAHC  SC6$$&&!99;#__!3TZZE
 yyY7Q1-V,OOA.9((QF3ANN1ii0ll1jj#t<..s3- . ""$$557EsCSYYJ 99Z3DA[[Aq)F[[(F*Ay$$QA/q)),A,,q**c48CJ 	!I.	
 %%%%chh0A5/I&JK))	 & 

 	"J'&

  	 	
 %%chh/A&BC""	  
 %%chh/A&BC""	  
 	*& 9% 	 		
 !!%%chh&?@''	 " 
 	8FO3 	 	
 &?FO	 	 	
 	"(AJ 	 	
 ""%%chh/L&MN''	 # 
 9** ))#((K3G*HIgg!"	 + K OO!Qx +w?1v&"   A81CII& 	 	
 __8CL JJ4	  
 	A s   !DX11
Y )r   rQ   )r   N)r   floatr   z
str | NonereturnNone)N)r`   zpaddle.Tensorra   zpir.Block | None)	__name__
__module____qualname____firstlineno____doc__r   rm   __static_attributes____classcell__)r   s   @r   r   r      s    'R 
D Dr   r   )
__future__r   rJ   r   r   paddle.utilsr   baser   base.data_feederr	   base.dygraphr
   initializerr   __all__r    r   r   <module>r      s2    #   $  8 # $
s sr   