
    Αi.                    h   % S SK Jr  S SKrS SKJr  S SKrS SKJr  S SK	r	S SK	J
r
  S SKJr  SSKJrJr  \(       a*  S S	KJr  S S
K	Jr  S SKJr  S SKJr  \\\   \S   4   rS\S'   / r  S         SS jjrS rS rS rS rS r S r!S r"S r#S r$0 \
RJ                  \_\
RL                  \_\
RN                  \_\
RP                  \_\
RR                  \_\
RT                  \_\
RV                  RX                  RZ                  \_\
R\                  \_\
R^                  \"_\
R`                  \"_\
Rb                  \_\
Rd                  \_\
Rf                  \"_\
Rh                  \ _\
Rj                  \ _\
Rl                  \ _\
Rn                  \!_\
Rp                  \!\
Rr                  \!0Er:  S         SS jjr;g)    )annotationsN)TYPE_CHECKING)	TypeAlias)nn)unwrap_decorators   )Tablestatic_flops)Callable)Tensor)Layer)Program).Nr   _CustomOpsAliasc                J   [        U [        R                  5      (       a<  [        U R                  5      u  o@l        [
        R                  " U5      n[        XX#S9$ [        U [
        R                  R                  5      (       a	  [        XS9$ [        R                  " S5        g)aE  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().__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 input 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)
        >>> # doctest: +SKIP('numpy print with different version')
        <class 'paddle.nn.layer.conv.Conv2D'>'s flops has been counted
        <class 'paddle.nn.layer.activation.ReLU'>'s flops has been counted
        Cannot find suitable count function for <class 'paddle.nn.layer.pooling.MaxPool2D'>. Treat it as zero FLOPs.
        <class 'paddle.nn.layer.common.Linear'>'s flops has been counted
        +--------------+-----------------+-----------------+--------+--------+
        |  Layer Name  |   Input Shape   |   Output Shape  | Params | Flops  |
        +--------------+-----------------+-----------------+--------+--------+
        |   conv2d_0   |  [1, 1, 28, 28] |  [1, 6, 28, 28] |   60   | 47040  |
        |   re_lu_0    |  [1, 6, 28, 28] |  [1, 6, 28, 28] |   0    |   0    |
        | max_pool2d_0 |  [1, 6, 28, 28] |  [1, 6, 14, 14] |   0    |   0    |
        |   conv2d_1   |  [1, 6, 14, 14] | [1, 16, 10, 10] |  2416  | 241600 |
        |   re_lu_1    | [1, 16, 10, 10] | [1, 16, 10, 10] |   0    |   0    |
        | max_pool2d_1 | [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
        >>> # doctest: -SKIP
        >>> print(FLOPs)
        347560
)inputs
custom_opsprint_detail)r   zKYour model must be an instance of paddle.nn.Layer or paddle.static.Program.)
isinstancer   r   r   forwardpaddlerandndynamic_flopsstaticr   r
   warningswarn)net
input_sizer   r   _r   s         Y/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/hapi/dynamic_flops.pyflopsr!   (   s    p #rxx   +3;;7;j):
 	
 
C..	/	/C;;Y	
     c                P   US   n[         R                  " U R                  R                  SS  5      nU R                  b  SOSn[        UR                  5       5      UR                  S   U R                  -  U-  U-   -  nU =R                  [        [        U5      5      -  sl        g Nr      r   )
npprodweightshapebiasintnumel_groups	total_opsabs)mxy
kernel_opsbias_opsr.   s         r    count_convNdr5      s    	!A+,JFF&qAHAGGI	
QYY+h6I KK3s9~&&Kr"   c                j    US   nUR                  5       nU =R                  [        U5      -  sl        g Nr   r,   r.   r+   )r0   r1   r2   	nelementss       r    count_leaky_relur:      s(    	!A	IKK3y>!Kr"   c                    US   nUR                  5       nU R                  (       d  SU-  nU =R                  [        [	        W5      5      -  sl        g )Nr   r%   )r,   trainingr.   r/   r+   )r0   r1   r2   r9   r.   s        r    count_bnr=      s=    	!A	I::	M	KK3s9~&&Kr"   c                    U R                   R                  S   nUR                  5       nX4-  nU =R                  [	        [        U5      5      -  sl        g r7   )r(   r)   r,   r.   r/   r+   )r0   r1   r2   	total_mulnum_elementsr.   s         r    count_linearrA      s>    q!I779L(IKK3s9~&&Kr"   c                l    SnUR                  5       nX4-  nU =R                  [        U5      -  sl        g )Nr   r8   )r0   r1   r2   r3   r@   r.   s         r    count_avgpoolrC      s,    J779L)IKK3y>!Kr"   c                F   [         R                  " US   R                  SS  5      [         R                  " UR                  SS  5      -  n[         R                  " U5      nSnXE-   nUR	                  5       nXg-  nU =R
                  [        [        U5      5      -  sl        g r$   )r&   arrayr)   r'   r,   r.   r/   r+   )	r0   r1   r2   kernel	total_add	total_divr3   r@   r.   s	            r    count_adap_avgpoolrI      sz    XXadjjn%!''!"+)>>FII&J779L)IKK3s9~&&Kr"   c                .    U =R                   S-  sl         g r7   )r.   r0   r1   r2   s      r    count_zero_opsrL      s    KK1Kr"   c                    SnU R                  5        H  nX4R                  5       -  nM     [        [        U5      5      U R                  S'   g r7   )
parametersr,   r/   r+   total_params)r0   r1   r2   rO   ps        r    count_parametersrQ      s=    L\\^	! C-.ANN1r"   c                h   U R                  S[        R                  " US   R                  5      5        [	        U[
        [        45      (       a4  U R                  S[        R                  " US   R                  5      5        g U R                  S[        R                  " UR                  5      5        g )Ninput_shaper   output_shape)register_bufferr   	to_tensorr)   r   listtuplerK   s      r    count_io_inforY      sx    mV%5%5adjj%AB!dE]##	.&*:*:1Q4::*FG	.&*:*:177*CDr"   c           
     N  ^^^ / m[        5       mTc  0 mUUU4S jnU R                  nU R                  5         U R                  U5        [        R
                  R                  5          U " U5        S S S 5        SnSnU R                  5        H  n[        [        UR                  5       5      5      S:  a  M+  1 SkR                  [        UR                  R                  5       5      5      (       d  Mf  XhR                  -  nXxR                  -  nM     U(       a  U R!                  5         T H  n	U	R#                  5         M     [%        / SQ5      n
U R'                  5        GHk  u  p[        [        UR                  5       5      5      S:  a  M.  1 SkR                  [        UR                  R                  5       5      5      (       d  Mi  U
R)                  UR+                  5       UR,                  R/                  5       R1                  5       UR2                  R/                  5       R1                  5       [5        UR                  5      [5        UR                  5      /5        UR                  R7                  S5        UR                  R7                  S5        UR                  R7                  S5        UR                  R7                  S5        GMn     U(       a  U
R9                  5         [;        S	[5        U5       S
[5        U5       35        [5        U5      $ ! , (       d  f       GN= f)Nc                  > [        [        U R                  5       5      5      S:  a  g U R                  S[        R
                  " S/SS95        U R                  S[        R
                  " S/SS95        [        U 5      nS nUT;   a  TU   nUT;  a  [        SU 35        O=U[        ;   a  [        U   nUT;  a  [        U S35        OUT;  a  [        S	U S
35        Ub"  U R                  U5      nTR                  U5        U R                  [        5      nU R                  [        5      nTR                  U5        TR                  U5        TR                  U5        g )Nr   r.   r   int64)dtyperO   z'Customize Function has been applied to z's flops has been countedz(Cannot find suitable count function for z. Treat it as zero FLOPs.)lenrW   childrenrU   r   zerostypeprintregister_hooksregister_forward_post_hookappendrQ   rY   add)	r0   m_typeflops_fnflops_handlerparams_handler
io_handlerr   handler_collectiontypes_collections	         r    	add_hooks dynamic_flops.<locals>.add_hooks   sM   tAJJL!"Q&	+v||QCw'GH	.&,,s'*JKaZ!&)H--?xHI~%%f-H-- 9:;-->vhF_` 88BM%%m4556FG11-@
!!.1!!*-V$r"   r   >   r.   rS   rT   rO   )z
Layer NamezInput ShapezOutput ShapeParamsFlopsr.   rO   rS   rT   zTotal Flops: z     Total Params: )setr<   evalapplyr   	frameworkno_grad	sublayersr^   rW   r_   issubset_bufferskeysr.   rO   trainremover	   named_sublayersadd_row	full_namerS   numpytolistrT   r+   popprint_tablerb   )modelr   r   r   rn   r<   r.   rO   r0   handlertablenrl   rm   s     `         @@r    r   r      sU    u
%> ~~H	JJL	KK					!	!	#f 
$ IL__tAJJL!"Q&

 (3qzz()
*+ $INN*L  % & HE %%'tAJJL!"Q&

 (3qzz()
*+ MMKKMMM'')002NN((*113'$ JJNN;'JJNN>*JJNN=)JJNN>*+ (, 	
I'':3|;L:MN y>k 
$	#s   (	L
L$)NF)
r   zLayer | Programr   z	list[int]r   _CustomOpsAlias | Noner   boolreturnr+   )
r   r   r   r   r   r   r   r   r   r+   )<
__future__r   r   typingr   r   r&   typing_extensionsr   r   r   'paddle.jit.dy2static.program_translatorr   r
   r	   collections.abcr   r   	paddle.nnr   paddle.staticr   dictra   r   __annotations____all__r!   r5   r:   r=   rA   rC   rI   rL   rQ   rY   Conv1DConv2DConv3DConv1DTransposeConv2DTransposeConv3DTransposelayernormBatchNorm2D	BatchNormReLUReLU6	LeakyReLULinearDropout	AvgPool1D	AvgPool2D	AvgPool3DAdaptiveAvgPool1DAdaptiveAvgPool2DAdaptiveAvgPool3Drc   r    r"   r    <module>r      sJ   #     '   E -(%!%d5k8I3F&F!GOYG
 *.	g	gg 'g 	g
 	gT'"''"'/EII|II| II| 	
   HHMMx LL( GG^ HHn LL" II| JJ LL- LL-  LL-!" ,#$ ,,'4 *.	ddd 'd 	d
 	dr"   