o
    &j]                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZddlmZ edZ	edZ
h dZh dZh dZG d	d
 d
ZG dd dZG dd dZG dd dZdd ZdS )    N   )traverse_treez(.+)(\[.+\])z\w+(\(.+\))>   ZForwardZBackwardCudaRuntimeZPythonUserDefinedZPythonOpZCommunicationUserDefinedZOperatorInnerZProfileStepOperatorZ
DataloaderZOptimization>   ZMemcpyZKernelZMemset>   ZAllocateZReservedFreeZFreeZReservedAllocatec                   @   ,   e Zd Zdd Zedd Zedd ZdS )HostNodec                 C   sX   d | _ d | _d| _d| _d| _d| _d| _i | _i | _d| _	g | _
g | _g | _g | _d S )Nr    )nametypestart_nsend_ns
process_id	thread_idcorrelation_idinput_shapesdtypes	callstackchildren_noderuntime_nodedevice_nodemem_nodeself r   n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/visualdl/component/profiler/parser/event_node.py__init__)   s   
zHostNode.__init__c                 C   s:  |  }|d  t|d dd|_|d |_tt|d d dd d	 |_	tt|d d
 dd d	 |_
|d |_|d  t|d dd|_d|d v r_|d d nd|_d|d v rn|d d ni |_d|d v r}|d d ni |_d|d v r|d d nd|_g |_g |_g |_g |_|S )Nr      r
   catargs
start_time r     end_timepidtidr   correlation idr	   r   Zinput_dtypesr   )replace_show_name_patternmatchgroupr   r   intfloatsplitr   r   r   _show_tid_patternr   r   r   r   r   r   r   r   r   clsZjson_objr   r   r   r   	from_json9   sX   

zHostNode.from_jsonc                 C   s   |  }|j |_ t|jdd |_|j|_|j|_|j|_|j|_|j|_|j	|_	|j
|_
|j|_g |_g |_g |_g |_|S N.r   )r   strr   r.   r   r   r   r   r   r   r   r   r   r   r   r   r1   objr   r   r   r   from_protobufT   s    zHostNode.from_protobufN__name__
__module____qualname__r   classmethodr2   r8   r   r   r   r   r   (   s    
r   c                   @   r   )MemNodec                 C   sF   d | _ d| _d| _d| _d| _d| _d | _d| _d| _d| _	d| _
d S )Nr   )r   timestamp_nsaddrr   r   increase_bytesplacecurrent_allocatedcurrent_reservedpeak_allocatedpeak_reservedr   r   r   r   r   i   s   
zMemNode.__init__c                 C   s.  |  }|d |_ |d d |_d|d v rtt|d d nd|_|d |_|d t|d 	d	d
|_
d|d v rD|d d nd|_d|d v rS|d d nd|_d|d v rb|d d nd|_d|d v rq|d d nd|_d|d v r|d d nd|_d|d v r|d d |_|S d|_|S )Nr   tsr#   r@   r    r   r%   r&   r   r
   rA   rB   z
Place(cpu)rC   rD   rE   rF   )r   r?   hexr,   r@   r   r(   r/   r*   r+   r   rA   rB   rC   rD   rE   rF   r0   r   r   r   r2   v   sh   


zMemNode.from_jsonc                 C   sx   |  }t |jdd |_|j|_tt|j|_|j|_|j|_|j	|_	|j
|_
|j|_|j|_|j|_|j|_|S r3   )r5   r   r.   r?   rH   r,   r@   r   r   rA   rB   rC   rD   rE   rF   r6   r   r   r   r8      s   zMemNode.from_protobufNr9   r   r   r   r   r>   h   s    
r>   c                   @   r   )
DeviceNodec                 C   s   d | _ d | _d| _d| _d| _d| _d| _d| _g d\| _| _	| _
g d\| _| _| _d| _d| _d| _d| _d| _d| _d| _d S )Nr   r   r   r   )r   r   r   r   	device_id	stream_id
context_idr   block_xblock_yblock_zgrid_xgrid_ygrid_zshared_memoryregisters_per_thread	num_bytesvalue	occupancyblocks_per_smwarps_per_smr   r   r   r   r      s"   
zDeviceNode.__init__c                 C   s  |  }|d  t|d dd|_|d |_tt|d d dd d	 |_	tt|d d
 dd d	 |_
|d |_|d |_d|d v rS|d d nd|_|d d |_d|d v ri|d d ng d\|_|_|_d|d v r|d d ng d\|_|_|_d|d v r|d d nd|_d|d v r|d d nd|_d|d v r|d d nd|_d|d v r|d d nd|_d|d v r|d d nd|_d|d v r|d d nd|_d|d v r|d d |_|S d|_|S )Nr   r   r
   r   r    r!   r"   r   r#   r$   r%   r&   contextr'   blockrJ   gridzshared memoryzregisters per threadbytesrW   z theoretical achieved occupancy %zblocks per SMzwarps per SM)r(   r)   r*   r+   r   r   r,   r-   r.   r   r   rK   rL   rM   r   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r0   r   r   r   r2      s   


zDeviceNode.from_jsonc                 C   s   |  }|j |_ t|jdd |_|j|_|j|_|j|_|j|_|j|_|j	|_	|j
|j|jg\|_
|_|_|j|j|jg\|_|_|_|j|_|j|_|j|_|j|_|jd |_|j|_|j|_|S )Nr4   r   d   )r   r5   r   r.   r   r   rK   rL   rM   r   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r6   r   r   r   r8      s*   zDeviceNode.from_protobufNr9   r   r   r   r   rI      s    
$rI   c                   @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )ProfilerResultc                 C   s   d | _ d | _d | _d | _d | _d| _d| _d| _d | _t	|t
r*| | || _d S | | td}||jd |d t| | _W d    d S 1 sTw   Y  d S )NTrjsonr   )device_infosspan_idxdata
extra_infoschema_versionhas_hostnodeshas_devicenodeshas_memnodesstart_in_timeline_ns
isinstancedict
parse_jsoncontentparse_protobuftempfileNamedTemporaryFilesaver   seekrb   loadsread)r   re   fpr   r   r   r      s$   




"zProfilerResult.__init__c                 C   s  |d | _ |d | _zdd |d D | _W n ty&   td i | _Y nw g }g }g }g }|d D ]b}|rC|d d	krD|d d
krDq3|d tv rx|d dksV|d dkr_|t| q3|t| |d j	dkrwt
|d d | _q3|d tv r|t| q3|d tv r|t| q3|r|D ]}| jd usJ |j| j |_q|sd| _|sd| _|sd| _| ||||| _|d | _d S )NZschemaVersionZ	span_indxc                 S   s   i | ]}|d  |qS )idr   ).0Zdevice_infor   r   r   
<dictcomp>  s    z-ProfilerResult.parse_json.<locals>.<dictcomp>ZdevicePropertiesBpaddlepaddle-gpu version is needed to get GPU device informations.ZtraceEventsphXir   r   Z
MluRuntimer	   r   rG   r#   FZ	ExtraInfo)rg   rd   rc   	Exceptionprinthost_node_type_mapappendr   r2   r   r,   rk   device_node_type_maprI   memory_node_event_mapr>   r?   rh   ri   rj   
build_treere   rf   )r   Z	json_data	hostnodesruntimenodesdevicenodesmemnodeseventmemnoder   r   r   rn   	  sb   



zProfilerResult.parse_jsonc                 C   s  |  | _t| | _zdd |  D | _W n ty*   t	d i | _Y nw |
 | _td| _d| _d| _d| _| }i }| D ]}\}}g }g }t|}|||< || || |r| }	| }
|	jD ]}| jdu rzd| _t|}|
j| || || qp|	jD ]}t|}|
j| |jD ]}t|}|j| qq|	jD ]}t|}|
j| q|seqHt|}| D ]\}}|dd  D ]}|j| jk r|j| _qq| D ]o\}}|D ]h}||d kr	| j| j8  _| j| j8  _|jD ]3}| j| j8  _| j| j8  _|jD ]}| jdu r,d| _| j| j8  _| j| j8  _q!q|jD ]}| jdu rNd| _| j| j8  _qCqq|| _ d S )	Nc                 S   s(   i | ]\}}||j |j|j|jd qS ))r   ZtotalGlobalMemZcomputeMajorZcomputeMinor)r   Ztotal_memorymajorminor)ry   rK   Zdevice_propertyr   r   r   rz   <  s    z1ProfilerResult.parse_protobuf.<locals>.<dictcomp>r{   infFTr   r   )!get_versionrg   r5   Zget_span_indxrd   Zget_device_propertyitemsrc   r   r   get_extra_inforf   r-   rk   rh   ri   rj   get_datar   r8   r   popr   r   r   rI   r   r>   r   r   r   r?   re   )r   Zprotobuf_dataZ
node_treesZnew_node_treesthreadidrootstack	new_stacknew_rootcurrent_nodeZnew_current_node
child_nodeZnew_child_noder   Znew_runtime_noder   Znew_device_noder   Znew_mem_nodeZnew_node_tree_listZnode_tree_listnoderuntimenoder   r   r   rp   8  s   






















zProfilerResult.parse_protobufc                 C   s|  t t}t t}t t}i }i }	t }
|D ]}||j | |
|j q|D ]}||j | |
|j |||j< q+|D ]}|j|vrKqC||j }|j| qC|D ]
}||j | qYdd }dd }|	 D ]\}}t
|t|d||< qp|	 D ]\}}t
|t|d||< q|	 D ]\}}t
|t|d||< q|
D ]}| || || || |	|< q|	S )Nc                 S   s0   | j |j k rdS | j |j kr| j|jkrdS dS Nr	   r   )r   r   )Z	hostnode1Z	hostnode2r   r   r   compare_hostnode_func  s   z8ProfilerResult.build_tree.<locals>.compare_hostnode_funcc                 S   s   | j |j krdS dS r   )r?   )Zmemnode1Zmemnode2r   r   r   compare_memnode_func  s   z7ProfilerResult.build_tree.<locals>.compare_memnode_func)key)collectionsdefaultdictlistsetr   r   addr   r   r   sorted	functools
cmp_to_key_build_tree_relationship)r   r   r   r   r   Zthread2host_event_nodesZthread2runtime_event_nodesZthread2mem_event_nodesZ!correlation_id2runtime_event_nodeZthread_event_treesZ
thread_idsZhostnoder   Z
devicenoder   r   r   r   r   r   r   r     s`   








zProfilerResult.build_treec                 C   s  t  }dddtjf\|_|_|_|_g }|| |D ]Y}	 |d }|j|jk r4|j| || n@|	  d}d}	t
|}
t|D ]'\}}|j|jkra|j|jkra|sZ|}	d}|j| qD|j|jkrk|}
 nqD|rs||	|
= qq|r|	 }d}	t
|}
d}t|D ]'\}}|j|jkr|j|jkr|s|}	d}|j| q|j|jkr|}
 nq|r||	|
= |swg }g }g }|| |d |r|	 }|	 }|dkr|| |d |jd d d D ]}|| |d qn|| |s|D ]D}d}d}	t
|}
t|D ]-\}}|j|jkr.|j|jkr.|j| |s,|}	d}q|j|jkr9|}
 nq|rC||	|
= q|S )Nz	root noder   r   Tr	   Fr   )r   sysmaxsizer   r   r   r   r   r   r   len	enumerater   r?   r   )r   Zhost_event_nodesZruntime_event_nodesZmem_event_nodesZ	root_nodeZ
node_stackZ	host_nodeZstack_top_nodeZhasenterZfirstpositionZlastpositionr~   r   r   Z
flag_stackZpost_order_nodesr   flagchildr   r   r   r   r   r     s   










z'ProfilerResult._build_tree_relationshipc                 C      | j S N)re   r   r   r   r   r   -     zProfilerResult.get_datac                 C   r   r   )rf   r   r   r   r   r   0  r   zProfilerResult.get_extra_infoc                 C   r   r   )rg   r   r   r   r   get_schema_version3  r   z!ProfilerResult.get_schema_versionc                 C   r   r   )rc   r   r   r   r   get_device_infos6  r   zProfilerResult.get_device_infosc                 C   r   r   )rd   r   r   r   r   get_span_idx9  r   zProfilerResult.get_span_idxc                 C   r   r   )ri   r   r   r   r   
has_device<  r   zProfilerResult.has_devicec                 C   r   r   )rh   r   r   r   r   has_host?  r   zProfilerResult.has_hostc                 C   r   r   )rj   r   r   r   r   
has_memoryB  r   zProfilerResult.has_memoryc                 C   s   d S r   r   )r   pathformatr   r   r   rs   E  s   zProfilerResult.saveN)r:   r;   r<   r   rn   rp   r   r   r   r   r   r   r   r   r   r   rs   r   r   r   r   r`      s    /LEdr`   c                 C   s   t t| d}t|S )Nra   )rb   loadopenr`   )	file_namero   r   r   r   load_profiler_jsonI  s   r   )r   r   rb   rer   rq   utilsr   compiler)   r/   r   r   r   r   r>   rI   r`   r   r   r   r   r   <module>   s&   

@8T  W