Implementation:Intel Ipex llm Get Int From Env
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Utilities |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete utility for reading distributed training environment variables provided by the IPEX-LLM common utilities.
Description
The get_int_from_env function reads environment variables commonly set by distributed launchers (mpirun, torchrun) such as LOCAL_RANK, WORLD_SIZE, and MASTER_PORT. It accepts a list of possible environment variable names and returns the first positive integer value found, or a default. This handles the variation between different launcher tools that use different env variable names.
Usage
Import this function when initializing distributed training environment on Intel XPU. Use it to read LOCAL_RANK, WORLD_SIZE, and MASTER_PORT before setting up DDP.
Code Reference
Source Location
- Repository: IPEX-LLM
- File: python/llm/example/GPU/LLM-Finetuning/common/utils/util.py
- Lines: 54-60
Signature
def get_int_from_env(env_keys: List[str], default) -> int:
"""Returns the first positive env value found in the `env_keys` list or the default."""
Import
from common.utils import get_int_from_env
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| env_keys | List[str] | Yes | List of environment variable names to check (e.g., ["LOCAL_RANK", "MPI_LOCALRANKID"]) |
| default | int or str | Yes | Default value if no env variable is found |
Outputs
| Name | Type | Description |
|---|---|---|
| return | int | The first positive integer value found, or int(default) |
Usage Examples
from common.utils import get_int_from_env
import os
# Read LOCAL_RANK from either mpirun or torchrun env vars
local_rank = get_int_from_env(["LOCAL_RANK", "MPI_LOCALRANKID"], "0")
world_size = get_int_from_env(["WORLD_SIZE", "PMI_SIZE"], "1")
port = get_int_from_env(["MASTER_PORT"], 29500)
# Set environment for DDP
os.environ["LOCAL_RANK"] = str(local_rank)
os.environ["WORLD_SIZE"] = str(world_size)
os.environ["RANK"] = str(local_rank)
os.environ["MASTER_PORT"] = str(port)