Source code for nemo_rl.utils.nvml
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
import os
import pynvml
[docs]
@contextlib.contextmanager
def nvml_context():
"""Context manager for NVML initialization and shutdown.
Raises:
RuntimeError: If NVML initialization fails
"""
try:
pynvml.nvmlInit()
yield
except pynvml.NVMLError as e:
raise RuntimeError(f"Failed to initialize NVML: {e}")
finally:
try:
pynvml.nvmlShutdown()
except:
pass
[docs]
def device_id_to_physical_device_id(device_id: int) -> int:
"""Convert a logical device ID to a physical device ID considering CUDA_VISIBLE_DEVICES."""
if "CUDA_VISIBLE_DEVICES" in os.environ:
device_ids = os.environ["CUDA_VISIBLE_DEVICES"].split(",")
try:
physical_device_id = int(device_ids[device_id])
return physical_device_id
except ValueError:
raise RuntimeError(
f"Failed to convert logical device ID {device_id} to physical device ID. Available devices are: {device_ids}."
)
else:
return device_id
[docs]
def get_device_uuid(device_idx: int) -> str:
"""Get the UUID of a CUDA device using NVML."""
# Convert logical device index to physical device index
global_device_idx = device_id_to_physical_device_id(device_idx)
# Get the device handle and UUID
with nvml_context():
try:
handle = pynvml.nvmlDeviceGetHandleByIndex(global_device_idx)
return pynvml.nvmlDeviceGetUUID(handle)
except pynvml.NVMLError as e:
raise RuntimeError(
f"Failed to get device UUID for device {device_idx} (global index: {global_device_idx}): {e}"
)