Overview
Intel_GPU_Utils provides utility functions for Intel GPU discovery and metrics collection via the xpu-smi CLI tool. It wraps subprocess calls to xpu-smi for GPU counting, device information retrieval, and command validation, enabling TorchServe to collect hardware metrics from Intel discrete GPUs.
Description
This module provides a lightweight wrapper around the xpu-smi command-line tool for Intel GPU hardware metrics. It is used by TorchServe's Intel Extension for PyTorch (IPEX) integration to discover available Intel GPUs and collect runtime metrics such as utilization, memory usage, and temperature.
Key Functions
- check_cmd(cmd) (lines 12-32): Validates and executes a
xpu-smi command, returning parsed CSV output
- count_gpus() (lines 35-41): Returns the number of available Intel GPUs by querying
xpu-smi discovery
- list_gpu_info(num_gpus) (lines 44-54): Collects detailed metrics for a specified number of GPUs
Dependencies
| Library |
Usage
|
csv |
Parsing CSV output from xpu-smi
|
subprocess |
Executing xpu-smi CLI commands
|
Code Reference
Source Location
| File |
Lines |
Repository
|
examples/intel_extension_for_pytorch/intel_gpu.py |
L1-54 |
pytorch/serve
|
Key Functions
def check_cmd(cmd):
"""
Validate and execute a xpu-smi command.
Lines 12-32.
Runs the given xpu-smi command via subprocess, captures
stdout, and parses the CSV-formatted output.
Parameters:
cmd (str): The xpu-smi command string to execute.
Returns:
list: Parsed CSV rows from command output.
Raises:
subprocess.CalledProcessError: If xpu-smi command fails.
"""
...
def count_gpus():
"""
Count the number of available Intel GPUs.
Lines 35-41.
Queries xpu-smi discovery to enumerate available
Intel discrete GPU devices.
Returns:
int: Number of detected Intel GPUs.
"""
...
def list_gpu_info(num_gpus):
"""
Collect detailed metrics for Intel GPUs.
Lines 44-54.
Iterates over the specified number of GPUs and
collects device-level metrics via xpu-smi.
Parameters:
num_gpus (int): Number of GPUs to query.
Returns:
list: Per-GPU metric dictionaries.
"""
...
Import
import csv
import subprocess
I/O Contract
| Function |
Input |
Output |
Notes
|
check_cmd(cmd) |
cmd: str (xpu-smi command) |
list of parsed CSV rows |
Wraps subprocess call to xpu-smi
|
count_gpus() |
None |
int (GPU count) |
Queries xpu-smi discovery
|
list_gpu_info(num_gpus) |
num_gpus: int |
list of per-GPU metric dicts |
Collects device-level metrics
|
Usage Examples
Example 1: Counting Available GPUs
from intel_gpu import count_gpus
num_gpus = count_gpus()
print(f"Detected {num_gpus} Intel GPU(s)")
Example 2: Collecting GPU Metrics
from intel_gpu import count_gpus, list_gpu_info
num_gpus = count_gpus()
gpu_metrics = list_gpu_info(num_gpus)
for i, metrics in enumerate(gpu_metrics):
print(f"GPU {i}: {metrics}")
Example 3: Validating xpu-smi Command
from intel_gpu import check_cmd
# Execute a specific xpu-smi command
result = check_cmd("xpu-smi discovery --dump 1")
# result contains parsed CSV output rows
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.