def blocked_autorange(self, callback=None, min_run_time=0.2): with common.set_torch_threads(self._num_threads): # Estimate the block size needed for measurement to be negligible # compared to the inner loop. This also serves as a warmup. overhead = np.median([self._timer.timeit(0) for _ in range(5)]) number = 1 while True: time_taken = self._timer.timeit(number) relative_overhead = overhead / time_taken if relative_overhead <= 1e-4 and time_taken >= min_run_time / 1000: break if time_taken > min_run_time: break number *= 10 total_time = 0.0 times = [] while total_time < min_run_time: time_taken = self._timer.timeit(number) total_time += time_taken if callback: callback(number, time_taken) times.append(time_taken) return self._construct_measurement(number_per_run=number, times=times)
def timeit(self, number=1000000): # Warmup self._timer.timeit(number=max(int(number // 100), 1)) with common.set_torch_threads(self._num_threads): return self._construct_measurement( number_per_run=number, times=[self._timer.timeit(number=number)])
def timeit(self, number=1000000): with common.set_torch_threads(self._task_spec.num_threads): # Warmup self._timer.timeit(number=max(int(number // 100), 1)) return common.Measurement( number_per_run=number, raw_times=[self._timer.timeit(number=number)], task_spec=self._task_spec )
def _estimate_block_size(self, min_run_time: float): with common.set_torch_threads(self._task_spec.num_threads): # Estimate the block size needed for measurement to be negligible # compared to the inner loop. This also serves as a warmup. overhead = np.median([self._timer.timeit(0) for _ in range(5)]) number = 1 while True: time_taken = self._timer.timeit(number) relative_overhead = overhead / time_taken if relative_overhead <= 1e-4 and time_taken >= min_run_time / 1000: break if time_taken > min_run_time: break number *= 10 return number
def _threaded_measurement_loop(self, number, time_hook, stop_hook, min_run_time: float, max_run_time: Optional[float] = None, callback=None): total_time = 0.0 can_stop = False times = [] with common.set_torch_threads(self._num_threads): while (total_time < min_run_time) or (not can_stop): time_spent = time_hook() times.append(time_spent) total_time += time_spent if callback: callback(number, time_spent) can_stop = stop_hook(times) if max_run_time and total_time > max_run_time: break return times
def _threaded_measurement_loop( self, number: int, time_hook: Callable[[], float], stop_hook: Callable[[List[float]], bool], min_run_time: float, max_run_time: Optional[float] = None, callback: Optional[Callable[[int, float], NoReturn]] = None ): total_time = 0.0 can_stop = False times: List[float] = [] with common.set_torch_threads(self._task_spec.num_threads): while (total_time < min_run_time) or (not can_stop): time_spent = time_hook() times.append(time_spent) total_time += time_spent if callback: callback(number, time_spent) can_stop = stop_hook(times) if max_run_time and total_time > max_run_time: break return times