Benchmarks any iterable (e.g `tf.data.Dataset`). Usage: ```py ds = tfds.load('mnist', split='train') ds = ds.batch(32).prefetch(buffer_size=tf.data.AUTOTUNE) tfds.benchmark(ds, batch_size=32) ``` Args: ds: Dataset to benchmark. Can be any iterable. Note: The iterable will be
(
ds: Iterable[Any],
*,
num_iter: Optional[int] = None,
batch_size: int = 1,
detailed_stats: bool = False,
)
| 160 | |
| 161 | |
| 162 | def raw_benchmark( |
| 163 | ds: Iterable[Any], |
| 164 | *, |
| 165 | num_iter: Optional[int] = None, |
| 166 | batch_size: int = 1, |
| 167 | detailed_stats: bool = False, |
| 168 | ) -> RawBenchmarkResult: |
| 169 | """Benchmarks any iterable (e.g `tf.data.Dataset`). |
| 170 | |
| 171 | Usage: |
| 172 | |
| 173 | ```py |
| 174 | ds = tfds.load('mnist', split='train') |
| 175 | ds = ds.batch(32).prefetch(buffer_size=tf.data.AUTOTUNE) |
| 176 | tfds.benchmark(ds, batch_size=32) |
| 177 | ``` |
| 178 | |
| 179 | Args: |
| 180 | ds: Dataset to benchmark. Can be any iterable. Note: The iterable will be |
| 181 | fully consumed. |
| 182 | num_iter: Number of iteration to perform (iteration might be batched) |
| 183 | batch_size: Batch size of the dataset, used to normalize iterations |
| 184 | detailed_stats: Whether to collect detailed statistics such as the time that |
| 185 | each iteration took. |
| 186 | |
| 187 | Returns: |
| 188 | raw results. |
| 189 | """ |
| 190 | try: |
| 191 | total = len(ds) # pytype: disable=wrong-arg-types |
| 192 | except TypeError: |
| 193 | total = num_iter |
| 194 | |
| 195 | if num_iter is not None: |
| 196 | total = min(total, num_iter) |
| 197 | |
| 198 | results = [] |
| 199 | actual_num_iter = 0 |
| 200 | first_batch_time = None |
| 201 | start_time = time.perf_counter_ns() |
| 202 | end_time = start_time |
| 203 | for _ in tqdm_utils.tqdm(iter(ds), total=total): |
| 204 | actual_num_iter += 1 |
| 205 | end_time = time.perf_counter_ns() |
| 206 | if first_batch_time is None: |
| 207 | first_batch_time = end_time |
| 208 | if detailed_stats: |
| 209 | results.append(end_time) |
| 210 | if num_iter and actual_num_iter >= num_iter: |
| 211 | break |
| 212 | if not actual_num_iter: |
| 213 | raise ValueError('Cannot benchmark dataset with 0 elements.') |
| 214 | |
| 215 | if num_iter and actual_num_iter < num_iter: |
| 216 | logging.warning( |
| 217 | 'Number of iterations is shorter than expected ({} vs {})'.format( |
| 218 | actual_num_iter, num_iter |
| 219 | ) |
no test coverage detected