Skip to content

vllm.utils.lite_profiler

Lightweight profiler for timing code execution with minimal overhead.

Modules:

Name Description
lite_profiler

Minimal helpers for opt-in lightweight timing collection.

lite_profiler_report

Summarize a single vLLM lite-profiler log in tabular form.

__all__ module-attribute

__all__ = ['LiteScope', 'maybe_emit_lite_profiler_report']

LiteScope

Lightweight context manager for timing code blocks with minimal overhead.

This class provides a simple way to measure and log the execution time of code blocks using Python's context manager protocol (with statement). It's designed for high-frequency profiling with minimal performance impact.

Source code in vllm/utils/lite_profiler/lite_profiler.py
class LiteScope:
    """Lightweight context manager for timing code blocks with minimal overhead.

    This class provides a simple way to measure and log the execution time of
    code blocks using Python's context manager protocol (with statement). It's
    designed for high-frequency profiling with minimal performance impact.
    """

    def __init__(self, name: str) -> None:
        self._name = name
        self._start_time: int | None = None

    def __enter__(self) -> None:
        self._start_time = time.perf_counter_ns()

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
    ) -> None:
        if self._start_time is not None and exc_type is None:
            elapsed_ns = time.perf_counter_ns() - self._start_time
            _write_log_entry(self._name, elapsed_ns)

_name instance-attribute

_name = name

_start_time instance-attribute

_start_time: int | None = None

__enter__

__enter__() -> None
Source code in vllm/utils/lite_profiler/lite_profiler.py
def __enter__(self) -> None:
    self._start_time = time.perf_counter_ns()

__exit__

__exit__(
    exc_type: type[BaseException] | None,
    exc_value: BaseException | None,
    traceback: TracebackType | None,
) -> None
Source code in vllm/utils/lite_profiler/lite_profiler.py
def __exit__(
    self,
    exc_type: type[BaseException] | None,
    exc_value: BaseException | None,
    traceback: TracebackType | None,
) -> None:
    if self._start_time is not None and exc_type is None:
        elapsed_ns = time.perf_counter_ns() - self._start_time
        _write_log_entry(self._name, elapsed_ns)

__init__

__init__(name: str) -> None
Source code in vllm/utils/lite_profiler/lite_profiler.py
def __init__(self, name: str) -> None:
    self._name = name
    self._start_time: int | None = None

maybe_emit_lite_profiler_report

maybe_emit_lite_profiler_report() -> None

Generate and display a summary report of profiling data if available.

This function serves as the main entry point for analyzing and displaying profiling results. It checks if profiling was enabled and a log file exists, then delegates to the lite_profiler_report module to generate statistics like function call counts, timing distributions, and performance insights.

Source code in vllm/utils/lite_profiler/lite_profiler.py
def maybe_emit_lite_profiler_report() -> None:
    """Generate and display a summary report of profiling data if available.

    This function serves as the main entry point for analyzing and displaying
    profiling results. It checks if profiling was enabled and a log file exists,
    then delegates to the lite_profiler_report module to generate statistics
    like function call counts, timing distributions, and performance insights.
    """

    log_path = envs.VLLM_LITE_PROFILER_LOG_PATH
    if log_path is None:
        return

    # Ensure the log file is flushed and closed before generating report
    global _log_file
    if _log_file is not None:
        _log_file.close()
        _log_file = None

    if not os.path.exists(log_path):
        logger.warning(
            "Lite profiler log not found. Ensure the profiled process sets "
            "the expected path."
        )
        return

    from vllm.utils.lite_profiler import lite_profiler_report

    logger.info("")
    logger.info("Lite profiler summary (%s):", log_path)
    try:
        # Generate and display the summary report
        lite_profiler_report.summarize_log(log_path)
        os.remove(log_path)
    except Exception as exc:  # pragma: no cover - avoid crashing benchmarks
        logger.error("Failed to summarize lite profiler log %s: %s", log_path, exc)