
    BVh                       d 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 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 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  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) ddlm*Z* ddl+m,Z, ddl+m-Z- dd l+m.Z. dd!l+m/Z0 dd"l1m2Z2 dd#l3m4Z4 dd$l3m5Z5 dd%l3m6Z6 dd&l7m8Z8 dd'l9m:Z: d(Z;d)Z<d*Z=d+Z>d,Z?d-Z@d.ZAd/ZBd0ZCd1ZDd2ZEd3ZFd4ZGd5ZHd6ZId7ZJd8ZKd9ZLd:ZMd;ZNd<ZOd=ZPd>ZQd?ZRd@ZSdAZTdBZUdCZVdDZWdEZXe4j                  ZZe4j                  Z\e4j                  Z^e4j                  Z`e4j                  Zbe4j                  Zde4j                  Zfe4j                  ZhdFZidGZjdHZkdIZldJZm ej                  dKdLdM      ZodN ZpdVdOZqdP ZrdQ ZsdVdRZtdS Zu G dT dU      Zvy)Wz(A utility to trace tensor values on TPU.    N)summary_pb2)
monitoring)constant_op)dtypes)
func_graph)function)graph_io)opstensor)tensor_util)file_io)	array_ops)array_ops_stack)cond)control_flow_case)control_flow_ops)control_flow_util)gen_math_ops)init_ops)
linalg_ops)logging_ops)math_ops)nn_impl)	state_ops)
string_ops)summary_ops_v2)variable_scope)	analytics)gfile)remote_utils)
tf_logging)summary_iterator)tensor_tracer_flags)tensor_tracer_report)tpu_replication)tpu_ops)training_utiltpucpu   znot-traced-outside-op-rangeznot-traced-unsafe-opznot-traced-special-whileloop-opznot-traced-control-flow-opznot-traced-in-control-flowznot-traced-unsafe-scalarznot-traced-scalarznot-traced-less-interesting-opznot-traced-device-mismatchznot-traced-dynamic-shapeztraced-scalarztraced-tensorztraced-user-includedznot-traced-user-excludedznot-traced-not-in-exec-pathznot-traced-non-numeric-tensorz%not-traced-feeds-special-whileloop-opzfile://tensor_tracer_variablestensor_tracer_summary_writersz	trace.allzcompact_trace.g      tensor_tracer_storagetensor_tracer_snapshotz#replica-id: Nonetensor_tracer_summarytensor_tracertensor_tracer_host_callz.tensor_tracer
   z /tensorflow/api/tensor_tracer/v1ztensor tracer usagemethodc                     | t        d      t        j                         }|j                  t	        |       j                  d             |j                         S )z=Generates and returns a summary tag name for the given graph.zgraph is Nonezutf-8)RuntimeErrorhashlibmd5updatereprencode	hexdigest)graphhash_ids     S/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/tensor_tracer.py_graph_summary_tagrA   u   sI     ]

''KKM'	..e##G,-				    c                     dt         j                  z  }| r#| j                         D ]  \  }}|d|d|z  } |t        j                  t         j
                  <   y)a]  Enables tensor tracer and sets its parameters.

  Example usage:
    tensor_tracer_parameters = {'trace_dir': '/usr/tmp/trace_dir',
                                'trace_mode': 'norm',
                                'report_file': '/usr/tmp/trace_dir/report.all'}
    tensor_tracer.set_parameters(tensor_tracer_parameters)

  This sets up the parameters for tensor tracer. A call to tensor tracer as
  below is necessary to enable debugging on CPUs and GPUs. On TPUs below can be
  skipped as this call is hooked into tpu.rewrite.
    tt = tensor_tracer.TensorTracer()
    loss = tt.trace_cpu(tf.get_default_graph(), tensor_fetches=loss)

  Args:
    tensor_tracer_params: Tensor tracer parameter dictionary. Below gives
    examples of these parameters: See tensor_tracer_report.py for all
      parameters.
        - enable: If set, tensor tracer will be enabled. Calling
          enable_tensor_tracer automatically adds this parameters.
        - trace_mode: The trace_mode to be used by tensor tracer. These include:
          - summary: Collects multiple statistics for traced tensors, and writes
            them a summary file that can be visualized using tensorboard. This
            mode currently only works for TPUEstimator. It can be also be used
            for other models, but outfeed must be handled by the user.
          - norm: Collects norm of each traced tensor and writes them into a
            text file pointed by 'trace_dir' flag. (Default mode).
          - nan-inf: Checks the existince of NaNs and Infs in the tensor, and
            writes a boolean value to a text file pointed by 'trace_dir' flag.
            Note that 'norm' mode can also capture this information with more
            numerical info.
          - max-abs: Collects the absolute max for each traced tensors and
            writes it into a text file pointed by 'trace_dir' flag.
          - full-tensor: Writes the full tensor content of the traced tensors
            into a text file pointed by 'trace_dir' flag.
          - part-tensor: Writes a part of the tensor content of the traced
            tensors into a text file pointed by 'trace_dir' flag.
          - full_tensor_summary: Writes the full tensors as binary event files.
            The outputs can be read using: trace =
              tensor_tracer.read_tensor_tracer_event_file(event_file_path)

        - report_file: Path to the metadata file that is written during graph
          construction. If not set, metadata will be printed to stdout during
          graph construction.
        - trace_dir: Path where the execution traces will be written during the
          graph execution. If not set, trace will be printed to stderr.
        - trace_level: Tensor tracer aims to trace everything it can. This
          introduces some overhead on graph execution and graph compilation
          times. Using trace_level parameter, it is possible to trace operation
          based on their priorities. For example, - trace_level=7 is the highest
          trace_level, in which every op is traced. - trace_level=6 will skip
          constant operations such as tf.constant. - trace_level=5 will skip
          less important ops such as tf.identities. - The default trace_level=3,
          that will skip concat ops, or random number generators. - To reduce
          the graph compile time overhead, trace_level can be set to 0, that
          will skip additions, and substractions, and multiplications as well.
        - excluded_opnames: If set, any matching op name will not be traced.
          excluded_opnames can be set as a regular expression. E.g,
          excluded_opnames=.* will exclude everything.
        - excluded_optypes: If set, any matching op type will not be traced.
          excluded_optypes can be set as a regular expression. E.g,
          excluded_optypes=.* will exclude everything. excluded_optypes=MatMul
          will exclude all MatMul ops from tracing.
        - included_opnames: If set, any matching op name will be forced to be
          traced. included_opnames can be set as a regular expression. E.g,
          '--included_opnames=some_op --excluded_opname=*.' will only trace
          some_op.
        - included_optypes: If set, any matching op type will be forced to be
          traced. included_optypes can be set as a regular expression. E.g,
          '--included_optypes=some_op_type --excluded_optypes=*.' will trace
          only the ops with type 'some_op_type'
        - flush_summaries: If summary mode is used, flush_summaries=1 will
          flush summaries using outside compilation. Note that, if used with
          low level APIs, flush_summaries=1 is necessary to obtain results.
        Advanced Flags:
        - trace_scalar: Scalar values are not traced by default. If this flag is
          set, scalar values will also be traced.
        - op_range: In the form of '%d:%d' that limits the tracing to the ops
          within this limit. --op_range='5:10' will trace only the ops that have
            topological order between 5-10.
        - submode: 'brief' or 'detailed'. If the trace mode is not compact,
          brief mode will print only the id of each traced tensor to save some
          space. 'detailed' mode prints the full tensor name.
        - use_fingerprint_subdirectory: The trace directory will be chosen as
          using the fingerprint of the trace metadata under the provided
          trace_dir.
  z--%s=1z --=N)r$   FLAG_NAME_ENABLEitemsosenvironFLAGS_ENV_VAR)tensor_tracer_paramsenable_flagskeyvalues       r@   set_parametersrN      sY    p /@@@,*002 0
UC//l02>"** ../rB   c                 J    | dv ry| dv ry| dv ry| dv ry| d	v ry
| dv ry| dv ryy)zReturns the priority of the op.

  If the priority of the op is k, it will be traced if trace_level>=k.
  Args:
    op_type: String name of the operation type.
  Returns:
    Integer value corresponding the priority of the op.
  )ConstShapeBroadcastGradientArgsRangeVariableShapeFillOneHotShapeN   )	IdentityCastReshape
ExpandDimsStopGradientPreventGradientSqueezeGatherGatherNd   )	ConcatV2ConcatStridedSliceSlicePackTileCollectivePermuteSplitVDynamicPartition   )PadRandomUniformIntGreaterEqual   )SumAddV2AddAddNBiasAddCrossReplicaSumr+   )NegSub   )
MulSquareMatMulRandomUniformSelectMaximumMeanVarianceExpRsqrt    )op_types    r@   op_priorityr      s      > >  E E  D D ;; MM   @ @  
rB   c                    t        j                  t              }g }t        j                  |       D ]  }|j	                  d      st        |j                  j                        dk7  r+t        dt        |j                  j                        z        |j                  }||xx   dz  cc<   ||   dz
  }t        |      }||k(  r+t        j                  t              }|j                  |       n||kD  rt        d||fz        |j                  j                  d   }|j                  }	|j                  j                  j                  D 
cg c]  }
|
j                    }}
t#        j$                  |j                  j&                  t)        j*                  |j                  j,                        j/                               j1                  |      }|||   |   |	<    |S c c}
w )a  Reads the event file written by tensor tracer.

  This can be used to read the full tensors written into binary event files by
  by TensorTracer with trace_mode=full_tensor_summary.

  Example usage:
    result_dict_list = tensor_tracer.read_tensor_tracer_event_file(
      event_file_path)
    for result_dict in result_dict_list:
      for step, tensor_dict in result_dict.items():
        for tensor_name, full_tensor_content in tensor_dict.items():
          logging.info(tensor_name, full_tensor_content)

  Args:
    event_file: Path to the event file that contains only tensor tracer events.
  Returns:
    A list of event dictionaries, each of which with the form:
    {step_number: {tensor_name: tensor_content}}. This is a list instead of
    a single event dictionary because it is possible that an event file may
    have multiple event traces, each of them covering the same step ranges.
  Raises:
    ValueError: If an unexpected trace is found.
  summaryr   z3Single step contains %d summary values, expected 1.z6Unexpected: occurrence_idx (%d) > occurrence_size (%d)r   )collectionsdefaultdictintr#   HasFieldlenr   rM   
ValueErrorstepdictappendtagr   tensor_shapedimsizenp
frombuffertensor_contentr   DTypedtypeas_numpy_dtypereshape)
event_filestep_occurrence_countstep_occurrence_listtrace_eventr   occurrence_idxoccurrence_sizenew_occurrencetensor_valuetensor_named
real_shaper   s                r@   read_tensor_tracer_event_filer     s   4 &11#6 %66zB !Mk	*
;$$%* &(+K,?,?,E,E(FG H HD$1$*4014N./O( #..t4n!!.1 
/	) 03A3B3DD E 	E &&,,Q/L""K"."5"5"B"B"F"FGQ!&&GJG]]**\((../>>@
 '*
  ?M(.{;C!MD 
 Hs   Gc                     || j                   }| j                  j                  t               | j                  j	                  t        | |f       | S )ay  Programmatic interface to trace a tensor with Tensor Tracer.

  Tensor Tracer, by default, traces all tensors in the execution. This function
  can be used to limit traced tensors. If this function is called for a subset
  of the tensors, only those will be traced.

  For example, Tensor Traacer will only trace c below.
    c = tf.MatMul(a, b)
    tensor_tracer.trace_tensor(c)
    d = tf.add(c, 1)
  Args:
     tensor: the tensor object for which the tracing is requested.
     tracepoint_name: an optional tensor tracepoint name string. A tracepoint
       name is an Tensor Tracer internal name for the tensor. It is useful when
       comparing equivalent traces from different models that have different
       tensor namings. Equivalent tensors (with different names) can be mapped
       to each other by assigning a common tracepoint_name.

  Returns:
    The provided tensor.
  )namer>   get_collection_TENSOR_TRACER_COLLECTIONadd_to_collection)r   tracepoint_names     r@   trace_tensorr   Q  sH    , kkO,,78,,  !:"(/!:<	-rB   c                    	 | j                   }t        j                  |      rt        |d|z         | S d}|D ]-  }t        j                  |      rt        |d||fz         |dz  }/ 	 | S # t        $ r Y | S t
        $ r Y | S w xY w)a  An interface for adding the tensor outputs of a keras layer.

  Encapsulates trace_tensor.

  Args:
     layer: A keras layer.
     checkpoint_name: a string name for the checkpoint. This name has to be a
     unique name if used within model comparison. The tensors that have the same
     checkpoint identifier is compared in model comparison.

  Returns:
    The provided layer.
  z%sr   z%s_%dr   )outputr   
is_tf_typer   AttributeErrorr7   )layercheckpoint_nameoutputsidxoutput_tensors        r@   keras_layer_tracepointr   o  s    	llGg&7DO45 
, c" -!!'*
}g#0F&F
Gq 
,	 
 	 
, 
 		,	s   0A+ 4A+ +	B7BBc                   6   e Zd ZdZ e       Zed        Zed        Zed        Z	ed        Z
ed        Zed        Zed        Zed	        Zed
        Zd Zed        Zd Zd Zd Zd Zd Zdej0                  fdZdej0                  fdZd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d  Z&d! Z'd" Z(d# Z)d$ Z*d% Z+d& Z,d' Z-d( Z.d) Z/d* Z0d+ Z1d, Z2d- Z3d. Z4d/ Z5d0 Z6d1 Z7d2 Z8d3 Z9d4 Z:d5 Z;d6 Z<d7 Z=d8 Z>d9 Z?d: Z@d; ZAd< ZB	 	 d@d=ZC	 	 	 	 dAd>ZDdBd?ZEy)CTensorTraceran  A software construct for tracing tensor values in a TF graph.

  This utility is disabled by default. It is hooked into tpu.rewrite, so it can
  easily be enabled on TPUs by setting the TENSOR_TRACER_FLAGS env variable as
  below without a code change.
    export TENSOR_TRACER_FLAGS="--enable=1"

  Below is the use example to enable it on CPUs or GPUs, or for more advance use
  cases on TPUs.

    a = x + 1
    b = a * 2
    rs = tf.reduce_sum(b)
    tensor_tracer.set_parameters({'trace_dir': 'path/to/trace_dir',
                             'report_file: 'path/to/report/file'})
    tt = tensor_tracer.TensorTracer()
    if on_tpu:
      rs = tt.trace_tpu(tf.get_default_graph(),
                          tensor_fetches=rs)
    else:
      rs = tt.trace_cpu(tf.get_default_graph(),
                          tensor_fetches=rs)
    session.run(rs)

  If it is enabled, it will trace the output tensor values of
  selected Ops in the graph. It has two outputs: (1) the traces and (2)
  a report. The traces are dumped to a specified directory during the graph
  execution, while the report is dumped during the graph construction.
  By passing options via the env variable, users can change:
     (1) the trace mode (e.g., detecting NaN/Inf, printing partial or
         full tensor values)
     (2) which Ops to be traced (via op.name or op.type)
     (3) output trace file path.

  c                      	 t        j                         j                         } | r$t        j	                  d      j                  d       | S # t        t        f$ r }t        j                  d|       Y d}~yd}~ww xY w)z(Returns True if TensorTracer is enabled.
is_enabledTzKTensor Tracer V1 flags processing error encountered in is_enabled check. %sN)
r$   TTParametersr   tt_gaugeget_cellsetr   r7   loggingwarning)enablees     r@   r   zTensorTracer.is_enabled  sl    "//1<<>f	""<044T:m% oo
 s   A	A A;A66A;c                 <    | t         t        fvrt        d| z        y)z)Checks if the given device type is valid.zInvalid device_type "%s"N)_DEVICE_TYPE_TPU_DEVICE_TYPE_CPUr   )device_types    r@   check_device_typezTensorTracer.check_device_type  s)     +-=>>1+=>> ?rB   c                 b    |t         j                  k(  r| t        k7  rt        d| d|d      yy)zChecks if the given trace mode work on the given device type.

    Args:
      device_type: Device type, TPU, GPU, CPU.
      trace_mode: Tensor tracer trace mode.
    Raises:
      ValueError: If the given trace mode is not supported for the device.
    zDevice_type "z'" is not yet supported for trace mode ""N)r$   TRACE_MODE_FULL_TENSOR_SUMMARYr   r   )r   
trace_modes     r@   check_trace_modezTensorTracer.check_trace_mode  sA     (GGG	(	(.9:G H 	H 
) HrB   c                     | j                   dv S )N)LoopCondRefLoopCondtypeops    r@   loop_cond_opzTensorTracer.loop_cond_op  s    77111rB   c                    t        j                  |       xsj t        j                  |       xsS t        j                  |       xs< t        j                  |       xs% t
        j                  |       xs | j                  dv S )  Returns true if op is one of the special ops of in a while loop.

    Args:
       op: A tf.Operation.

    Returns:
       True if the given op is one of [Switch, Merge, Enter, Exit,
       NextIteration, LoopCond], which are all building blocks for TF while
       loops.
    )RefNextIterationNextIteration)r   IsLoopSwitchIsLoopMergeIsLoopEnter
IsLoopExitr   r   r   r   s    r@   while_loop_opzTensorTracer.while_loop_op  s{     ++B/ >**2.>**2.> ))"-> &&r*	>
 WW==?rB   c                 Z    t        j                  |       xs t        j                  |       S )r   )r   IsSwitchIsMerger   s    r@   control_flow_opzTensorTracer.control_flow_op  s*     ''+ +&&r*,rB   c                 $    | j                   dk(  ryy)z1Returns True if this op is not safe to be traced.AssignTFr   r   s    r@   	unsafe_opzTensorTracer.unsafe_op  s     
ww(rB   c                 b    | t         k(  r&t        j                  |j                  j                  vS y)NF)r   r&   _TPU_REPLICATE_ATTRnode_defattr)r   r   s     r@   device_mismatchzTensorTracer.device_mismatch  s)    &&008H8HHHrB   c                 @    | j                   dv ry| j                   dv ryy)zEReturn true if scalar output tensor from Op is not safe to be traced.)r   EnterMergerP   SwitchLessReadVariableOpT)VarHandleOpIteratorToStringHandleIteratorGetNextOneShotIterator
IteratorV2MakeIteratorBatchDatasetV2
MapDatasetFixedLengthRecordDatasetTakeDataset
ZipDatasetPlaceholderPlaceholderWithDefaultre   Fr   r   s    r@   unsafe_scalar_tracez TensorTracer.unsafe_scalar_trace  s5    
 
ww 7 7 
ww L L rB   c                 Z    t        |j                        | j                  j                  k  S )zDReturns True if the given op is not an interesting one to be traced.)r   r   _parameterstrace_levelselfr   s     r@   _is_interesting_opzTensorTracer._is_interesting_op&  s"    rww4#3#3#?#???rB   c                     d| |fz  S )z5Returns reason why the Op at op_idx is traced or not.z%d %sr   )op_idxdetailss     r@   reasonzTensorTracer.reason*  s     FG$$$rB   c                     d| _         t        j                         | _        t	        j
                         | _        i | _        i | _        i | _	        t               | _        d| _        i | _        d| _        d| _        y)zoInitializes a TensorTracer.

    Sets the various member fields from the flags (if given) or the defaults.
    N )_replica_idr%   TensorTracerConfig
_tt_configr$   r   r  _host_call_fn_cache_variables_history_value_cacher   _traced_op_names_report_proto_temp_cache_var_report_proto_path_outmost_contextr  s    r@   __init__zTensorTracer.__init__0  sq    
 D*==?DO*779DD D "DEDDD D DrB   c                 H    | j                   r| j                   S t        d      )a  Getter for tensor_tracer.proto object for summary and full_tensor_summary modes.

    Returns:
      A tensor_tracer.proto object.
    Raises:
      ValueError if called before tracing happens, or when trace mode is not
      summary or full_tensor_summary.
    zuCall to report_proto must be done after tracing.Report proto only exists for trace_mode=[summary|full_tensor_summary])r  r   r  s    r@   report_protozTensorTracer.report_protoE  s.      B C CrB   c                     | j                   S )zkGetter for path where tensor_tracer.proto object should be written.

    Returns:
      A string path.
    )r  r  s    r@   report_proto_pathzTensorTracer.report_proto_pathU  s     """rB   c                 F    |j                  dd      j                  dd      S )N/_:)replace)r  variable_names     r@   _escape_namescopeszTensorTracer._escape_namescopes]  s"      c*223<<rB   c                 Z    || j                   vri | j                   |<   | j                   |   S N)r  )r  r>   s     r@   _cache_variable_for_graphz&TensorTracer._cache_variable_for_graph`  s1    D)))%'dE"  ''rB   Nc                 n   |t        d      || j                  vri | j                  |<   || j                  |   vr|t        d      |j                  rt        t              }nt        }|j                         5 }|j                  d      5  t        j                  d| j                  |      z   ||t        j                  |      ddt        t        j                  j                  g      | j                  |   |<   ddd       ddd       | j                  |   |   S # 1 sw Y   #xY w# 1 sw Y   'xY w)a  Creates a variable as the cache to store historic intermediate tensor values.

    Args:
      cache_name: Name to be given to the cache (an instance of tf.variable).
      graph: Tensorflow graph.
      shape: A list of dimensions.
      dtype: Data type of created cache.
    Returns:
      A ref to newly created or existing cache with the given dimensions.
    Raises:
      ValueError:
        (1) If graph is None, or
        (2) shape is None when a new cache needs to be created.
    NInvalid graph.)shape must be provided at cache creation.tt_history_FTshaper   initializer	trainableuse_resourcer   )r   r  
is_integerr   _COMPACT_TRACE_ENTRY_INIT_VALUE
as_default
name_scoper   get_variabler%  r   constant_initializer_TENSOR_TRACER_STORAGEr
   	GraphKeysLOCAL_VARIABLES)r  
cache_namer>   r.  r   init_valgs          r@   *_create_or_get_tensor_history_values_cachez7TensorTracer._create_or_get_tensor_history_values_cachee  s4   & }'((D---)+d&22599	DEE			672  ALL$6 (55"T%<%<Z%HH$99(C!*CMM,I,I	 	!!%(	  $$U+J77   s%   ;D+A0D=D+D(	$D++D4c                 (   |t        d      | j                  |      }||vr|t        d      |j                  rt        t              }nt        }|j                         5 }|j                  d      5  t        j                  t        dz   | j                  |      z   ||t        j                  |      ddt        t        j                  j                   g      ||<   ddd       ddd       ||   S ||   S # 1 sw Y   xY w# 1 sw Y   ||   S xY w)a  Creates a variable as the cache to store intermediate tensor values.

    Args:
      cache_name: Name to be given to the cache (an instance of tf.variable).
      graph: Tensorflow graph.
      shape: A list of dimensions.
      dtype: Data type of created cache.
    Returns:
      A ref to newly created or existing cache with the given dimensions.
    Raises:
      ValueError:
        (1) If graph is None, or
        (2) shape is None when a new cache needs to be created.
    Nr*  r+  r!  FTr-  )r   r(  r2  r   r3  r4  r5  r   r6  _TT_SNAPSHOTr%  r   r7  r8  r
   r9  r:  )r  r;  r>   r.  r   graph_cache_varr<  r=  s           r@   "_create_or_get_tensor_values_cachez/TensorTracer._create_or_get_tensor_values_cache  s     }'((44U;O(	DEE			672  QALL$6 Q&4&A&A3!8!8!DDu 55h?/1N1NO'Q
#Q Q :&&?:&&Q Q Q :&&s%   "D4A*C8D8D	=DDc                    | j                   j                  rat        j                  d      5  t	        j
                  t        t        | j                   j                              d      | _        ddd       yd| _        y# 1 sw Y   yxY w)z5Adds nodes for computing the replica ID to the graph.Ntt_replica_idr   unknown)	r  num_replicasr
   control_dependenciesr'   tpu_replicated_inputlistranger  r  s    r@   _add_replica_id_to_graphz%TensorTracer._add_replica_id_to_graph  sp     ####D) ""77t3345 "" " #d" "s   AB  B	c                     || j                   j                  d   k  ry| j                   j                  d   dk  xs || j                   j                  d   k  S )z<Return True if the given index is inside the selected range.r   Fr   )r  op_range)r  r   s     r@   _inside_op_rangezTensorTracer._inside_op_range  s\     T&&q))%%a(1, 04##,,Q//1rB   c                     | j                   j                  D ]  }|j                  |j                        s y | j                   j                  D ]  }|j                  |j
                        s y y)a  Checks whether the op is included in the tensor tracer flags.

    Args:
      op: tf Operation
    Returns:
      True, if the op is included.
      An op is included if:
      - Its op name is given in included_opnames
      - Its op type is given in included_optypes
      - The op is at most _trace_ops_before_included hops before an included op
      - The op is at most _trace_ops_after_included hops after an included op
    TF)r  included_opname_re_listmatchr   included_optype_re_listr   r  r   	opname_re	optype_res       r@   _is_user_included_opz!TensorTracer._is_user_included_op  sg     %%== 			! %%== 			! rB   c                     | j                   j                  D ]  }|j                  |j                        s y | j                   j                  D ]  }|j                  |j
                        s y y)NTF)r  excluded_opname_re_listrR  r   excluded_optype_re_listr   rT  s       r@   _is_user_excluded_opz!TensorTracer._is_user_excluded_op  se    %%== 			! %%== 			! rB   c                 j   | j                   j                  t        t        j                  t        j
                  t        j                  t        j                  g      v r| j                   j                  diS | j                   j                  t        j                  k(  r| j                   j                  S i S )z^Returns a dictionary holding the order of signatures in the cache for the selected trace mode.r   )
r  r   r   r$   TRACE_MODE_NAN_INFTRACE_MODE_NORMTRACE_MODE_HISTORYTRACE_MODE_MAX_ABSTRACE_MODE_SUMMARYsummary_signaturesr  s    r@   _signature_typeszTensorTracer._signature_types  s    ""c..++....	+0 '1 1
 ))1--""&9&L&LL000IrB   c                 4    t        | j                               S r'  )r   rc  r  s    r@   _num_signature_dimensionsz&TensorTracer._num_signature_dimensions  s    t$$&''rB   c                 r    | j                         ry| j                         r| j                  j                  S y)zReturns true if the intermediate values should be stacked instead of being stored in a tf.Variable.

    Returns:
      A boolean, denoting whether to use a temporary cache or not.
    F)_use_tensor_buffer_use_tensor_values_cacher  use_temp_cache_varr  s    r@   _use_temp_cachezTensorTracer._use_temp_cache   s6      $$&000 rB   c                 .    | j                   j                  S )zCReturns True if immediate tensors should be first saved to a cache.)r  use_compact_tracer  s    r@   rh  z%TensorTracer._use_tensor_values_cache  s    ---rB   c                 P    | j                   j                  t        j                  k(  S )zGReturns true if the whole tensor needs to be cached/buffered in memory.)r  r   r$   r   r  s    r@   rg  zTensorTracer._use_tensor_buffer  s%    ''>>? @rB   c                 ^   g }| j                         dkD  ra| j                         t        |j                         fd      D ]  \  }}|j	                  |        t        j                  |dd      }|S | j                         dk(  r|j                         \  \  }}|}|S t        d      )aA  Returns a tensor that merges the given signatures.

    Args:
      signatures: A dictionary of the signature updates from signature name to
      a tensor of dimension [1].
    Returns:
      A tensor that concats the signature values in a predefined order.
    Raises:
      ValueError: Unable to merge signatures.
    r   c                     | d      S )Nr   r   )itemsignature_indicess    r@   <lambda>z7TensorTracer._merge_tensor_signatures.<locals>.<lambda>)  s    ,=d1g,F rB   rL   r   merge_single_op_signaturesaxisr   zHCannot merge 0 signatures. Check the value passed for flag --signatures.)re  rc  sortedrF   r   r   stackr   )r  
signaturessorted_updater!  valupdatesrq  s         @r@   _merge_tensor_signaturesz%TensorTracer._merge_tensor_signatures  s     M%%'!+//1:++-FH "&!SS!"  %%
a&BDg N 
	'	'	)Q	.""$ihq#g N  , - -rB   c                 H   | j                  |      }t        j                  || j                         g      }|| j                  vrt        d      |t        | j                  |         k\  r&t        d|t        | j                  |         fz        || j                  |   |<   y)a  Returns an op that will save the given updates to an entry in the cache.

    Args:
      cache_idx: The cache index of the tensor within the cache.
      updates: A dictionary of the signature updates from signature name to
      a tensor of dimension [1].
      graph: A TensorFlow graph.
    Raises:
      RuntimeError:
        (1) graph is not already in self._temp_cache_var, or
        (2) cache_idx is out of range.
    $graph is not in self._temp_cache_varz#cache_idx (%d) is out of range (%d)N)r}  r   r   re  r  r7   r   )r  	cache_idxr|  r>   s       r@   _save_tensor_value_to_tmp_cachez,TensorTracer._save_tensor_value_to_tmp_cache6  s     ++G4G!%!?!?!A BDGD(((?@@C,,U344>
S--e45B7 7 8 8-4D	*rB   c                 
   | j                  |      }t        j                  |d| j                         g      }t	        j
                  |g      }| j                  t        |      }t        j                  |||      j                  S )a  Returns an op that will save the given updates to an entry in the cache.

    Args:
      cache_idx: The cache index of the tensor within the cache.
      updates: A dictionary of the signature updates.
      graph: A TensorFlow graph.
    Returns:
      Cache update operation.
    r   )r}  r   r   re  r   constantrB  _TT_SUMMARY_TAGr   scatter_updater   )r  r  r|  r>   indicescaches         r@   _save_tensor_value_to_cache_opz+TensorTracer._save_tensor_value_to_cache_opM  sw     ++G4G!"D$B$B$D EGG""I;/G33OUKE##E7G<???rB   c                     | j                  |j                  |j                  j                  |j                  j                         |j                        }t        j                  ||      j                  S )zCreates a new tf.Variable and a new tf.Operation that assigns the value of the tensor to this variable.

    Args:
      tensor: tensor whose values will be stored in a new tf.Variable.
    Returns:
      An assignment operation.
    )	rB  r   r   r>   r.  as_listr   r   assign)r  r   snapshot_variables      r@   _snapshot_tensorzTensorTracer._snapshot_tensora  sX     ??VYY__. -v6999rB   c                    d }ddfd}dfd	}dfd	}dfd	}dfd	}dd}dfd		}	| j                   j                  t        j                  k(  r| j                   j                   ||      iS | j                   j                  t        j                  k(  r| j                   j                  |iS | j                   j                  t        j
                  t        j                  fv r| j                   j                  |iS | j                   j                  t        j                  k(  r3| j                   j                  t        j                   ||      d
g      iS | j                   j                  t        j                  k(  r3| j                   j                  t        j                   ||      d
g      iS | j                   j                  t        j                  k(  r| j                   j                   |	|      iS | j                   j                  t        j                  k(  r2t        j                  |t        j                         }i }
t"        | j%                         v st&        | j%                         v r ||d      \  }}t)        | j%                         j+                         d       D ]  \  }}|t,        k(  r ||d      }n|t.        k(  r ||d      }nr|t0        k(  r |	|d      }n^|t2        k(  r ||d      }nJ|t4        k(  r	 ||      }n8|t6        k(  r	 ||      }n&|t"        k(  r}n|t&        k(  r}nt9        d|z        ||
|<    |
S t;        d| j                   j                  z        )zComputes NAN/Norm/Max on TPUs before sending to CPU.

    Args:
      tensor: The tensor to be traced.
    Returns:
      A tensor that should be input to the trace_function.
    Raises:
      RuntimeError: If the signature is invalid.
    c                 2   | j                   j                  rjt        j                  t	        j
                  t	        j                  |       t	        j                  |                   }t        j                  |d d       }|S t        j                  dg      }|S )z7Trace function for detecting any NaN/Inf in the tensor.c                  .    t        j                  dg      S )Ng      ?r   r  r   rB   r@   rr  zQTensorTracer._preprocess_traced_tensor.<locals>._detect_nan_inf.<locals>.<lambda>      K((#/ rB   c                  .    t        j                  dg      S )N        r  r   rB   r@   rr  zQTensorTracer._preprocess_traced_tensor.<locals>._detect_nan_inf.<locals>.<lambda>  r  rB   r  )r   is_floatingr   
reduce_anyr   
logical_oris_nanis_infr   r   r  )r   maskr   s      r@   _detect_nan_infz?TensorTracer._preprocess_traced_tensor.<locals>._detect_nan_infz  s     
	!	!""####F+\-@-@-HJK 		//1  $,,cU3rB   c                     |r$t        j                  | t        j                        }  ||       }|j	                         j                         st        j                  |g       }|S r'  )r   castr   float32	get_shapeis_fully_definedr   r   )r   tf_opcast_to_f32r   s       r@   _compute_signaturezBTensorTracer._preprocess_traced_tensor.<locals>._compute_signature  sQ    	vv~~6Fmm $$&779!))-<rB   c                 ~     | t         j                  d      }t        j                  |t        j
                        S )NFr  )r   r   r   r  r   r  )r   tsizer  s     r@   
_show_sizez:TensorTracer._preprocess_traced_tensor.<locals>._show_size  s.    
 !UKe ]]5&..11rB   c                 4     | t         j                  |      S r'  )r   
reduce_maxr   r  r  s     r@   	_show_maxz9TensorTracer._preprocess_traced_tensor.<locals>._show_max      (;(;[IIrB   c                 4     | t         j                  |      S r'  )r   
reduce_minr  s     r@   	_show_minz9TensorTracer._preprocess_traced_tensor.<locals>._show_min  r  rB   c                 4     | t         j                  |      S r'  )r   normr  s     r@   
_show_normz:TensorTracer._preprocess_traced_tensor.<locals>._show_norm  s    
EErB   c                 $    fd} | ||      S )Nc                     t        j                  t        j                  |             }t        j                  |       }t	        j
                  t        j                  ||            S r'  )r   greater_equalabsr  r   zero_fractionr  )r   	non_zerosnans	tolerances      r@   sparsity_fnzSTensorTracer._preprocess_traced_tensor.<locals>._show_sparsity.<locals>.sparsity_fn  sJ    **8<<+?K	v&$$X%8%8D%IJJrB   r   )r   r  r  r  r  s     ` r@   _show_sparsityz>TensorTracer._preprocess_traced_tensor.<locals>._show_sparsity  s    K
  [AArB   c                    |r$t        j                  | t        j                        } t	        j
                  t        j                  | dg      dg      \  }}|j                         j                         st        j                  |g       }|j                         j                         st        j                  |g       }||fS )z2Returns the mean and variance of the given tensor.r   )axes)
r   r  r   r  r   momentsr   r   r  r  )r   r  meanvars       r@   _show_mean_and_variancezGTensorTracer._preprocess_traced_tensor.<locals>._show_mean_and_variance  s    	vv~~6//)"3"3FRD"ALidC^^..0  r*]]_--/R(3YrB   c                      | d |      S )Nc                 R    t        j                  t        j                  |             S r'  )r   r  r  )ts    r@   rr  zOTensorTracer._preprocess_traced_tensor.<locals>._show_max_abs.<locals>.<lambda>  s    H//Q@ rB   r   r  s     r@   _show_max_absz=TensorTracer._preprocess_traced_tensor.<locals>._show_max_abs  s    
@+O OrB   r   Fr  c                     | d   S Nr   r   )xs    r@   rr  z8TensorTracer._preprocess_traced_tensor.<locals>.<lambda>  s
    AaD rB   rs  zUnknown signature type :%s.z(Unsupported signature for trace mode %s.)T)Tgư>)r  r   r$   r]  TRACE_MODE_PART_TENSORTRACE_MODE_FULL_TENSORr   r^  r   r   r_  r`  ra  r   r  r   r  _TT_SUMMARY_MEANrc  _TT_SUMMARY_VARrw  rF   _TT_SUMMARY_NORM_TT_SUMMARY_MAX_TT_SUMMARY_MAX_ABS_TT_SUMMARY_MIN_TT_SUMMARY_SPARSITY_TT_SUMMARY_SIZEr   r7   )r  r   r  r  r  r  r  r  r  r  result_dictr  variancesignature_namer!  signature_result_tensorr  s                   @r@   _preprocess_traced_tensorz&TensorTracer._preprocess_traced_tensoro  s1   2JJFBO ""&9&L&LL))?6+BCC##22	3))622##22::(< 	< ))622""&9&I&II))9+<+<
V
qc,# $ $""&9&L&LL))9+<+<
V
qc,# $ $""&9&L&LL))=+@AA""&9&L&LL}}VV^^4fk d335
5
T224
40UKh%d&;&;&=&C&C&E*8 : >
.!--$.v5$I
!.$-f%$H
!22$1&e$L
!.$-f%$H
!33$26$:
!//$.v$6
!//$(
!.$,
!8>IJ
J&=N#+>, 
2



%
%	&' 'rB   c                      fdfd}fd} j                   j                  t        j                  k(  r|S  j                   j                  t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  fv r|S t        d j                   j                  z        )al  Makes the tensor tracing function called by outside compilation.

    Args:
      tensor_name: name of the tensor being traced.
      tensor_trace_order: TensorTraceOrder object holding tensorname to id map.
    Returns:
      A function to be passed as the first argument to outside compilation.

    Raises:
      RuntimeError: If the trace mode is invalid.
    c                    j                   j                         r3| j                  vrt        d|d| d      dj                  |    z  }nd| z  }j                   j                  rSt
        j                  j                  j                   j                  t        j                         z         }t        |z   }nt        j                  }t        j                  |t        j                   |      dj"                  d|d||	      S )	a  Prints a tensor value to a file.

      Args:
        tensor_name: name of the tensor being traced.
        num_elements: number of elements to print (-1 means print all).
        tensor: the tensor needs to be returned.
        output_tensor: the tensor needs to be printed.

      Returns:
        The same tensor passed via the "tensor" argument.

      Raises:
        ValueError: If tensor_name is not already in
                    tensor_trace_order.tensorname_to_cache_idx.
      zTensor z with name z& is not in the tensorname_to_cache_idx%dz"%s"@
	summarizeoutput_stream)r  is_brief_modetensorname_to_cache_idxr   	trace_dirrG   pathjoin_TRACE_FILE_NAME_get_outfile_suffix_OUTPUT_STREAM_ESCAPEsysstderrr   print_v2r   r.  r  )	r   num_elementsr   r   msgoutput_pathr  r  tensor_trace_orders	          r@   _print_tensorz:TensorTracer._make_tensor_trace_fun.<locals>._print_tensor  s    " 
			'	'	)0HHH{$% % '??LL{"				#	#ggll&&t7799; .;

!!#y}'E"%t'7'7"&t,80=	? ?rB   c                 "     t         | |       S )z/Trace function for printing part of the tensor.)_TRACE_MODE_PART_TENSOR_SIZEr   r  r   s    r@   _show_part_tensorz>TensorTracer._make_tensor_trace_fun.<locals>._show_part_tensor.  s     ;(D!6+ +rB   c                      d| |       S )z.Trace function for printing the entire tensor.r  r   r  s    r@   _show_full_tensorz>TensorTracer._make_tensor_trace_fun.<locals>._show_full_tensor4  s     ;FF;;rB   z7Full tensor support is not available with trace mode %s)r  r   r$   r  r]  r^  r  r`  ra  r_  r7   )r  r   r  r  r  r  s   ```  @r@   _make_tensor_trace_funz#TensorTracer._make_tensor_trace_fun  s    %?N+<
 	##22	3
 ""..++22......'
 
 
P((334 5 5rB   c                 ,    t        j                  |      S )zReturns true if the given op is inside a tf.cond or in tf.while_loop.

    Args:
      op: A tensorflow op that should be checked whether in control flow or not.
    Returns:
      A boolean value whether the op is in control flow or not.
    )r   IsInCondr  s     r@   _is_in_control_flowz TensorTracer._is_in_control_flowM  s     %%b))rB   c                     | j                  |      }t        j                  |      }|t        j                  | j                        k(  S )zReturns true if the op is at the same level with the training loop.

    Returns false if the op is in an inner while loop or if it is outside of the
    training loop.
    Args:
      op: tf.Operation

    Returns:
      A boolean.
    )_get_op_control_flow_contextr   GetContainingWhileContextr  )r  r   ctxtouter_while_contexts       r@   _is_in_outmost_while_loopz&TensorTracer._is_in_outmost_while_loopW  sI     ,,R0D+EEdK"3"M"M#  rB   c                     | j                         ry| j                  j                  t        k(  r"| j	                         xs | j                         S y)zMReturns false incase it is not safe to trace ops in tf.cond or tf.while_loop.FT)rj  r  r   r   rh  rg  r  s    r@   _should_trace_in_control_flowz*TensorTracer._should_trace_in_control_flowg  sH    
 		$	$(8	8 **,I0G0G0IIrB   c                    t         j                  |      r+|j                  |t         j                  |t                     yt         j                  |      r+|j                  |t         j                  |t                     yt         j                  |      r+|j                  |t         j                  |t                     yt         j                  | j                  j                  |      r+|j                  |t         j                  |t                     y||vr+|j                  |t         j                  |t                     y| j                  |      s| j                  |      s;| j!                         s+|j                  |t         j                  |t"                     y| j%                  |      re|j                  |t         j                  |t&                     t(        j*                  j,                  r t/        j0                  d|j2                         y| j5                  |      s+|j                  |t         j                  |t6                     y| j9                  |      s+|j                  |t         j                  |t:                     y| j=                  |      re|j                  |t         j                  |t>                     t(        j*                  j,                  r t/        j0                  d|j2                         yy)aU  Returns True if we should not trace Op.

    Args:
      op_id: Topological index of the op.
      op: tf.Operation
      ops_in_exec_path: Set of operations that are in the execution path.
      report_handler: An instance of tensor_tracer_report.TTReportHandle.
    Returns:
      True if the op should not be traced, false otherwise.
    TzUSER_INCLUDED op %sFzUSER_EXCLUDED op %s) r   r   instrument_opr  _REASON_WHILELOOP_OPr   _REASON_CONTROLFLOW_OPr   _REASON_UNSAFE_OPr   r  r   _REASON_DEVICE_MISMATCH_REASON_NOT_EXECUTEDr  r  r  _REASON_IN_CONTROL_FLOWrW  _REASON_USER_INCLUDEDr$   TT_CHECK_FILTERrM   r   infor   rO  _REASON_OUTSIDE_OP_RANGEr  _REASON_LESS_INTERESTING_OPr[  _REASON_USER_EXCLUDED)r  op_idr   ops_in_exec_pathreport_handlers        r@   _skip_opzTensorTracer._skip_opu  sj    !!"%""
l!!%)=>@##B'""
l!!%)?@Bb!""
l!!%):;=##DOO$?$?D""
l!!%)@AC	!!""
l!!%)=>@ #4+I+I"+M//1$$##E+BC	E  $""
l!!%)>?A		,	,	2	2*BGG4  '""
l!!%)ABD""2&""
l!!%)DEG  $""
l!!%)>?A		,	,	2	2*BGG4rB   c                 N   t        t        j                  t        j                  t        j                  g      }|j
                  |v r+|j                  |t        j                  |t                     y|j                         D cg c]  }t        j                  |      r| c}r+|j                  |t        j                  |t                     y| j                  |j                        re|j                  |t        j                  |t                     t         j"                  j$                  r t'        j(                  d|j*                         y| j-                  |j                        re|j                  |t        j                  |t.                     t         j"                  j$                  r t'        j(                  d|j*                         y|j1                         j3                         s| j4                  j6                  t         j8                  t         j:                  t         j<                  t         j>                  t         j@                  fv r+|j                  |t        j                  |tB                     y|j                  |t        j                  |tD                     ytG        |jH                        }|dk  r| j4                  jJ                  rut        jM                  |j                        r+|j                  |t        j                  |tN                     y|j                  |t        j                  |tP                     y|j                  |t        j                  |tR                     y|j                  |t        j                  |tB                     yc c}w )a-  Returns True if we should not trace out_tensor.

    Args:
      op_id: Topological index of the op producing tensor.
      out_tensor: tf.Tensor
      report_handler: An instance of tensor_tracer_report.TTReportHandle.
    Returns:
      True if the tensor should not be traced, false otherwise.
    TzUSER_INCLUDED tensor %sFzUSER_EXCLUDED tensor %sr   )*r   r   variantresourcestringr   instrument_tensorr   r  _REASON_NON_NUMERIC_TENSOR	consumersr   _REASON_FEEDS_WHILELOOP_OPrW  r   r  r$   r  rM   r   r  r   r[  r  r  r  r  r   r]  r^  r_  r`  ra  _REASON_TENSOR_GET_TRACED_REASON_DYNAMIC_SHAPEr   r.  trace_scalar_opsr  _REASON_UNSAFE_SCALAR_REASON_SCALAR_GET_TRACED_REASON_SKIP_SCALAR)r  r  
out_tensorr  non_numeric_tensor_typesconsumerranks          r@   _skip_tensorzTensorTracer._skip_tensor  s     #FNNFOO$*MM$3  433&&
l))%1KLN!+!5!5!7 .X""8, 	 .&&
l))%1KLN  /&&
l))%1FGI		,	,	2	2.
@  /&&
l))%1FGI		,	,	2	2.
@!224 
			$	$

0
0

-
-

0
0

0
0

0
0) 
 	((++E3LM	O((++E3HI	Kz Dax				*	*++JMM:

*
*,--e5JKM

*
*,--e5NOQ((++E3FG	I &&
l))%1JKMm.s   N"c                    |t        |      S t        |t        t        f      s|g}g }|D ]q  }t        |t        j
                        r|j                  |       /t        |t        j                        r|j                  |j                         et        d|z         t        |      }t        |      }	 |s	 |S |j                         }|j                  D cg c]  }|j                   }	}|	j                  |j                         |	D ]?  }
|
|vst        j!                  |
      r|j#                  |
       |j                  |
       A c c}w )zFReturns the set of ops in the execution path to compute given fetches.z-Given fetch:%s is neither a tensor nor an op.)r   
isinstancerJ  tupler
   	Operationr   
tensor_libTensorr   r7   popinputsextendcontrol_inputsr   r   add)r  
operationsfetches
op_fetchesfetchexecution_path_operationstraverse_stackhead_optensor_input	input_opsinput_ops              r@   !_filter_execution_path_operationsz.TensorTracer._filter_execution_path_operations
  sJ    _ge}-	gJ #	E3==	)% eZ../%((#J!" # 	## !$J*%N
 %$ ""$g7>~~F|<??FiFw--. *(44 &&x0
#
'
'
1



)*  Gs   Ec                    g }t        |D cg c]  \  }}|j                   c}}      }t        |j                        D ]x  \  }	}
|r|
|vr| j	                  |	|
||      r"t        t        |
j                              D ]6  }|
j                  |   }| j                  |	||      r&|j                  |       8 z |S c c}}w )a  Determines the tensors to trace and instruments the trace details.

    Args:
      graph_order: graph_order tuple containing graph (tf.graph), operations
        (list of operations), op_to_idx (op id mapping), (tensors) list of
        tensors, tensor_to_idx (tensor id mapping), contains_cycle (whether
        there is a cycle in the graph), topological_order_or_cycle (list of ops
        in topological order or list of ops creating a cycle).
      ops_in_exec_path: Set of ops in the execution path.
      tensor_trace_points: Collection of programatic tensor trace points.
      report_handler: An instance of tensor_tracer_report.TTReportHandle.
    Returns:
      List of tensors to be traced.
    )
r   r   	enumerater4  r  rK  r   r   r(  r   )r  graph_orderr  tensor_trace_pointsr  traced_tensorsr   r!  checkpoint_operationsr  r   ir$  s                r@   (_determine_and_instrument_traced_tensorsz5TensorTracer._determine_and_instrument_traced_tensors1  s    & N4G!I%0fa "( !I J{556 ,	r	2-B#B	ub"2N	CS_% ,!ZZ]
  
NC




+,, !Is   B=
c                    | j                   j                  sy| j                   j                  t        j                  k(  ryt        j                  | j                   j                        sut        j                  | j                   j                         t        j                  | j                   j                        s"t        d| j                   j                  z        yy)z9Checks if any requirements for trace files are satisfied.Nz&Failed to create trace directory at %s)
r  r  r   r$   ra  r    Existsr   recursive_create_dirr7   r  s    r@   _check_trace_fileszTensorTracer._check_trace_filesR  s     %%""&9&L&LL <<((223""4#3#3#=#=>\\$**445C++556 7 	7 6 4rB   c                     t        j                  t        t        j                  |g      }t        |      D cg c]  }| c}| j                  |<   yc c}w )a}  Creates a temporary cache with the given dimensions.

    Fills the self._temp_cache_var with num_traced_tensors tf.constant() ops
    that have shape of [num_signatures].
    Args:
      num_traced_tensors: Int, denoting total number of traced tensors.
      num_signatures: Int, denoting the number of statistics collected per
        tensors.
      graph: TensorFlow graph.
    r   r.  N)r   r  r3  r   r  rK  r  )r  num_traced_tensorsnum_signaturesr>   
init_valuer!  s         r@   _create_temp_cachezTensorTracer._create_temp_cacheb  sQ     %%&E,2NN-;,<>J ""45#7
#7D #7s   	Ac                    | j                          t        j                  |      }|j                  t              }t        j
                         }| j                  ||||      }t        j                  dt        |             |r%t        j                  j                  rt        d      t        j                  ||      }| j                         }	|	r| j!                         r| j#                         r| j%                  t        |      |	|       nj| j'                  t(        |t        |      |	g       | j*                  j,                  t        j.                  v r"| j1                  t(        |t        |      |	g       | j*                  j,                  t        j2                  t        j4                  fv rN|j7                  | j8                  | j*                  ||| j;                               | _        | j*                  j>                  rt@        jB                  jE                  | j*                  jF                  | j<                  jH                        | j*                  _#        t        j                  d| j*                  jF                         |jK                  | j*                  jF                  |      | _&        | j*                  jN                  tP        k7  r1|jS                  | jL                  | j<                  | j*                         |S | j*                  j,                  t        j.                  vr(|jU                  | j8                  | j*                  ||       |S )a  Work needs to be done prior to TPU or CPU tracing.

    Args:
      graph: tf.graph
      ops_in_exec_path: Set of operations in the execution path.
      graph_summary_tag: the summary tag name for the given graph.
    Returns:
      An instance of tensor_tracer_report.TensorTraceOrder, containing list of
      tensors to be traced with their topological order information.
    Raises:
      RuntimeError: If opname filtering is incorrectly set.
    z#TensorTracer is tracing %d tensors.z)Verify ops being traced by tensor tracer.z%TensorTracer updating trace_dir to %s)+rJ  r%   sort_tensors_and_opsr   r   TTReportHandlerF  r   r  r   r$   r  rM   r7   TensorTraceOrderre  rh  rj  rP  rB  r  r  r   r_  r>  ra  r   create_report_protor  rc  r  use_fingerprint_subdirrG   r  r  r  fingerprintr  r  report_file_path_SKIP_REPORT_FILEwrite_report_protocreate_report)
r  r>   r  graph_summary_tagrA  rB  r  rC  r  rN  s
             r@   "_determine_trace_and_create_reportz/TensorTracer._determine_trace_and_create_reports  s    	&;;EBK../HI)88:NBB%':NLNLL6N8KL-==CCDEE->>{?MO335N$779				N 3^UK//US%8.$I	K&&224

9
9us>':N&KM""..::'< < *==
//4++-?
t4468d 
			0	0%'WW\\&&(:(:(F(F&H"<%%//	1 . @ @



$
$&7!9d 
			*	*.?	?))$*A*A*.*<*<d>N>N	P 	 
			$	$

0
0
2$$T__d6F6F%79L	NrB   c                 n    | j                   j                  t        j                  t        j                  fv S r'  )r  r   r$   ra  r   r  s    r@   _create_host_callzTensorTracer._create_host_call  s2    &&..::+< < <rB   c                      fd}|j                   s%t        j                  d       t        j                  S  j
                  j                  t        j                  k(  r*t        j                  t        j                  |      d      }nOt        j                  t        j                  t        j                  |      t        j                   |                  }t#        j"                  |d d       } j
                  j$                  r#dd|dd	|d
|d j
                  j                  z  g	}	ndd	|d
|d j
                  j                  z  g}	t'        |j(                  j+                         d       D ]e  \  }
} j
                  j$                  r'|	j-                  dd|dd	|d|
d
 |||df         g
       C|	j-                  dd	|d|
d
 |||df         g       g t/        j0                  |	d|dS )a  Generates a print operation to print trace inspection.

    Args:
      cache: Tensor storing the trace results for the step.
      replica_id: Tensor storing the replica id of the running core.
      step_num: Step number.
      output_stream: Where to print the outputs, e.g., file path, or sys.stderr.
      tensor_trace_order: TensorTraceOrder object holding tensorname to id map.

    Returns:
      The Op to flush the cache to file.
    c                     j                   j                  t        j                  k(  r-t	        j                  t        j                  | d      d d       S | S )z5Returns the text to be printed for inspection output.r  c                       y)Nzhas NaNs/Infs!r   r   rB   r@   rr  zNTensorTracer._inspect_summary_cache.<locals>._inspect_tensor.<locals>.<lambda>      rB   c                       y)Nzhas no NaNs or Infs.r   r   rB   r@   rr  zNTensorTracer._inspect_summary_cache.<locals>._inspect_tensor.<locals>.<lambda>  rc  rB   )r  r   r$   r]  r   r   greater)r   r  s    r@   _inspect_tensorz<TensorTracer._inspect_summary_cache.<locals>._inspect_tensor  sN    



%
%

0
01yyVS)$*, 	,
 rB   z2Inspect mode has no tensors in the cache to check.r  c                       y)NzNaNs or Infs in the step!r   r   rB   r@   rr  z5TensorTracer._inspect_summary_cache.<locals>.<lambda>  rc  rB   c                       y)Nz1No numerical issues have been found for the step.r   r   rB   r@   rr  z5TensorTracer._inspect_summary_cache.<locals>.<lambda>  rc  rB   

core:,step:-->zPrinting tensors for mode:%s...c                     | d   S r  r   rp  s    r@   rr  z5TensorTracer._inspect_summary_cache.<locals>.<lambda>  
    a rB   rs  r  r   r  r  )rC  r   warnr   no_opr  r   r$   r]  r   re  
reduce_sumr  r   r  r  r  r   collect_summary_per_corerw  r  rF   r1  r   r  )r  r  
replica_idstep_numr  r  rf  step_has_nan_or_infstep_error_messagestatsr   r  s   `           r@   _inspect_summary_cachez#TensorTracer._inspect_summary_cache  s   	 ,,llGH### ""&9&L&LL$,,X-@-@-GM %//

!
!!!%(,*=*=e*DFG
 +CE 00w
C(E!043C3C3N3NNPe w%1C043C3C3N3NNPe #)2288: #" 
GY 
			2	2':sGXsil0C DF 	G 	'8Sil0C DF 	G
G ".;= =rB   c                    |j                   s%t        j                  d       t        j                  S dd|dd|gg }t        |j                  j                         d       D ]{  \  }}||df   }| j                  |j                  |j                  j                  |j                  j                         |j                        }	t        j                   |	g      5  t#        j$                  |	d	      }
d
d
d
       t        j                   
g      5  t'        j(                  |t*        j,                        }t'        j.                  t'        j0                  |
|            }t#        j2                  |	|      }|j5                  |       d
d
d
       t        j                   g      5  t#        j$                  |	d	      }d
d
d
       j7                  dd|dd|d|d|
g       ~ t9        j:                  |      }t'        j<                  |      }t?        j>                  t'        j@                  |tB        jD                  jF                        fdd       S # 1 sw Y   cxY w# 1 sw Y   xY w# 1 sw Y   xY w)a  Generates a conditional print operation to log differences in tensor values.

    Args:
      cache: Tensor storing the trace results for the step.
      replica_id: Tensor storing the replica id of the running core.
      step_num: Step number.
      tensor_trace_order: TensorTraceOrder object holding tensorname to id map.

    Returns:
      The Op to flush the cache to file.
    z5TT history mode has no tensors in the cache to check.ri  rj  rk  rl  c                     | d   S r  r   ro  s    r@   rr  z5TensorTracer._inspect_history_cache.<locals>.<lambda>  rp  rB   rs  r   r  Nr  rm  c                  .    t        j                   ddiS )Nr  r  )r   r  )ry  s   r@   rr  z5TensorTracer._inspect_history_cache.<locals>.<lambda>/  s    $$e:r: rB   c                  *    t        j                         S r'  )r   rr  r   rB   r@   rr  z5TensorTracer._inspect_history_cache.<locals>.<lambda>0  s     &&( rB   )$rC  r   rq  r   rr  rw  r  rF   r>  r   r   r>   r.  r  r   r
   rH  r   
assign_addr   r  r   r  r  subtractr  r   r1  r   rx  r  r   re  r$   DELTA_THRESHOLDrM   )r  r  ru  rv  r  diffsr   r  tensor_to_writer  	old_value	new_valuedeltaupdatednew_value_from_var
diff_stackstep_maxry  s                    @r@   _inspect_history_cachez#TensorTracer._inspect_history_cache  s;    ,,llJK###Wj#wAEE"(2288: #" EY il+oII


 2 2 8 8



'
'
)?+@+@B ##%6$78 A(():C@	A ##YK0 MM/6>>B	X..y)DE""#4i@U	
 ##WI. J&112CSIJ ll
S'8S
ui);UD E)E0 !&&u-J"":.H99#6#F#F#L#LM:(* *%A A 
J Js%   IA5I #I,I	 I)	,I5	c                 ~    t        j                  | j                  j                        rt        j                         S y)Nr  )r!   is_remote_pathr  r  get_appendable_file_encodingr  s    r@   r  z TensorTracer._get_outfile_suffix2  s.    ""4#3#3#=#=>6688rB   c           	      >     fd} j                  t        |      } j                         r|}n|j                         }r j                  j
                  s% j                  |      } j                  |      d   }t        j                  || j                  t        j                  t        j                                     }n(t        j                         }	 || j                  |	      } j                         r?t        j                   |g      5  t#        j$                  d      j&                  cddd       S t        j                   |g      5  t#        j$                  t(        |j*                  |j,                        }
t/        j0                  ||
      j&                  }t        j                   |g      5  t#        j$                  d      j&                  cddd       cddd       S # 1 sw Y   yxY w# 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)aE  Generates an Op that will flush the cache to file.

    Args:
      num_replicas: total number of replicas.
      on_tpu: if the graph is executed on TPU.
      tensor_trace_order: TensorTraceOrder object holding tensorname to id map.
      graph: TensorFlow graph.

    Returns:
      The Op to flush the cache to file.
    c                 "     	
fd}fd}i } |d      | |d      <   t        d      D ]E  }r0	j                  j                  st        j                  | ||      <   5 ||      | ||      <   G t        j                  |d      S )z8Flushes the cache to a file corresponding to replica_id.c                       fd}|S )z2Generates a func that flushes the cache to a file.c                     dz  } 	j                   j                  rVt        j                  j	                  	j                   j                  t
              | z   	j                         z   }t        |z   }nt        j                  }t        | z   }g }	j                   j                  r	j                         dkD  rt        d      	j                   j                  t        j                   v r%|j#                  	j%                  
             nu|j#                  	j'                  
|             nOt)        	j                               D ]3  }|j#                  t+        j,                  |ddd|f   dd|	             5 t/        j0                  |      5  t3        j4                  d
      j6                  cddd       S # 1 sw Y   yxY w)zFlushes the cache to a file.r  r   z.Inspecting multi signatures are not supported.)r  ru  rv  r  )r  ru  rv  r  r  r  Nr  r  r   )r  r  rG   r  r  _COMPACT_TRACE_FILE_PREFIXr  r  r  r  _REPLICA_ID_TAGinspect_tracere  r   r   r$   r_  r   r  rz  rK  r   r  r
   rH  r   r  r   )replica_strr  r  new_step_line	print_opsrE  r  
file_indexru  r  rv  r  s         r@   _print_cachez[TensorTracer._generate_flush_cache_op.<locals>._flush_fun.<locals>._f.<locals>._print_cacheK  s   
*+''77<<(8(8(B(B(BD()+/+C+C+EFK 2K?MJJM)K7-)++--/!3OPP**#668--!!+');	 . => --!!+'$1); . => 499;< 0{331+t -	 / 00 ''	2 .''*--. . .s   G  G	r   )r  r  r  ru  r  rv  r  s   ` r@   _fzETensorTracer._generate_flush_cache_op.<locals>._flush_fun.<locals>._fI  s    (	. (	.R rB   c                 0    t        j                  |       S r'  )r   equal)r  ru  s    r@   _eqzFTensorTracer._generate_flush_cache_op.<locals>._flush_fun.<locals>._eqv  s    ~~j*55rB   r   r   T)	exclusive)rK  r  rt  r   rr  r   case)r  ru  rv  r  r  flush_op_casesrE  rG  on_tpur  r  s   ```    r@   
_flush_funz9TensorTracer._generate_flush_cache_op.<locals>._flush_funF  s    + +Z6 n!!unSVQ% )!$**CC $4#9#9.Q
 #%a5.Q
 ) ##NdCCrB   r   NrL  )rB  r  rj  rM   r  rt  merge_caches_on_tpuaggregate_global_cacher&   outside_compilationr  r   identityr(   get_or_create_global_stepr
   rH  r   r  r   r3  r   r.  r   r  )r  rG  r  r  r>   r  r  	cache_valflush_opglobal_stepreset_value	assign_ops   ````        r@   _generate_flush_cache_opz%TensorTracer._generate_flush_cache_op8  s   ADF 33OUKEi++-i 66,,Y7	//	:1=	 44
i!1!1


]DDF
GIh ";;=kIt'7'7Eh##XJ/ *##A&))* * ##XJ/ ,!**+J1616> $$UK8;;	%%yk2 	,%%a(++	, 	,, ,	* *	, 	, 	,, , ,s1   G1A'H?G=	H1G:=H	HHc                 ^   |j                   st        j                  d       |S t        j                  ||D cg c]  }|j
                   c}z         5  | j                  | j                  j                  |||      }t        j                  ||g      cddd       S c c}w # 1 sw Y   yxY w)a  Flushes the intermediate tensor values in the graph to the cache.

    Args:
      tensor_fetches: list of tensor results returned by the model_fn.
      op_fetches: list of ops that are returned by the model_fn, e.g., train_op.
      on_tpu: if the graph is executed on TPU.
      tensor_trace_order: TensorTraceOrder object holding tensorname to id map.
      graph: TensorFlow graph.

    Returns:
      An identical copy of tensor_fetches.
    z7No tensor values being traced. No flush cache op added.r2  N)rC  r   rq  r
   rH  r   r  r  rG  r   r+  )r  tensor_fetchesr6  r  r  r>   r   flush_cache_ops           r@   _flush_tensor_values_cachez'TensorTracer._flush_tensor_values_cache  s      ,,llLM		!	!*;I"J699"J#K 
L E44
//
&
&0BEKn##N4B3CE	E E"JE Es   B
A B##B,c                     |t        d      t        |t        t        f      s|g}n|st        d      g }|D ];  }t        |t        j
                        r|j                  |       /t        d|z         |S )z>Check that tensor_fetches is not empty and have valid tensors.z8tensor_fetches provided to tensor_tracer cannot be None.z>tensor_fetches provided to tensor_tracer cannot be empty list.z&Given tensor_fetch:%s is not a tensor.)r7   r*  rJ  r+  r-  r.  r   )r  r  r5  r7  s       r@   _process_tensor_fetchesz$TensorTracer._process_tensor_fetches  s      ! " "ntUm4&'n ' ( (G M	E:,,	-uCeKLL	M
 NrB   c                 F   |g S t        |t        t        f      s|g}g }|D ]|  }t        |t        j                        r|j                  |       /t        |t        j                        r|j                  |j                         et        j                  d|z         ~ |S )z%Check that op_fetches have valid ops.z3Ignoring the given op_fetch:%s, which is not an op.)r*  rJ  r+  r
   r,  r   r-  r.  r   r   r   )r  r6  r5  r7  s       r@   _process_op_fetchesz TensorTracer._process_op_fetches  s    ij4-0<jG 	E3==	)ueZ../uxx M 	 NrB   c                     t        |t        j                        rt        |      dk7  rt	        d      |d   S t        |      t        |      k7  rt	        d      t        |t
              rt        |      S |S )zBChanges current_fetches' format, so that it matches input_fetches.r   z0Tensor tracer input/output fetches do not match.r   )r*  r-  r.  r   r7   r+  )r  input_fetchescurrent_fetchess      r@    _convert_fetches_to_input_formatz-TensorTracer._convert_fetches_to_input_format  so    -!2!23	_		"MNNQ	_	_!5	5MNNmU+_%%rB   c                 `    |j                   }t        j                  |      r|j                  }|S )a7  Returns the control flow of the given op.

    Args:
      op: tf.Operation for which the control flow context is requested.
    Returns:
      op_control_flow_context: which the is control flow context of the given
      op. If the operation type is LoopExit, returns the outer control flow
      context.
    )_control_flow_contextr   r   outer_context)r  r   op_control_flow_contexts      r@   r  z)TensorTracer._get_op_control_flow_context  s0     !66##B' 7 E E""rB   c                 r   t        j                  || j                  j                  g|j                  j                         z         }t        j                  j                  r|S t        j                  |dd| j                  j                  t        t        | j                  j                              g      S )zMerges the given caches on tpu.

    Args:
      local_tpu_cache_tensor: A local tensor that needs to be merged
        by concanting data from other tpu cores.
    Returns:
      A merged tf.Tensor.
    )r.  r   )concat_dimensionsplit_dimensionsplit_countgroup_assignment)r   broadcast_tor  rG  r.  r  r$   TT_SINGLE_CORE_SUMMARIESrM   r'   
all_to_allrJ  rK  )r  local_tpu_cache_tensorr  s      r@   r  z TensorTracer.merge_caches_on_tpu	  s     	++,$$,,./	0A
 3399h	AqOO00uT__%A%ABCDF FrB   c                    | j                   j                         }| j                         }g }t        |j	                         t        j                  d            D ]C  \  }}||vrt        d|z        |dddd|f   }||   } ||d      }	|j                  |	       E t        j                  |      }
t        j                  |
      }t        j                  |d      S )a  Merges the given caches on tpu.

    Args:
      global_tt_summary_cache: The global tensor tracer summary cache tensor
        with shape (num_cores, num_traced_tensors, num_traced_signatures). First
        dimension corresponds to core_id, where global_tpu_cache_tensor[i]
        correspond to the local cache from core-i.
    Returns:
      An aggregated tf.Tensor.
    Raises:
      RuntimeError: if there is no aggregate function defined for a signature.
    r   rs  z4No aggregation function is defined for signature %s.Nr   )rv  )r  get_signature_to_agg_fn_maprc  rw  rF   operator
itemgetterr7   r   r   rx  r   	transposeexpand_dims)r  global_tt_summary_cache
agg_fn_mapsignature_idx_mapaggregation_result	signaturer   signature_tensoragg_fn
agg_tensormerged_signaturestransposed_signaturess               r@   r  z#TensorTracer.aggregate_global_cache  s      !!==?J--/ !2!8!8!:%-%8%8%;= ,	3	*	$ +-67 8 	8 1As;)$f*3j
+,  (--.@A
 &//0AB   !6Q??rB   c                 r     j                   j                  t        d      d fd	}t        j                         }t        j                  |dg      }i  _        ||D cg c]  }|j                   c}z   }	i }
t        j                  |	      5   j                        }|j                         D ]L  \  }}dg}|j                  |j                  j                                t        j                  ||      }||
|<   N 	 ddd       ||
d<   ||
f j                  t         <   yc c}w # 1 sw Y   )xY w)an  Creates a host call function that will write the cache as tb summary.

    Args:
      processed_t_fetches: List of tensor provided to session.run.
      op_fetches: List of operations provided to session.run.
      graph: TensorFlow graph.
      graph_summary_tag: the summary_tag name for the given graph.
    Raises:
      ValueError if trace_dir is not set.
    NzMProvide a trace_dir for tensor tracer in summary mode. --trace_dir=/model/dirc                    t         }|t        j                  ||gd      }g }t        j                  j
                  j                  |t              }j                  t        |       | d   }|j                  }|j                  t        j                        rb|j                  t        j                        rC|j                  t        j                        r$t!        j"                  |t        j                        }|j%                         5  t'        j(                  t&        j(                  j+                  t,                    }|j/                         D ]  \  }	}
j
                  j0                  s:|	t2        k(  r1|
j4                  j7                         d   dk7  rj9                  |
      }
t;        j<                  |j?                         g      5  |jA                  t        jB                  t2        d	z   |	z   dz   z   |
||
             ddd        	 ddd       tE        jF                  |      S # 1 sw Y   xY w# 1 sw Y   *xY w)a  Writes the given caches as tensor summary.

      Args:
        step: Step tensor with dimension [num_cores].
        event_file_suffix: Event filename suffix tensor.
        **kwargs: The dictionary of tensors that needs to be written as
          summaries. Key and value pairs within kwargs correspond to the tag
          name, and tensor content that will be written using summary.write.
          The trace_modes that use this function are:
            - summary: In summary mode, kwargs includes a single (tag, content)
            pair which are, _TT_SUMMARY_TAG and a tf.float32 signature_cache
            variable. The dimension of the signature_cache is:
              num_cores x num_traced_tensors x num_signatures.
            - full_tensor_summary: kwargs will include all traced tensors. Tag
            and content correspond to the name of the tensor, and its actual
            content.
      Returns:
        A tf.Operation that needs to be executed for the host call dependencies.
      N.)	separator)filename_suffix	max_queuer   )plugin_name)plugin_datar   r   )metadatar   )$_TT_EVENT_FILE_SUFFIXr   string_joinr   create_file_writer_v2r  r  _TT_SUMMARY_MAX_QUEUEr    TENSOR_TRACER_SUMMARY_COLLECTIONr   __ne__r   int64uint64float64r   r  r4  r   SummaryMetadata
PluginData_TT_TENSORBOARD_PLUGIN_NAMErF   rt  r  r.  r  r  r
   rH  initr   writer   group)r   event_file_suffixkwargsfile_suffixsummary_write_opssummary_writer
step_valuedtsummary_metadatarL   rM   r>   r\  r  s              r@   _write_cachez8TensorTracer._prepare_host_call_fn.<locals>._write_cache\  s   ( *k		& ,,k;L-M7:< 44



$
$%)+n 
*N< 7jb 
6<<	 RYY
--&YYv~~6]]:v||<
$$& "&66#33>>7 ? 9: !,,. 	"JC !!::
 o%%++*=*=*?*Ba*G11%8e'')<)<)>(?@ "$$W]]#%+c14EE 0&! "" "	"	"& ##$566" "" "s%   CH=9H1	
H=1H:6H==Ir   r   r'  )r  r  r   r(   r  r   r   r  r   r
   rH  r(  rF   r1  r.  r  _TT_HOSTCALL_KEY)r  processed_t_fetchesr6  r>   r\  r  r  r   r   host_call_depscaches_to_write
all_cachesr;  cache_variablenew_cache_shaper  s   `  ``           r@   _prepare_host_call_fnz"TensorTracer._prepare_host_call_fnL  s6    !!) 0 1 1?7B  99;K[1#.DD;N"O699"OONO		!	!.	1 ,11%8j(2(8(8(: ,
$*n #~33;;=>!!./B&+
#,, #OF -9/+JD'() #P, ,s   $D(A1D--D6c                     | j                   S r'  )r  r  s    r@   host_call_deps_and_fnz"TensorTracer.host_call_deps_and_fn  s    rB   c                     | j                   S )z#Returns the set of traced op names.)r  r  s    r@   get_traced_op_namesz TensorTracer.get_traced_op_names  s       rB   c                    '( d } j                   j                  } j                  j                  }|j	                          _        t        j                  d||g       t        j                  |       t        j                  ||        j                  |      } j                  |      }||D 	cg c]  }	|	j                   c}	z   }
 j                  |j                         |
      }t!        |      } j#                  |||      't%        |      }g }t'        |      }|j)                  d        |D ]V  }t+        t-        |j.                              D ]1  }|j.                  |   }|j0                  }|'j2                  vr. j4                  j7                  |j0                         |j9                         }|D cg c]	  }||v s| }}||v }|s|s j;                  |      }|r|j=                  |        j?                  |      }r#|jA                         D ]  } |||         ||<     jC                         rX jE                         r%'j2                  |   } jG                  |||       d}nŉ'j2                  |   } jI                  |||      }n jK                         rGt-        |      dk7  rtM        d      t'        |jO                               d   } jQ                  |      }nK 'fd	}t-        |      dk7  rtM        d      tS        tU        |jO                                     } |||      }|r|j=                   j
                         |s|r|jW                  |       |D ]  }|jY                  |        4 Y |j=                   j
                         |rt[        j\                  ||
      } jC                         s jK                         rɉ jE                         rV j_                  |      }| j`                  vrtM        d      tc        jd                   j`                  |   dd      |tf        <    ji                         r< jk                  ||||       sH jl                  tn           \  (}  (di | }!t[        j\                  ||!g
      } jl                  tn        = n j                   jp                  rމ jl                  tn           \  (} tf        | v rd| v r| d   }"| tf           }# js                  |#d         }$ j                   jt                  s jw                  |$      }$(fd}%ty        jz                  |%|" j|                  |$      }&t[        j\                  ||&g
      } jl                  tn        = n!t        d       j                  ||'|      } j                  ||      S c c}	w c c}w )ay  Commong tracing function for both CPU and TPUs.

    The caller function should set device_type, num_replicas,
    num_replicas_per_host, num_hosts and replica_id before calling
    _trace_execution.


    Args:
      graph: the graph of Ops executed on the TPU.
      tensor_fetches: a (list,tuple,or a single object) of tensor fetches
        returned by model_fn given to session.run. Function must be provided
        with as least one tensor to fetch.
      op_fetches: A list of op fetches returned by model_fn given to
        session.run. op_fetches and tensor_fetches are used to determine the
        nodes that will be executed. Can be None.
      on_tpu: True if executing on TPU.

    Returns:
      tensor_fetches: an exact copy of tensor_fetches that has additional
                      dependencies.
    Raises:
      RuntimeError: If tensor_fetches is None or empty.
    c                    | j                   j                  t        j                        r$t	        j
                  | t        j                        S | j                   j                  t        j                        s)| j                   j                  t        j                        r$t	        j
                  | t        j                        S | S )z!Casts tensor to a supported type.)
r   __eq__r   r  r   r  int32bfloat16float16r  r   s    r@   _cast_unsupported_dtypesz?TensorTracer._trace_execution.<locals>._cast_unsupported_dtypes  sy     
		V\\	*}}VV\\22			V__	-1D1D
..2 }}VV^^44mrB   r2   c                     | j                   S r'  rE  r   s    r@   rr  z/TensorTracer._trace_execution.<locals>.<lambda>  s
    BGG rB   rs  Nr   z0Multiple stats are only allowed in compact mode.r   c                 h    j                  |      }rt        j                  ||       S  ||       S )z7Wraps the trace_fn with outside compilation if on TPUs.)r  r&   r  )r   out_tensor_nametensor_trace_fnr  r  r  s      r@   tpu_wrap_trace_fnz8TensorTracer._trace_execution.<locals>.tpu_wrap_trace_fn<  sA    "99/:LNO$88!6+ + %V,,rB   r  r  stack_all_op_signaturesru  r   c                      t        j                   t        j                  |d       fdt        j                        S )Nr   c                        d       S )N)r   r  r1   r   )r   
tt_summarywrite_caches   r@   rr  zHTensorTracer._trace_execution.<locals>.write_if_core_0.<locals>.<lambda>  s    +44<FH rB   )r   r   r  r   rr  )r   ru  r  r  s   ` `r@   write_if_core_0z6TensorTracer._trace_execution.<locals>.write_if_core_0  s1    YY..Q/H"((	* *rB   )r   ru  r  z9Outside compiled flush in only supported for summary mode)r  r  r>   r   )Br  r   r  r   _get_control_flow_contextr  r   track_usager   r   r   r  r  r   r>  get_operationsrA   r]  r   rJ  sortrK  r   r   r   r  r  r3  r  r  _set_control_flow_contextr  keysrh  rj  r  r  rg  r7   valuesr  nextiterr   _add_control_inputr   r+  r(  r  r   rx  r  r_  r  r  r  $flush_summaries_with_outside_compiler  rt  r  r&   r  r  r   r  r  ))r  r>   r  r6  r  r  r   r   r  r   all_fetchesexec_op_setr\  tensor_fetch_settracing_opssorted_exec_op_listr   rE  r$  r   r  copis_a_fetched_tensorr  processed_tensorsr  r  trace_opprocessed_out_tensorr  consumer_oprA  r  cache_write_opr   r1   tt_core_summaryr  write_opr  r  s)   `   `                                  @@r@   _trace_executionzTensorTracer._trace_execution  s   6
 !!,,J//--K!;;=D /J+DE"";/!!+z:66~F))*5J8KLf		LLK 889M9M9O9DFK*51 @@{-/ ./K{+!34! X5S_% W5!ZZ]
 oo0HHH
!!"''* ((*	 %.DS1CSD	D ),<<$7
"&"C"CB"G"

)
)*A
B !:::F,113 .i+C!),,.i(. ((* !!#*BB;OI001B168 H*BB;OI::9;L;@BH $$&"#q(  ' ( (!%&7&>&>&@!A!!D
**+?@(- "#q(  ' ( (
 "&d+<+C+C+E&F!G
&';[I("

)
)$*?*?
@ x( ' 5k**845kW5X5x 
##D$9$9: -223FBMO$$&$*A*A*C				 88?,,,CD
D+:+@+@  'a6O,Q(				!""#6
E#4	6)-););<L)M
&+&99. 0 6 6!>2B!D
  !12BB)-););<L)M
&+0V5N"6*D$3O$D!"667LQ7OPO##<< $ ; ;O Lo* '::++*	,H
 #3"8"8#XJ#@""#34 , - - #==F1 >  001DF FW  M> Es   3W ?	W	Wc                    t        |t        j                        st        |t        j                        rt        j                  d       |S |t        j                  v rt        j                  d       |S t        j                  j                  |       t        j                         | _        t        | j                  _        || j                  _        || j                  _        || j                  _        | j                  j                   | j                  j"                  d| j                  _        | j                  j$                  E|| j                  j"                  z  || j                  j"                  z  dkD  z   | j                  _        | j                  j&                  r+t)        j*                  || j                  j&                  d       |j-                         5  | j/                          | j1                  |||d      }ddd       | j                  j&                  r+t)        j*                  || j                  j&                  d	       |S # 1 sw Y   LxY w)
a  Traces the tensors generated by TPU Ops in a TF graph.

    Args:
      graph: the graph of Ops executed on the TPU.
      tensor_fetches: a (list,tuple,or a single object) of tensor fetches
        returned by model_fn given to session.run. Function must be provided
        with as least one tensor to fetch.
      op_fetches: A list of op fetches returned by model_fn given to
        session.run. op_fetches and tensor_fetches are used to determine the
        nodes that will be executed. Can be None.
      num_replicas: number of replicas used on the TPU.
      num_replicas_per_host: number of replicas per TPU host.
      num_hosts: total number of TPU hosts.

    Returns:
      tensor_fetches: an exact copy of tensor_fetches that has additional
                      dependencies.
    HTensor Tracer is not supported for tracing FuncGraphs. Ignoring tracing.GGraph is already rewritten with tensor tracer, ignoring multiple calls.N   r   graph_before_tt.pbtxtTr  graph_after_tt.pbtxt)r*  r   	FuncGraphr   
_FuncGraphr   r   r   _traced_graphsr3  r$   r   r  r   r  r   rG  num_replicas_per_host	num_hostsgraph_dump_pathr	   write_graphr4  rL  r1  )r  r>   r  r6  rG  r<  r=  s          r@   	trace_tpuzTensorTracer.trace_tpu  s   0 %--.*x""3$oo * ++++oo ( )!!%%e,*779D"2DOO#/DOO ,ADOO) )DOO##/		.	.	601-		"	"	*DOOAAADOOAAAAEG 	! ''5$"2"2"B"B24				 :
##%,,UNJ48 - :n: ''5$"2"2"B"B13: :s   &IIc                 t   t        |t        j                        st        |t        j                        rt        j                  d       |S |t        j                  v rt        j                  d       |S t        j                  j                  |       t        j                         | _        t        | j                  _        d| j                  _        d| j                  _        d| j                  _        d| _        | j                  j(                  r+t+        j,                  || j                  j(                  d       |j/                         5  | j1                  |||d      }ddd       | j                  j(                  r+t+        j,                  || j                  j(                  d	       |S # 1 sw Y   LxY w)
a{  Traces the tensors generated by CPU Ops in a TF graph.

    Args:
      graph: the graph of Ops executed on the CPU.
      tensor_fetches: a (list,tuple,or a single object) of tensor fetches
        returned by model_fn given to session.run. Function must be provided
        with as least one tensor to fetch.
      op_fetches: A list of op fetches returned by model_fn given to
        session.run. op_fetches and tensor_fetches are used to determine the
        nodes that will be executed. Can be None.

    Returns:
      tensor_fetches: an exact copy of tensor_fetches that has additional
                      dependencies.
    r3  r4  r   r   r6  Fr7  Nr8  )r*  r   r9  r   r:  r   r   r   r;  r3  r$   r   r  r   r  r   rG  r<  r=  r  r>  r	   r?  r4  r1  )r  r>   r  r6  s       r@   	trace_cpuzTensorTracer.trace_cpu  sf     %--.*x""3$oo * ++++oo ( )!!%%e,*779D"2DOO#$DOO ,-DOO) !DOOD''5$"2"2"B"B24				 ;,,UNJ49 - ;n; ''5$"2"2"B"B13; ;s   F..F7)NT)NNNNr'  )F__name__
__module____qualname____doc__r   r;  staticmethodr   r   r   r   r   r   r   r   r  r  r  r  r  r  r%  r(  r   r  r>  rB  rL  rO  rW  r[  rc  re  rj  rh  rg  r}  r  r  r  r  r  r  r  r  r  r(  r>  rF  rJ  rP  r]  r_  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r1  r@  rB  r   rB   r@   r   r     s&   "H 5.  ? ? H H 2 2 ? ?$ , ,      $@ % %
!*C #=( 8<7=~~	/8d 046>>&'P
#1,
(".@
85.@(:I'VQ5f* DLM^%%NB7 7"?B<
C=J2*hp,dE4&&#"F,+@ZiKV! #'"_FF  !&*<|-rB   r   r'  )wrF  r   r8   r  rG   os.pathr  numpyr   tensorflow.core.frameworkr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r   r	   r
   r   r-  r   tensorflow.python.lib.ior   tensorflow.python.opsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   tensorflow.python.platformr   r    r!   r"   r   tensorflow.python.summaryr#   tensorflow.python.tpur$   r%   r&   tensorflow.python.tpu.opsr'   tensorflow.python.trainingr(   r   r   r  r  r  r  r  r  r!  r#  r  r	  r  r"  r  r  r  r
  r  r  r  r   r  r  r  r3  r8  r@  r  rY  TT_SUMMARY_NORMr  TT_SUMMARY_MAXr  TT_SUMMARY_MAX_ABSr  TT_SUMMARY_MINr  TT_SUMMARY_MEANr  TT_SUMMARY_VARr  TT_SUMMARY_SIZEr  TT_SUMMARY_SPARSITYr  r  r  r  r  r  	BoolGauger   rA   rN   r   r   r   r   r   r   rB   r@   <module>r]     s   /    	  
  1 . 3 . 2 0 0 + < 3 , + 1 & 3 2 3 . * , - * ) + , ; 0 0 , 3 < 6 5 6 1 - 4    8 * 8 5 6 2 ) > 6 2 + + . 2 4 < D ! 5 #B   - "& 0 '! &66 %44)<< %44&66 %44&66 *>> )- , (  : B 5xA	\?~*ZAH<>| |rB   