Skip to content

Commit

Permalink
[documentation] adding desc for adaptive_autorange (pytorch#111612)
Browse files Browse the repository at this point in the history
Summary: This prevented it from showing up in docs

Test Plan: no functional changes

Reviewers:

Subscribers:

Tasks:

Tags:
Pull Request resolved: pytorch#111612
Approved by: https://github.com/cpuhrsch
  • Loading branch information
HDCharles authored and andreigh committed Oct 26, 2023
1 parent 0236b15 commit f6263ff
Showing 1 changed file with 63 additions and 30 deletions.
93 changes: 63 additions & 30 deletions torch/utils/benchmark/utils/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,36 +328,6 @@ def _estimate_block_size(self, min_run_time: float) -> int:
number *= 10
return number

def adaptive_autorange(
self,
threshold: float = 0.1,
*,
min_run_time: float = 0.01,
max_run_time: float = 10.0,
callback: Optional[Callable[[int, float], NoReturn]] = None,
) -> common.Measurement:
number = self._estimate_block_size(min_run_time=0.05)

def time_hook() -> float:
return self._timeit(number)

def stop_hook(times: List[float]) -> bool:
if len(times) > 3:
return common.Measurement(
number_per_run=number,
raw_times=times,
task_spec=self._task_spec
).meets_confidence(threshold=threshold)
return False
times = self._threaded_measurement_loop(
number, time_hook, stop_hook, min_run_time, max_run_time, callback=callback)

return common.Measurement(
number_per_run=number,
raw_times=times,
task_spec=self._task_spec
)

def blocked_autorange(
self,
callback: Optional[Callable[[int, float], NoReturn]] = None,
Expand Down Expand Up @@ -418,6 +388,69 @@ def stop_hook(times: List[float]) -> bool:
task_spec=self._task_spec
)

def adaptive_autorange(
self,
threshold: float = 0.1,
*,
min_run_time: float = 0.01,
max_run_time: float = 10.0,
callback: Optional[Callable[[int, float], NoReturn]] = None,
) -> common.Measurement:
"""Similar to `blocked_autorange` but also checks for variablility in measurements
and repeats until iqr/median is smaller than `threshold` or `max_run_time` is reached.
At a high level, adaptive_autorange executes the following pseudo-code::
`setup`
times = []
while times.sum < max_run_time
start = timer()
for _ in range(block_size):
`stmt`
times.append(timer() - start)
enough_data = len(times)>3 and times.sum > min_run_time
small_iqr=times.iqr/times.mean<threshold
if enough_data and small_iqr:
break
Args:
threshold: value of iqr/median threshold for stopping
min_run_time: total runtime needed before checking `threshold`
max_run_time: total runtime for all measurements regardless of `threshold`
Returns:
A `Measurement` object that contains measured runtimes and
repetition counts, and can be used to compute statistics.
(mean, median, etc.)
"""
number = self._estimate_block_size(min_run_time=0.05)

def time_hook() -> float:
return self._timeit(number)

def stop_hook(times: List[float]) -> bool:
if len(times) > 3:
return common.Measurement(
number_per_run=number,
raw_times=times,
task_spec=self._task_spec
).meets_confidence(threshold=threshold)
return False
times = self._threaded_measurement_loop(
number, time_hook, stop_hook, min_run_time, max_run_time, callback=callback)

return common.Measurement(
number_per_run=number,
raw_times=times,
task_spec=self._task_spec
)

@overload
def collect_callgrind(
self,
Expand Down

0 comments on commit f6263ff

Please sign in to comment.