o
    &jF                     @   sH   d dl Z ddlmZ d dlmZ d dlmZ G dd deZdd	 ZdS )
    N   )ProfilerReader)
gen_result)resultc                   @   s@  e Zd Zdd Ze dd Ze dd Ze dd Ze d	d
 Ze dd Z	e dd Z
dd Ze dd Ze dNddZe 	dNddZe 	dNddZe dNddZe dNddZe dNddZe d d! Ze 	dNd"d#Ze 	dNd$d%Ze dNd&d'Ze 	dNd(d)Ze dNd*d+Ze d,d- Ze d.d/ Ze dNd0d1Zed2d3id4d5d6 Ze d7d8 Zed2d3id4dNd9d:Zed2d3id4d;ed<d=dfd>d?Z ed2d3id4	=dOd@dAZ!e dBdC Z"e dDdE Z#e dFdG Z$e dHdI Z%e dJdK Z&e dLdM Z'd=S )PProfilerApic                 C   s   t || _d S N)r   _reader)selflogdir r   l/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/visualdl/component/profiler/profiler_server.py__init__   s   zProfilerApi.__init__c                 C   s
   | j  S r   )r   runsr	   r   r   r   r      s   
zProfilerApi.runsc                 C   s$   | j |}|d u rg S t| S r   )r   get_run_managerlistZ	get_views)r	   runrun_managerr   r   r   views   s   zProfilerApi.viewsc                 C   s0   |dkrdgS | j |}|d u rg S ||S )NZDistributedAll)r   r   get_workers)r	   r   viewr   r   r   r   workers%   s   
zProfilerApi.workersc                 C   s2   | j |}|d u rg S |dkr| S ||S )Nr   )r   r   Zget_distributed_spansZ	get_spans)r	   r   workerr   r   r   r   spans.   s   
zProfilerApi.spansc                 C   s   g dS )N)nsusmssr   r   r   r   r   	timeunits7   s   zProfilerApi.timeunitsc                 C   s(   |dks|d u r
d}|  }| j|S )N	undefinedzh)lowerr   Zget_descriptions)r	   langr   r   r   descriptions;   s   zProfilerApi.descriptionsc                 C   s   t  }|| jjdd |S )zE
        Get all component tabs supported by readers in Api.
        T)update)setr%   r   component_tabs)r	   tabsr   r   r   r'   B   s   zProfilerApi.component_tabsc                 C   sV   | j |}|d u rg S t|}|||}|r)| }t|d}||d< |S d S )NZOverviewnum_workers)r   r   strget_profiler_dataZget_device_infoslenr   )r	   r   r   spanr   profiler_datar   r)   r   r   r   overview_environmentJ   s   z ProfilerApi.overview_environmentr   c                 C   6   | j |}|d u rg S |||}|r||S d S r   )r   r   r+   Zget_model_perspectiver	   r   r   r-   	time_unitr   r.   r   r   r   model_perspectiveW      
zProfilerApi.model_perspectivec                 C   @   |  }| j|}|d u rg S |||}|r|||S d S r   )r"   r   r   r+   Zget_model_perspective_perstepr	   r   r   r-   device_typer2   r   r.   r   r   r   model_perspective_perstep`      z%ProfilerApi.model_perspective_perstepc                 C   r5   r   )r"   r   r   r+   Zget_event_type_perspectiver6   r   r   r   event_type_perspectivep   r9   z"ProfilerApi.event_type_perspectivec                 C   r0   r   )r   r   r+   Z get_event_type_model_perspectiver1   r   r   r   event_type_model_perspective   r4   z(ProfilerApi.event_type_model_perspectivec                 C   r0   r   )r   r   r+   Zget_userdefined_perspectiver1   r   r   r   userdefined_perspective   r4   z#ProfilerApi.userdefined_perspectivec                 C   @   | j |}|d u rg S |||}t|}|r|||S d S r   )r   r   r+   intZget_operator_pier	   r   r   r-   topkr2   r   r.   r   r   r   operator_pie      zProfilerApi.operator_piec           	      C   sJ   |  }| j|}|d u rg S |||}t|}|r#||||S d S r   )r"   r   r   r+   r>   Zget_operator_pie_expand)	r	   r   r   r-   r@   r7   r2   r   r.   r   r   r   operator_pie_expand   s   zProfilerApi.operator_pie_expandc           	      C   :   | j |}|d u rg S |||}|r||||S d S r   )r   r   r+   Zget_operator_table	r	   r   r   r-   group_bysearch_namer2   r   r.   r   r   r   operator_table      zProfilerApi.operator_tablec                 C      d S r   r   )r	   r   r   r-   op_namerF   Zinput_shaper2   r   r   r   operator_stack_table   s   	z ProfilerApi.operator_stack_tablec                 C   r=   r   )r   r   r+   r>   Zget_kernel_pier?   r   r   r   
kernel_pie   rB   zProfilerApi.kernel_piec           	      C   rD   r   )r   r   r+   Zget_kernel_tablerE   r   r   r   kernel_table   rI   zProfilerApi.kernel_tablec                 C   s@   | j |}|d u rg S |||}|rt|}|||S d S r   )r   r   r+   r>   Zget_kernel_tc_pier?   r   r   r   kernel_tc_pie   s   zProfilerApi.kernel_tc_piec                 C   6   | j |}|d u rg S ||}|d u rd S | S r   )r   r   get_distributed_profiler_dataZget_distributed_infor	   r   r   r-   r   distributed_profiler_datar   r   r   distributed_info      zProfilerApi.distributed_infoc                 C   rP   r   )r   r   rQ   Zget_distributed_stepsrR   r   r   r   distributed_steps   rU   zProfilerApi.distributed_stepsc                 C   s:   | j |}|d u rg S ||}|d u rd S |||S r   )r   r   rQ   Zget_distributed_histogram)r	   r   r   r-   stepr2   r   rS   r   r   r   distributed_histogram   s   z!ProfilerApi.distributed_histogramzcontent-encodinggzip)headersc                 C   4   | j |}|d u rg S |||}|r| S d S r   )r   r   r+   Zget_trace_datar	   r   r   r-   r   r.   r   r   r   trace     zProfilerApi.tracec                 C   r[   r   )r   r   r+   Zget_memory_devicesr\   r   r   r   memory_devices  r^   zProfilerApi.memory_devicesc                 C   sD   |dkrd S | j |}|d u rg S |||}|r |||S d S Nr    )r   r   r+   Zget_memory_curver6   r   r   r   memory_curve  s   zProfilerApi.memory_curver   infNc	                 C   s   |dkrd S zt |}W n ty   d}Y nw zt |}W n ty+   t d}Y nw |dks2|s4d }| j|}	|	d u r@g S |	||}
|
rQ|
|||||S d S )Nr    r   rb   )float	Exceptionr   r   r+   Zget_memory_events)r	   r   r   r-   r7   min_sizemax_sizerG   r2   r   r.   r   r   r   memory_events(  s.   

zProfilerApi.memory_eventsc                 C   sT   |dks|sd }|dkrd S | j |}|d u rg S |||}|r(|||S d S r`   )r   r   r+   Zget_op_memory_events)r	   r   r   r-   r7   rG   r   r.   r   r   r   op_memory_eventsF  s   zProfilerApi.op_memory_eventsc                 C   rJ   r   r   r	   base_runbase_worker	base_spanexp_run
exp_workerexp_spanr   r   r   comparison_phaseX     zProfilerApi.comparison_phasec                 C   rJ   r   r   ri   r   r   r   comparison_phase_diff]  rq   z!ProfilerApi.comparison_phase_diffc                 C   rJ   r   r   ri   r   r   r   comparison_phase_tableb  rq   z"ProfilerApi.comparison_phase_tablec                 C   rJ   r   r   r	   rj   rk   rl   rm   rn   ro   
phase_namer   r   r   comparison_phase_innerg  rq   z"ProfilerApi.comparison_phase_innerc                 C   rJ   r   r   rt   r   r   r   comparison_phase_diff_innerl  rq   z'ProfilerApi.comparison_phase_diff_innerc                 C   rJ   r   r   rt   r   r   r   comparison_phase_table_innerq  s   z(ProfilerApi.comparison_phase_table_inner)r   r   )(__name__
__module____qualname__r   r   r   r   r   r   r   r$   r'   r/   r3   r8   r:   r;   r<   rA   rC   rH   rL   rM   rN   rO   rT   rV   rX   r]   r_   ra   rc   rg   rh   rp   rr   rs   rv   rw   rx   r   r   r   r   r      s    






	

		











r   c                    s6  t | }i d|jg fd|jdgfd|jddgfd|jddgfd|jg fd	|jd
gfd|jg dfd|jg dfd|j	g dfd|j
g dfd|jg dfd|jg dfd|jg dfd|jg dfd|jg dfd|jg dfd|jg dfi d|jg dfd|jg dfd|jg dfd |jg dfd!|jg d"fd#|jg dfd$|jg dfd%|jg dfd&|jg d'fd(|jg d)fd*|jg d+fd,|jg d+fd-|jg d+fd.|jg d/fd0|j g d/fd1|j!g d/fd2|j"g f d3t#f fd4d5}|S )6Nr   r   r   r   r   r   r   r   r$   r#   zoverview/environment)r   r   r-   zoverview/model_perspective)r   r   r-   r2   z"overview/model_perspective_perstep)r   r   r-   r7   r2   zoverview/event_type_perspectivez%overview/event_type_model_perspectivez overview/userdefined_perspectivezoperator/pie)r   r   r-   r@   r2   zoperator/pie_expand)r   r   r-   r@   r7   r2   zoperator/table)r   r   r-   rF   rG   r2   zoperator/stack_table)r   r   r-   rK   rF   Ztime_unitinput_shapez
kernel/piezkernel/tensorcore_piezkernel/tablezdistributed/infozdistributed/stepszdistributed/histogram)r   r   r-   rW   r2   r]   zmemory/deviceszmemory/curvezmemory/memory_events)r   r   r-   r7   re   rf   rG   r2   zmemory/op_memory_events)r   r   r-   r7   rG   zcomparison/phase)rj   rk   rl   rm   rn   ro   zcomparison/phase_diffzcomparison/phase_tablezcomparison/phase_inner)rj   rk   rl   rm   rn   ro   ru   zcomparison/phase_diff_innerzcomparison/phase_table_innerr'   pathc                    sH    | }|sttddddd fS |\}} fdd|D }|| S )Nr   zapi not found)statusmsgzapplication/jsonc                    s   g | ]}  |qS r   )get).0nameargsr   r   
<listcomp>  s    z:create_profiler_api_call.<locals>.call.<locals>.<listcomp>)r   jsondumpsr   )r|   r   ZroutemethodZcall_arg_namesZ	call_argsZroutesr   r   call  s   
z&create_profiler_api_call.<locals>.call)$r   r   r   r   r   r   r$   r/   r3   r8   r:   r;   r<   rA   rC   rH   rL   rM   rO   rN   rT   rV   rX   r]   r_   ra   rg   rh   rp   rr   rs   rv   rw   rx   r'   r*   )r
   apir   r   r   r   create_profiler_api_callx  s   

	"$&)*,/0148;?CGKO
SV	r   )	r   Zprofiler_readerr   Zvisualdl.server.apir   r   objectr   r   r   r   r   r   <module>   s     d