Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Pytorch Serve Print Env Info

From Leeroopedia

Overview

Print_Env_Info is the system environment diagnostics utility for TorchServe that collects and reports comprehensive information about the runtime environment including Python, Java, OS, GPU (CUDA/HIP), npm, and C++ compiler details. It provides the get_pretty_env_info() function that produces a human-readable summary useful for debugging, bug reporting, and environment validation.

Knowledge Sources
Domains Infrastructure, Diagnostics
Last Updated 2026-02-13 18:52 GMT

Description

The print_env_info.py script (493 lines) is a comprehensive environment introspection tool that gathers detailed information about every component relevant to a TorchServe deployment. It probes system binaries, Python packages, GPU drivers, and hardware capabilities to produce a structured diagnostic report.

Key Responsibilities

  • Python Environment: Collects Python version, pip version, and installed packages (PyTorch, torchvision, torchaudio, torchtext, torchserve, torch-model-archiver)
  • Java Environment: Detects Java version (required for TorchServe frontend)
  • Operating System: Reports OS name, version, and platform details
  • GPU Diagnostics: Detects NVIDIA GPUs via nvidia-smi, reports CUDA/HIP versions, driver versions, and GPU model names
  • Build Tools: Checks for npm, Node.js, gcc/g++ availability
  • Structured Output: Produces a formatted multi-section report via get_pretty_env_info()

Code Reference

Source Location

File Lines Repository
ts_scripts/print_env_info.py L1-493 pytorch/serve

Core Utility Functions

def run(command):
    """
    Execute a shell command and return its stdout.

    Args:
        command (str): Shell command to execute.

    Returns:
        str: Command stdout, or empty string on failure.
    """
    ...

def run_and_parse_first_match(command, regex):
    """
    Execute a shell command and return the first regex match group.

    Args:
        command (str): Shell command to execute.
        regex (str): Regular expression with at least one capture group.

    Returns:
        str|None: First capture group from the first matching line, or None.
    """
    ...

Information Gathering Functions

def get_pip_packages():
    """
    Get versions of key pip packages relevant to TorchServe.
    Checks: torch, torchvision, torchaudio, torchtext,
    torchserve, torch-model-archiver, torch-workflow-archiver.

    Returns:
        dict: Package name -> version string mappings.
    """
    ...

def get_java_version():
    """
    Detect the installed Java version by running 'java -version'.

    Returns:
        str: Java version string, or 'Not Found'.
    """
    ...

def get_platform():
    """
    Get the platform identifier string.

    Returns:
        str: Platform string (e.g., 'linux-x86_64', 'darwin-arm64').
    """
    ...

def get_os():
    """
    Get the operating system name and version.

    Returns:
        str: OS description (e.g., 'Ubuntu 22.04', 'macOS 13.0').
    """
    ...

def get_gpu_info():
    """
    Detect GPU hardware using nvidia-smi or rocm-smi.

    Returns:
        str: GPU model name(s) and count, or 'No GPU detected'.
    """
    ...

def get_nvidia_driver_version():
    """
    Get the NVIDIA driver version from nvidia-smi.

    Returns:
        str: Driver version string (e.g., '535.129.03'), or 'N/A'.
    """
    ...

def get_running_cuda_version():
    """
    Get the CUDA runtime version reported by nvidia-smi.

    Returns:
        str: CUDA version string (e.g., '12.1'), or 'N/A'.
    """
    ...

def get_torchserve_version():
    """
    Get the installed TorchServe version.

    Returns:
        str: Version string, or 'Not installed'.
    """
    ...

Report Generation

def populate_env_info():
    """
    Collect all environment information into a structured object.
    Calls all get_* functions and assembles the results.

    Returns:
        dict: Structured environment information dictionary.
    """
    ...

def get_pretty_env_info():
    """
    Generate a human-readable formatted environment report.
    Calls populate_env_info() and formats the output as a
    multi-section text report.

    Returns:
        str: Formatted environment report string.
    """
    ...

Import

# Run as a standalone script:
# python ts_scripts/print_env_info.py

# When imported:
from ts_scripts.print_env_info import get_pretty_env_info, run

I/O Contract

Function Input Output Notes
run(command) Shell command string str (stdout) Returns empty string on failure
run_and_parse_first_match(command, regex) Shell command, regex pattern str or None First capture group from first match
get_pip_packages() None dict of package versions Probes pip for TorchServe-related packages
get_java_version() None str version Runs java -version
get_gpu_info() None str GPU description Runs nvidia-smi or rocm-smi
get_nvidia_driver_version() None str driver version Parses nvidia-smi output
get_running_cuda_version() None str CUDA version Parses nvidia-smi output
get_torchserve_version() None str version Checks pip metadata
populate_env_info() None dict full environment Aggregates all get_* results
get_pretty_env_info() None str formatted report Human-readable multi-section output

Usage Examples

Example 1: Run diagnostics from CLI

python ts_scripts/print_env_info.py

Example 2: Sample output

TorchServe Environment Info
===========================

OS: Ubuntu 22.04.3 LTS
Platform: linux-x86_64
Python: 3.10.12

Java: openjdk 17.0.8 2023-07-18

Packages:
  torch: 2.1.0+cu121
  torchvision: 0.16.0+cu121
  torchaudio: 2.1.0+cu121
  torchserve: 0.9.0
  torch-model-archiver: 0.9.0

GPU:
  NVIDIA A100-SXM4-80GB x 4
  Driver: 535.129.03
  CUDA: 12.1

npm: 9.8.1
Node.js: v18.17.1
gcc: 11.4.0

Example 3: Programmatic usage

from ts_scripts.print_env_info import get_pretty_env_info, get_gpu_info

# Get full formatted report
env_report = get_pretty_env_info()
print(env_report)

# Get just GPU info
gpu = get_gpu_info()
print(f"GPU: {gpu}")

Example 4: Using individual diagnostic functions

from ts_scripts.print_env_info import (
    get_java_version,
    get_running_cuda_version,
    get_nvidia_driver_version,
    get_torchserve_version,
)

# Validate environment before starting TorchServe
java_ver = get_java_version()
if java_ver == "Not Found":
    raise RuntimeError("Java is required but not installed")

cuda_ver = get_running_cuda_version()
driver_ver = get_nvidia_driver_version()
ts_ver = get_torchserve_version()

print(f"Java: {java_ver}")
print(f"CUDA: {cuda_ver}, Driver: {driver_ver}")
print(f"TorchServe: {ts_ver}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment