
    {-jXl                    6   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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 d	dlmZ ddlmZ g Zd dlmZ  G d de          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )    )annotationsN)OrderedDict)IterableIteratorMappingSequence)chain)Any)Self)Tensor   )param_guard)	Parameter   )Layer)param_one_aliasc                       e Zd ZdZ eddg          	 d d! fd            Zd"dZd#dZd$dZd%dZ	d&dZ
d'dZd(dZd"dZd)dZd*dZd+dZ eddg          d,d            Z xZS )-	LayerDicta(  
    LayerDict holds sublayers in the ordered dictionary, and sublayers it contains are properly registered.
    Held sublayers can be accessed like a regular ordered python dictionary.

    Parameters:
        sublayers (LayerDict|OrderedDict|list[(key,Layer)...], optional): iterable of key/value pairs, the type of value is 'paddle.nn.Layer' .

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> import numpy as np
            >>> from collections import OrderedDict

            >>> sublayers = OrderedDict([
            ...     ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
            ...     ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
            ...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
            >>> ])

            >>> layers_dict = paddle.nn.LayerDict(sublayers=sublayers)

            >>> l = layers_dict['conv1d']

            >>> for k in layers_dict:
            ...     l = layers_dict[k]
            ...
            >>> print(len(layers_dict))
            3

            >>> del layers_dict['conv2d']
            >>> print(len(layers_dict))
            2

            >>> conv1d = layers_dict.pop('conv1d')
            >>> print(len(layers_dict))
            1

            >>> layers_dict.clear()
            >>> print(len(layers_dict))
            0

    	sublayersmodulesNKLayerDict | typing.Mapping[str, Layer] | Sequence[tuple[str, Layer]] | NonereturnNonec                z    t                                                       ||                     |           d S d S Nsuper__init__update)selfr   	__class__s     Y/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/layer/container.pyr   zLayerDict.__init__T   sA     	 KK	""""" !     keystrr   c                    | j         |         S r   _sub_layersr    r$   s     r"   __getitem__zLayerDict.__getitem__b   s    $$r#   sublayerc                .    |                      ||          S r   )add_sublayer)r    r$   r+   s      r"   __setitem__zLayerDict.__setitem__e   s      h///r#   c                    | j         |= d S r   r'   r)   s     r"   __delitem__zLayerDict.__delitem__h   s    S!!!r#   intc                *    t          | j                  S r   lenr(   r    s    r"   __len__zLayerDict.__len__k       4#$$$r#   Iterator[str]c                *    t          | j                  S r   )iterr(   r5   s    r"   __iter__zLayerDict.__iter__n       D$%%%r#   boolc                    || j         v S r   r'   r)   s     r"   __contains__zLayerDict.__contains__q   s    d&&&r#   c                8    | j                                          dS )a  
        Clear all the sublayers in the LayerDict.

        Parameters:
            None.

        Examples:
            .. code-block:: python

                >>> import paddle
                >>> from collections import OrderedDict

                >>> sublayers = OrderedDict([
                ...     ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
                ...     ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
                ...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
                >>> ])

                >>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
                >>> len(layer_dict)
                3

                >>> layer_dict.clear()
                >>> len(layer_dict)
                0

        N)r(   clearr5   s    r"   rA   zLayerDict.cleart   s    8 	     r#   c                    | |         }| |= |S )a  
        Remove the key from the LayerDict and return the layer of the key.

        Parameters:
            key (str): the key to be removed.

        Examples:
            .. code-block:: python

                >>> import paddle
                >>> from collections import OrderedDict

                >>> sublayers = OrderedDict([
                ...     ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
                ...     ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
                ...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
                >>> ])

                >>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
                >>> len(layer_dict)
                3

                >>> layer_dict.pop('conv2d')
                >>> len(layer_dict)
                2

         r    r$   vs      r"   popzLayerDict.pop   s    8 IIr#   Iterable[str]c                4    | j                                         S )a  
        Return the iterable of the keys in LayerDict.

        Parameters:
            None.

        Examples:
            .. code-block:: python

                >>> import paddle
                >>> from collections import OrderedDict

                >>> sublayers = OrderedDict([
                ...     ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
                ...     ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
                ...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
                >>> ])

                >>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
                >>> for k in layer_dict.keys():
                ...     print(k)
                conv1d
                conv2d
                conv3d

        )r(   keysr5   s    r"   rI   zLayerDict.keys   s    6 $$&&&r#   Iterable[tuple[str, Layer]]c                4    | j                                         S )a  
        Return the iterable of the key/value pairs in LayerDict.

        Parameters:
            None.

        Examples:
            .. code-block:: python

                >>> import paddle
                >>> from collections import OrderedDict

                >>> sublayers = OrderedDict([
                ...     ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
                ...     ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
                ...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
                >>> ])

                >>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
                >>> for k, v in layer_dict.items():
                ...     print(f"{k}:", v)
                conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
                conv2d : Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
                conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)

        )r(   itemsr5   s    r"   rL   zLayerDict.items   s    6 %%'''r#   Iterable[Layer]c                4    | j                                         S )al  
        Return the iterable of the values in LayerDict.

        Parameters:
            None.

        Examples:
            .. code-block:: python

                >>> import paddle
                >>> from collections import OrderedDict

                >>> sublayers = OrderedDict([
                ...     ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
                ...     ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
                ...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
                >>> ])

                >>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
                >>> for v in layer_dict.values():
                ...     print(v)
                Conv1D(3, 2, kernel_size=[3], data_format=NCL)
                Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
                Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)

        r(   valuesr5   s    r"   rP   zLayerDict.values   s    6 &&(((r#   DLayerDict | typing.Mapping[str, Layer] | Sequence[tuple[str, Layer]]c           	     .   t          |t                    sJ dt          |          j        z               t          |t          t
          t          f          r2|                                D ]\  }}|                     ||           dS t          |          D ]|\  }}t          |          dk    rBt          dt          |          z   dz   t          t          |                    z   dz             |                     |d         |d                    }dS )	aE  
        Update the key/values pairs in sublayers to the LayerDict, overwriting the existing keys.

        Parameters:
            sublayers (LayerDict|OrderedDict|list[(key,Layer)...]): iterable of key/value pairs, the type of value is 'paddle.nn.Layer' .

        Examples:
            .. code-block:: python

                >>> import paddle
                >>> from collections import OrderedDict

                >>> sublayers = OrderedDict([
                ...     ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
                ...     ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
                ...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
                >>> ])

                >>> new_sublayers = OrderedDict([
                ...     ('relu', paddle.nn.ReLU()),
                ...     ('conv2d', paddle.nn.Conv2D(4, 2, 4)),
                >>> ])
                >>> layer_dict = paddle.nn.LayerDict(sublayers=sublayers)

                >>> layer_dict.update(new_sublayers)

                >>> for k, v in layer_dict.items():
                ...     print(f"{k}:", v)
                conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
                conv2d : Conv2D(4, 2, kernel_size=[4, 4], data_format=NCHW)
                conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
                relu : ReLU()

        zSThe type of sublayers is not iterable of key/value pairs, the type of sublayers is    The length of the z's element in sublayers is , which must be 2.r   r   N)
isinstancer   type__name__r   r   r   rL   r-   	enumerater4   
ValueErrorr%   )r    r   r$   layerikvs         r"   r   zLayerDict.update	  s<   T )X.. 	
 	
a9oo&'	
 	
.
 i+y'!BCC 	0'oo// . .
U!!#u----. . #9-- 	0 	02r77a<<$,a&&!78 c"gg,,' /	/   !!"Q%A////	0 	0r#   r   )r   r   r   r   )r$   r%   r   r   )r$   r%   r+   r   r   r   )r$   r%   r   r   r   r1   r   r8   )r$   r%   r   r=   )r   r   )r   rG   )r   rJ   )r   rM   )r   rQ   r   r   )rX   
__module____qualname____doc__r   r   r*   r.   r0   r6   r;   r?   rA   rF   rI   rL   rP   r   __classcell__r!   s   @r"   r   r   '   s{       * *X _k9-.. # # # # # # /.#% % % %0 0 0 0" " " "% % % %& & & &' ' ' '! ! ! !<   @' ' ' ':( ( ( (:) ) ) ): _k9-..<0 <0 <0 /.<0 <0 <0 <0 <0r#   r   c                  P     e Zd ZdZ	 dd fdZddZddZddZddZddZ	 xZ
S )ParameterDictaT  
    Holds parameters in a dictionary.

    ParameterDict can be indexed like a regular Python dictionary, but Parameters it contains are properly registered.

    Parameters:
        parameters (iterable, optional): a mapping (dictionary) of (string : Any) or an iterable of key-value pairs of type (string, Any)

    Examples:
        .. code-block:: pycon

            >>> import paddle

            >>> class MyLayer(paddle.nn.Layer):
            ...     def __init__(self, num_stacked_param):
            ...         super().__init__()
            ...         # create ParameterDict with iterable Parameters
            ...         self.params = paddle.nn.ParameterDict(
            ...             {f"t{i}": paddle.create_parameter(shape=[2, 2], dtype='float32') for i in range(num_stacked_param)}
            ...         )
            ...
            ...     def forward(self, x):
            ...         for i, key in enumerate(self.params):
            ...             x = paddle.matmul(x, self.params[key])
            ...         return x
            >>> x = paddle.uniform(shape=[5, 2], dtype='float32')
            >>> num_stacked_param = 4
            >>> model = MyLayer(num_stacked_param)
            >>> print(len(model.params))
            4
            >>> res = model(x)
            >>> print(res.shape)
            paddle.Size([5, 2])

            >>> replaced_param = paddle.create_parameter(shape=[2, 3], dtype='float32')
            >>> model.params['t3'] = replaced_param  # replace t3 param
            >>> res = model(x)
            >>> print(res.shape)
            paddle.Size([5, 3])
            >>> model.params['t4'] = paddle.create_parameter(shape=[3, 4], dtype='float32')  # append param
            >>> print(len(model.params))
            5
            >>> res = model(x)
            >>> print(res.shape)
            paddle.Size([5, 4])
    N
parametersJParameterDict | Mapping[str, Tensor] | Sequence[tuple[str, Tensor]] | Noner   r   c                z    t                                                       ||                     |           d S d S r   r   )r    rg   r!   s     r"   r   zParameterDict.__init__y  sA     	!KK
##### "!r#   r$   r%   r   c                x    t          | j                  5  | j        |         cd d d            S # 1 swxY w Y   d S r   )r   _parametersr)   s     r"   r*   zParameterDict.__getitem__  s    )** 	) 	)#C(	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   /33paramc                V    t          |t                    sJ t          | ||           d S r   )rV   r   setattr)r    r$   rl   s      r"   r.   zParameterDict.__setitem__  s0    %+++++c5!!!!!r#   r1   c                *    t          | j                  S r   r4   rk   r5   s    r"   r6   zParameterDict.__len__  r7   r#   r8   c                *    t          | j                  S r   )r:   rk   r5   s    r"   r;   zParameterDict.__iter__  r<   r#   CParameterDict | Mapping[str, Tensor] | Sequence[tuple[str, Tensor]]c           	        t          |t                    sJ dt          |          j        z               t          |t          t
          t          f          r2|                                D ]\  }}|                     ||           dS t          |          D ]]\  }}t          |          dk    r#t          d| dt          |           d          |                     |d         |d                    ^dS )	zUpdate a given parameter at the end of the dict.

        Parameters:
            parameters (Parameter): parameter to update
        zTThe type of parameters is not iterable of key/value pairs, the type of sublayers is rS   rT   z's element in parameters is rU   r   r   N)rV   r   rW   rX   r   rf   r   rL   add_parameterrY   r4   rZ   )r    rg   r$   	parameterr\   r]   s         r"   r   zParameterDict.update  s#    *h// 	
 	
b:'(	
 	
/
 j;w"GHH 		1","2"2"4"4 3 3Y""3	22223 3 #:.. 1 12r77a<<$gQggCPRGGggg   ""2a5"Q%00001 1r#   r   )rg   rh   r   r   )r$   r%   r   r   )r$   r%   rl   r   r   r   r^   r_   )rg   rr   r   r   )rX   r`   ra   rb   r   r*   r.   r6   r;   r   rc   rd   s   @r"   rf   rf   I  s        - -l $ $ $ $ $ $ $) ) ) )" " " "% % % %& & & &1 1 1 1 1 1 1 1r#   rf   c                  N     e Zd ZdZdd fdZddZddZddZddZddZ	 xZ
S )ParameterLista  ParameterList Container.

    This container acts like a Python list, but parameters it contains will be properly added.

    Parameters:
        parameters (iterable, optional): Iterable Parameters to be added.

    Examples:
        .. code-block:: pycon

            >>> import paddle

            >>> class MyLayer(paddle.nn.Layer):
            ...     def __init__(self, num_stacked_param):
            ...         super().__init__()
            ...         # create ParameterList with iterable Parameters
            ...         self.params = paddle.nn.ParameterList(
            ...             [paddle.create_parameter(shape=[2, 2], dtype='float32') for _ in range(num_stacked_param)]
            ...         )
            ...
            ...     def forward(self, x):
            ...         for i, p in enumerate(self.params):
            ...             x = paddle.matmul(x, p)
            ...         return x
            >>> x = paddle.uniform(shape=[5, 2], dtype='float32')
            >>> num_stacked_param = 4
            >>> model = MyLayer(num_stacked_param)
            >>> print(len(model.params))
            4
            >>> res = model(x)
            >>> print(res.shape)
            paddle.Size([5, 2])
            >>> replaced_param = paddle.create_parameter(shape=[2, 3], dtype='float32')
            >>> model.params[num_stacked_param - 1] = replaced_param
            >>> res = model(x)
            >>> print(res.shape)
            paddle.Size([5, 3])
            >>> model.params.append(paddle.create_parameter(shape=[3, 4], dtype='float32'))  # append param
            >>> print(len(model.params))
            5
            >>> res = model(x)
            >>> print(res.shape)
            paddle.Size([5, 4])
    Nrg   Iterable[Tensor] | Noner   r   c                    t                                                       |Ot          |          D ]A\  }}t          |t                    sJ |                     t          |          |           @d S d S r   )r   r   rY   rV   r   rt   r%   )r    rg   idxrl   r!   s       r"   r   zParameterList.__init__  s    !'
33 4 4
U!%33333""3s88U3333 "!4 4r#   rz   r1   r   c                    t          | j                  5  | j        t          |                   cd d d            S # 1 swxY w Y   d S r   )r   rk   r%   r    rz   s     r"   r*   zParameterList.__getitem__  s    )** 	. 	.#CHH-	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   <A A rl   c           	         t          |t          t          f          st          dt	          |                     t          j        |t          | t          |                               d S )Nz6param should be 'Parameter' or 'Tensor', but received )	rV   r   r   	TypeErrorrW   paddleassigngetattrr%   )r    rz   rl   s      r"   r.   zParameterList.__setitem__  sf    %)V!455 	VeVV   	eWT3s884455555r#   c                *    t          | j                  S r   rp   r5   s    r"   r6   zParameterList.__len__  r7   r#   Iterator[Tensor]c                    t          | j                  5  t          | j                                                  cd d d            S # 1 swxY w Y   d S r   )r   rk   r:   rP   r5   s    r"   r;   zParameterList.__iter__  s    )** 	3 	3(//1122	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s   &AAAru   r   c                t    t          | j                  }|                     t          |          |           | S )zAppends a given parameter at the end of the list.

        Parameters:
            parameter (Parameter): parameter to append
        )r4   rk   rt   r%   )r    ru   rz   s      r"   appendzParameterList.append  s5     $"##3s88Y///r#   r   )rg   rx   r   r   )rz   r1   r   r   )rz   r1   rl   r   r   r   r^   )r   r   )ru   r   r   r   )rX   r`   ra   rb   r   r*   r.   r6   r;   r   rc   rd   s   @r"   rw   rw     s        + +Z4 4 4 4 4 4 4. . . .6 6 6 6% % % %3 3 3 3       r#   rw   c                  0    e Zd ZdZ eddg          d$d% fd            Zd&dZd Zd'dZd(dZ	d)dZ
d*dZd+dZd,dZd-dZd. fdZ eddg          d/d            Z eddg          d0d            Z eddg          d1d             Zd2d#Z xZS )3	LayerListam  
    LayerList holds sublayers, and sublayers it contains are properly registered.
    held sublayers can be indexed like a regular python list.

    Parameters:
        sublayers (iterable of Layer, optional): sublayers to hold

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> class MyLayer(paddle.nn.Layer):
            ...     def __init__(self):
            ...         super().__init__()
            ...         self.linears = paddle.nn.LayerList(
            ...             [paddle.nn.Linear(10, 10) for i in range(10)])
            ...
            ...     def forward(self, x):
            ...         # LayerList can act as an iterable, or be indexed using ints
            ...         for i, l in enumerate(self.linears):
            ...             x = self.linears[i // 2](x) + l(x)
            ...         return x
    r   r   NIterable[Layer] | Noner   r   c                    t                                                       |8t          |          D ]*\  }}|                     t	          |          |           )d S d S r   )r   r   rY   r-   r%   )r    r   rz   r[   r!   s       r"   r   zLayerList.__init__  sk     '	22 3 3
U!!#c((E2222 ! 3 3r#   rz   r1   c           
        t          |t                    rvt          |            |cxk    rt          |           k     s6n t          d| dt          |             dt          |            d          |dk     r|t          |           z  }|S )Nindex z1 is out of range, should be an integer in range [, )r   )rV   r1   r4   
IndexErrorr|   s     r"   _get_abs_idxzLayerList._get_abs_idx#  s    c3 	!YYJ#1111D		1111 mSmmTWX\T]T]S]mmadeiajajmmm   Qwws4yy 
r#   c                F    t          |                     |                    S r   )r%   r   r|   s     r"   _get_abs_string_indexzLayerList._get_abs_string_index-  s    4$$S))***r#   r   c                   t          |t                    r?|                     t          | j                                                  |                   S |                     |          }| j        t          |                   S r   )rV   slicer!   listr(   rP   r   r%   r|   s     r"   r*   zLayerList.__getitem__0  si    c5!! 	.>>$t'7'>'>'@'@"A"A#"FGGG##C((C#CHH--r#   r+   c                h    |                      |          }t          | t          |          |          S r   )r   rn   r%   )r    rz   r+   s      r"   r.   zLayerList.__setitem__7  s-    $$tSXXx000r#   c           	        t          |t                    rHt          t          | j                            |         D ]}t          | t          |                      n2|                     |          }t          | t          |                     d t          t          | j                            D             }t          t          t          || j                                                                      | _        d S )Nc                ,    g | ]}t          |          S rC   )r%   ).0r\   s     r"   
<listcomp>z)LayerList.__delitem__.<locals>.<listcomp>B  s    DDD!s1vvDDDr#   )rV   r   ranger4   r(   delattrr%   r   r   r   ziprP   )r    rz   kstr_indicess       r"   r0   zLayerList.__delitem__;  s    c5!! 	$3t/0011#6 & &c!ff%%%%& ##C((CD#c((###DDuS1A-B-B'C'CDDD&[$"2"9"9";";<<==
 
r#   c                *    t          | j                  S r   r3   r5   s    r"   r6   zLayerList.__len__G  r7   r#   Iterator[Layer]c                N    t          | j                                                  S r   r:   r(   rP   r5   s    r"   r;   zLayerList.__iter__J      D$++--...r#   rM   r   c                ,    |                      |          S r   )extend)r    r   s     r"   __iadd__zLayerList.__iadd__M  s    {{7###r#   otherc                    t                      }t          t          | |                    D ](\  }}|                    t	          |          |           )|S r   )r   rY   r	   
add_moduler%   )r    r   combinedr\   modules        r"   __add__zLayerList.__add__P  sT    ;;"5u#5#566 	0 	0IAvA////r#   	list[str]c                `    t                                                      }d |D             }|S )Nc                :    g | ]}|                                 |S rC   )isdigit)r   r$   s     r"   r   z%LayerList.__dir__.<locals>.<listcomp>X  s%    9993;;==9999r#   )r   __dir__)r    rI   r!   s     r"   r   zLayerList.__dir__V  s.    ww  99t999r#   r   c                f    |                      t          t          |                     |           | S )a  
        Appends a sublayer to the end of the list.

        Parameters:
            sublayer (Layer): sublayer to append

        Examples:
            .. code-block:: python

                >>> import paddle

                >>> linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
                >>> another = paddle.nn.Linear(10, 10)
                >>> linears.append(another)
                >>> print(len(linears))
                11
        r-   r%   r4   )r    r+   s     r"   r   zLayerList.append[  s+    & 	#c$ii..(333r#   indexc                   t          |t                    r4t          | j                   |cxk    rt          | j                  k    s.n J dt          |             dt          |            d            |dk     r|t          |           z  }t	          t          | j                  |d          D ]4}| j        t          |dz
                     | j        t          |          <   5|| j        t          |          <   dS )a  
        Insert a sublayer before a given index in the list.

        Parameters:
            index (int): index to insert.
            sublayer (Layer): sublayer to insert

        Examples:
            .. code-block:: python

                >>> import paddle

                >>> linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
                >>> another = paddle.nn.Linear(10, 10)
                >>> linears.insert(3, another)
                >>> print(linears[3] is another)
                True
                >>> another = paddle.nn.Linear(10, 10)
                >>> linears.insert(-1, another)
                >>> print(linears[-2] is another)
                True
        z%index should be an integer in range [r   ]r   r   N)rV   r1   r4   r(   r   r%   )r    r   r+   r\   s       r"   insertzLayerList.insertq  s%   0 %%% 	
3,
 ,
 +
+, +, +, +,$*+++, +, +, +, +, OSYYJNN#d))NNN+, +, , 199SYYEs4+,,eR88 	D 	DA'+'7AE

'CDSVV$$'/U$$$r#   c                    t          |           }t          |          D ]-\  }}t          ||z             }|                     ||           .| S )a  
        Appends sublayers to the end of the list.

        Parameters:
            sublayers (iterable of Layer): iterable of sublayers to append

        Returns:
            None

        Examples:
            .. code-block:: python

                >>> import paddle

                >>> linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
                >>> another_list = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(5)])
                >>> linears.extend(another_list)
                >>> print(len(linears))
                15
                >>> print(another_list[0] is linears[10])
                True
        )r4   rY   r%   r-   )r    r   offsetr\   r+   rz   s         r"   r   zLayerList.extend  sW    0 T$Y// 	- 	-KAxfqj//Cc8,,,,r#   r$   int | slicec                    | |         }| |= |S r   rC   rD   s      r"   rF   zLayerList.pop  s    IIr#   r   )r   r   r   r   )rz   r1   r   r1   )rz   r1   r   r   )rz   r1   r+   r   r   r   )rz   r1   r   r   r^   r   r   )r   rM   r   r   )r   rM   r   r   )r   r   )r+   r   r   r   )r   r1   r+   r   r   r   )r   rM   r   r   )r$   r   r   r   )rX   r`   ra   rb   r   r   r   r   r*   r.   r0   r6   r;   r   r   r   r   r   r   rF   rc   rd   s   @r"   r   r     s        2 _k9-..3 3 3 3 3 3 /.3   + + +. . . .1 1 1 1

 

 

 

% % % %/ / / /$ $ $ $        
 _j(+,,   -,* _j(+,,!0 !0 !0 -,!0F _k9-..   /.:       r#   r   c                  l     e Zd ZdZd fdZdd
ZddZd dZd!dZd"dZ	d#dZ
d$dZd%dZd&dZ xZS )'
Sequentiala  Sequential container.
    Sub layers will be added to this container in the order of argument in the constructor.
    The argument passed to the constructor can be iterable Layers or iterable name Layer pairs.

    Parameters:
        layers(Layer|list|tuple): Layer or list/tuple of iterable name Layer pair.

    Returns:
        None.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> data = paddle.uniform(shape=[30, 10], dtype='float32')
            >>> # create Sequential with iterable Layers
            >>> model1 = paddle.nn.Sequential(
            ...     paddle.nn.Linear(10, 1), paddle.nn.Linear(1, 2)
            >>> )
            >>> model1[0]  # access the first layer
            >>> res1 = model1(data)  # sequential execution

            >>> # create Sequential with name Layer pairs
            >>> model2 = paddle.nn.Sequential(
            ...     ('l1', paddle.nn.Linear(10, 2)),
            ...     ('l2', paddle.nn.Linear(2, 3))
            >>> )
            >>> model2['l1']  # access l1 layer
            >>> model2.add_sublayer('l3', paddle.nn.Linear(3, 3))  # add sublayer
            >>> res2 = model2(data)  # sequential execution

            >>> # append single layer at the end of sequential
            >>> model2 = paddle.nn.Sequential(paddle.nn.Linear(10, 20))
            >>> model2.append(paddle.nn.Linear(20, 30))
            >>> res2 = model2(data)  # [30, 30]

            >>> # insert single layer at the given position
            >>> model2 = paddle.nn.Sequential(paddle.nn.Linear(20, 30))
            >>> model2.insert(0, paddle.nn.Linear(10, 20))
            >>> res2 = model2(data)  # [30, 30]

            >>> # extend sequential with given sequence of layer(s) at the end
            >>> model2 = paddle.nn.Sequential()
            >>> model2.extend([paddle.nn.Linear(10, 20), paddle.nn.Linear(20, 30)])
            >>> res2 = model2(data)  # [30, 30]
    layers?Layer | tuple[str, Layer] | list[Any] | OrderedDict[str, Layer]r   r   c                .   t                                                       t          |          dk    rSt          |d         t                    r8|d                                         D ]\  }}|                     ||           d S t          |          dk    rBt          |d         t          t          f          r |D ]\  }}|                     ||           d S t          |          D ](\  }}|                     t          |          |           )d S )Nr   r   )r   r   r4   rV   r   rL   r-   r   tuplerY   r%   )r    r   namer[   rz   r!   s        r"   r   zSequential.__init__  s$    	v;;!
6!9k B B%ay00 / /e!!$..../ /[[1__F1Ie}!E!E_% / /e!!$..../ / (// 3 3
U!!#c((E22223 3r#   r   str | slice | intr   c                L   t          |t                    r4 | j        t          | j                                                  |          S t          |t                    r| j        |         S |t          | j                  k    rt          d| d          |dk     r1|t          | j                   k    r|t          | j                  z  }n,|t          | j                   k     rt          d| d          t          | j                                                  |         S )Nr   z is out of ranger   )	rV   r   r!   r   r(   rP   r%   r4   r   r    r   s     r"   r*   zSequential.__getitem__  s   dE"" 	9!4>D)9)@)@)B)B$C$CD$IKKc"" 		9#D))s4+,,,, !@$!@!@!@AAAds4+;'<'<&<<<D,---T-..... !@$!@!@!@AAA(//1122488r#   r%   r[   c                p    t          |t                    sJ t          | t          |          |           d S r   )rV   r   rn   r%   )r    r   r[   s      r"   r.   zSequential.__setitem__
  s6    %'''''c$ii'''''r#   c                J    t          |          }|| j        v sJ | j        |= d S r   )r%   r(   r   s     r"   r0   zSequential.__delitem__  s2    4yyt'''''T"""r#   r1   c                *    t          | j                  S r   r3   r5   s    r"   r6   zSequential.__len__  r7   r#   inputr
   c                T    | j                                         D ]} ||          }|S r   rO   )r    r   r[   s      r"   forwardzSequential.forward  s4    %,,.. 	! 	!EE%LLEEr#   r   c                f    |                      t          t          |                     |           | S r   r   )r    r   s     r"   r   zSequential.append  s)    #c$ii..&111r#   r   c                   t          |t                    st          dt                     t          | j                  }| |cxk    r|k    sn t          d|           |dk     r||z  }t          ||d          D ]4}| j        t          |dz
                     | j        t          |          <   5|| j        t          |          <   | S )Nzmodule should be of type: zIndex out of range: r   r   r   )rV   r   AssertionErrorr4   r(   r   r   r%   )r    r   r   nr\   s        r"   r   zSequential.insert  s    &%(( 	G !Ee!E!EFFF !!e    q    ;E;;<<<199QJEq%$$ 	D 	DA'+'7AE

'CDSVV$$'-U$r#   
sequentialrM   c                :    |D ]}|                      |           | S r   )r   )r    r   r[   s      r"   r   zSequential.extend,  s+     	 	EKKr#   r   c                N    t          | j                                                  S r   r   r5   s    r"   r;   zSequential.__iter__1  r   r#   )r   r   r   r   )r   r   r   r   )r   r%   r[   r   r   r   )r   r%   r   r   r^   )r   r
   r   r
   )r   r   r   r   )r   r1   r   r   r   r   )r   rM   r   r   r   )rX   r`   ra   rb   r   r*   r.   r0   r6   r   r   r   r   r;   rc   rd   s   @r"   r   r     s        . .`3 3 3 3 3 3$9 9 9 9( ( ( (# # # #
% % % %   
         
/ / / / / / / /r#   r   )
__future__r   typingcollectionsr   collections.abcr   r   r   r   	itertoolsr	   r
   typing_extensionsr   r   r   base.dygraph.baser   base.frameworkr   r   r   __all__paddle.utils.decorator_utilsr   r   rf   rw   r   r   rC   r#   r"   <module>r      s   # " " " " "  # # # # # # A A A A A A A A A A A A             " " " " " "        , , , , , , ' ' ' ' ' '      
     
_0 _0 _0 _0 _0 _0 _0 _0D	d1 d1 d1 d1 d1E d1 d1 d1NO O O O OE O O Odt t t t t t t tny/ y/ y/ y/ y/ y/ y/ y/ y/ y/r#   