
    BVh%                         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  G d	 d
ej                         Zy)z6Test utilities for tf.data benchmarking functionality.    N)session)dataset_ops)options)nest)context)testc                   J    e Zd ZdZd Z	 ddZd	dZ	 	 	 	 d
dZ	 	 	 	 	 ddZy)DatasetBenchmarkBasez"Base class for dataset benchmarks.c                 b   g }t        j                         st        d      t        |      D ]l  }|rt	        |      }t        |       t	        |      }t        j                         }t        |       t        j                         }|j                  ||z
         n t        j                  |      S )aJ  Benchmark the iterable in eager mode.

    Runs the iterable `iters` times. In each iteration, the benchmark measures
    the time it takes to go execute the iterable.

    Args:
      iterable: The tf op or tf.data Dataset to benchmark.
      iters: Number of times to repeat the timing.
      warmup: If true, warms up the session caches by running an untimed run.

    Returns:
      A float, representing the median time (with respect to `iters`)
      it takes for the iterable to be executed `iters` num of times.

    Raises:
      RuntimeError: When executed in graph mode.
    z7Eager mode benchmarking is not supported in graph mode.)
r   executing_eagerlyRuntimeErrorrangeiternexttimeappendnpmedian)	selfiterableiterswarmupdeltas_iteratorstartends	            `/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/data/benchmarks/benchmark_base.py_run_eager_benchmarkz)DatasetBenchmarkBase._run_eager_benchmark   s    & F$$&
CE E 5\ 	!	>Xhhiike
8nIIKcmmC%K 	! 99V    Nc                    g }t        j                         rt        d      t        |      D ]  }t	        j
                  |      5 }|r$|r|j                  |       |j                  |       |r|j                  |       t        j                         }	|j                  |       t        j                         }
ddd       |j                  
	z
          t        j                  |      S # 1 sw Y   4xY w)a.  Benchmarks the iterable in graph mode.

    Runs the iterable `iters` times. In each iteration, the benchmark measures
    the time it takes to go execute the iterable.

    Args:
      iterable: The tf op or tf.data Dataset to benchmark.
      iters: Number of times to repeat the timing.
      warmup: If true, warms up the session caches by running an untimed run.
      session_config: A ConfigProto protocol buffer with configuration options
        for the session. Applicable only for benchmarking in graph mode.
      initializer: The initializer op required to initialize the iterable.

    Returns:
      A float, representing the median time (with respect to `iters`)
      it takes for the iterable to be executed `iters` num of times.

    Raises:
      RuntimeError: When executed in eager mode.
    z7Graph mode benchmarking is not supported in eager mode.)configN)r   r   r   r   r   Sessionrunr   r   r   r   )r   r   r   r   session_configinitializerr   r   sessr   r   s              r   _run_graph_benchmarkz)DatasetBenchmarkBase._run_graph_benchmarkC   s    6 F  "
CE E 5\ !??.1 THH[!
((8

((;
		iik mmC%K ! 99V s   A3C,,C5	c                 |    t        j                         r| j                  |||      S | j                  ||||      S )a  Benchmarks the op.

    Runs the op `iters` times. In each iteration, the benchmark measures
    the time it takes to go execute the op.

    Args:
      op: The tf op to benchmark.
      iters: Number of times to repeat the timing.
      warmup: If true, warms up the session caches by running an untimed run.
      session_config: A ConfigProto protocol buffer with configuration options
        for the session. Applicable only for benchmarking in graph mode.

    Returns:
      A float, representing the per-execution wall time of the op in seconds.
      This is the median time (with respect to `iters`) it takes for the op
      to be executed `iters` num of times.
    r   r   r   )r   r   r   r%   )r   r   r   r(   )r   opr   r   r%   s        r   run_op_benchmarkz%DatasetBenchmarkBase.run_op_benchmarks   sK    &   "&&%&OO$$5 % P Pr    c                    t        j                         }||j                  _        |j	                  |      }|j                  |dz
        }t        j                         r"| j                  |||      }|t        |      z  S t        j                  |      }	|	j                         }
t        j                  |
      d   j                  }| j!                  |||||	j"                        }|t        |      z  S )a  Benchmarks the dataset.

    Runs the dataset `iters` times. In each iteration, the benchmark measures
    the time it takes to go through `num_elements` elements of the dataset.

    Args:
      dataset: Dataset to benchmark.
      num_elements: Number of dataset elements to iterate through each benchmark
        iteration.
      iters: Number of times to repeat the timing.
      warmup: If true, warms up the session caches by running an untimed run.
      apply_default_optimizations: Determines whether default optimizations
        should be applied.
      session_config: A ConfigProto protocol buffer with configuration options
        for the session. Applicable only for benchmarking in graph mode.

    Returns:
      A float, representing the per-element wall time of the dataset in seconds.
      This is the median time (with respect to `iters`) it takes for the dataset
      to go through `num_elements` elements, divided by `num_elements.`
       r*   r   )r   r   r   r%   r&   )options_libOptionsexperimental_optimizationapply_default_optimizationswith_optionsskipr   r   r   floatr   make_initializable_iteratorget_nextr   flattenr+   r(   r&   )r   datasetnum_elementsr   r   r2   r%   r   median_durationr   next_elementr+   s               r   run_benchmarkz"DatasetBenchmarkBase.run_benchmark   s    > !!#G# %%A""7+G ll<!+,G  "11% 2 8ou\22266w?H$$&L	l	#A	&	)	)B//%(( 0 *O U<000r    c	                     | j                  ||||||      }	|i }t        j                         rdj                  |      }d|d<   ndj                  |      }d|d<   ||d<   | j	                  |	|||       |	S )	a  Benchmarks the dataset and reports the stats.

    Runs the dataset `iters` times. In each iteration, the benchmark measures
    the time it takes to go through `num_elements` elements of the dataset.
    This is followed by logging/printing the benchmark stats.

    Args:
      dataset: Dataset to benchmark.
      num_elements: Number of dataset elements to iterate through each benchmark
        iteration.
      name: Name of the benchmark.
      iters: Number of times to repeat the timing.
      extras: A dict which maps string keys to additional benchmark info.
      warmup: If true, warms up the session caches by running an untimed run.
      apply_default_optimizations: Determines whether default optimizations
        should be applied.
      session_config: A ConfigProto protocol buffer with configuration options
        for the session. Applicable only for benchmarking in graph mode.

    Returns:
      A float, representing the per-element wall time of the dataset in seconds.
      This is the median time (with respect to `iters`) it takes for the dataset
      to go through `num_elements` elements, divided by `num_elements.`
    )r9   r:   r   r   r2   r%   z{}.eagereagerimplementationz{}.graphgraphr:   )	wall_timer   nameextras)r=   r   r   formatreport_benchmark)
r   r9   r:   rC   r   rD   r   r2   r%   rB   s
             r   run_and_report_benchmarkz-DatasetBenchmarkBase.run_and_report_benchmark   s    B ""!$?% # 'I ~f  "t$d!(ft$d!(f)F>5tF  Dr    )N)r.   TN)r.   TFN)   NTFN)	__name__
__module____qualname____doc__r   r(   r,   r=   rG    r    r   r
   r
      sF    *"R (,.`P8 05#'91~ &'&*&*;@.23r    r
   )rL   r   numpyr   tensorflow.python.clientr   tensorflow.python.data.opsr   r   r/   tensorflow.python.data.utilr   tensorflow.python.eagerr   tensorflow.python.platformr   	Benchmarkr
   rM   r    r   <module>rU      s4    =   , 2 = , + +^4>> ^r    