
    BVhiu                        d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ  G d d ej$                  dd	            Z G d
 d ej$                  dd            Z G d de      Z G d de      Z G d de      Zy)z@Timeline visualization for TensorFlow using Chrome Trace Format.    N)AnyDictListOptionalTupleUnion)step_stats_pb2)
build_info)
tf_loggingc                       e Zd ZdZy)AllocationMaximuma  Stores the maximum allocation for a given allocator within the timelne.

  Parameters:
    timestamp: `tensorflow::Env::NowMicros()` when this maximum was reached.
    num_bytes: the total memory used at this time.
    tensors: the set of tensors allocated at this time.
  N__name__
__module____qualname____doc__     Q/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/client/timeline.pyr   r      s    
r   r   	timestamp	num_bytestensorsc                       e Zd ZdZy)StepStatsAnalysiszStores the step stats analysis output.

  Parameters:
    chrome_trace: A dict containing the chrome trace analysis.
    allocator_maximums: A dict mapping allocator names to AllocationMaximum.
  Nr   r   r   r   r   r   -   s    
r   r   chrome_traceallocator_maximumsc                      e Zd ZdZd deddfdZdededed	ed
ededeee	eef   f   fdZ
ded	eddfdZd Zdeded	ed
edededeeef   ddfdZdededed	ed
ededdfdZdededed	ed
ededdfdZdededed	ed
ededeeef   ddfdZdeded	ed
ededdfdZdeded	ed
ededdfdZdeded	ededededdfdZd Zd dedefdZy)!_ChromeTraceFormatterz<A helper class for generating traces in Chrome Trace Format.show_memoryreturnNc                 .    || _         g | _        g | _        y)z(Constructs a new Chrome Trace formatter.N)_show_memory_events	_metadata)selfr!   s     r   __init__z_ChromeTraceFormatter.__init__=   s    #DDLDNr   phcategorynamepidtidr   c                 F    i }||d<   ||d<   ||d<   ||d<   ||d<   ||d<   |S )aH  Creates a new Chrome Trace event.

    For details of the file format, see:
    https://github.com/catapult-project/catapult/blob/master/tracing/README.md

    Args:
      ph:  The type of event - usually a single character.
      category: The event category as a string.
      name:  The event name as a string.
      pid:  Identifier of the process generating this event as an integer.
      tid:  Identifier of the thread generating this event as an integer.
      timestamp:  The timestamp of this event as a long integer.

    Returns:
      A JSON compatible event object.
    r)   catr+   r,   r-   tsr   )r'   r)   r*   r+   r,   r-   r   events           r   _create_eventz#_ChromeTraceFormatter._create_eventC   sD    2 EE$KE%LE&ME%LE%LE$KLr   c                 j    i }d|d<   d|d<   ||d<   d|i|d<   | j                   j                  |       y)zAdds a process metadata event to the trace.

    Args:
      name:  The process name as a string.
      pid:  Identifier of the process as an integer.
    process_namer+   Mr)   r,   argsNr&   append)r'   r+   r,   r1   s       r   emit_pidz_ChromeTraceFormatter.emit_pide   sC     E"E&ME$KE%LTNE&MNN% r   c                 t    i }d|d<   d|d<   ||d<   ||d<   d|i|d<   | j                   j                  |       y)	zAdds a thread metadata event to the trace.

    Args:
      name:  The thread name as a string.
      pid:  Identifier of the process as an integer.
      tid:  Identifier of the thread as an integer.
    thread_namer+   r5   r)   r,   r-   r6   Nr7   )r'   r+   r,   r-   r1   s        r   emit_tidz_ChromeTraceFormatter.emit_tids   sL     E!E&ME$KE%LE%LTNE&MNN% r   durationr6   c                 z    | j                  d|||||      }||d<   ||d<   | j                  j                  |       y)a  Adds a region event to the trace.

    Args:
      timestamp:  The start timestamp of this region as a long integer.
      duration:  The duration of this region as a long integer.
      pid:  Identifier of the process generating this event as an integer.
      tid:  Identifier of the thread generating this event as an integer.
      category: The event category as a string.
      name:  The event name as a string.
      args:  A JSON-compatible dictionary of event arguments.
    Xdurr6   Nr2   r%   r8   )	r'   r   r=   r,   r-   r*   r+   r6   r1   s	            r   emit_regionz!_ChromeTraceFormatter.emit_region   sB    * sHdCiHEE%LE&MLLr   	object_idc                 p    | j                  d|||||      }||d<   | j                  j                  |       y)a  Adds an object creation event to the trace.

    Args:
      category: The event category as a string.
      name:  The event name as a string.
      timestamp:  The timestamp of this event as a long integer.
      pid:  Identifier of the process generating this event as an integer.
      tid:  Identifier of the thread generating this event as an integer.
      object_id: Identifier of the object as an integer.
    NidNrA   r'   r*   r+   r   r,   r-   rC   r1   s           r   emit_obj_createz%_ChromeTraceFormatter.emit_obj_create   9    & sHdCiHEE$KLLr   c                 p    | j                  d|||||      }||d<   | j                  j                  |       y)a  Adds an object deletion event to the trace.

    Args:
      category: The event category as a string.
      name:  The event name as a string.
      timestamp:  The timestamp of this event as a long integer.
      pid:  Identifier of the process generating this event as an integer.
      tid:  Identifier of the thread generating this event as an integer.
      object_id: Identifier of the object as an integer.
    DrF   NrA   rG   s           r   emit_obj_deletez%_ChromeTraceFormatter.emit_obj_delete   rI   r   snapshotc                 ~    | j                  d|||||      }||d<   d|i|d<   | j                  j                  |       y)a  Adds an object snapshot event to the trace.

    Args:
      category: The event category as a string.
      name:  The event name as a string.
      timestamp:  The timestamp of this event as a long integer.
      pid:  Identifier of the process generating this event as an integer.
      tid:  Identifier of the thread generating this event as an integer.
      object_id: Identifier of the object as an integer.
      snapshot:  A JSON-compatible representation of the object.
    OrF   rM   r6   NrA   )	r'   r*   r+   r   r,   r-   rC   rM   r1   s	            r   emit_obj_snapshotz'_ChromeTraceFormatter.emit_obj_snapshot   sG    * sHdCiHEE$K*E&MLLr   flow_idc                 p    | j                  dd||||      }||d<   | j                  j                  |       y)a  Adds a flow start event to the trace.

    When matched with a flow end event (with the same 'flow_id') this will
    cause the trace viewer to draw an arrow between the start and end events.

    Args:
      name:  The event name as a string.
      timestamp:  The timestamp of this event as a long integer.
      pid:  Identifier of the process generating this event as an integer.
      tid:  Identifier of the thread generating this event as an integer.
      flow_id: Identifier of the flow as an integer.
    sDataFlowrF   NrA   r'   r+   r   r,   r-   rQ   r1   s          r   emit_flow_startz%_ChromeTraceFormatter.emit_flow_start   9     sJc3	JEE$KLLr   c                 p    | j                  dd||||      }||d<   | j                  j                  |       y)a  Adds a flow end event to the trace.

    When matched with a flow start event (with the same 'flow_id') this will
    cause the trace viewer to draw an arrow between the start and end events.

    Args:
      name:  The event name as a string.
      timestamp:  The timestamp of this event as a long integer.
      pid:  Identifier of the process generating this event as an integer.
      tid:  Identifier of the thread generating this event as an integer.
      flow_id: Identifier of the flow as an integer.
    trT   rF   NrA   rU   s          r   emit_flow_endz#_ChromeTraceFormatter.emit_flow_end   rW   r   countervaluec                 t    | j                  d|||d|      }||i|d<   | j                  j                  |       y)a{  Emits a record for a single counter.

    Args:
      category: The event category as a string.
      name:  The event name as a string.
      pid:  Identifier of the process generating this event as an integer.
      timestamp:  The timestamp of this event as a long integer.
      counter: Name of the counter as a string.
      value:  Value of the counter as an integer.
    Cr   r6   NrA   )r'   r*   r+   r,   r   r[   r\   r1   s           r   emit_counterz"_ChromeTraceFormatter.emit_counter  s>    & sHdCIFEe$E&MLLr   c                     | j                  d|||d|      }|j                         |d<   | j                  j                  |       y)aX  Emits a counter record for the dictionary 'counters'.

    Args:
      category: The event category as a string.
      name:  The event name as a string.
      pid:  Identifier of the process generating this event as an integer.
      timestamp:  The timestamp of this event as a long integer.
      counters: Dictionary of counter values.
    r^   r   r6   N)r2   copyr%   r8   )r'   r*   r+   r,   r   countersr1   s          r   emit_countersz#_ChromeTraceFormatter.emit_counters"  s?     sHdCIFEMMOE&MLLr   prettyc                     i }| j                   | j                  z   |d<   |rt        j                  |dd      S t        j                  |d      S )zFormats the chrome trace to a string.

    Args:
      pretty: (Optional.)  If True, produce human-readable JSON output.

    Returns:
      A JSON-formatted string in Chrome Trace format.
    traceEvents   ),z: )indent
separators)rh   :)rj   )r&   r%   jsondumps)r'   rd   traces      r   format_to_stringz&_ChromeTraceFormatter.format_to_string0  sG     E>>DLL8E-ZZaK@@ZZ*55r   )F)r   r   r   r   boolr(   strintr   r   r2   r9   r<   r   rB   rH   rL   rP   rV   rZ   r_   rc   ro   r   r   r   r    r    :   s   D$ 4  
    	 
       CsCx ! D!3 !S !T !!   	
    cN 4  	
    .  	
    .  	
    S#X 4"%,/69DG&"%,/69DG&  	
    .6T 6c 6r   r    c                       e Zd ZdZdededededededd	fd
Zedefd       Zedefd       Z	edefd       Z
edefd       Zedefd       Zedefd       Zedefd       Zdedd	fdZdedd	fdZy	)_TensorTrackerz4An internal class to track the lifetime of a Tensor.r+   rC   r   r,   	allocatorr   r"   Nc                 t    || _         || _        || _        || _        || _        || _        g | _        g | _        y)ak  Creates an object to track tensor references.

    This class is not thread safe and is intended only for internal use by
    the 'Timeline' class in this file.

    Args:
      name:  The name of the Tensor as a string.
      object_id:  Chrome Trace object identifier assigned for this Tensor.
      timestamp:  The creation timestamp of this event as a long integer.
      pid:  Process identifier of the associated device, as an integer.
      allocator:  Name of the allocator used to create the Tensor.
      num_bytes:  Number of bytes allocated (long integer).

    Returns:
      A 'TensorTracker' object.
    N)_name_pid
_object_id_create_time
_allocator
_num_bytes
_ref_times_unref_times)r'   r+   rC   r   r,   ru   r   s          r   r(   z_TensorTracker.__init__D  s>    2 DJDIDO!DDODODODr   c                     | j                   S )zName of this tensor.)rw   r'   s    r   r+   z_TensorTracker.namef  s     ::r   c                     | j                   S )z9ID of the process which created this tensor (an integer).)rx   r   s    r   r,   z_TensorTracker.pidk  s     99r   c                     | j                   S )z6Timestamp when this tensor was created (long integer).)rz   r   s    r   create_timez_TensorTracker.create_timep  s     r   c                     | j                   S )z7Returns the object identifier of this tensor (integer).)ry   r   s    r   rC   z_TensorTracker.object_idu       ??r   c                     | j                   S )z,Size of this tensor in bytes (long integer).)r|   r   s    r   r   z_TensorTracker.num_bytesz  r   r   c                     | j                   S )z:Name of the allocator used to create this tensor (string).)r{   r   s    r   ru   z_TensorTracker.allocator  r   r   c                 ,    t        | j                        S )z9Last unreference timestamp of this tensor (long integer).)maxr~   r   s    r   
last_unrefz_TensorTracker.last_unref  s     t  !!r   c                 :    | j                   j                  |       y)zAdds a reference to this tensor with the specified timestamp.

    Args:
      timestamp:  Timestamp of object reference as an integer.
    N)r}   r8   r'   r   s     r   add_refz_TensorTracker.add_ref  s     	OO9%r   c                 :    | j                   j                  |       y)zAdds an unref to this tensor with the specified timestamp.

    Args:
      timestamp:  Timestamp of object unreference as an integer.
    N)r~   r8   r   s     r   	add_unrefz_TensorTracker.add_unref  s     	Y'r   )r   r   r   r   rq   rr   r(   propertyr+   r,   r   rC   r   ru   r   r   r   r   r   r   rt   rt   A  s2   <     	 
        D C   3   3            "# " "&s &t &( ( (r   rt   c                      e Zd ZdZ	 d'dej
                  dee   ddfdZde	fdZ
de	fdZd	edeeeee   f   fd
Zd Zd(dZdej$                  de	deddfdZdede	de	de	dej,                  ddfdZdede	de	dede	defdZdedefdZd(dZdeddfdZd eddfd!Zd(d"Zd#eddfd$Z	 	 	 d)d eded#edefd%Z 	 	 	 d*d eded#edefd&Z!y)+Timelinez@A class for visualizing execution timelines of TensorFlow steps.N
step_statsgraphr"   c                     || _         d| _        || _        t               | _        d| _        i | _        i | _        i | _        d| _	        i | _
        i | _        i | _        y)a  Constructs a new Timeline.

    A 'Timeline' is used for visualizing the execution of a TensorFlow
    computation.  It shows the timings and concurrency of execution at
    the granularity of TensorFlow Ops.
    This class is not thread safe.

    Args:
      step_stats: The 'step_stats_pb2.StepStats' proto recording execution
        times.
      graph: (Optional) The 'Graph' that was executed.
    Nr   )_origin_step_stats_step_stats_graphr    _chrome_trace	_next_pid_device_pids_tensor_pids_tensors_next_flow_id_flow_starts_alloc_times_allocator_maximums)r'   r   r   s      r   r(   zTimeline.__init__  sd      )DDDK.0DDNDDDMDDD!Dr   c                 H    | j                   }| xj                   dz  c_         |S )zAllocate a process Id.   )r   )r'   r,   s     r   
_alloc_pidzTimeline._alloc_pid  s    
..CNNaNJr   c                 H    | j                   }| xj                   dz  c_         |S )zAllocate a flow Id.r   )r   )r'   rQ   s     r   _alloc_flow_idzTimeline._alloc_flow_id  s#      G!Nr   labelc                     t        j                  d|      }|ddg fS |j                         \  }}}|sg }n|j                  d      }|||fS )+Parses the fields in a node timeline label.z(.*) = (.*)\((.*)\)unknownz, )rematchgroupssplit)r'   r   r   nnopinputss         r   _parse_op_labelzTimeline._parse_op_label  sZ    
 HH+U3E}	2%%\\^NBFf||D!fr6>r   c                     |j                  d      }|j                  d      }|dk\  r|dk\  r|dz   |k  r||dz   | }|j                  d      dgz   }|dd \  }}||fS )r   @@#r      rk   r   N)findr   )r'   r   	node_namestartendfieldsr+   r   s           r   _parse_kernel_labelzTimeline._parse_kernel_label  ss     JJtE
**S/CzcQh519s?	C(i__S!YK/FbqzHD"8Or   c                 h   | j                   j                  D ]  }dg}|j                  D ]  }d}t        |      D ]4  \  }}|j                  |kD  s|}|j                  |j
                  z   ||<    n |dk  r3t        |      }|j                  |j                  |j
                  z          ||_          y)z@Assigns non-overlapping lanes for the activities on each device.r   N)	r   	dev_stats
node_stats	enumerateall_start_microsall_end_rel_microslenr8   	thread_id)r'   device_statslanesnsliltss          r   _assign_laneszTimeline._assign_lanes  s    ((22  ce'' 
"& 	FAs  3&A**R-B-BBE!H		
 q5%j!
,,r**R-B-BB
C
	r   	nodestatsr,   is_gputracec           	         |j                   }|j                  }|j                  }|j                  }g }|r | j	                  |j
                  |      \  }}	n'|dk(  rd}	n| j                  |j
                        \  }
}	}||	d}t        j                  d   r!|j
                  j                  d      d   |d<   t        |      D ]  \  }}||d|z  <    | j                  j                  ||||d|	|       y	)
a  Generates a Chrome Trace event to show Op execution.

    Args:
      nodestats: The 'step_stats_pb2.NodeExecStats' proto recording op
        execution.
      pid: The pid assigned for the device where this op ran.
      is_gputrace: If True then this op came from the GPUTracer.
    
RecvTensor)r+   r   is_rocm_buildr   r   kernelzinput%dOpN)r   r   r   r   r   timeline_labelr   r
   r   r   r   rB   )r'   r   r,   r   r   r   r=   r-   r   r   _r6   r   inames                 r   _emit_opzTimeline._emit_op  s     ##I&&E++H


CF..

"
"Imi 
l	"b**9+C+CDmaVR(D_- //55d;A>d8nf% "5!d9q="""5(CdBMr   tensorr   r-   r\   c           	          t        |j                        j                  dd      }d|i}| j                  j	                  d|j
                  ||||j                  |       y)as  Generate Chrome Trace snapshot event for a computed Tensor.

    Args:
      tensor: A 'TensorTracker' object.
      timestamp:  The timestamp of this snapshot as a long integer.
      pid: The pid assigned for showing the device where this op ran.
      tid: The tid of the thread computing the tensor snapshot.
      value: A JSON-compliant snapshot of the object.
    " tensor_descriptionTensorN)rq   r   replacer   rP   r+   rC   )r'   r   r   r,   r-   r\   descrM   s           r   _emit_tensor_snapshotzTimeline._emit_tensor_snapshot  sX    " u''(00b9D$d+H((&++y#sF4D4Dhr   r+   tensors_pidru   r   c                 n    t        | j                        }t        ||||||      }|| j                  |<   |S )zCreates a new tensor tracker.)r   r   rt   )r'   r+   r   r   ru   r   rC   r   s           r   _produce_tensorzTimeline._produce_tensor*  s>     DMM"IiKIF !DMM$Mr   device_namec                     d|v xs d|v S )z=Returns true if this device is part of the GPUTracer logging.z/stream:z/memcpyr   )r'   r   s     r   _is_gputrace_devicezTimeline._is_gputrace_device:  s    $@	[(@@r   c                    | j                         | _        | j                  j                  d| j                         | j                  j
                  D ]  }| j                         }|| j                  |j                  <   | j                         }|| j                  |j                  <   | j                  j                  |j                  dz   |       | j                  j                  |j                  dz   |        y)zJAllocate fake process ids for each device in the step_stats_pb2.StepStats.
Allocatorsz Computez TensorsN)	r   _allocators_pidr   r9   r   r   r   devicer   )r'   r   
device_pidr   s       r   _allocate_pidszTimeline._allocate_pids>  s    ??,Dd.B.BC %%// N	??$j,6d	(()OO%k,7d	(()
!!)"2"2Z"?L
!!)"2"2Z"?MNr   r!   c                    | j                   j                  D ]j  }| j                  |j                     }| j                  |j                     }|j
                  D ]%  }|j                  }|j                  }|j                  }|j                  |j                  z   }	t        |j                        D ]  \  }
}|
rd||
fz  }n|}|j                  j                  }|j                  }|j                  }| j!                  |||||      }|j#                  |       |j%                  |	       |	||f| j&                  |<   |s| j(                  j+                  d|||||j,                         | j/                  ||	dz
  |||        ( m y)z,Analyze tensor references to track dataflow.z%s:%dr   r   N)r   r   r   r   r   r   r   r   r   r   r   outputr   allocation_descriptionrequested_bytesallocator_namer   r   r   r   r   rH   rC   r   )r'   r!   r   r   r   r   r-   r   
start_timeend_timeindexr   output_name
allocationr   r   r   s                    r   _analyze_tensorszTimeline._analyze_tensorsL  s   %%// #	$$Y%5%56j%%i&6&67k!,,  *""((	00
..1N1NN&z'8'89 	ME6!Y$66K#K00GG* 00)%44.'':{NI& ..
$


8
$,4j#+F$

K
(..   &&1k33	 #r   show_dataflowc           
         | j                   j                  D ]  }|j                  }| j                  |   }| j	                  |      }|j
                  D ]q  }|j                  }|j                  }|j                  |j                  z   }	| j                  |||       |s|j                  dk(  rZ| j                  |j                        \  }
}
}|D ]  }|| j                  vr|j                  d      }|dkD  r|d| }|| j                  v r| j                  |   }|j                  |       |j!                  |	dz
         |sq| j"                  |   \  }}}||k7  s||k7  s| j%                         }| j&                  j)                  |||||       | j&                  j+                  |||||       t-        j.                  dd|        t  y)z#Visualize the computation activity.r   z/_r   Nr   z&Can't find tensor %s - removed by CSE?)r   r   r   r   r   r   r   r   r   r   r   r   r   r   rfindr   r   r   r   r   rV   rZ   loggingvlog)r'   r   r   r   r   r   r   r-   r   r   r   r   
input_namer   r   r   
create_pid
create_tidrQ   s                      r   _show_computezTimeline._show_computes  s   %%// 0	$$k$$[1j,,[9k!,, +*""00
..1N1NNj*k:*..,>
++J,E,EF1f  !	Jt}},
 $$T*Eqy%fu-j4==(]]:.FNN:&X\*484E4E51k:z
 z)Z3->--/""22ZW ""00
JW LL;Z?!	+0r   c           
      h   i }| j                   D ]  }| j                   |   }| j                  j                  d||j                  |j                  d|j
                         |j                  }||vrg ||<   |j                  }||   j                  |j                  ||f       ||   j                  |j                  | |f        i }|D ]  }||   }|j                          d}t               }	t        ddt                     ||<   t        |d       D ]  \  }
}}||z  }|dk  r|	j                  |       n|	j                  |       |||   j                  kD  r$t        |
|t!        j"                  |	            ||<   | j                  j%                  d|| j&                  |
||         || _        y)z3Produce a counter series for each memory allocator.r   r   r   c                     | d   S )Nr   r   )r   s    r   <lambda>z0Timeline._show_memory_counters.<locals>.<lambda>  s
    Z] r   )keyMemoryN)r   r   rL   r   r,   rC   ru   r   r8   r   sortsetr   sorteddiscardaddra   deepcopyr_   r   r   )r'   allocationsr+   r   ru   r   alloc_maxes
alloc_listtotal_bytesalloc_tensor_settimes              r   _show_memory_counterszTimeline._show_memory_counters  s   
 K 
K}}T"f
((
D&++VZZF<L<L ""i	+	%!#I""i)##V%7%7D$IJ)##V%6%6
D$IJ
K K ! 
	y)jook0CE k) $*
:$ 

$	4 	y q=

"
"4
(


t
$Y/999#4#mm$45$+i
  	''  	
!

@  +Dr   op_timec                 z   |dk(  r| j                   | _        yt        j                  | j                         | _        g }g }| j                  j                  D ]B  }d|j
                  v r|j                  |       #d|j
                  v s2|j                  |       D i }i }|D ]  }|j                  D ]  }| j                  |j                  |j                        \  }}	|j                  }
|j                  |j                  z   }||v r%t        ||   |
      ||<   t        ||   |      ||<   z|
||<   |||<     |D ]  }|j                  D ]n  }|j                  |v st        ||j                     |j                  |j                  z         }|dk(  r||j                     |_        ||j                  z
  |_        p  y)aN  Update the start and end time of ops in step stats.

    Args:
      op_time: How the execution time of op is shown in timeline. Possible
        values are "schedule", "gpu" and "all".  "schedule" will show op from
        the time it is scheduled to the end of the scheduling. Notice by the end
        of its scheduling its async kernels may not start yet. It is shown using
        the default value from step_stats. "gpu" will show op with the execution
        time of its kernels on GPU. "all" will show op from the start of its
        scheduling to the end of its last kernel.
    scheduleNz/stream:allz/jobgpu)r   r   ra   r  r   r   r8   r   r   r   r   r   r   minr   )r'   r  stream_all_stats	job_statsstatsop_gpu_start
op_gpu_endr   r+   r   r   r   r   s                r   _preprocess_op_timezTimeline._preprocess_op_time  s    *00d}}T%<%<=DI!!++  	%,,	&&U\\!	  LJ! !$$ !&**!!6#3#3
a ''%%(A(AA<"<#5u=,t
 D!137*T
$,t
 *T
!!  	<   <"<<<'&!!B$9$99# ".r||"<B"%(;(;";"
<	<r   c                    | j                  |       | j                          | j                          | j                  |       | j	                  |       |r| j                          t        | j                  | j                        S )a  Analyze the step stats and format it into Chrome Trace Format.

    Args:
      show_dataflow: (Optional.) If True, add flow events to the trace
        connecting producers and consumers of tensors.
      show_memory: (Optional.) If True, add object snapshot events to the trace
        showing the sizes and lifetimes of tensors.
      op_time: (Optional.) How the execution time of op is shown in timeline.
        Possible values are "schedule", "gpu" and "all". "schedule" will show op
        from the time it is scheduled to the end of the scheduling. Notice by
        the end of its scheduling its async kernels may not start yet. It is
        shown using the default value from step_stats. "gpu" will show op with
        the execution time of its kernels on GPU. "all" will show op from the
        start of its scheduling to the end of its last kernel.

    Returns:
      A 'StepStatsAnalysis' object.
    r   )	r  r   r   r   r   r  r   r   r   )r'   r   r!   r  s       r   analyze_step_statszTimeline.analyze_step_stats  st    0 	W%+&}%
  "''33 r   c                 b    | j                  |||      }|j                  j                  d      S )a  Produces a trace in Chrome Trace Format.

    Args:
      show_dataflow: (Optional.) If True, add flow events to the trace
        connecting producers and consumers of tensors.
      show_memory: (Optional.) If True, add object snapshot events to the trace
        showing the sizes and lifetimes of tensors.
      op_time: (Optional.) How the execution time of op is shown in timeline.
        Possible values are "schedule", "gpu" and "all".
        "schedule" will show op from the time it is scheduled to the end of
          the scheduling.
          Notice by the end of its scheduling its async kernels may not start
          yet. It is shown using the default value from step_stats.
        "gpu" will show op with the execution time of its kernels on GPU.
        "all" will show op from the start of its scheduling to the end of
          its last kernel.
    Returns:
      A JSON formatted string in Chrome Trace format.
    )r   r!   r  T)rd   )r  r   ro   )r'   r   r!   r  step_stats_analysiss        r   generate_chrome_trace_formatz%Timeline.generate_chrome_trace_format9  s=    6 11#g 2  ++<<D<IIr   )N)r"   N)TTr  )TFr  )"r   r   r   r   r	   	StepStatsr   r   r(   rr   r   r   rq   r   r   r   r   r   NodeExecStatsrp   r   rt   
NodeOutputr   r   r   r   r   r   r  r  r   r  r"  r   r   r   r   r     s   H JN"&00"9A#"":# c S#tCy !
$N%33N:=NLPNN@  	
  && .  	
    AS AT AN%$ %4 %N2 2$ 2h5+n5< 5< 5<r !	"" " 	"
 "L !	JJ J 	J
 Jr   r   )r   collectionsra   rl   r   typingr   r   r   r   r   r   tensorflow.core.frameworkr	   tensorflow.python.platformr
   r   r   
namedtupler   r   objectr    rt   r   r   r   r   <module>r,     s    G    	 : : 4 2 <KB
KC
D6F D6NV(V V(r~Jv ~Jr   