Skip to content

llm.timer

Performance timer.

Timer

Timer(synchronize: bool = False)

Performance timer.

Parameters:

  • synchronize (bool, default: False ) –

    Synchronize CUDA workers before and after the timer.

Source code in llm/timer.py
def __init__(self, synchronize: bool = False) -> None:
    self._synchronize = synchronize
    self._history: list[float] = []
    self._start_time = time.time()

start()

start() -> None

Start the timer.

Source code in llm/timer.py
def start(self) -> None:
    """Start the timer."""
    if torch.cuda.is_available() and self._synchronize:
        torch.cuda.synchronize()
    self._start_time = time.time()

stop()

stop() -> float

Stop the timer and return the elapsed time.

Source code in llm/timer.py
def stop(self) -> float:
    """Stop the timer and return the elapsed time."""
    if torch.cuda.is_available() and self._synchronize:
        torch.cuda.synchronize()

    elapsed = time.time() - self._start_time
    self._history.append(elapsed)
    return elapsed

lap()

lap() -> float

Log a lap of the timer.

Source code in llm/timer.py
def lap(self) -> float:
    """Log a lap of the timer."""
    lap_time = self.stop()
    self.start()
    return lap_time

get_history()

get_history() -> list[float]

Get the history of all lap times.

Source code in llm/timer.py
def get_history(self) -> list[float]:
    """Get the history of all lap times."""
    return self._history

get_history_sum()

get_history_sum() -> float

Get the sum of all lap times.

Source code in llm/timer.py
def get_history_sum(self) -> float:
    """Get the sum of all lap times."""
    return sum(self._history)

get_history_mean()

get_history_mean() -> float

Get the mean of all lap times.

Source code in llm/timer.py
def get_history_mean(self) -> float:
    """Get the mean of all lap times."""
    return sum(self._history) / len(self._history)