o
    &jQ                     @   sz   d dl mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lm	Z	 g d
Z
G dd dZdS )    )defaultdict   )get_device_nodes)intersection_ranges)merge_ranges)merge_self_ranges)rebuild_node_trees)
sum_ranges)traverse_tree)Z	allreduce	broadcastZrpcc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	DistributedParserz
    Analysis communication and computation time range, and their overlap.
    The computation time is all kernel except kernels for communication like nccl.
    c                 C   s4   t dd | _t dd | _t dd | _i | _d S )Nc                   S      t tS Nr   list r   r   v/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/visualdl/component/profiler/parser/distributed_parser.py<lambda>#       z,DistributedParser.__init__.<locals>.<lambda>c                   S   r   r   )r   intr   r   r   r   r   $   r   c                   S   r   r   )r   floatr   r   r   r   r   %   r   )r   
steps_datacalls
steps_timeprofile_steps_time)selfr   r   r   __init__"   s   
zDistributedParser.__init__c                 C   s  d}t |}t|}d}| D ](\}}|dd D ]}|jdkr3|dkr-||j|j 7 }| | qq|d7 }qtdd }|| jd< | j	 D ]\}	}
t
|
d	 | j|	 d	< t
|
d
 | j|	 d
< t|
d	 dd||	 d	< t|
d
 dd||	 d
< t||	 d	 ||	 d
 dd||	 d< t|
d dd||	 d< t||	 d ||	 d dd||	 d< t||	 d | j|	 d< t||	 d | j|	 d< t||	 d | j|	 d< | j|	 | j|	 d  | j|	 d  | j|	 d  | j|	 d< qI|| _	dS )zH
        Collect all communication and computation time ranges.
        g        r   r   NZProfileStepc                   S   r   r   r   r   r   r   r   r   :   r   z)DistributedParser.parse.<locals>.<lambda>Allcpu_communication_rangegpu_communication_rangeF)Z	is_sortedTZcommunication_rangecomputation_rangeZoverlap_rangeZcommunication_timeZcomputation_timeZoverlap_timeZothers_time)r   r
   itemstypeend_nsstart_ns_parse_stepr   r   r   lenr   r   r   r   r	   r   )r   Z	nodetrees
total_timeZthread2hostnodesZthread_countZthreadidZ	hostnodeshostnodeZnew_steps_datastepZ	step_datar   r   r   parse(   s   











zDistributedParser.parsec                    s^  |j dd }|j|j | j|< g }g }|| |r2| }|| |jD ]}|| q(|s|D ]  jdkr| j	| d  j jf | j	d d  j jf t
 }|D ]#}|jdkr| j	| d |j|jf | j	d d |j|jf q]q4 jdkrt fd	d
tD r| j	| d  j jf | j	d d  j jf t
 }|D ]#}|jdkr| j	| d |j|jf | j	d d |j|jf qq4 jD ]O}	|	jD ]I}
|
jdkr*d|
j  v r| j	| d |
j|
jf | j	d d |
j|
jf q| j	| d |
j|
jf | j	d d |
j|
jf qqq4d S )N#r   ZCommunicationr   r   ZKernelr   Operatorc                    s   g | ]	}| j  v qS r   )namelower).0r-   r(   r   r   
<listcomp>{   s    z1DistributedParser._parse_step.<locals>.<listcomp>Zncclr    )r-   splitr#   r$   r   appendpopZchildren_noder"   r   r   any_CommunicationOpNameZruntime_nodedevice_noder.   )r   Zprofile_step_noder)   nodesstackZcurrent_nodeZ	childnodeZdevice_nodesr7   ZruntimenodeZ
devicenoder   r0   r   r%   ^   s   

















zDistributedParser._parse_stepN)__name__
__module____qualname____doc__r   r*   r%   r   r   r   r   r      s
    6r   N)collectionsr   utilsr   r   r   r   r   r	   r
   r6   r   r   r   r   r   <module>   s   