o
    Ne+                     @   sD  d dl 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	 g Z
d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i ejeejeejeejeejeejeejjjeejeejeej eej!eej"eej#eej$eej%eej&eej'eej(eej)eiZ*dddZ+dS )    N   )static_flopsTable)unwrap_decoratorsFc                 C   s^   t | tjrt| j\}| _t|}t| |||dS t | tjj	r(t
| |dS td dS )a  Print a table about the FLOPs of network.

    Args:
        net (paddle.nn.Layer||paddle.static.Program): The network which could be a instance of paddle.nn.Layer in 
                    dygraph or paddle.static.Program in static graph.
        input_size (list): size of input tensor. Note that the batch_size in argument ``input_size`` only support 1.
        custom_ops (A dict of function, optional): A dictionary which key is the class of specific operation such as 
                    paddle.nn.Conv2D and the value is the function used to count the FLOPs of this operation. This 
                    argument only work when argument ``net`` is an instance of paddle.nn.Layer. The details could be found
                    in following example code. Default is None.
        print_detail (bool, optional): Whether to print the detail information, like FLOPs per layer, about the net FLOPs.
                    Default is False.

    Returns:
        Int: A number about the FLOPs of total network.

    Examples:
        .. code-block:: python

            import paddle
            import paddle.nn as nn

            class LeNet(nn.Layer):
                def __init__(self, num_classes=10):
                    super(LeNet, self).__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()
            # m is the instance of nn.Layer, x is the intput of layer, y is the output of layer.
            def count_leaky_relu(m, x, y):
                x = x[0]
                nelements = x.numel()
                m.total_ops += int(nelements)

            FLOPs = paddle.flops(lenet, [1, 1, 28, 28], custom_ops= {nn.LeakyReLU: count_leaky_relu},
                                print_detail=True)
            print(FLOPs)

            #+--------------+-----------------+-----------------+--------+--------+
            #|  Layer Name  |   Input Shape   |   Output Shape  | Params | Flops  |
            #+--------------+-----------------+-----------------+--------+--------+
            #|   conv2d_2   |  [1, 1, 28, 28] |  [1, 6, 28, 28] |   60   | 47040  |
            #|   re_lu_2    |  [1, 6, 28, 28] |  [1, 6, 28, 28] |   0    |   0    |
            #| max_pool2d_2 |  [1, 6, 28, 28] |  [1, 6, 14, 14] |   0    |   0    |
            #|   conv2d_3   |  [1, 6, 14, 14] | [1, 16, 10, 10] |  2416  | 241600 |
            #|   re_lu_3    | [1, 16, 10, 10] | [1, 16, 10, 10] |   0    |   0    |
            #| max_pool2d_3 | [1, 16, 10, 10] |  [1, 16, 5, 5]  |   0    |   0    |
            #|   linear_0   |     [1, 400]    |     [1, 120]    | 48120  | 48000  |
            #|   linear_1   |     [1, 120]    |     [1, 84]     | 10164  | 10080  |
            #|   linear_2   |     [1, 84]     |     [1, 10]     |  850   |  840   |
            #+--------------+-----------------+-----------------+--------+--------+
            #Total Flops: 347560     Total Params: 61610
    )inputs
custom_opsprint_detail)r   zKYour model must be an instance of paddle.nn.Layer or paddle.static.Program.)
isinstancennZLayerr   forwardpaddleZrandndynamic_flopsZstaticZProgramr   warningswarn)netZ
input_sizer   r   _r    r   ID:\Projects\ConvertPro\env\Lib\site-packages\paddle/hapi/dynamic_flops.pyflops   s   N
r   c                 C   sn   |d }t | jjdd  }| jd urdnd}t| |jd | j | |  }|  jt	t|7  _d S Nr      r   )
npproductweightshapeZbiasintnumelZ_groups	total_opsabs)mxy
kernel_opsZbias_opsr   r   r   r   count_convNdz   s   r$   c                 C   s&   |d }|  }|  jt|7  _d S Nr   r   r   r   )r    r!   r"   	nelementsr   r   r   count_leaky_relu   s   r(   c                 C   s8   |d }|  }| jsd| }|  jtt|7  _d S )Nr   r   )r   trainingr   r   r   )r    r!   r"   r'   r   r   r   r   count_bn   s
   r*   c                 C   s6   | j jd }| }|| }|  jtt|7  _d S r%   )r   r   r   r   r   r   )r    r!   r"   Z	total_mulnum_elementsr   r   r   r   count_linear   s   r,   c                 C   s*   d}|  }|| }|  jt|7  _d S )Nr   r&   )r    r!   r"   r#   r+   r   r   r   r   count_avgpool   s   r-   c           	      C   sl   t |d jdd  t |jdd   }t |}d}|| }| }|| }|  jtt|7  _d S r   )r   arrayr   r   r   r   r   r   )	r    r!   r"   ZkernelZ	total_addZ	total_divr#   r+   r   r   r   r   count_adap_avgpool   s   ,
r/   c                 C   s   |  j td7  _ d S r%   )r   r   r    r!   r"   r   r   r   count_zero_ops   s   r1   c                 C   s4   d}|   D ]}|| 7 }qtt|| jd< d S r%   )
parametersr   r   r   total_params)r    r!   r"   r3   pr   r   r   count_parameters   s   r5   c                 C   sZ   |  dt|d j t|ttfr!|  dt|d j d S |  dt|j d S )Ninput_shaper   output_shape)register_bufferr   Z	to_tensorr   r
   listtupler0   r   r   r   count_io_info   s   r;   c              	      s  g t   d u ri   fdd}| j}|   | | tj  | | W d    n1 s3w   Y  d}d}|  D ]%}tt	|
 dkrMq@h dt t	|j re||j7 }||j7 }q@|rl|   D ]}	|	  qntg d}
|  D ]Q\}}tt	|
 dkrqh dt t	|j r|
| t	|j t	|j t|jt|jg |jd |jd |jd |jd	 q|r|
  td
t|t| t|S )Nc                    s  t t|  dkrd S | dtjdgdd | dtjdgdd t| }d }| v r> | }|vr=td| n|t	v rRt	| }|vrQtd| n|vr]td	| |d urk| 
|}| | 
t}| 
t}| | | d S )
Nr   r   r   Zint64)Zdtyper3   z)Customize Function has been applied to {}z{}'s flops has been countedzCCannot find suitable count function for {}. Treat it as zero FLOPs.)lenr9   childrenr8   r   Zzerostypeprintformatregister_hooksZregister_forward_post_hookappendr5   r;   add)r    Zm_typeZflops_fnZflops_handlerZparams_handlerZ
io_handlerr   Zhandler_collectionZtypes_collectionr   r   	add_hooks   s>   





z dynamic_flops.<locals>.add_hooksr   >   r3   r   r6   r7   )z
Layer NamezInput ShapezOutput ShapeZParamsZFlopsr   r3   r6   r7   z$Total Flops: {}     Total Params: {})setr)   evalapplyr   Z	frameworkZno_gradZ	sublayersr<   r9   r=   issubset_bufferskeysr   r3   trainremover   Znamed_sublayersadd_rowZ	full_namer6   numpyr7   r   popZprint_tabler?   r@   )modelr   r   r   rE   r)   r   r3   r    handlertablenr   rD   r   r      sj    




r   )NF),r   r   Z	paddle.nnr   rO   r   r   r   Z9paddle.fluid.dygraph.dygraph_to_static.program_translatorr   __all__r   r$   r(   r*   r,   r-   r/   r1   r5   r;   ZConv1DZConv2DZConv3DZConv1DTransposeZConv2DTransposeZConv3DTransposelayerZnormZBatchNorm2DZ	BatchNormZReLUZReLU6Z	LeakyReLUZLinearZDropoutZ	AvgPool1DZ	AvgPool2DZ	AvgPool3DZAdaptiveAvgPool1DZAdaptiveAvgPool2DZAdaptiveAvgPool3DrA   r   r   r   r   r   <module>   sp   
a	

	
