o
    &j>                     @   sH   d dl Z d dlZd dlZd dlZdZdd Zdd Zdd Zd	d
 Z	dS )    Nz1.0.0c                 C   s,   ||  d D ]}t ||| q||  dS )z
    Traversal a tree in post order.
    Args:
        root: current node of the tree.
        all_ops: used to index all nodes.
        post_order_results(list): used to store traversal results in place.
    children_nodeN)post_order_traverseappend)rootall_opspost_order_resultschild r	   i/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/visualdl/component/graph/graph_component.pyr      s   
r   c                 C   sN  | dks| dkr
d} | |vrui || < t  ||  d< | ||  d< tj|| d ||  d< i ||  d< t  ||  d< t  ||  d< tj||  d ||  d	< t  ||  d
< t  ||  d< d||  d< g ||  d< g ||  d< d||  d< | || d< ||  d | ||  | ||  ||  | dkrdS ttj| | || dS )a~  
    Create a path from leaf to root, e.g. /a/b/c -> /a/b -> /a -> /. If node in path not exists,         create one and fill information.
    Args:
        parent_node_name: name of parent node
        child_node_name: name of current node
        all_ops: used to store and index all nodes.
        general_children_dict: used to store all descendants for each non-leaf node.
    / r   name	show_nameattrsinput_nodesoutput_nodestype
input_varsoutput_varsparent_nodeedge_input_nodesedge_output_nodesFis_leaf_nodeN)setospathdirnamebasenameaddupdatecreate_non_leaf_nodes)Zparent_node_nameZchild_node_namer   general_children_dictr	   r	   r
   r    %   sB   

r    c                    sZ   | d }| d } fdd}|r'|r)|D ]}||kr!q||| qdS dS dS )a_  
    Construct path edges from var's from_node to to_nodes.
    Algorithm:
        1. Judge if src_node and dst_node have the same parent node, if yes, link them directly
        and fill information in all_edges, return.
        2. Find the closest common ancestor, repeat link node and its parent until reach the common ancestor.
        Every time construct a new edge, fill information in all_edges.
    Args:
        var_name: name of variable to process
        all_ops: used to index all nodes.
        all_vars:  used to index all variables.
        all_edges: used to store and index all edges
    	from_nodeto_nodesc                    sv  |  d | d kr-| |f vr | |hdd | |f< d S  | |f d   d S tj| |g}t| }| }	 | d }||krJn!||f vr]||hdd ||f< n ||f d   |}q?|}	 | d }||kryn!||f vr||hdd ||f< n ||f d   |}qn||f vr||hdd ||f< d S  ||f d   d S )Nr   r   )r"   to_nodevarslabelr%   )r   r   r   
commonpathpathlibPathas_posix)src_nodeZdst_nodeZcommon_ancestorZsrc_base_noder   Zdst_base_node	all_edgesr   var_namer	   r
   _construct_edgeb   sz   5.z(construct_edges.<locals>._construct_edgeNr	   )r.   r   all_varsr-   r"   r#   r/   r$   r	   r,   r
   construct_edgesQ   s   ;r1   c           )      C   s
  zddl m} W n ty   td Y nw |j}|jd|jd|jd|jd|j	d|j
d	|jd
|jd|jd|jd|jd|jdi}|j}ddlm} || }i }i }i }	tt}
tt}t| D ]}|dkriqb||}| D ]}z\| }i ||< ||| d< | || d< t| || d< t| || d< g || d< |  || d< i }|! D ]	}|"|||< q||| d< d|| d< g || d< W qr ty/   | }i ||< ||| d< d|| d< t| || d< d|| d< g || d< |  || d< i }|! D ]
}|"|||< q||| d< d|| d< g || d< Y qrw qbt| D ]}|dkrAq7||}t|# D ]}|$|}|"d|t|  }i ||< ||| d< t%&dd||| d< t| || d< i || d< d|| d < |' ( D ]#\}}||| d |< |
| )| |D ]}|| d *| qqi || d!< |+ ( D ] \}}||| d! |< || )| |D ]	}||| d< qאqi }i }|! D ]8}z+|d"krW q|"|||< |,|}||v r|| nt|-d#d$ ||< W q ty#   Y qw ||| d< ||| d%< g || d&< g || d'< g || d(< g || d)< g || d*< qLq7|D ]5}|| d dkraqT|| d }|| d D ]}||kr|| d( *| || d' *| qmqTtt.}t|/ }|D ]}t0t1j23|||| qg }t4d+|| |D ]}|| } t| d& | d&< | d& rm| d& D ]q}!||! d' D ]}"|"|| v rڐq| d' 5|" q||! d( D ]}#|#|| v rq| d( 5|# q|
|! D ]}$||$ d || vr| d 5|$ q||! D ]}%||% d D ]}||| vr3| d! 5|% q#qqt| d' | d'< t| d( | d(< t| d |
|< t| d! ||< d,t| d i| d< d-t| d! i| d!< q|/ D ]
}t6||||	 qs|	/ D ]m\}&}'||& d* *|' ||' d) *|& t|	|&|'f d. |	|&|'f d.< t7|	|&|'f d. d$krtt7|	|&|'f d. d/ |	|&|'f d0< qt7|	|&|'f d. d$krt||	|&|'f d. d  d |	|&|'f d0< qt8t|9 t|9 t|	9 d1}(|(S )2Nr   )corezPaddlepaddle is required to use add_graph interface.
              Please refer to               https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html              to install paddlepaddle.INTINTSLONGLONGSFLOATFLOATSSTRINGSTRINGSBOOLBOOLSBLOCKBLOCKS)generater   shaper   dtypevaluepersistabler   r   r"   r#   Zop_namescopez\[(\w|\.)*\]r   r   Tr   r   Z	sub_block.   Z
attr_typesr   r   r   r   r   r   XYr%   z tensorsr&   )versionnodesr%   edges):Zpaddle.frameworkr2   	ExceptionprintAttrTyper3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   ProgramDescZpaddle.utils.unique_namer?   collectionsdefaultdictlistrangeZ
num_blocksblockr0   r   r@   strr   rA   rC   Z
attr_namesattrZop_sizeopresubZinputsitemsextendr   Zoutputs	attr_typesplitr   keysr    r   r   r   r   r   r1   len_graph_versionvalues))Zmodel_pbr2   rM   Zattr_type_namerN   r?   Zprogram_descr   r0   r-   Zop_inputvars_dictZop_outputvars_dictiZ
block_descZvar_descr.   Z	attr_dict	attr_namejZop_descZop_nameZ
input_nameZvariable_listZvariable_nameZoutput_nameZattr_type_dictr[   Zfrom_node_nameZto_node_namer!   Zall_op_namesr   rV   Zchild_opZ
input_nodeZoutput_nodeZ	input_varZ
output_varr+   r$   Z
final_datar	   r	   r
   analyse_model   s  


"







/





rd   )
rO   Zos.pathr   r(   rW   r_   r   r    r1   rd   r	   r	   r	   r
   <module>   s   ,S