
    BVha                         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 Zd Z	 	 ddZ	 	 ddZ edg      dd       Zy)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_export_trainable_variablesflopsc                    |j                   j                  D ]J  }|j                  D ]7  }|j                  s	 | j	                  |j
                        }t        |j                        t        |j                        k7  rYt        |j                        D ]  \  }}|j                  |   j                         j                         r2|j                  j                  j                  }t        j                   |D 	cg c]  }	|	j"                   c}	      }
	 |j                  |   j%                  |j                  |   j                         j'                  |
              : M | S # t        $ r}Y d}~Od}~ww xY wc c}	w # t(        $ r;}t*        j,                  j/                  d|j
                  d|d       Y d}~-d}~ww xY w)zBFill Tensor shapes in 'graph' with run time shape from 'run_meta'.NzNode z incompatible shapes: z.
)
step_stats	dev_stats
node_statsoutputget_operation_by_name	node_nameKeyErrorlenoutputs	enumerate	get_shapeis_fully_definedtensor_descriptionshapedimr   TensorShapesize	set_shape
merge_with
ValueErrorsysstderrwrite)graphrun_metadev_stat	node_statopeinode_stat_outnode_stat_dimsdnode_stat_shapes              X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/profiler/tfprof_logger.py_fill_missing_graph_shaper0   "   s   %%// 5h(( 5	(()<)<= 
Y	#bjj/	1 ))*:*: ; 5
1m::a=""$557
&99??CC&22+,QVV,.	5
**Q-
!
!"**Q-"9"9";"F"F#  5550 
,%   -  	5
**

%//4 5 5	5s0   E':E>AF'	E;6E;	G	0G	G	c                 N    |j                  | d      }|t        |      }||| <   |S )zMaps string to id.N)getr   )s	str_to_idnums      r/   _str_idr6   ?   s.    a#[
i.CIaL	*    c                    |rt        | |      } d}i }i }t        |      |d<   | j                         D ](  }	 t        j                  | |j
                  t              }t        j                         }	|j                  |	_
        d}
|r(|j                  rt        |j                        |	_        d}
|r|j                  r|j                  D ]|  \  }}}}|	j                  j                   j#                         }|rt%        ||      nd|_        |r|nd|_        |rt%        ||      nd|_        |rt%        ||      nd|_        d|_        ~ d}
|
s|	||	j                  <   + |r| j1                  t        j2                  j4                        D ]  }|j6                  j                  |vr^t        j                         }	|j6                  j                  |	_
        |	j8                  j;                  t4               |	||	j                  <   y||j6                  j                     j8                  j;                  t4                |dkD  r$|s"t<        j>                  jA                  d|z         ||fS # t        $ r |dz  }d}Y w xY w)	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.
)!r0   r   get_operationsr   get_stats_for_node_defnode_defREGISTERED_FLOP_STATSr    r   
OpLogEntrynamevalueint	float_ops	tracebackcode_deftracesaddr6   file_idlinenofunction_idline_idfunc_start_lineget_collection	GraphKeysTRAINABLE_VARIABLESr(   typesappendr!   r"   r#   )r$   r%   	add_traceadd_trainable_varop_missing_shape
logged_opsstring_to_idr(   statsentry	add_entryfilenamerJ   funcnamelinetracevs                    r/   _get_logged_opsr`   H   s:    %eX6E*,\*,v  " %b((
35e %%'EEJIEKK(eoi	02 	%,Hfh..''++-%=E'(L91%-#)r%,AIgh=q%
9='$51%-"$%
	% i$j;%> !!#--"C"CD @	
*	$))+TTYY
./!&
5::14499##**+>?@ (JJH%& '	\	!!Q  !es   %I((I<;I<c                    | s(t        j                         st        j                         } t	        j
                         }| s|S t        | |||      \  }}|s+|j                  j                  |j                                n#i }|j                  D ]  }	|	||	j                  <    |j                         D ]  \  }
}	|
|v r||
   j                  j                  |	j                         |	j                  dkD  r&||
   j                  dk(  r|	j                  ||
   _        |	j                  j                  s~||
   j                  j                  r||
   j                  j!                  |	j                         |	||
<    |j                  j                  |j                                |j                         D ]  \  }}||j"                  |<    |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.
  )rS   rT   r   )r   executing_eagerlyr   get_default_graphr   
OpLogProtor`   log_entriesextendvaluesrA   itemsrQ   rD   rF   rG   	MergeFromid_to_string)r$   op_logr%   rS   rT   
tmp_op_logrV   rW   all_opsrY   op_namer3   r*   s                r/   merge_default_with_oplogro      s    
w002!!#E((**	,X>OQ*l 
!!*"3"3"56G## "!gejj"$**, !	G	%%ekk2??Q77#3#=#=#B',''

$>>  )9)B)B)I)I
'

#
#
-
-enn
= ! !!'.."23  " #da!"JA#	r7   zprofiler.write_op_log)v1c                 D   | s(t        j                         st        j                         } t	        | |||      }t        j                  t        j                  j                  |d      d      5 }|j                  |j                                ddd       y# 1 sw Y   yxY w)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.
  
tfprof_logwN)r   rb   r   rc   ro   r   Openospathjoinr#   SerializeToString)r$   log_dirrk   r%   rS   logs         r/   write_op_logr{      sy    , 
w002!!#E#E68YG&zz"'',,w5s; *sIIf&&()* * *s   - BB)NTT)NNTT)NNT)__doc__ru   r!   tensorflow.core.profilerr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   tensorflow.python.platformr   #tensorflow.python.profiler.internalr    tensorflow.python.util.tf_exportr	   rP   r?   r0   r6   r`   ro   r{    r7   r/   <module>r      sw    
 
 3 + + 4 , > 6,  : 59&*B"J ;??C,^ &'(* )*r7   