
    ukic                    b   d dl m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	 	 d dl
mZ d dl
mZ dZdZd	Z ej$                  e
      Zej)                  ddde       ej)                  dde       ej)                  dde       ej)                  ddd       ej)                  ddde       ej)                  ddde       ej)                  ddde       ej)                  d dd!e       	 	 	 	 	 	 	 	 	 	 d'd"Zd# Zd$ Zd% Zed&k(  r eej9                                yy# e$ r	  ed      w xY w)(    )annotationsN)profiler)_pywrap_profiler_plugin)raw_to_tool_dataz-This script requires `xprof` to be installed.a  
To profile running JAX programs, you first need to start the profiler server
in the program of interest. You can do this via
`jax.profiler.start_server(<port>)`. Once the program is running and the
profiler server has started, you can run `collect_profile` to trace the execution
for a provided duration. The trace file will be dumped into a directory
(determined by `--log_dir`) and by default, a Perfetto UI link will be generated
to view the resulting trace.
zgrpc://   )descriptionz	--log_dirzJDirectory to store log files. Uses a temporary directory if none provided.)defaulthelptypeportzPort to collect trace)r
   r   duration_in_msz)Duration to collect trace in millisecondsz--no_perfetto_linkz Disable creating a perfetto link
store_true)r
   actionz--hostz	127.0.0.1z,Host to collect trace. Defaults to 127.0.0.1z--host_tracer_level   zProfiler host tracer levelz--device_tracer_level   zProfiler device tracer levelz--python_tracer_levelzProfiler Python tracer levelc           	        |||d}t        |      j                  d      }	t        j                  ||nt	        j
                               }
|	r|n
t        |
      }t        j                  t        | d|  t              |dd|t        |       t        d|        |	r|st        d       y |
j                         }|dz  d	z  }|j                         D cg c]  }||z  	 }}t        |t        j                   j"                  
      }t%        |j'                  d            }t)        j*                  |gdi       \  }}t-        j.                  t        |dz        d      5 }|j1                  |j3                  d             d d d        |s+t5        j6                  |
      }t5        j8                  |       y y c c}w # 1 sw Y   <xY w)N)host_tracer_leveldevice_tracer_levelpython_tracer_levelzgs://: Tz!Dumped profiling information in: z@Perfetto link is not supported for GCS paths, skipping creation.pluginsprofile)keyz*.xplane.pbtrace_viewerzremote.trace.json.gzwbzutf-8)str
startswithpathlibPathtempfilemkdtempr   trace_strip_addresses_GRPC_PREFIXDEFAULT_NUM_TRACING_ATTEMPTSprintresolveiterdirmaxospathgetmtimenextglobconvertxspace_to_tool_datagzipopenwriteencodejax_profiler_write_perfetto_trace_file_host_perfetto_trace_file)r   r   hostlog_dirr   r   r   no_perfetto_linkoptionsIS_GCS_PATHlog_dir_str_log_dir	curr_pathroot_trace_foldertrace_foldertrace_folderslatest_folderxplaneresult_fpr,   s                         N/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/collect_profile.pycollect_profilerJ   B   s   
 -00'
 G''0+\\W%8'h>N>N>PQ(&CM+$q'6
" 
+K=9: NO

  ))+i7'//13$|3 3- 3m)9)9:-""=12&))6(NBG)&!yy]%;;<dC %rHHV]]7#$% 
228<D**40 
3% %s   F6!F;;Gc                D    | j                  |      r| t        |      d  S | S N)r   len)sprefixs     rI   _strip_prefixrP   q   s"    LL03v;<7a7    c           	     |    dj                  | j                  d      D cg c]  }t        ||       c}      S c c}w )N,)joinsplitrP   )	addressesrO   rN   s      rI   r$   r$   t   s/    	Y__S5IJ=F+J	KKJs   9c           
         t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                         y rL   )	rJ   r   r   r9   r:   r   r   r   r;   )argss    rI   mainrY   w   sG    $))T00$))T\\(($*B*B**D,A,ACrQ   __main__)r   intr   r[   r9   r   r:   zos.PathLike | str | Noner   r[   r   r[   r   r[   r;   bool)
__future__r   argparser2   r+   r   r!   jax._srcr   r6   xprof.convertr   r   r0   ImportError_DESCRIPTIONr%   r&   ArgumentParserparseradd_argumentr   r[   rJ   rP   r$   rY   __name__
parse_args rQ   rI   <module>ri      s   #   	   .737   	 	 	 \	:   KI     F!8s  C   $D3  P   (;'  )   HkG     )15C  A   +Q7c  C   +Q7c  C-15-1JM-1),-1CF-1 '+-1^8LC
 zv A  75	7 77s   D   D.