
    Vpf?                    &   U d dl mZ d dlmZ d dlmZ 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Zd dlZd dlZd dlmZ d dlmZ  ej        e           d dlmZ d d	lmZ dad
ed<    ej        e          Zd1dZd Z  G d d          Z! e!            Z"	 	 d2d3dZ#d Z$ G d dej%        j&                  Z'd Z(d Z)d4d Z*ed2d!            Z+ G d" d#ej,        j-                  Z. G d$ d%e.          Z/d5d6d*Z0d5d7d-Z1d5d8d.Z2 G d/ d0          Z3dS )9    )annotations)Callable)contextmanager)wrapsN)Any)traceback_util)
xla_bridge)
xla_clientz)xla_client.profiler.ProfilerServer | None_profiler_serverportintreturn"xla_client.profiler.ProfilerServerc                    t           t          d          t          j                     t          j                            |           a t           S )a  Starts the profiler server on port `port`.

  Using the "TensorFlow profiler" feature in `TensorBoard
  <https://www.tensorflow.org/tensorboard>`_ 2.2 or newer, you can
  connect to the profiler server and sample execution traces that show CPU,
  GPU, and/or TPU device activity.
  Nz1Only one profiler server can be active at a time.)r   
ValueErrorr	   get_backendr
   profilerstart_server)r   s    Q/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/profiler.pyr   r   )   sG     !
H
I
II (55d;;	    c                 6    t           t          d          da dS )z"Stops the running profiler server.NzNo active profiler server.)r   r    r   r   stop_serverr   A   s%     
1
2
22r   c                      e Zd Zd Zd ZdS )_ProfileStatec                n    d | _         d | _        d| _        d| _        t	          j                    | _        d S NF)profile_sessionlog_dircreate_perfetto_linkcreate_perfetto_trace	threadingLocklockselfs    r   __init__z_ProfileState.__init__J   s4    DDL %D!&D  DIIIr   c                f    d t           _        dt           _        dt           _        d t           _        d S r   )_profile_stater   r    r!   r   r%   s    r   resetz_ProfileState.resetQ   s(    %)N"*/N'+0N(!Nr   N)__name__
__module____qualname__r'   r*   r   r   r   r   r   I   s2        ! ! !" " " " "r   r   Fr    boolr!   Nonec                b   t           j        5  t           j        t          d          t	          j                     t          j                                        t           _        |t           _	        |p|t           _
        t          |           t           _        ddd           dS # 1 swxY w Y   dS )a  Starts a profiler trace.

  The trace will capture CPU, GPU, and/or TPU activity, including Python
  functions and JAX on-device operations. Use :func:`stop_trace` to end the trace
  and save the results to ``log_dir``.

  The resulting trace can be viewed with TensorBoard. Note that TensorBoard
  doesn't need to be running when collecting the trace.

  Only one trace may be collected at a time. A RuntimeError will be raised if
  :func:`start_trace` is called while another trace is running.

  Args:
    log_dir: The directory to save the profiler trace to (usually the
      TensorBoard log directory).
    create_perfetto_link: A boolean which, if true, creates and prints link to
      the Perfetto trace viewer UI (https://ui.perfetto.dev). The program will
      block until the link is opened and Perfetto loads the trace.
    create_perfetto_trace: A boolean which, if true, additionally dumps a
      ``perfetto_trace.json.gz`` file that is compatible for upload with the
      Perfetto trace viewer UI (https://ui.perfetto.dev). The file will also be
      generated if ``create_perfetto_link`` is true. This could be useful if you
      want to generate a Perfetto-compatible trace without blocking the
      process.
  NzHProfile has already been started. Only one profile may be run at a time.)r)   r$   r   RuntimeErrorr	   r   r
   r   ProfilerSessionr    r!   strr   r   r    r!   s      r   start_tracer5   [   s    6  * *%1 B C C C %/%8%H%H%J%JN"*>N'5!5 ( \\N* * * * * * * * * * * * * * * * * *s   B
B$$B(+B(c                  
 t           j                            |           }t           j                            |dd          

fdt          j        
          D             }t          |t           j        j                  }t          j        t           j                            |d                    }t          |          dk    rt          d|           |\  }t                              d           t          j        |d	          5 }t          j        |          }|d
= d d d            n# 1 swxY w Y   d}t           j                            ||          }	t                              d           t          j        |	d          5 }|                    t          j        |                              d                     d d d            n# 1 swxY w Y   |	S )Npluginsprofilec                P    g | ]"}t           j                            |          #S r   )ospathjoin).0trace_folderroot_trace_folders     r   
<listcomp>z._write_perfetto_trace_file.<locals>.<listcomp>   s:     5 5 5 7<< 1<@@ 5 5 5r   )keyz*.trace.json.gz   zInvalid trace folder: z2Loading trace.json.gz and removing its metadata...rbmetadatazperfetto_trace.json.gzz!Writing perfetto_trace.json.gz...wzutf-8)r:   r;   abspathr<   listdirmaxgetmtimegloblenr   loggerinfogzipopenjsonloadwritedumpsencode)r   	curr_pathtrace_folderslatest_foldertrace_jsons
trace_jsonfptracefilenameperfetto_tracer?   s             @r   _write_perfetto_trace_filer^      s   goog&&)gll9iCC5 5 5 5j!2335 5 5-m)9:::-	"',,}6GHHII+
=m==
>
>>+*++BCCC
 yT"" bIbMMEj               &(7<<x88.++1222y%% 0HHTZ%%g..///0 0 0 0 0 0 0 0 0 0 0 0 0 0 0	s$   D00D47D4;GGGc                  2     e Zd ZdZ fdZ fdZd Z xZS )_PerfettoServerz<Handles requests from `ui.perfetto.dev` for the `trace.json`c                p    |                      dd           t                                                      S )NzAccess-Control-Allow-Origin*)send_headersuperend_headersr&   	__class__s    r   re   z_PerfettoServer.end_headers   s0    2C88877   r   c                f    | j         | j        _        t                                                      S N)r;   serverlast_requestrd   do_GETrf   s    r   rl   z_PerfettoServer.do_GET   s#    #yDK77>>r   c                2    |                      dd           d S )Ni  zFile not found)
send_errorr%   s    r   do_POSTz_PerfettoServer.do_POST   s    OOC)*****r   )r+   r,   r-   __doc__re   rl   ro   __classcell__rg   s   @r   r`   r`      sf        DD! ! ! ! !    + + + + + + +r   r`   c                   d}t           j                            t          j                              }t           j                            |           \  }}	 t          j        |           dt          j        _        t          j        d|ft                    5 }d| d| }t          d|            |j                            d          d|z   k    r5|                                 |j                            d          d|z   k    5d d d            n# 1 swxY w Y   t          j        |           d S # t          j        |           w xY w)Ni)#  Tz	127.0.0.1z1https://ui.perfetto.dev/#!/?url=http://127.0.0.1:/zOpen URL in browser: rk   )r:   r;   rF   getcwdsplitchdirsocketserver	TCPServerallow_reuse_addressr`   print__dict__gethandle_request)r;   r   orig_directory	directoryr\   httpdurls          r   _host_perfetto_trace_filer      sr    
$7??29;;//.d++)XHY15L.		d 3_	E	E QQQxQQc)C))*** N~..#.@@ N~..#.@@               H^BH^s2   AD5 A1DD5 DD5 DD5 5Ec                    t           j        5  t           j        t          d          t           j        } |                     |                                 t           j                   t           j        r4t          t           j                  }t           j	        rt          |           t                                            ddd           dS # 1 swxY w Y   dS )zStops the currently-running profiler trace.

  The trace will be saved to the ``log_dir`` passed to the corresponding
  :func:`start_trace` call. Raises a RuntimeError if a trace hasn't been started.
  NNo profile started)r)   r$   r   r1   exportstopr   r!   r^   r    r   r*   )sessabs_filenames     r   
stop_tracer      s      	 	%--...)DKK		^3444+ 0/0FGGl		, 0!,///	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B3CCCbytes | strc                 4   t           j        5  t           j        t          d          t           j                                        } t
          j                            |           }t                                            |cddd           S # 1 swxY w Y   dS )zStops the currently-running profiler trace and export fdo_profile.

  Currently, this is only supported for GPU.
  Raises a RuntimeError if a trace hasn't been started.
  Nr   )	r)   r$   r   r1   r   r
   r   get_fdo_profiler*   )xspacefdo_profiles     r   stop_and_get_fdo_profiler      s       %--...+0022F%55f==K                 s   A3BBBc              #  x   K   t          | ||           	 dV  t                       dS # t                       w xY w)a  Context manager to take a profiler trace.

  The trace will capture CPU, GPU, and/or TPU activity, including Python
  functions and JAX on-device operations.

  The resulting trace can be viewed with TensorBoard. Note that TensorBoard
  doesn't need to be running when collecting the trace.

  Only one trace may be collected at a time. A RuntimeError will be raised if a
  trace is started while another trace is running.

  Args:
    log_dir: The directory to save the profiler trace to (usually the
      TensorBoard log directory).
    create_perfetto_link: A boolean which, if true, creates and prints link to
      the Perfetto trace viewer UI (https://ui.perfetto.dev). The program will
      block until the link is opened and Perfetto loads the trace.
    create_perfetto_trace: A boolean which, if true, additionally dumps a
      ``perfetto_trace.json.gz`` file that is compatible for upload with the
      Perfetto trace viewer UI (https://ui.perfetto.dev). The file will also be
      generated if ``create_perfetto_link`` is true. This could be useful if you
      want to generate a Perfetto-compatible trace without blocking the
      process.
  N)r5   r   r4   s      r   r[   r[      sE      4 g+-BCCC	EEELLLLLJLLLLs   ) 9c                      e Zd ZdZdS )TraceAnnotationa  Context manager that generates a trace event in the profiler.

  The trace event spans the duration of the code enclosed by the context.

  For example:

  >>> x = jnp.ones((1000, 1000))
  >>> with jax.profiler.TraceAnnotation("my_label"):
  ...   result = jnp.dot(x, x.T).block_until_ready()

  This will cause a "my_label" event to show up on the trace timeline if the
  event occurs while the process is being traced.
  N)r+   r,   r-   rp   r   r   r   r   r     s          $r   r   c                  $     e Zd ZdZd fdZ xZS )StepTraceAnnotationaA  Context manager that generates a step trace event in the profiler.

  The step trace event spans the duration of the code enclosed by the context.
  The profiler will provide the performance analysis for each step trace event.

  For example, it can be used to mark training steps and enable the profiler to
  provide the performance analysis per step:

  >>> while global_step < NUM_STEPS:                                           # doctest: +SKIP
  ...   with jax.profiler.StepTraceAnnotation("train", step_num=global_step):  # doctest: +SKIP
  ...     train_step()                                                         # doctest: +SKIP
  ...     global_step += 1                                                     # doctest: +SKIP

  This will cause a "train xx" event to show up on the trace timeline if the
  event occurs while the process is being traced by TensorBoard. In addition,
  if using accelerators, the device trace timeline will also show a "train xx"
  event. Note that "step_num" can be set as a keyword argument to pass the
  global step number to the profiler.

  namer3   c                @     t                      j        |fddi| d S )N_rrB   )rd   r'   )r&   r   kwargsrg   s      r   r'   zStepTraceAnnotation.__init__-  s-    EGGT**a*6*****r   )r   r3   )r+   r,   r-   rp   r'   rq   rr   s   @r   r   r     sG         *+ + + + + + + + + +r   r   funcr   r   
str | Nonec                     pt           dd          p j        t                      fd            S )a  Decorator that generates a trace event for the execution of a function.

  For example:

  >>> @jax.profiler.annotate_function
  ... def f(x):
  ...   return jnp.dot(x, x.T).block_until_ready()
  >>>
  >>> result = f(jnp.ones((1000, 1000)))

  This will cause an "f" event to show up on the trace timeline if the
  function execution occurs while the process is being traced by TensorBoard.

  Arguments can be passed to the decorator via :py:func:`functools.partial`.

  >>> from functools import partial

  >>> @partial(jax.profiler.annotate_function, name="event_name")
  ... def f(x):
  ...   return jnp.dot(x, x.T).block_until_ready()

  >>> result = f(jnp.ones((1000, 1000)))
  r-   Nc                 b    t          fi 5   | i |cd d d            S # 1 swxY w Y   S ri   )r   )argsr   decorator_kwargsr   r   wrappers     r   r   z"annotate_function.<locals>.wrapperM  s    		2	2!1	2	2 # #T4"6""# # # # # # # # # # # # # # # #Ns   $(()getattrr+   r   )r   r   r   r   s   ```@r   annotate_functionr   1  si    4 
	4~t44$		$;;       ; 
.r   backendbytesc                N    t          j        t          j        |                     S )a  Captures a JAX device memory profile as ``pprof``-format protocol buffer.

  A device memory profile is a snapshot of the state of memory, that describes the JAX
  :class:`~jax.Array` and executable objects present in memory and their
  allocation sites.

  For more information how to use the device memory profiler, see
  :doc:`/device_memory_profiling`.

  The profiling system works by instrumenting JAX on-device allocations,
  capturing a Python stack trace for each allocation. The instrumentation is
  always enabled; :func:`device_memory_profile` provides an API to capture it.

  The output of :func:`device_memory_profile` is a binary protocol buffer that
  can be interpreted and visualized by the `pprof tool
  <https://github.com/google/pprof>`_.

  Args:
    backend: optional; the name of the JAX backend for which the device memory
      profile should be collected.

  Returns:
    A byte string containing a binary `pprof`-format protocol buffer.
  )r
   heap_profiler	   r   )r   s    r   device_memory_profiler   U  s     2 
	 !7!@!@	A	AAr   c                    t          |          }t          | d          5 }|                    |           ddd           dS # 1 swxY w Y   dS )a  Collects a device memory profile and writes it to a file.

  :func:`save_device_memory_profile` is a convenience wrapper around :func:`device_memory_profile`
  that saves its output to a ``filename``. See the
  :func:`device_memory_profile` documentation for more information.

  Args:
    filename: the filename to which the profile should be written.
    backend: optional; the name of the JAX backend for which the device memory
      profile should be collected.
  wbN)r   rO   rR   )r\   r   r8   fs       r   save_device_memory_profiler   q  s     "'**'Hd qGGG                 s   AA
Ac                  ^    e Zd ZddZddZd Zd	 Zd
 Zd Ze	e
dd                        ZdS )PGLEProfilerretriesr   
percentilec                Z    || _         || _        d | _        d| _        g | _        d | _        d S Nr   )r   r   collected_fdocalled_timesfdo_profilescurrent_session)r&   r   r   s      r   r'   zPGLEProfiler.__init__  s6    DL%DO%)DD#%DGKDr   r   r   c                    | j         | j         S |                                 r| j        | j        k    rd S t          j                            | j        | j                  | _         | j         S ri   )	r   
is_enabledr   r   r
   r   aggregate_profiled_instructionsr   r   r%   s    r   consume_fdo_profilez PGLEProfiler.consume_fdo_profile  sj    %??  1T\ A AT#,LL4? D r   c                    | j         d uS ri   )r   r%   s    r   is_fdo_consumedzPGLEProfiler.is_fdo_consumed  s    T))r   c                    d| _         d S r   r   r%   s    r   disablezPGLEProfiler.disable  s    DLLLr   c                    | j         dk    S r   r   r%   s    r   r   zPGLEProfiler.is_enabled  s    <!r   c                    | j         d uS ri   )r   r%   s    r   
is_runningzPGLEProfiler.is_running  s    t++r   runnerPGLEProfiler | Nonec              #    K   |<|                                 s(|                                r|                                rd V  d S t          j                                        }d|_        t          j                            |          |_        	 d V  |j        	                                }|j
                            t          j                            |                     d |_        |xj        dz  c_        d S # |j        	                                }|j
                            t          j                            |                     d |_        |xj        dz  c_        w xY w)NTrB   )r   r   r   r
   r   ProfileOptionsenable_hlo_protor2   r   r   r   appendr   r   )clsr   optionsr   s       r   r[   zPGLEProfiler.trace  sl      	&++--  "" 	&,&<&<&>&> 	eeeee#2244g!%g)2BB7KKf	!',,..""//77	
 	
 	
 "&q  ',,..""//77	
 	
 	
 "&q s   C> >A)E'N)r   r   r   r   )r   r   )r   r   )r+   r,   r-   r'   r   r   r   r   r   classmethodr   r[   r   r   r   r   r     s        L L L L
 
 
 
* * *    , , , ! ! ! > ;! ! !r   r   )r   r   r   r   )FF)r    r.   r!   r.   r   r/   )r   r   ri   )r   r   r   r   )r   r   r   r   )r   r   r   r/   )4
__future__r   collections.abcr   
contextlibr   	functoolsr   rJ   rN   http.serverhttprP   loggingr:   rx   r"   typingr   jax._srcr   register_exclusion__file__r	   jax._src.libr
   r   __annotations__	getLoggerr+   rL   r   r   r   r)   r5   r^   rj   SimpleHTTPRequestHandlerr`   r   r   r   r[   r   TraceMer   r   r   r   r   r   r   r   r   <module>r      s
   # " " " " " " $ $ $ $ $ $ % % % % % %               				               # # # # # # ! !( + + +       # # # # # #>B  B B B B		8	$	$   0  " " " " " " " "  7<.3)* )* )* )* )*X  6+ + + + +dk: + + +  (  $       @    j)1   "+ + + + +/ + + +4! ! ! ! !HB B B B B8    &6! 6! 6! 6! 6! 6! 6! 6! 6! 6!r   