
    {-j                        d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dlm	Z	m
Z
mZmZ erd dlmZ d dlmZ d d	lmZ ddZedd            Ze	 ddd            Ze	 ddd            ZdS )    )annotations)reduce)TYPE_CHECKINGN)_C_ops)_create_tensor_dygraph_tracerdygraph_onlyin_dygraph_mode)Iterable)Tensor)	ShapeLikexr   shaper   returnNonec                l   t          d          }t                      rht          j        j                                        5  t          j        | |          }|                    |            d d d            d S # 1 swxY w Y   d S t                      
                    dd| i| |dd|id           d S )	Nint64dtypereshape2X)OutXShaper   Ttypeinputsoutputsattrsstop_gradient)r   r
   paddlebasedygraphno_gradr   reshape_share_underline_tensor_tor   trace_op)r   r   x_shapetmp_outs       d/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/utils/transform_parameters.py_inplace_reshape_dygraphr*   %   s   7+++G 
[ ((** 	2 	2nQ..G..q111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	""811E" 	# 	
 	
 	
 	
 	
s   +A::A>A>paramc                h   t          | j                  dk    sJ | j        d         | j        d         g}t          j        j                                        5  t          j        | |          }t          j        |ddg          }|                    |            ddd           dS # 1 swxY w Y   dS )a  
    A tool function. Permute date of parameter as a 'columns' stride. Now, it only support 2-D parameter.

    Args:
        param(Tensor): The param that will be strided according to 'columns'.

    Examples:
       .. code-block:: python

            >>> import paddle
            >>> paddle.seed(100)

            >>> linear = paddle.nn.Linear(2, 3)
            >>> print(linear.weight)
            Parameter containing:
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=False,
                   [[ 0.11732829, -0.64161885, -1.06996548],
                    [ 0.03456247, -0.29862350, -0.52380574]])

            >>> paddle.nn.utils._stride_column(linear.weight)
            >>> print(linear.weight)

          r   N)	lenr   r    r!   r"   r#   r$   	transposer%   )r+   r   reshape_vartranspose_vars       r)   _stride_columnr3   5   s    2 u{q    [^U[^,E			$	$	&	& 8 8nUE22(q!f==007778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   AB''B+.B+
parametersIterable[Tensor]name
str | Nonec                J   | d         j         }g }| D ]-}|                    |j                   t          |dg           .t	          |          }t                      rft          j        j        	                                5  t          j        | d          }|                    |           ddd           n# 1 swxY w Y   n-t                                          dd| id|giddid	
           t          |           D ]\  }}t          |||                    d|_        |S )a  
    Flatten parameters to a 1-D Tensor.

    Args:
        parameters(Iterable[Tensor]): Iterable Tensors that are trainable parameters of a Layer.
        name(str, 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 1-D Tensor, which represents the parameters of a Layer.


    Examples:
       .. code-block:: pycon

            >>> import paddle
            >>> paddle.seed(2023)
            >>> linear = paddle.nn.Linear(10, 15)

            >>> t = paddle.nn.utils.parameters_to_vector(linear.parameters())
            >>> print(t.shape)
            paddle.Size([165])

    r   r   Nconcatr   r   axisTr   F)r   appendr   r*   r   r
   r    r!   r"   r#   r   r:   r%   r   r&   	enumerater   )r4   r6   r   origin_shapesr+   outtmpis           r)   parameters_to_vectorrB   V   s   8 qMEM . .U[))) ----
u
%
%
%C 
[ ((** 	0 	0-
A..C**3///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	""$SEN1+ 	# 	
 	
 	
 j)) : :5 a(89999CJs   +B88B<?B<vecc                   t          | j                  dk    sJ g }g }d}|D ]J}|j        }|                    |           t          d |d          }||z  }|                    |           Kt          |          dk    r|                    d           t	                      rt
          j        j                                        5  g }	|| j        d         k    rt          j
        | |d          }	nM|| j        d         k     r-d}
|D ]'}|	                    | |
|
|z                       |
|z  }
(nt          d          t          dt          |                    D ]#}|	|                             ||                    $	 ddd           n# 1 swxY w Y   n.t                                          dd| gid|id|d	d
           t!          |          D ]\  }}t#          |||                    dS )a]  
    Transform a 1-D Tensor to the input ``parameters`` .

    Args:
        vec (Tensor): A 1-D Tensor, which will be sliced and copied to the input ``parameters`` .
        parameters (Iterable[Tensor]): Iterable Tensors that are trainable parameters of a Layer.
        name(str, 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`.

    Examples:
       .. code-block:: python

            >>> import paddle
            >>> weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(3.))
            >>> linear1 = paddle.nn.Linear(10, 15, weight_attr)

            >>> vec = paddle.nn.utils.parameters_to_vector(linear1.parameters())

            >>> linear2 = paddle.nn.Linear(10, 15)
            >>> # copy weight of linear1 to linear2
            >>> paddle.nn.utils.vector_to_parameters(vec, linear2.parameters())
            >>> print((linear1.weight == linear2.weight).all())
            Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
            True)
    r.   r   c                    | |z  S N )r   ys     r)   <lambda>z&vector_to_parameters.<locals>.<lambda>   s
    AE     zaThe total_elements of vec should be equal to or larger than the number of elements in parameters.Nsplitr   r   )r;   sectionsTr   )r/   r   r<   r   r
   r    r!   r"   r#   r   rK   
ValueErrorranger%   r   r&   r=   r*   )rC   r4   r6   r>   rL   total_elementsr+   r   numelrespointersectionrA   s                r)   vector_to_parametersrT      s   : sy>>QMHN  U###))5!44%
8}} 
[ ((** 	A 	AC1--l3!44#)A,..' ' 'GJJs7Ww->#>?@@@w&GG' !w   1c*oo.. A AA11*Q-@@@@A	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A  	""#<J'(33 	# 	
 	
 	
 j)) : :5 a(89999: :s   B9FFF)r   r   r   r   r   r   )r+   r   r   r   rF   )r4   r5   r6   r7   r   r   )rC   r   r4   r5   r6   r7   r   r   )
__future__r   	functoolsr   typingr   r    r   paddle.base.frameworkr   r   r	   r
   collections.abcr   r   paddle._typingr   r*   r3   rB   rT   rG   rJ   r)   <module>r[      sk   # " " " " "                                      )((((((((((((
 
 
 
  8 8 8 8@ 591 1 1 1 1h BFD: D: D: D: D: D: D:rJ   