o
    Ne                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZm	Z	 g Z
G dd deZG dd deZG dd	 d	eZd
d Zdd Zdd Zdd Zdd Zdd ZdddZdddZG dd deZdS )    N)OrderedDict)Programprogram_guardVariablec                   @   s$   e Zd Zdd Zdd Zdd ZdS )
VarWrapperc                 C   s,   t |tsJ t |tsJ || _|| _d S N)
isinstancer   GraphWrapper_var_graph)selfvargraph r   HD:\Projects\ConvertPro\env\Lib\site-packages\paddle/hapi/static_flops.py__init__   s   
zVarWrapper.__init__c                 C      | j jS )z/
        Get the name of the variable.
        )r
   namer   r   r   r   r          zVarWrapper.namec                 C   r   )z0
        Get the shape of the varibale.
        )r
   shaper   r   r   r   r   &   r   zVarWrapper.shapeN)__name__
__module____qualname__r   r   r   r   r   r   r   r      s    r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
	OpWrapperc                 C   s   t |tsJ || _|| _d S r   )r   r	   _opr   )r   opr   r   r   r   r   /   s   
zOpWrapper.__init__c                 C   r   )z0
        Get the type of this operator.
        )r   typer   r   r   r   r   4   r   zOpWrapper.typec                    s*   | j jv r fdd j |D S g S )z:
        Get all the varibales by the input name.
        c                       g | ]} j |qS r   r   r   .0var_namer   r   r   
<listcomp>?   s    z$OpWrapper.inputs.<locals>.<listcomp>)r   Zinput_namesinputr   r   r   r   r   inputs:   s
   

zOpWrapper.inputsc                    s    fdd j |D S )z;
        Get all the varibales by the output name.
        c                    r   r   r   r    r   r   r   r#   I   s    z%OpWrapper.outputs.<locals>.<listcomp>)r   outputr%   r   r   r   outputsE   s   zOpWrapper.outputsN)r   r   r   r   r   r&   r(   r   r   r   r   r   -   s
    r   c                       s8   e Zd ZdZdg g f fdd	Zdd Zdd Z  ZS )	r	   aP  
    It is a wrapper of paddle.fluid.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                    s   t t|   |du rt n|| _i | _i | _| j D ]}|jr'|| j|j	< qd| _
|du r1g n|}|du r9g n|}t|| _t|| _t | _dS )z	
        N)superr	   r   r   programZpersistablesZteacher_persistablesZ	list_varsZpersistabler   Zcompiled_graphr   in_nodes	out_nodesZ_attrs)r   r*   r+   r,   r   	__class__r   r   r   [   s   

zGraphWrapper.__init__c                 C   s2   g }| j jD ]}|jD ]
}|t||  qq|S )zK
        Return all operator nodes included in the graph as a set.
        )r*   blocksopsappendr   )r   r0   blockr   r   r   r   r0   l   s   
zGraphWrapper.opsc                 C   s0   | j jD ]}||rt|||   S qdS )z4
        Get the variable by variable name.
        N)r*   r/   Zhas_varr   r   )r   r   r2   r   r   r   r   v   s
   
zGraphWrapper.var)r   r   r   __doc__r   r0   r   __classcell__r   r   r-   r   r	   L   s
    
r	   c                 C   sv   |  dd  }t|dd  }t|  ddkrdnd}t| dd  dd  }|||  }t|}|S )NFilterr      ZBiasOutput)r&   r   npproductlenr(   abs)r   Zfilter_shapeZ
filter_opsZbias_opsoutput_numel	total_opsr   r   r   count_convNd   s    r>   c                 C   s$   t | dd  dd  }|S )Nr7   r   r6   )r8   r9   r(   r   )r   r=   r   r   r   count_leaky_relu   s    r?   c                 C   s4   t | dd  dd  }d| }t|}|S )NYr   r6      )r8   r9   r(   r   r;   )r   r<   r=   r   r   r   count_bn   s    rB   c                 C   sJ   |  dd  d }t| dd  dd  }|| }t|}|S )Nr@   r   Outr6   )r&   r   r8   r9   r(   r;   )r   Z	total_mulZnumelr=   r   r   r   count_linear   s
    rD   c           	      C   s   |  dd  }| dd  }t|dd  t|dd   }t|}d}|| }t|dd  }|| }t|}|S )NXr   rC   rA   r6   )r&   r   r(   r8   arrayr9   r;   )	r   input_shapeZoutput_shapeZkernelZ	total_addZ	total_divZ
kernel_opsZnum_elementsr=   r   r   r   count_pool2d   s   $
rH   c                 C   s0   |  dd  }t|dd  }t|}|S )NrE   r   r6   )r&   r   r8   r9   r;   )r   rG   r=   r   r   r   count_element_op   s   rI   Fc                 C   s  t | tsJ d}d}tg d}|  D ]q}d}| dv r1t|}||7 }|dd  }nE| dkr@t|}||7 }n6| dv rXt	|}||7 }|dd  }n| d	krgt
|}||7 }n| d
rvt|}||7 }|dkr|| ||g d}q|r|  |S )Nr   )zOP Typez
Param nameZFlops )Zconv2dZdepthwise_conv2dr5   Zpool2d)mulmatmulr@   Z
batch_normelement)r   r	   Tabler0   r   r>   r&   r   rH   rD   rB   
startswithrI   add_rowprint_table)r   detailZflopsZop_flopstabler   
param_namer   r   r   _graph_flops   s:   

rU   c                 C   s   t | }t||dS )N)rR   )r	   rU   )r*   Zprint_detailr   r   r   r   static_flops   s   rV   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )rN   c                 C   s:   || _ g | _g | _t|| _|D ]
}| jt| qd S r   )table_heads	table_lendatar:   col_numr1   )r   rW   headr   r   r   r      s   
zTable.__init__c                 C   s   t |ts	td t|| jkrtdt|| j t| jD ]}tt|| | j| kr:tt|| | j|< q | j	
| d S )NzThe row_str should be a listznThe length of row data should be equal the length of table heads, but the data: {} is not equal table heads {})r   listprintr:   rZ   formatrangestrrX   rY   r1   )r   Zrow_strir   r   r   rP      s   
zTable.add_rowc                 C   sL   d}t | jD ]}|dt|| | j| d  7 }q|d7 }t| d S )NrJ   |rA   )r_   rZ   r`   centerrX   r]   )r   rowstringra   r   r   r   	print_row   s
   &zTable.print_rowc                 C   s<   d}| j D ]}|d7 }|d|d  7 }q|d7 }t| d S )NrJ   +-rA   )rX   r]   )r   re   lengthr   r   r   print_shelf   s   
zTable.print_shelfc                 C   s>   |    | | j |    | jD ]}| | q|    d S r   )rj   rf   rW   rY   )r   rY   r   r   r   rQ      s   
zTable.print_tableN)r   r   r   r   rP   rf   rj   rQ   r   r   r   r   rN      s    rN   )F)copynumpyr8   Zpaddlecollectionsr   Zpaddle.staticr   r   r   __all__objectr   r   r	   r>   r?   rB   rD   rH   rI   rU   rV   rN   r   r   r   r   <module>   s$   4


!