o
    ?ea                     @   s   d 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
ZdZdd Zdd Z		dddZ		dddZedgddddZdS )zqLogging tensorflow::tfprof::OpLogProto.

OpLogProto is used to add extra model information for offline analysis.
    N)tfprof_log_pb2)context)ops)tensor_shape)gfile)flops_registry)	tf_exportZ_trainable_variablesZflopsc           
      C   s  |j jD ]}|jD ]~}|jsq	z| |j}W n ty) } zW Y d}~q	d}~ww t|jt|jkr5q	t	|jD ]L\}}|j| 
  rHq:|jjj}tdd |D }	z|j| |j| 
 |	 W q: ty } ztjd|j|f  W Y d}~q:d}~ww q	q| S )zBFill Tensor shapes in 'graph' with run time shape from 'run_meta'.Nc                 S   s   g | ]}|j qS  )size).0dr	   r	   i/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/profiler/tfprof_logger.py
<listcomp>5   s    z-_fill_missing_graph_shape.<locals>.<listcomp>z!Node %s incompatible shapes: %s.
)Z
step_statsZ	dev_statsZ
node_statsoutputZget_operation_by_nameZ	node_nameKeyErrorlenZoutputs	enumerate	get_shapeZis_fully_definedZtensor_descriptionshapedimr   ZTensorShape	set_shapeZ
merge_with
ValueErrorsysstderrwrite)
graphrun_metaZdev_statZ	node_statopeiZnode_stat_outZnode_stat_dimsZnode_stat_shaper	   r	   r   _fill_missing_graph_shape"   s@   


r    c                 C   s(   | | d}|du rt|}||| < |S )zMaps string to id.N)getr   )sZ	str_to_idnumr	   r	   r   _str_id?   s
   r$   Tc              	   C   s  |rt | |} d}i }i }t||d< |  D ]y}z
t| |jt}W n ty2   |d7 }d}Y nw t	 }	|j
|	_
d}
|rJ|jrJt|j|	_d}
|r|jr|jD ]4\}}}}|	jj }|ret||nd|_|rl|nd|_|rvt||nd|_|rt||nd|_d|_qRd}
|
r|	||	j
< q|r| tjjD ]'}|jj
|vrt	 }	|jj
|	_
|	jt |	||	j
< q||jj
 jt q|dkr|stj d|  ||fS )	a  Extract trainable model parameters and FLOPs for ops from a Graph.

  Args:
    graph: tf.Graph.
    run_meta: RunMetadata proto used to complete shape information.
    add_trace: Whether to add op trace information.
    add_trainable_var: Whether to assign tf.compat.v1.trainable_variables() op
      type '_trainable_variables'.
  Returns:
    logged_ops: dict mapping from op_name to OpLogEntry.
    string_to_id: dict mapping from string to id.
  r   none   NFTz0%d ops no flops stats due to incomplete shapes.
)!r    r   Zget_operationsr   Zget_stats_for_node_defZnode_defREGISTERED_FLOP_STATSr   r   Z
OpLogEntrynamevalueint	float_ops	tracebackcode_deftracesaddr$   Zfile_idlinenoZfunction_idZline_idZfunc_start_lineZget_collectionZ	GraphKeysTRAINABLE_VARIABLESr   typesappendr   r   r   )r   r   	add_traceadd_trainable_varZop_missing_shape
logged_opsstring_to_idr   statsentry	add_entryfilenamer1   funcnamelinetracevr	   r	   r   _get_logged_opsH   s^   



rA   c                 C   s  | s
t  s
t } t }| s|S t| |||d\}}|s'|j|	  nUi }|jD ]}	|	||	j
< q,| D ];\}
}	|
|v ro||
 j|	j |	jdkr[||
 jdkr[|	j||
 _|	jjrn||
 jjsn||
 j|	j q8|	||
< q8|j|	  | D ]	\}}||j|< q|S )a  Merge the tfprof default extra info with caller's op_log.

  Args:
    graph: tf.Graph. If None and eager execution is not enabled, use
        default graph.
    op_log: OpLogProto proto.
    run_meta: RunMetadata proto used to complete shape information.
    add_trace: Whether to add op trace information.
    add_trainable_var: Whether to assign tf.compat.v1.trainable_variables() op
      type '_trainable_variables'.
  Returns:
    tmp_op_log: Merged OpLogProto proto.
  )r5   r6   r   )r   executing_eagerlyr   get_default_graphr   Z
OpLogProtorA   Zlog_entriesextendvaluesr)   itemsr3   r,   r.   r/   Z	MergeFromZid_to_string)r   op_logr   r5   r6   Z
tmp_op_logr7   r8   Zall_opsr:   Zop_namer"   r   r	   r	   r   merge_default_with_oplog   s4   


rH   zprofiler.write_op_log)v1c                 C   sl   | s
t  s
t } t| |||}ttj	|dd}|
|  W d   dS 1 s/w   Y  dS )a  Log provided 'op_log', and add additional model information below.

    The API also assigns ops in tf.compat.v1.trainable_variables() an op type
    called '_trainable_variables'.
    The API also logs 'flops' statistics for ops with op.RegisterStatistics()
    defined. flops calculation depends on Tensor shapes defined in 'graph',
    which might not be complete. 'run_meta', if provided, completes the shape
    information with best effort.

  Args:
    graph: tf.Graph. If None and eager execution is not enabled, use
        default graph.
    log_dir: directory to write the log file.
    op_log: (Optional) OpLogProto proto to be written. If not provided, an new
        one is created.
    run_meta: (Optional) RunMetadata proto that helps flops computation using
        run time shape information.
    add_trace: Whether to add python code trace information.
        Used to support "code" view.
  Z
tfprof_logwN)r   rB   r   rC   rH   r   ZOpenospathjoinr   ZSerializeToString)r   Zlog_dirrG   r   r5   logr	   r	   r   write_op_log   s   "rO   )NTT)NNTT)NNT)__doc__rK   r   Ztensorflow.core.profilerr   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   r   Ztensorflow.python.platformr   Z#tensorflow.python.profiler.internalr   Z tensorflow.python.util.tf_exportr   r2   r(   r    r$   rA   rH   rO   r	   r	   r	   r   <module>   s,   	
E

/