Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    def timeit(self, number: int = 1000000) -> common.Measurement:
        """Mirrors the semantics of timeit.Timer.timeit().

        Execute the main statement (`stmt`) `number` times.
        https://docs.python.org/3/library/timeit.html#timeit.Timer.timeit
        """
        with common.set_torch_threads(self._task_spec.num_threads):
            # Warmup
            self._timeit(number=max(int(number // 100), 2))

            return common.Measurement(number_per_run=number,
                                      raw_times=[self._timeit(number=number)],
                                      task_spec=self._task_spec)
Exemplo n.º 3
0
 def _estimate_block_size(self, min_run_time: float) -> int:
     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
Exemplo n.º 4
0
 def _estimate_block_size(self, min_run_time: float) -> int:
     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 = torch.tensor([self._timeit(0)
                                  for _ in range(5)]).median().item()
         number = 1
         while True:
             time_taken = self._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
             # Avoid overflow in C++ pybind11 interface
             if number * 10 > 2147483647:
                 break
             number *= 10
     return number
Exemplo n.º 5
0
 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