Spaces:
Running
Running
File size: 4,241 Bytes
5a5d5a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: OpenMDW-1.1
import atexit
import os
import sys
from typing import Any
import torch.distributed as dist
from loguru._logger import Core, Logger
RANK0_ONLY = True
LEVEL = os.environ.get("LOGURU_LEVEL", "INFO")
RANK = int(os.environ.get("RANK", "0"))
def make_new_logger(depth: int = 1) -> Logger:
return Logger(
core=Core(),
exception=None,
depth=depth,
record=False,
lazy=False,
colors=False,
raw=False,
capture=True,
patchers=[],
extra={},
)
logger = make_new_logger(depth=1)
atexit.register(logger.remove)
def _add_relative_path(record: dict[str, Any]) -> None:
try:
start = os.getcwd()
record["extra"]["relative_path"] = os.path.relpath(record["file"].path, start)
except OSError:
# CWD may have been removed (e.g. on some ranks in distributed jobs).
# Fall back to the absolute path so logging still works.
record["extra"]["relative_path"] = f"<cwd-unavailable>:{record['file'].path}"
*options, _, extra = logger._options # type: ignore
logger._options = tuple([*options, [_add_relative_path], extra]) # type: ignore
def init_loguru_stdout() -> None:
logger.remove()
datetime_format = get_datetime_format()
machine_format = get_machine_format()
message_format = get_message_format()
logger.add(
sys.stdout,
level=LEVEL,
format=f"{datetime_format}{machine_format}{message_format}",
filter=_rank0_only_filter,
)
def init_loguru_file(path: str) -> None:
datetime_format = get_datetime_format()
machine_format = get_machine_format()
message_format = get_message_format()
logger.add(
path,
encoding="utf8",
level=LEVEL,
format=f"{datetime_format}{machine_format}{message_format}",
rotation="100 MB",
filter=lambda result: _rank0_only_filter(result) or not RANK0_ONLY,
enqueue=True,
)
def get_datetime_format() -> str:
return "[<green>{time:MM-DD HH:mm:ss}</green>|"
def get_machine_format() -> str:
node_id = os.environ.get("NGC_ARRAY_INDEX", "0")
num_nodes = int(os.environ.get("NGC_ARRAY_SIZE", "1"))
machine_format = ""
rank = 0
if dist.is_available():
if not RANK0_ONLY and dist.is_initialized():
rank = dist.get_rank()
world_size = dist.get_world_size()
machine_format = (
f"<red>[Node{node_id:<3}/{num_nodes:<3}][RANK{rank:<5}/{world_size:<5}]" + "[{process.name:<8}]</red>| "
)
return machine_format
def get_message_format() -> str:
message_format = "<level>{level}</level>|<cyan>{extra[relative_path]}:{line}:{function}</cyan>] {message}"
return message_format
def _rank0_only_filter(record: Any) -> bool:
is_rank0 = record["extra"].get("rank0_only", True)
if RANK == 0 and is_rank0:
return True
if not is_rank0:
record["message"] = f"[RANK {RANK}] " + record["message"]
return not is_rank0
def trace(message: str, rank0_only: bool = True) -> None:
logger.opt(depth=1).bind(rank0_only=rank0_only).trace(message)
def debug(message: str, rank0_only: bool = True) -> None:
logger.opt(depth=1).bind(rank0_only=rank0_only).debug(message)
def info(message: str, rank0_only: bool = True) -> None:
logger.opt(depth=1).bind(rank0_only=rank0_only).info(message)
def success(message: str, rank0_only: bool = True) -> None:
logger.opt(depth=1).bind(rank0_only=rank0_only).success(message)
def warning(message: str, rank0_only: bool = True) -> None:
logger.opt(depth=1).bind(rank0_only=rank0_only).warning(message)
def error(message: str, rank0_only: bool = True) -> None:
logger.opt(depth=1).bind(rank0_only=rank0_only).error(message)
def critical(message: str, rank0_only: bool = True) -> None:
logger.opt(depth=1).bind(rank0_only=rank0_only).critical(message)
def exception(message: str, rank0_only: bool = True) -> None:
logger.opt(depth=1).bind(rank0_only=rank0_only).exception(message)
# Execute at import time.
init_loguru_stdout()
|