Spaces:
Sleeping
Sleeping
File size: 8,348 Bytes
19583ce | 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | # Copyright 2026 NVIDIA CORPORATION & AFFILIATES
#
# 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.
#
# SPDX-License-Identifier: Apache-2.0
import dataclasses
import datetime
import logging
import os
import os.path
import os.path as osp
import time
from dataclasses import dataclass
from typing import List, Optional
import torch
import torch.distributed as dist
from .dist_util import is_main_process
def _format_cuda_memory() -> str:
if not torch.cuda.is_available():
return ''
d = torch.cuda.current_device()
alloc = torch.cuda.memory_allocated(d) / (1024**3)
reserved = torch.cuda.memory_reserved(d) / (1024**3)
peak = torch.cuda.max_memory_allocated(d) / (1024**3)
return f'[cuda:{d} alloc {alloc:.2f}GiB reserved {reserved:.2f}GiB peak_alloc {peak:.2f}GiB] '
def _format_log_value(value) -> str:
if abs(value) < 1e-4:
return f'{value:.3e}'
return f'{value:.4f}'
# ----------- file/logger util ----------
def get_time_str():
return time.strftime('%Y%m%d_%H%M%S', time.localtime())
def make_exp_dirs(cfg):
"""Make dirs for experiments."""
path_opt = cfg['path'].copy()
# An existing experiment directory is reused as it is: that is how a run resumes.
if cfg['mode'] == 'train':
os.makedirs(path_opt.pop('experiments_root'), exist_ok=True)
else:
os.makedirs(path_opt.pop('results_root'), exist_ok=True)
for key, path in path_opt.items():
if ('strict_load' in key) or ('pretrain_network' in key) or ('resume' in key) or ('param_key' in key) or ('lora_path' in key):
continue
else:
os.makedirs(path, exist_ok=True)
def copy_cfg_file(cfg, experiments_root):
"""Write the config the run actually uses -- command-line overrides applied -- next to its outputs."""
import sys
import yaml
filename = osp.join(experiments_root, osp.basename(cfg['config_path']))
with open(filename, 'w', encoding='utf-8') as handle:
handle.write(f'# GENERATE TIME: {time.asctime()}\n# CMD:\n# {" ".join(sys.argv)}\n'
'# The resolved config, with every command-line override applied.\n\n')
yaml.safe_dump(cfg, handle, sort_keys=False)
def set_path_logger(cfg):
if 'path' not in cfg:
cfg['path'] = {}
if cfg['mode'] == 'train':
experiments_root = cfg['path'].get(
'experiments_root', osp.join('experiments', cfg['name'])
)
cfg['path']['experiments_root'] = experiments_root
cfg['path']['models'] = osp.join(experiments_root, 'models')
cfg['path']['log'] = experiments_root
cfg['path']['visualization'] = osp.join(experiments_root, 'visualization')
else:
results_root = osp.join('results', cfg['name'])
cfg['path']['results_root'] = results_root
cfg['path']['log'] = results_root
cfg['path']['visualization'] = osp.join(results_root, 'visualization')
# Handle the output folder creation
if is_main_process():
make_exp_dirs(cfg)
if cfg['mode'] == 'train':
copy_cfg_file(cfg, cfg['path']['experiments_root'])
log_file = osp.join(cfg['path']['log'], f"train_{cfg['name']}_{get_time_str()}.log")
set_logger(log_file)
else:
copy_cfg_file(cfg, cfg['path']['results_root'])
log_file = osp.join(cfg['path']['log'], f"test_{cfg['name']}_{get_time_str()}.log")
set_logger(log_file)
else:
get_logger().propagate = False
get_logger().setLevel(logging.ERROR)
dist.barrier()
def get_logger():
logger = logging.getLogger('o2laf')
return logger
def set_logger(log_file=None):
# Make one log on every process with the configuration for debugging.
format_str = '%(asctime)s %(levelname)s: %(message)s'
log_level = logging.INFO
logger = get_logger()
logger.propagate = False
logger.setLevel(log_level)
file_handler = logging.FileHandler(log_file, 'w')
file_handler.setFormatter(logging.Formatter(format_str))
file_handler.setLevel(log_level)
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter(format_str))
logger.addHandler(stream_handler)
def dict2str(opt, indent_level=1):
"""dict to string for printing options.
Args:
opt (dict): Option dict.
indent_level (int): Indent level. Default: 1.
Return:
(str): Option string for printing.
"""
msg = '\n'
for k, v in opt.items():
if isinstance(v, dict):
msg += ' ' * (indent_level * 2) + k + ':['
msg += dict2str(v, indent_level + 1)
msg += ' ' * (indent_level * 2) + ']\n'
else:
msg += ' ' * (indent_level * 2) + k + ': ' + str(v) + '\n'
return msg
class MessageLogger():
"""Message logger for printing.
Args:
opt (dict): Config. It contains the following keys:
name (str): Exp name.
logger (dict): Contains 'print_freq' (str) for logger interval.
train (dict): Contains 'total_iter' (int) for total iters.
use_tb_logger (bool): Use tensorboard logger.
start_iter (int): Start iter. Default: 1.
tb_logger (obj:`tb_logger`): Tensorboard logger. Default: None.
"""
def __init__(self, opt, start_iter=1):
self.exp_name = opt['name']
self.interval = opt['logger']['print_freq']
self.start_iter = start_iter
self.max_iters = opt['train']['total_iter']
self.start_time = time.time()
def __call__(self, log_vars):
"""Format logging message.
Args:
log_vars (dict): It contains the following keys:
epoch (int): Epoch number.
iter (int): Current iter.
lrs (list): List for learning rates.
time (float): Iter time.
data_time (float): Data time for each iter.
"""
# epoch, iter, learning rates
current_iter = log_vars.pop('iter')
message = f'[{self.exp_name}][Iter:{current_iter:8,d}] '
# time and estimated time
total_time = time.time() - self.start_time
time_sec_avg = total_time / (current_iter - self.start_iter + 1)
eta_sec = time_sec_avg * (self.max_iters - current_iter - 1)
eta_str = str(datetime.timedelta(seconds=int(eta_sec)))
message += f'[eta: {eta_str}] '
# other items, especially losses
for k, v in log_vars.items():
if 'lr' in k:
message += f'{k}: {v:.3e} '
else:
message += f'{k}: {_format_log_value(v)} '
message += _format_cuda_memory()
get_logger().info(message)
def setup_wandb(name, save_dir):
try:
import wandb
except ImportError:
raise ImportError(
'You are trying to use wandb which is not currently installed. '
'Please install it using pip install wandb'
)
wandb_config = WANDB_CONFIG()
wandb_config.name = name
wandb_config.dir = save_dir
# auto resume
wandb_latest_dir = os.path.join(save_dir, 'wandb', 'latest-run')
if os.path.exists(wandb_latest_dir):
run_id = os.readlink(wandb_latest_dir).split('-')[-1]
wandb_config.id = run_id
init_dict = dataclasses.asdict(wandb_config)
run = wandb.init(**init_dict)
return run
@dataclass
class WANDB_CONFIG:
project: str = 'o2laf'
entity: Optional[str] = None
job_type: Optional[str] = None
tags: Optional[List[str]] = None
group: Optional[str] = None
notes: Optional[str] = None
mode: Optional[str] = None
name: Optional[str] = None
dir: Optional[str] = None
resume: str = 'allow'
id: Optional[str] = None
|