o
    Ne                     @   s6   d dl mZ d dlmZ ddlmZ dgZdd ZdS )    )print_function)OrderedDict   )Programop_freq_statisticc                 C   s  t | tstdt|  t }t }t }dd | jd  D }|  jD ],}d}|j	D ]$}||v r5q.|sI|
|jrI||j  d7  < d}q.|sRd||j< d}q.q'i }|  jD ]]}|jD ]=}||v rgq`|
|rt|| dksvJ |
|jr||j || d  q`|| d g||j< q`td	||jf  q`|j	D ]}|
|r|| |j q|jg||< qq[| D ]!\}}	|	D ]}
|
d
 | }|
|r||  d7  < qd||< qqt| dd dd}t| dd dd}||fS )ac  
    Statistics of Op frequency.

    Args:
        program(Program): The current Program.

    Returns:
        uni_op_freq(dict): the single op frequency.
        adj_2_op_freq(dict): the two adjacent ops frequency.

    Examples:

        >>> import paddle.fluid as fluid
        >>> uni_op_freq, adj_2_op_freq = fluid.contrib.op_freq_statistic(
        >>>        fluid.default_main_program())
        >>> for op_type, op_num in uni_op_freq:
        >>>     print("%s  	  %d" % (op_type, op_num))
        >>> for op_type, op_num in adj_2_op_freq:
        >>>     print("%s  	  %d" % (op_type, op_num))

    z5The input type should be Porgram.But you passed in %sc                 S   s   g | ]}|j qS  )name).0pr   r   QD:\Projects\ConvertPro\env\Lib\site-packages\paddle/fluid/contrib/op_frequence.py
<listcomp>6   s    z%op_freq_statistic.<locals>.<listcomp>r   F   Tz%Var's generate op is not found,%s, %sz->c                 S      | d S Nr   r   itemr   r   r   <lambda>d       z#op_freq_statistic.<locals>.<lambda>)keyreversec                 S   r   r   r   r   r   r   r   r   g   r   )
isinstancer   	TypeErrortyper   blocksZall_parametersZglobal_blockopsZoutput_arg_namesZhas_keyZinput_arg_nameslenappendprint	iteritemssorteditems)programZuni_op_freqZadj_2_op_freqZ	op_in_ops
parametersopZhad_recordedvar_nameZ
var_gen_opZin_opsZin_opZop_opr   r   r   r      sn   








N)
__future__r   collectionsr   Z	frameworkr   __all__r   r   r   r   r   <module>   s
   