o
    * ik                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlZ	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 erDd d	lmZ g ZG d
d deZ			ddddZe dddZdS )    )annotationsN)OrderedDict)TYPE_CHECKING)	TypedDict)Tensornn)no_grad)	InputSpec)Sequencec                   @  s   e Zd ZU ded< ded< dS )ModelSummaryinttotal_paramstrainable_paramsN)__name__
__module____qualname____annotations__ r   r   e/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/hapi/model_summary.pyr   $   s   
 r   netnn.Layer
input_sizeLint | tuple[int, ...] | InputSpec | list[tuple[int, ...] | InputSpec] | Nonedtypesstr | Sequence[str] | Noneinput4Tensor | Sequence[Tensor] | dict[str, Tensor] | Nonereturnc                   s  |du r|du rt d|du ra|durat|rt|j}nBt|ttfr6g }|D ]
}|t|j q*n+t|trOg }|	 D ]}|t|| j qAnt|tj
jjr]t|j}nt dt|trlt|j}nCt|trg }|D ]-}t|tr|f}t|ttfsJ dt| t|tr|t|j qu|| qunt|tr|f}n|}t std d}n| j}|r|   dd d	d
  fdd  |}t| |||\}	}
t|	 |r|   |
S )aL7  Prints a string summary of the network.

    Args:
        net (Layer): The network which must be a subinstance of Layer.
        input_size (tuple|InputSpec|list[tuple|InputSpec]|None, optional): Size of input tensor. if model only
                    have one input, input_size can be tuple or InputSpec. if model
                    have multiple input, input_size must be a list which contain
                    every input's shape. Note that input_size only dim of
                    batch_size can be None or -1. Default: None. Note that
                    input_size and input cannot be None at the same time.
        dtypes (str|Sequence[str]|None, optional): If dtypes is None, 'float32' will be used, Default: None.
        input (Tensor|Sequence[paddle.Tensor]|dict[str, paddle.Tensor]|None, optional): If input is given, input_size and dtype will be ignored, Default: None.

    Returns:
        dict: A summary of the network including total params and total trainable params.

    Examples:
        .. code-block:: python
            :name: code-example-1

            >>> # example 1: Single Input Demo
            >>> import paddle
            >>> import paddle.nn as nn
            >>> # Define Network
            >>> class LeNet(nn.Layer):
            ...     def __init__(self, num_classes=10):
            ...         super().__init__()
            ...         self.num_classes = num_classes
            ...         self.features = nn.Sequential(
            ...             nn.Conv2D(1, 6, 3, stride=1, padding=1),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2),
            ...             nn.Conv2D(6, 16, 5, stride=1, padding=0),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2))
            ...
            ...         if num_classes > 0:
            ...             self.fc = nn.Sequential(
            ...                 nn.Linear(400, 120),
            ...                 nn.Linear(120, 84),
            ...                 nn.Linear(84, 10))
            ...
            ...     def forward(self, inputs):
            ...         x = self.features(inputs)
            ...
            ...         if self.num_classes > 0:
            ...             x = paddle.flatten(x, 1)
            ...             x = self.fc(x)
            ...         return x
            ...
            >>> lenet = LeNet()
            >>> params_info = paddle.summary(lenet, (1, 1, 28, 28)) # doctest: +NORMALIZE_WHITESPACE
            ---------------------------------------------------------------------------
             Layer (type)       Input Shape          Output Shape         Param #
            ===========================================================================
               Conv2D-1       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
                ReLU-1        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
              MaxPool2D-1     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
               Conv2D-2       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
                ReLU-2       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
              MaxPool2D-2    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
               Linear-1          [[1, 400]]            [1, 120]           48,120
               Linear-2          [[1, 120]]            [1, 84]            10,164
               Linear-3          [[1, 84]]             [1, 10]              850
            ===========================================================================
            Total params: 61,610
            Trainable params: 61,610
            Non-trainable params: 0
            ---------------------------------------------------------------------------
            Input size (MB): 0.00
            Forward/backward pass size (MB): 0.11
            Params size (MB): 0.24
            Estimated Total Size (MB): 0.35
            ---------------------------------------------------------------------------
            <BLANKLINE>
            >>> print(params_info)
            {'total_params': 61610, 'trainable_params': 61610}

        .. code-block:: python
            :name: code-example-2

            >>> # example 2: multi input demo
            >>> import paddle
            >>> import paddle.nn as nn
            >>> class LeNetMultiInput(nn.Layer):
            ...     def __init__(self, num_classes=10):
            ...         super().__init__()
            ...         self.num_classes = num_classes
            ...         self.features = nn.Sequential(
            ...             nn.Conv2D(1, 6, 3, stride=1, padding=1),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2),
            ...             nn.Conv2D(6, 16, 5, stride=1, padding=0),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2))
            ...
            ...         if num_classes > 0:
            ...             self.fc = nn.Sequential(
            ...                 nn.Linear(400, 120),
            ...                 nn.Linear(120, 84),
            ...                 nn.Linear(84, 10))
            ...
            ...     def forward(self, inputs, y):
            ...         x = self.features(inputs)
            ...
            ...         if self.num_classes > 0:
            ...             x = paddle.flatten(x, 1)
            ...             x = self.fc(x + y)
            ...         return x
            ...
            >>> lenet_multi_input = LeNetMultiInput()

            >>> params_info = paddle.summary(lenet_multi_input,
            ...                              [(1, 1, 28, 28), (1, 400)],
            ...                              dtypes=['float32', 'float32']) # doctest: +NORMALIZE_WHITESPACE
            ---------------------------------------------------------------------------
             Layer (type)       Input Shape          Output Shape         Param #
            ===========================================================================
               Conv2D-1       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
                ReLU-1        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
              MaxPool2D-1     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
               Conv2D-2       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
                ReLU-2       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
              MaxPool2D-2    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
               Linear-1          [[1, 400]]            [1, 120]           48,120
               Linear-2          [[1, 120]]            [1, 84]            10,164
               Linear-3          [[1, 84]]             [1, 10]              850
            ===========================================================================
            Total params: 61,610
            Trainable params: 61,610
            Non-trainable params: 0
            ---------------------------------------------------------------------------
            Input size (MB): 0.00
            Forward/backward pass size (MB): 0.11
            Params size (MB): 0.24
            Estimated Total Size (MB): 0.35
            ---------------------------------------------------------------------------
            <BLANKLINE>
            >>> print(params_info)
            {'total_params': 61610, 'trainable_params': 61610}

        .. code-block:: python
            :name: code-example-3

            >>> # example 3: List Input Demo
            >>> import paddle
            >>> import paddle.nn as nn

            >>> # list input demo
            >>> class LeNetListInput(nn.Layer):
            ...     def __init__(self, num_classes=10):
            ...         super().__init__()
            ...         self.num_classes = num_classes
            ...         self.features = nn.Sequential(
            ...             nn.Conv2D(1, 6, 3, stride=1, padding=1),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2),
            ...             nn.Conv2D(6, 16, 5, stride=1, padding=0),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2))
            ...
            ...         if num_classes > 0:
            ...             self.fc = nn.Sequential(
            ...                 nn.Linear(400, 120),
            ...                 nn.Linear(120, 84),
            ...                 nn.Linear(84, 10))
            ...
            ...     def forward(self, inputs):
            ...         x = self.features(inputs[0])
            ...
            ...         if self.num_classes > 0:
            ...             x = paddle.flatten(x, 1)
            ...             x = self.fc(x + inputs[1])
            ...         return x
            ...
            >>> lenet_list_input = LeNetListInput()
            >>> input_data = [paddle.rand([1, 1, 28, 28]), paddle.rand([1, 400])]
            >>> params_info = paddle.summary(lenet_list_input, input=input_data) # doctest: +NORMALIZE_WHITESPACE
            ---------------------------------------------------------------------------
             Layer (type)       Input Shape          Output Shape         Param #
            ===========================================================================
               Conv2D-1       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
                ReLU-1        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
              MaxPool2D-1     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
               Conv2D-2       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
                ReLU-2       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
              MaxPool2D-2    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
               Linear-1          [[1, 400]]            [1, 120]           48,120
               Linear-2          [[1, 120]]            [1, 84]            10,164
               Linear-3          [[1, 84]]             [1, 10]              850
            ===========================================================================
            Total params: 61,610
            Trainable params: 61,610
            Non-trainable params: 0
            ---------------------------------------------------------------------------
            Input size (MB): 0.00
            Forward/backward pass size (MB): 0.11
            Params size (MB): 0.24
            Estimated Total Size (MB): 0.35
            ---------------------------------------------------------------------------
            <BLANKLINE>
            >>> print(params_info)
            {'total_params': 61610, 'trainable_params': 61610}


        .. code-block:: python
            :name: code-example-4

            >>> # example 4: Dict Input Demo
            >>> import paddle
            >>> import paddle.nn as nn

            >>> # Dict input demo
            >>> class LeNetDictInput(nn.Layer):
            ...     def __init__(self, num_classes=10):
            ...         super().__init__()
            ...         self.num_classes = num_classes
            ...         self.features = nn.Sequential(
            ...             nn.Conv2D(1, 6, 3, stride=1, padding=1),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2),
            ...             nn.Conv2D(6, 16, 5, stride=1, padding=0),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2))
            ...
            ...         if num_classes > 0:
            ...             self.fc = nn.Sequential(
            ...                 nn.Linear(400, 120),
            ...                 nn.Linear(120, 84),
            ...                 nn.Linear(84, 10))
            ...
            ...     def forward(self, inputs):
            ...         x = self.features(inputs['x1'])
            ...
            ...         if self.num_classes > 0:
            ...             x = paddle.flatten(x, 1)
            ...             x = self.fc(x + inputs['x2'])
            ...         return x
            ...
            >>> lenet_dict_input = LeNetDictInput()
            >>> input_data = {'x1': paddle.rand([1, 1, 28, 28]),
            ...               'x2': paddle.rand([1, 400])}
            >>> # The module suffix number indicates its sequence in modules of the same type, used for differentiation identification
            >>> params_info = paddle.summary(lenet_dict_input, input=input_data) # doctest: +NORMALIZE_WHITESPACE
            ---------------------------------------------------------------------------
             Layer (type)       Input Shape          Output Shape         Param #
            ===========================================================================
               Conv2D-1       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
                ReLU-1        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
              MaxPool2D-1     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
               Conv2D-2       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
                ReLU-2       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
              MaxPool2D-2    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
               Linear-1          [[1, 400]]            [1, 120]           48,120
               Linear-2          [[1, 120]]            [1, 84]            10,164
               Linear-3          [[1, 84]]             [1, 10]              850
            ===========================================================================
            Total params: 61,610
            Trainable params: 61,610
            Non-trainable params: 0
            ---------------------------------------------------------------------------
            Input size (MB): 0.00
            Forward/backward pass size (MB): 0.11
            Params size (MB): 0.24
            Estimated Total Size (MB): 0.35
            ---------------------------------------------------------------------------
            <BLANKLINE>
            >>> print(params_info)
            {'total_params': 61610, 'trainable_params': 61610}
    Nz4input_size and input cannot be None at the same timezcInput is not tensor, list, tuple and dict, unable to determine input_size, please input input_size.z`When input_size is list,             expect item in input_size is a tuple or InputSpec, but got zZYour model was created in static graph mode, this may not get correct summary information!Fc                 S  s"   | D ]}t |ttfr dS qdS NFT
isinstancelisttuple)shapeitemr   r   r   	_is_shapex  s
   zsummary.<locals>._is_shapec                 S  s   d}g }t t| D ]3}| | }|d u s|dkr'|d7 }|dkr$tdd}nt|tjr8|dkr8td| || q
t|S )Nr      z?Option input_size only the dim of batch_size can be None or -1.z:Expected element in input size greater than zero, but got )rangelen
ValueErrorr    numbersNumberappendr"   )r#   Znum_unknownZ	new_shapeir$   r   r   r   _check_shape~  s$   zsummary.<locals>._check_shapec                   s0   t | ttfr| r| S  fdd| D S )Nc                      g | ]} |qS r   r   .0r.   )_check_inputr   r   
<listcomp>      z1summary.<locals>._check_input.<locals>.<listcomp>r   )r   r3   r/   r%   r   r   r3     s   zsummary.<locals>._check_input)r*   paddleZ	is_tensorr"   r#   r    r!   r-   dictkeysbaseZ	frameworkVariabler	   r   typein_dynamic_modewarningswarnZtrainingevalsummary_stringprinttrain)r   r   r   r   xkeyZ_input_sizer$   Zin_train_moderesultZparams_infor   r6   r   summary)   st     






rG   c                   sv  dd   fddt |ttfs||}d}d}tt fddfd	d
	fdd}t |trB|g} fddt 	g | |d ur^|}| n	||}|  D ]}|  qidd }	|		}
|d|
d  d 7 }dd|
d d|
d d|
d d|
d }||d 7 }|d|
d  d 7 }d}d}d}d}	D ]y}d||
d t		| d |
d t		| d  |
d d!	| d" |
d }|	| d" 7 }z|t
t
j	| d  d#d$7 }W n   	| d  D ]}|t
t
j|d#d$7 }qY d%	| v r#	| d% r#|	| d& 7 }||d 7 }q fd'd(|d}td)| d* d+ }t|d* d+ }|| | }|d|
d  d 7 }|d,|d-d 7 }|d.|d-d 7 }|d/|| d-d 7 }|d|
d  d 7 }|d0|d1d 7 }|d2|d1d 7 }|d3|d1d 7 }|d4|d1d 7 }|d|
d  d 7 }|||d5fS )6Nc                 S  s    | D ]}t |tjs dS qdS r   )r    r+   r,   )itemsr$   r   r   r   _all_is_number  s
   z&summary_string.<locals>._all_is_numberc                   s<    d u rd t | ttfr| r gS  fdd| D S )NZfloat32c                      g | ]} |qS r   r   r1   )_build_dtypesdtyper   r   r4         z9summary_string.<locals>._build_dtypes.<locals>.<listcomp>r   )r   rL   )rI   rK   )rL   r   rK     s
   z%summary_string.<locals>._build_dtypesr'    c                   sH   t | tjjtjjjjfrt| jS t | tt	fr" fdd| D S d S )Nc                   r0   r   r   )r2   xx_get_shape_from_tensorr   r   r4     r5   zBsummary_string.<locals>._get_shape_from_tensor.<locals>.<listcomp>)
r    r7   r:   r;   coreeagerr   r!   r#   r"   )rD   rP   r   r   rQ     s
   
z.summary_string.<locals>._get_shape_from_tensorc                   sD   t | ttfr fdd| D }|S t| drt| j}|S g }|S )Nc                   r0   r   r   )r2   o_get_output_shaper   r   r4     r5   z=summary_string.<locals>._get_output_shape.<locals>.<listcomp>r#   )r    r!   r"   hasattrr#   )outputoutput_shaperU   r   r   rV     s   

z)summary_string.<locals>._get_output_shapec                   sx    fdd}t | tjs&t | tjs&| krdk r&| | d S t| dr8| jr:| | d S d S d S )Nc                   s  t | jdd dd }zt| jdd }W n   t}Y | d|d  }t |< z
|| d< W n   td	 g | d< Y z
 || d
< W n   td | d
  Y d}t	
 rp| j}n|  }d| d< d}| D ]D\}	}
|t|
j7 }z-t| |	jrt| |	js| d  t|
j7  < d| d< d}n|sd| d< W q   d| d< Y q|| d< d S )N.r&   'r   _-r'   input_shapez Get layer {} input shape failed!rY   z!Get layer {} output shape failed!r   FT	trainable	nb_params)str	__class__splitr   Z
_full_namer)   r   r>   r?   r7   r=   _parametersZ
state_dictrH   npprodr#   getattrr_   Zstop_gradient)layerr   rX   
class_nameZ	layer_idxZm_keyparamsZlayer_state_dictZtrainable_flagkv)rV   rQ   rG   r   r   hook  sL   




z3summary_string.<locals>.register_hook.<locals>.hookr'   could_use_cudnn)r    r   Z
SequentialZ	LayerListr-   Zregister_forward_post_hookrW   rn   )rh   rm   )rV   rQ   depthhooksmodelrG   r   r   register_hook  s   
2
z%summary_string.<locals>.register_hookc                   s`   t | ttfr$ | r$t |ttfr|d }n|}ttt| |S fddt| |D S )Nr   c                   s   g | ]	\}} ||qS r   r   )r2   r.   rL   )build_inputr   r   r4     s    z7summary_string.<locals>.build_input.<locals>.<listcomp>)r    r!   r"   r7   castZrandzip)r   r   rL   )rI   rs   r   r   rs     s   

z#summary_string.<locals>.build_inputc                 S  s   dddddd}| D ]b}|d t t| | d k r&t t| | d |d< |d t t| | d k r@t t| | d |d< |d	 t t|k rRt t||d	< |d
 t t| | d k rlt t| | d |d
< q
d}| D ]\}}|dkr||7 }qs|d |d k r|d |d< |S )N      K   )layer_widthinput_shape_widthoutput_shape_widthparams_widthtable_widthr{   rY   rz   r^   ry   r|   r`   r   r}      )r)   ra   rH   )rG   Zhead_lengthrh   Z_temp_widthrk   rl   r   r   r   _get_str_length(  sH   z'summary_string.<locals>._get_str_lengthr]   r}   
z{:^{}} {:^{}} {:^{}} {:^{}}zLayer (type)ry   zInput Shaperz   zOutput Shaper{   zParam #r|   =r   r^   rY   z{:,}r`   r&   )Zaxisr_   r   c                   sL   t | ttfr| rtt| d d   S t fdd| D   S )N      @      0Ac                   rJ   r   r   r1   )_get_input_sizesizer   r   r4     rM   z;summary_string.<locals>._get_input_size.<locals>.<listcomp>)r    r!   r"   absre   rf   sum)r   r   )rI   r   )r   r   r     s
   z'summary_string.<locals>._get_input_sizeg       @r   r   zTotal params: ,zTrainable params: zNon-trainable params: zInput size (MB): z0.2fz!Forward/backward pass size (MB): zParams size (MB): zEstimated Total Size (MB): )r   r   )r    r!   r"   r)   Z	sublayersr   applyremoveformatra   re   r   rf   r   )rq   r   r   r   Z
batch_sizeZsummary_strrr   rD   hr   r}   Zline_newr   Ztotal_outputr   
max_lengthrh   rY   Ztotal_input_sizeZtotal_output_sizeZtotal_params_size
total_sizer   )
rI   rK   r   rV   rQ   rs   ro   rp   rq   rG   r   rA     s   	
	
<



)



rA   )NNN)
r   r   r   r   r   r   r   r   r   r   )
__future__r   r+   r>   collectionsr   typingr   numpyre   Ztyping_extensionsr   r7   r   r   Zpaddle.autogradr   Zpaddle.staticr	   collections.abcr
   __all__r   rG   rA   r   r   r   r   <module>   s.     |