[Misc] Use logger instead of print() in utils/common.py (#29004)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
hirakunaramuka2
2026-06-24 09:45:01 +00:00
committed by GitHub
co-authored by Claude Fable 5
parent 10e0bcd622
commit 7430c56b20
2 changed files with 126 additions and 19 deletions
+28 -18
View File
@@ -566,9 +566,11 @@ def get_available_gpu_memory(
assert gpu_id < num_gpus
if torch.cuda.current_device() != gpu_id:
print(
f"WARNING: current device is not {gpu_id}, but {torch.cuda.current_device()}, ",
"which may cause useless memory allocation for torch CUDA context.",
logger.warning(
"current device is not %s, but %s, which may cause useless "
"memory allocation for torch CUDA context.",
gpu_id,
torch.cuda.current_device(),
)
if empty_cache:
@@ -588,9 +590,11 @@ def get_available_gpu_memory(
assert gpu_id < num_gpus
if torch.xpu.current_device() != gpu_id:
print(
f"WARNING: current device is not {gpu_id}, but {torch.xpu.current_device()}, ",
"which may cause useless memory allocation for torch XPU context.",
logger.warning(
"current device is not %s, but %s, which may cause useless "
"memory allocation for torch XPU context.",
gpu_id,
torch.xpu.current_device(),
)
if empty_cache:
@@ -604,9 +608,11 @@ def get_available_gpu_memory(
assert gpu_id < num_gpus
if torch.hpu.current_device() != gpu_id:
print(
f"WARNING: current device is not {gpu_id}, but {torch.hpu.current_device()}, ",
"which may cause useless memory allocation for torch HPU context.",
logger.warning(
"current device is not %s, but %s, which may cause useless "
"memory allocation for torch HPU context.",
gpu_id,
torch.hpu.current_device(),
)
free_gpu_memory, total_gpu_memory = torch.hpu.mem_get_info()
@@ -621,9 +627,11 @@ def get_available_gpu_memory(
assert gpu_id < num_gpus
if torch.npu.current_device() != gpu_id:
print(
f"WARNING: current device is not {gpu_id}, but {torch.npu.current_device()}, ",
"which may cause useless memory allocation for torch NPU context.",
logger.warning(
"current device is not %s, but %s, which may cause useless "
"memory allocation for torch NPU context.",
gpu_id,
torch.npu.current_device(),
)
if empty_cache:
empty_device_cache(torch.npu)
@@ -642,9 +650,11 @@ def get_available_gpu_memory(
assert gpu_id < num_gpus
if torch.musa.current_device() != gpu_id:
print(
f"WARNING: current device is not {gpu_id}, but {torch.musa.current_device()}, ",
"which may cause useless memory allocation for torch MUSA context.",
logger.warning(
"current device is not %s, but %s, which may cause useless "
"memory allocation for torch MUSA context.",
gpu_id,
torch.musa.current_device(),
)
if empty_cache:
empty_device_cache(torch.musa)
@@ -1539,7 +1549,7 @@ def delete_directory(dirpath):
# This will remove the directory and all its contents
shutil.rmtree(dirpath)
except OSError as e:
print(f"Warning: {dirpath} : {e.strerror}")
logger.warning("Failed to delete directory %s: %s", dirpath, e.strerror)
# Temporary directory for prometheus multiprocess mode
@@ -3848,7 +3858,7 @@ def get_nvidia_driver_version() -> tuple:
@lru_cache(maxsize=1)
def get_nvidia_driver_version_str() -> str:
def get_nvidia_driver_version_str() -> str | None:
"""Return the NVIDIA driver version string, e.g. '595.58.03'.
Returns None on failure."""
try:
@@ -3930,7 +3940,7 @@ def get_device_sm_nvidia_smi():
except (subprocess.CalledProcessError, FileNotFoundError, ValueError) as e:
# Handle cases where nvidia-smi isn't available or output is unexpected
print(f"Error getting compute capability: {e}")
logger.error("Error getting compute capability: %s", e)
return (0, 0) # Default/fallback value