
    {-j!                        d dl mZ d dlZd dlmZm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 g Z G d de          ZdS )    )annotationsN)_C_opspir)unique_name   )	framework)check_variable_and_dtype)no_grad   )Initializerc                  0     e Zd ZdZdd fd
ZdddZ xZS )
Orthogonalu  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
          ?Ngainfloatname
str | NonereturnNonec                n    |
J d            t                                                       || _        d S )Nzgain should not be None)super__init___gain)selfr   r   	__class__s      `/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/initializer/orthogonal.pyr   zOrthogonal.__init__F   s9    !:


    varpaddle.Tensorblockpir.Block | Nonec                F   t          |t          j                  r|                                r
J d            |                     |          }t          |t          j        t          j        j        t          j	        j
        f          sJ t          |t          j        t          j        f          sJ |j        j        | _        |j        }t!          |          dk    s
J d            |d         }d}|dd         D ]}||z  }t#          ||          t%          ||          g}t          j                    rt)                      5  t          j                    }t-          j        |dd| j        |j        |          }	t-          j        |	d	          \  }
}t-          j        |dd          }t-          j        |          }t-          j        |
|          }
||k     rt-          j        |
ddg          }
t-          j        |
|j                  }
t-          j        |
| j         dd
          }|!                    |           	 ddd           dS # 1 swxY w Y   nt          j"                    rt          j                    }t-          j        |dd| j        |j        |          }	t-          j        |	d	          \  }
}t-          j        |dd          }t-          j        |          }t-          j        |
|          }
||k     rt-          j        |
ddg          }
t-          j        |
|j                  }
t-          j        |
| j         dd
          }|S tG          |dddgd           |$                    tK          j&        d'                    ddg                    |j        dd
          }	|(                    di d|	idd|| j        |j        dd
           |$                    tK          j&        d'                    g d                    |	j        dd
          }
|$                    tK          j&        d'                    g d                    |	j        dd
          }|(                    dd|	gi|
|ddd	id
           |$                    tK          j&        d'                    ddg                    |j        dd
          }|(                    dd|id|idddd
           |}|(                    dd|gid|id
            |(                    d!|
|d"d|
ii d
           |$                    tK          j&        d'                    g d#                    |
j        dd
          }||k     rj|$                    tK          j&        d'                    d$dg                    |
j        dd
          }|(                    d%d|
i||d&d'ddgid
           |}
|(                    d(d|
i|
|d&d)|j        id
           |(                    d*d|
id|i| j         dd+,          }|S )-a  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        r   reducedTOutfloat32float64r   .gaussian_randomtmpF)r   dtypepersistablestop_gradient)meanstdshapeseedr+   )typeinputsoutputsattrsr-   )qrqr*   )r6   rr*   r6   X)QRmodediagdiag_v2)offsetpadding_valuesign)r2   r3   r4   r-   elementwise_mul)r9   Y)	transposer0   r*   rD   
transpose2)r%   XShapeaxisreshape2r0   scale)rI   bias)r2   r3   r4   r5   ))
isinstancer   EagerParamBaseis_dist_check_blockVariablepaddler   ValuecoreParameterMetaBlockprogramrandom_seed_seedr0   lenmaxminin_dygraph_moder
   _current_expected_placer   gaussianr+   r6   r=   rA   multiplyrD   reshaperI   r   _share_underline_tensor_toin_pir_moder	   
create_varr   generatejoin	append_op)r   r   r    r0   rowcoliflatten_shapeplace
normal_varr7   r8   r_diagr_signr*   x_shapeq_transposeops                     r   __call__zOrthogonal.__call__K   s4    sI455	
:=++--	
 	
 V	
 	
 

 !!%(()$fj&68NO
 
 	
 	
 
 %)/39!=>>>>>].
	5zzQT  Ahqrr 	 	A1HCCS#C6$&& ,	  !9;;#_!3TZE 
 yY771Q1--V,,OAv..99(QF33AN1ci00l1dj#t<<..s333-                . "$$ 	577EsCSY J 9Z33DAq[Aq))F[((F6**ASyy$QA//q#),,A,q$*c488CJ 	!I.	
 	
 	
 %%%chh0A5/I&J&JKK)	 & 
 

 	"J'&
   	 	
 	
 	
 %chh/A/A/A&B&BCC"	  
 
 %chh/A/A/A&B&BCC"	  
 
 	*&  9% 	 		
 		
 		
 !!%chh&?&?@@'	 " 
 
 	8FO33 	 	
 	
 	
 &?FO	 	 	
 	
 	
 	"((AJ 	 	
 	
 	
 ""%chh/L/L/L&M&MNN'	 # 
 
 99** )#((K3G*H*HIIg!"	 +  K OO!Qx +w??1v&"     A811CI& 	 	
 	
 	
 __8CL J44	  
 
 	s   C7I

II)r   N)r   r   r   r   r   r   )N)r   r   r    r!   )__name__
__module____qualname____doc__r   rq   __classcell__)r   s   @r   r   r      so        ' 'R      
D D D D D D D D Dr   r   )
__future__r   rP   r   r   paddle.utilsr   baser   base.data_feederr	   base.dygraphr
   initializerr   __all__r    r   r   <module>r      s    # " " " " "          $ $ $ $ $ $       8 8 8 8 8 8 # # # # # # $ $ $ $ $ $
s s s s s s s s s sr   