o
    pi!                     @  sx   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 deZdS )    )annotationsN)_C_opspir)unique_name   )	framework)check_variable_and_dtype)no_grad   )Initializerc                      s0   e Zd ZdZdd fd
dZddddZ  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                   s$   |d usJ dt    || _d S )Nzgain should not be None)super__init___gain)selfr   r   	__class__ g/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/nn/initializer/orthogonal.pyr   F   s   

zOrthogonal.__init__varpaddle.Tensorblockpir.Block | Nonec              
   C  s0  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 ]}||9 }qKt||t||g}t rt U t }t|dd| j|j|}	t|	d	\}
}t|dd}t|}t|
|}
||k rt|
ddg}
t|
|j}
t|
| j dd
}|!| 	 W d   dS 1 sw   Y  nMt" rt }t|dd| j|j|}	t|	d	\}
}t|dd}t|}t|
|}
||k rt|
ddg}
t|
|j}
t|
| j dd
}|S t#|dddgd |j$t%&d'ddg|jdd
d}	|j(di d|	idd|| j|jdd
d |j$t%&d'g d|	jdd
d}
|j$t%&d'g d|	jdd
d}|j(dd|	gi|
|ddd	id
d |j$t%&d'ddg|jdd
d}|j(dd|id|idddd
d |}|j(dd|gid|id
d  |j(d!|
|d"d|
ii d
d |j$t%&d'g d#|
jdd
d}||k r|j$t%&d'd$dg|
jdd
d}|j(d%d|
i||d&d'ddgid
d |}
|j(d(d|
i|
|d&d)|jid
d |j(d*d|
id|i| j d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   ZreducedTOutZfloat32Zfloat64r   .Zgaussian_randomtmpF)r   dtypeZpersistablestop_gradient)meanZstdshapeseedr$   )typeinputsoutputsattrsr%   )qrqr#   )r-   rr#   r-   X)QRmodediagZdiag_v2)offsetZpadding_valuesign)r)   r*   r+   r%   Zelementwise_mul)r0   Y)	transposer'   r#   r8   Z
transpose2)r!   ZXShapeZaxisZreshape2r'   scale)r9   Zbias)r)   r*   r+   r,   ))
isinstancer   ZEagerParamBaseZis_distZ_check_blockVariablepaddler   ValuecoreZParameterMetaZBlockprogramZrandom_seedZ_seedr'   lenmaxminZin_dygraph_moder	   Z_current_expected_placer   Zgaussianr$   r-   r4   r6   multiplyr8   Zreshaper9   r   Z_share_underline_tensor_toZin_pir_moder   Z
create_varr   generatejoinZ	append_op)r   r   r   r'   rowcoliZflatten_shapeZplaceZ
normal_varr.   r/   Zr_diagZr_signr#   Zx_shapeZq_transposeopr   r   r   __call__K   s8  





"




zOrthogonal.__call__)r   N)r   r   r   r   r   r   )N)r   r   r   r   )__name__
__module____qualname____doc__r   rJ   __classcell__r   r   r   r   r      s    )r   )
__future__r   r<   r   r   Zpaddle.utilsr   baser   Zbase.data_feederr   Zbase.dygraphr	   Zinitializerr   __all__r   r   r   r   r   <module>   s   