o
    #jv                     @   sl   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 dd Z	edd Z
edd	d
ZedddZdS )    )reduceN)_C_ops)_create_tensor_dygraph_tracerdygraph_onlyin_dygraph_modec                 C   s   t dd}t r,tjj  t| |}||  W d    d S 1 s%w   Y  d S t	 j
dd| i| |dd|idd d S )	NZint64dtypeZreshape2X)OutZXShapeshapeTtypeZinputsZoutputsattrsstop_gradient)r   r   paddlebasedygraphno_gradr   reshape_share_underline_tensor_tor   trace_op)xr   Zx_shapeZtmp_out r   e/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/nn/utils/transform_parameters.py_inplace_reshape_dygraph   s   
"
r   c                 C   s~   t | jdks	J | jd | jd g}tjj  t| |}t|ddg}||  W d   dS 1 s8w   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   Z	transposer   )paramr   Zreshape_varZtranspose_varr   r   r   _stride_column,   s   "r    c                 C   s   | d j }g }| D ]}||j t|dg q	t|d}t rBtjj	  t
| d}|| W d   n1 s<w   Y  nt jdd| id|giddid	d
 t| D ]\}}t|||  qXd|_|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:: python

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

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

    r   r   Nconcatr
   r   axisTr   F)r	   appendr   r   r   r   r   r   r   r   r   r"   r   r   r   	enumerater   )
parametersnamer	   origin_shapesr   outtmpir   r   r   parameters_to_vectorM   s.   

r,   c           
      C   s  g }g }|D ]}|j }|| tdd |d}|| qt|dkr)|d t r]tjj " t	
| |d}tdt|D ]}	||	 ||	  qAW d   n1 sWw   Y  nt jdd| gid|id|d	d
d t|D ]\}	}t|||	  qtd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)
    c                 S   s   | | S Nr   )r   yr   r   r   <lambda>   s    z&vector_to_parameters.<locals>.<lambda>r   r   Nsplitr
   r   )r#   sectionsTr   )r   r$   r   r   r   r   r   r   r   r   r0   ranger   r   r   r%   r   )
Zvecr&   r'   r(   r1   r   r   Znumelresr+   r   r   r   vector_to_parameters   s6   

r4   r-   )	functoolsr   r   r   Zpaddle.base.frameworkr   r   r   r   r   r    r,   r4   r   r   r   r   <module>   s   	
 2