
    zj                         d dl mZ d dlZd dlmZmZ g Z G d d          Z G d d          Z	 G d d	          Z
d
 Zd Zd Zd Zd Zd ZddZddZ G d d          ZdS )    )OrderedDictN)ProgramVariablec                        e Zd Zd Zd Zd ZdS )
VarWrapperc                 ~    t          |t                    sJ t          |t                    sJ || _        || _        d S N)
isinstancer   GraphWrapper_var_graph)selfvargraphs      h/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/hapi/static_flops.py__init__zVarWrapper.__init__   s@    #x(((((%.....	    c                     | j         j        S )z/
        Get the name of the variable.
        )r   namer   s    r   r   zVarWrapper.name   s     y~r   c                     | j         j        S )z0
        Get the shape of the variable.
        )r   shaper   s    r   r   zVarWrapper.shape%   s     yr   N)__name__
__module____qualname__r   r   r    r   r   r   r      sA                r   r   c                   &    e Zd Zd Zd Zd Zd ZdS )	OpWrapperc                 P    t          |t                    sJ || _        || _        d S r	   )r
   r   _opr   )r   opr   s      r   r   zOpWrapper.__init__-   s*    %.....r   c                     | j         j        S )z0
        Get the type of this operator.
        )r    typer   s    r   r#   zOpWrapper.type2   s     x}r   c                 p     | j         j        v r& fd j                             |          D             S g S )z:
        Get all the variables by the input name.
        c                 D    g | ]}j                             |          S r   r   r   .0var_namer   s     r   
<listcomp>z$OpWrapper.inputs.<locals>.<listcomp>=   s4       .6))  r   )r    input_namesinputr   r   s   ` r   inputszOpWrapper.inputs8   sU     48'''   :>(..:N:N    Ir   c                 P      fd j                             |          D             S )z;
        Get all the variables by the output name.
        c                 D    g | ]}j                             |          S r   r&   r'   s     r   r*   z%OpWrapper.outputs.<locals>.<listcomp>G   s'    PPPh))PPPr   )r    outputr-   s   ` r   outputszOpWrapper.outputsC   s.     QPPP$(//$:O:OPPPPr   N)r   r   r   r   r#   r.   r2   r   r   r   r   r   ,   sU          
  	 	 	Q Q Q Q Qr   r   c                   6     e Zd ZdZdg g f fd	Zd Zd Z xZS )r   aN  
    It is a wrapper of paddle.base.framework.IrGraph with some special functions
    for paddle slim framework.

    Args:
        program(framework.Program): A program with
        in_nodes(dict): A dict to indicate the input nodes of the graph.
                        The key is user-defined and human-readable name.
                        The value is the name of Variable.
        out_nodes(dict): A dict to indicate the input nodes of the graph.
                        The key is user-defined and human-readable name.
                        The value is the name of Variable.
    Nc                    t                                                       |t                      n|| _        i | _        i | _        | j                                        D ]}|j        r|| j        |j        <   d| _	        |g n|}|g n|}t          |          | _        t          |          | _        t                      | _        dS ) N)superr   r   programpersistablesteacher_persistables	list_varspersistabler   compiled_graphr   in_nodes	out_nodes_attrs)r   r7   r=   r>   r   	__class__s        r   r   zGraphWrapper.__init__Y   s    $+Owyyy$&!<))++ 	2 	2C 2.1!#(+"!)22x#+BB	#H--$Y//!mmr   c                     g }| j         j        D ]/}|j        D ]%}|                    t	          ||                      &0|S )zK
        Return all operator nodes included in the graph as a set.
        )r7   blocksopsappendr   )r   rC   blockr!   s       r   rC   zGraphWrapper.opsi   sW     \( 	0 	0Ei 0 0

9R..////0
r   c                     | j         j        D ]<}|                    |          r%t          |                    |          |           c S =dS )z4
        Get the variable by variable name.
        N)r7   rB   has_varr   r   )r   r   rE   s      r   r   zGraphWrapper.vars   sV     \( 	9 	9E}}T"" 9!%))D//4888889tr   )r   r   r   __doc__r   rC   r   __classcell__)r@   s   @r   r   r   J   sn           $bB $ $ $ $ $ $         r   r   c                    |                      d          d                                         }t          j        |dd                    }t	          |                      d                    dk    rdnd}t          j        |                     d          d                                         dd                    }|||z   z  }t          |          }|S )NFilterr      BiasOutput)r.   r   npprodlenr2   abs)r!   filter_shape
filter_opsbias_opsoutput_numel	total_opss         r   count_convNdrX   }   s    99X&&q)//11Labb)**J		&))**Q..qqAH72::h//288::122>??L
X 56IIIr   c                     t          j        |                     d          d                                         dd                    }|S )NrN   r   rL   )rO   rP   r2   r   )r!   rW   s     r   count_leaky_relurZ      s=    

8,,Q/5577;<<Ir   c                     t          j        |                     d          d                                         dd                    }d|z  }t	          |          }|S )NYr   rL      )rO   rP   r2   r   rR   )r!   rV   rW   s      r   count_bnr^      sP    72::c??1-3355abb9::LL IIIr   c                 "   |                      d          d                                         d         }t          j        |                     d          d                                         dd                    }||z  }t          |          }|S )Nr\   r   OutrL   )r.   r   rO   rP   r2   rR   )r!   	total_mulnumelrW   s       r   count_linearrc      sw    		#q!''))!,IGBJJu%%a(..00455EE!IIIr   c                    |                      d          d                                         }|                     d          d                                         }t          j        |dd                    t          j        |dd                    z  }t          j        |          }d}||z   }t          j        |dd                    }||z  }t          |          }|S )NXr   r`   r]   rL   )r.   r   r2   rO   arrayrP   rR   )	r!   input_shapeoutput_shapekernel	total_add	total_div
kernel_opsnum_elementsrW   s	            r   count_pool2drn      s    ))C..#))++K::e$$Q'--//LXk!""o&&"(<3C*D*DDFIIY&J7<+,,L\)IIIr   c                     |                      d          d                                         }t          j        |dd                    }t	          |          }|S )Nre   r   rL   )r.   r   rO   rP   rR   )r!   rg   rW   s      r   count_element_oprp      sJ    ))C..#))++KABB((IIIr   Fc                    t          | t                    sJ d}d}t          g d          }|                                 D ]|}d}|                                dv rBt          |          }||z  }|                    d          d                                         }n|                                dk    rt          |          }||z  }n|                                dv rBt          |          }||z  }|                    d          d                                         }nh|                                d	k    rt          |          }||z  }n;|                                                    d
          rt          |          }||z  }|dk    r*|                    |                                ||g           d}~|r|                                 |S )Nr   )zOP Typez
Param nameFlops )conv2ddepthwise_conv2drK   pool2d)mulmatmulr\   
batch_normelement)r
   r   TablerC   r#   rX   r.   r   rn   rc   r^   
startswithrp   add_rowprint_table)r   detailflopsop_flopstabler!   
param_names          r   _graph_flopsr      s   e\*****EH44455Eiikk  
7799666#B''HXE8,,Q/4466JJWWYY(""#B''HXEEWWYY+++#B''HXE3*//11JJWWYY,&&||HXEEWWYY!!),, 	'++HXEq==MM27799j(;<<< Lr   c                 B    t          |           }t          ||          S )N)r   )r   r   )r7   print_detailr   s      r   static_flopsr      s"    !!El3333r   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )r{   c                     || _         g | _        g | _        t          |          | _        |D ])}| j                            t          |                     *d S r	   )table_heads	table_lendatarQ   col_numrD   )r   r   heads      r   r   zTable.__init__   s_    &	;'' 	- 	-DN!!#d)),,,,	- 	-r   c                    t          |t                    st          d           t          |          | j        k    r't          dt          |           d| j                    t          | j                  D ]]}t          t          ||                             | j        |         k    r*t          t          ||                             | j        |<   ^| j        	                    |           d S )NzThe row_str should be a listzPThe length of row data should be equal the length of table heads, but the data: z is not equal table heads )
r
   listprintrQ   r   rangestrr   r   rD   )r   row_stris      r   r}   zTable.add_row   s    '4(( 	20111w<<4<'' Zcfgncoco  Z  Z  LP  LX  Z  Z   t|$$ 	9 	9A3wqz??##dnQ&777$'GAJ$8$8q!	!!!!!r   c                     d}t          | j                  D ]>}|dt          ||                                       | j        |         dz             z   z  }?|dz  }t          |           d S )Nrs   |r]   )r   r   r   centerr   r   )r   rowstringr   s       r   	print_rowzTable.print_row   so    t|$$ 	F 	FAcCAKK..t~a/@1/DEEEEFF#fr   c                 f    d}| j         D ]}|dz  }|d|dz   z  z  }|dz  }t          |           d S )Nrs   +-r]   )r   r   )r   r   lengths      r   print_shelfzTable.print_shelf   sN    n 	) 	)FcMFcVaZ((FF#fr   c                     |                                   |                     | j                   |                                   | j        D ]}|                     |           |                                   d S r	   )r   r   r   r   )r   r   s     r   r~   zTable.print_table   sy    t'(((I 	! 	!DNN4    r   N)r   r   r   r   r}   r   r   r~   r   r   r   r{   r{      s_        - - -
" 
" 
"        r   r{   )F)collectionsr   numpyrO   paddle.staticr   r   __all__r   r   r   rX   rZ   r^   rc   rn   rp   r   r   r{   r   r   r   <module>r      sx   $ # # # # #     + + + + + + + +
       (Q Q Q Q Q Q Q Q<0 0 0 0 0 0 0 0f    
    
 
 
     B4 4 4 4
* * * * * * * * * *r   