
    6bi                         S r SSKrSSKrSSKJr  SSKJr  S rS r	 SS jr
 " S S	\R                  5      r           SS
 jrg)zCommon utils for benchmarks.    N)	callbacks)distribution_utilc                 v    SU ;  d  SU ;  a  [        S5      eU R                  S5      S   R                  S5      $ )aL  Split the suffix of the benchmark name.

For example, for the name = 'benchmark_layer_call__Conv2D_small_shape',
the return value is ['Conv2D', 'small', 'shape'].

This is to generate the metadata of the benchmark test.

Args:
  name: A string, the benchmark name.

Returns:
  A list of strings of the suffix in the benchmark name.
___z*The format of the benchmark name is wrong.)
ValueErrorsplit)names    `/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/benchmarks/benchmark_util.pyget_benchmark_namer      s=     43d?EFF::dB%%c**    c            
          / nU  H_  nUR                  U Vs/ s H  o3S   S-   4USS -   PM     sn5        UR                  U Vs/ s H  o3S   S-   4USS -   PM     sn5        Ma     U$ s  snf s  snf )zExtend the benchmark names with CPU and GPU suffix.

Args:
  *params_list: A list of tuples represents the benchmark parameters.

Returns:
  A list of strings with the benchmark name extended with CPU and GPU
  suffix.
r   _CPU   N_GPU)extend)params_listbenchmark_paramsparamsparams       r   !generate_benchmark_params_cpu_gpur   ,   s     =CDVEQx& "U12Y.VD	
 	=CDVEQx& "U12Y.VD	
	   E Es   A*
A/
c                 *    SX-   S[        U5      -   S.$ )Nkeras_examplesbs_)
model_nameimplementation
parameters)str)keras_model
batch_sizeimpls      r   get_keras_examples_metadatar#   A   s#     '%,c*o- r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	TimerCallBackK   z1Callback for logging time in each epoch or batch.c                 ~    / U l         [        R                  U l        [        R                  " 5       U l        SU l        g )NF)timestimeitdefault_timertimerstartup_timerecorded_startup)selfs    r   __init__TimerCallBack.__init__N   s/    
))
"002 %r   c                 .    U R                  5       U l        g N)r+   epoch_start_timer.   elogss      r   on_epoch_beginTimerCallBack.on_epoch_beginT   s     $

r   c                 p    U R                   R                  U R                  5       U R                  -
  5        g r2   )r(   appendr+   r3   r4   s      r   on_epoch_endTimerCallBack.on_epoch_endW   s%    

$**,)>)>>?r   c                 z    U R                   (       d*  U R                  5       U R                  -
  U l        SU l         g g )NT)r-   r+   r,   r4   s      r   on_batch_endTimerCallBack.on_batch_endZ   s1    $$ $

t/@/@ @D$(D! %r   )r3   r-   r,   r+   r(   N)
__name__
__module____qualname____firstlineno____doc__r/   r7   r;   r>   __static_attributes__ r   r   r%   r%   K   s    ;&-@)r   r%   c           
         Uc  [        S5      eUc  [        S5      eUc  [        S5      eU
S:  a  [        S5      eUR                  S   n/ / / pn/ / / nnnX<-  n[        R                  " XS9n[	        U5       GHK  n[
        R                  nU" 5       n[        R                  " U5      nU   U" 5       nU " 5       nU" 5       U-
  nU" 5       nUR                  UUUS9  U" 5       U-
  nSSS5        WR                  XUS	S
9  [        5       nU" 5       nUR                  UUUUU/U	S9  U" 5       n UR                  W5        UR                  W5        UR                  UR                  5        UR                  [        R                  " UR                  5      5        UR                  U U-
  5        UR                  UU U-
  -  5        GMN     / nUR                  S[        R                  " U5      S.5        UR                  S[        R                  " U5      S.5        UR                  S[        R                  " U5      S.5        UR                  S[        R                  " U5      S.5        UR                  S[        R                  " U5      S.5        UR                  SUS.5        [        R                  " U5      n!UU
S.n"UU!U"4$ ! , (       d  f       GN= f)au  Run models and measure the performance.

Args:
  model_fn: Model function to be benchmarked.
  x: Input data. See `x` in the `fit()` method of `keras.Model`.
  y: Target data. See `y` in the `fit()` method of `keras.Model`.
  epochs: Integer. Number of epochs to train the model.
    If unspecified, `epochs` will default to 2.
  batch_size: Integer. Number of samples per gradient update. If
    unspecified, `batch_size` will default to 32.
  run_iters: Integer. Number of iterations to run the performance
    measurement.  If unspecified, `run_iters` will default to 4.
  optimizer: String (name of optimizer) or optimizer instance. See
    `keras.optimizers`.
  loss: String (name of objective function), objective function or
    `keras.losses.Loss` instance. See `keras.losses`.
  metrics: Lists of metrics to be evaluated by the model during training.
    See `metrics` in the `compile()` method of  `keras.Model`.
  verbose: 0, 1, 2. Verbosity mode. See `verbose` in the `fit()` method of
    `keras.Model`. If unspecified, `verbose` will default to 0.
  num_gpus: Number of GPUs to run the model.
  distribution_strategy: Distribution strategies. It could be
    `multi_worker_mirrored`, `one_device`, `mirrored`. If unspecified,
    `distribution_strategy` will default to 'off'. Note that, `TPU`
    and `parameter_server` are not supported yet.

Returns:
  Performance summary, which contains build_time, compile_time,
  startup_time, avg_epoch_time, wall_time, exp_per_sec, epochs,
  distribution_strategy.

Raise:
  ValueError: If `x` is none or if `optimizer` is not provided or
  if `loss` is not provided or if `num_gpus` is negative.
NzInput data is required.zOptimizer is required.zLoss function is required.r   z`num_gpus` cannot be negative)distribution_strategynum_gpus)	optimizerlossmetricsr   )xyr!   epochs)rM   rN   r!   rO   r   verbose
build_time)r   valuecompile_timer,   avg_epoch_timeexp_per_secrO   )r	   shaper   get_distribution_strategyranger)   r*   get_strategy_scopecompilefitr%   r:   r,   npmeanr(   )#model_fnrM   rN   rO   r!   	run_itersrJ   rK   rL   rP   rI   rH   num_examplesbuild_time_listcompile_time_liststartup_time_listavg_epoch_time_listwall_time_listexp_per_sec_listtotal_num_examplesstrategyr   r+   
start_timestrategy_scopet0modelrQ   t1rS   cbkt2end_time	wall_timeextrass#                                      r   measure_performancers   `   s   b 	y233		122	566	A899 771:L<>B(9O<>B)9. ::3H 9$$W
*==hGBJE2JBMM#  
 !7R<L  			Az!	<oW		!e 	 	
 7z*  .  !1!12""277399#56h34 2hm DEI L GNNL277?3KLMNN"''2C*DE NN"''2C*DE NN!BGG4G,HI NNMBGG<L4MNONNHv67'I!6F
 Iv%%k ^s   ,;K
K	)z.keras.cfit_graph)NN          NNNr   r   off)rD   r)   numpyr\   tf_keras.srcr   tf_keras.src.benchmarksr   r   r   r#   Callbackr%   rs   rF   r   r   <module>r|      se    #   " 5+&, #6)I&& ). 
	@&r   