Spaces:
Running
Running
File size: 9,070 Bytes
966ae59 |
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 |
# -*- coding: utf-8 -*-
# Copyright (c) XiMing Xing. All rights reserved.
# Author: XiMing Xing
# Description:
from typing import Union, List
from pathlib import Path
from datetime import datetime
import logging
from omegaconf import OmegaConf, DictConfig
from pprint import pprint
import torch
from accelerate.utils import LoggerType
from accelerate import Accelerator
from ..utils.logging import get_logger
class ModelState:
"""
Handling logger and `hugging face` accelerate training
features:
- Precision
- Device
- Optimizer
- Logger (default: python system print and logging)
- Monitor (default: wandb, tensorboard)
"""
def __init__(
self,
args: DictConfig,
log_path_suffix: str = None,
ignore_log=False, # whether to create log file or not
) -> None:
self.args: DictConfig = args
# set cfg
self.state_cfg = args.state
self.x_cfg = args.x
"""check valid"""
mixed_precision = self.state_cfg.get("mprec")
# Bug: omegaconf convert 'no' to false
mixed_precision = "no" if type(mixed_precision) == bool else mixed_precision
"""create working space"""
# rule: ['./config'. 'method_name', 'exp_name.yaml']
# -> result_path: ./runs/{method_name}-{exp_name}, as a base folder
now_time = datetime.now().strftime('%Y-%m-%d-%H-%M')
results_folder = self.args.get("result_path", None)
if results_folder is None:
self.result_path = Path("./workdir") / f"{self.x_cfg.method}-{now_time}"
else:
self.result_path = Path(results_folder) / f"{self.x_cfg.method}-{now_time}"
# update result_path: ./runs/{method_name}-{exp_name}/{log_path_suffix}
# noting: can be understood as "results dir / methods / ablation study / your result"
config_name_only = str(self.x_cfg.method).split(".")[0]
if log_path_suffix is not None:
self.result_path = self.result_path / f"{config_name_only}-{log_path_suffix}"
else:
self.result_path = self.result_path / f"{config_name_only}"
"""init visualized tracker"""
# TODO: monitor with WANDB or TENSORBOARD
self.log_with = []
# if self.state_cfg.wandb:
# self.log_with.append(LoggerType.WANDB)
# if self.state_cfg.tensorboard:
# self.log_with.append(LoggerType.TENSORBOARD)
"""HuggingFace Accelerator"""
self.accelerator = Accelerator(
device_placement=True,
mixed_precision=mixed_precision,
cpu=True if self.state_cfg.cpu else False,
log_with=None if len(self.log_with) == 0 else self.log_with,
project_dir=self.result_path / "vis",
)
"""logs"""
if self.accelerator.is_local_main_process:
# logging
self.log = logging.getLogger(__name__)
# log results in a folder periodically
self.result_path.mkdir(parents=True, exist_ok=True)
if not ignore_log:
self.logger = get_logger(
logs_dir=self.result_path.as_posix(),
file_name=f"{now_time}-{args.seed}-log.txt"
)
print("==> system args: ")
sys_cfg = OmegaConf.masked_copy(args, ["x"])
print(sys_cfg)
print("==> yaml config args: ")
print(self.x_cfg)
print("\n***** Model State *****")
print(f"-> Mixed Precision: {mixed_precision}, AMP: {self.accelerator.native_amp}")
print(f"-> Weight dtype: {self.weight_dtype}")
if self.accelerator.scaler_handler is not None and self.accelerator.scaler_handler.enabled:
print(f"-> Enabled GradScaler: {self.accelerator.scaler_handler.to_kwargs()}")
print(f"-> Working Space: '{self.result_path}'")
"""glob step"""
self.step = 0
"""log process"""
self.accelerator.wait_for_everyone()
print(f'Process {self.accelerator.process_index} using device: {self.accelerator.device}')
self.print("-> state initialization complete \n")
@property
def device(self):
return self.accelerator.device
@property
def is_main_process(self):
return self.accelerator.is_main_process
@property
def weight_dtype(self):
weight_dtype = torch.float32
if self.accelerator.mixed_precision == "fp16":
weight_dtype = torch.float16
elif self.accelerator.mixed_precision == "bf16":
weight_dtype = torch.bfloat16
return weight_dtype
@property
def n_gpus(self):
return self.accelerator.num_processes
@property
def no_decay_params_names(self):
no_decay = [
"bn", "LayerNorm", "GroupNorm",
]
return no_decay
def no_decay_params(self, model, weight_decay):
"""optimization tricks"""
optimizer_grouped_parameters = [
{
"params": [
p for n, p in model.named_parameters()
if not any(nd in n for nd in self.no_decay_params_names)
],
"weight_decay": weight_decay,
},
{
"params": [
p for n, p in model.named_parameters()
if any(nd in n for nd in self.no_decay_params_names)
],
"weight_decay": 0.0,
},
]
return optimizer_grouped_parameters
def optimized_params(self, model: torch.nn.Module, verbose=True) -> List:
"""return parameters if `requires_grad` is True
Args:
model: pytorch models
verbose: log optimized parameters
Examples:
>>> params_optimized = self.optimized_params(uvit, verbose=True)
>>> optimizer = torch.optim.AdamW(params_optimized, lr=1e-3)
Returns:
a list of parameters
"""
params_optimized = []
for key, value in model.named_parameters():
if value.requires_grad:
params_optimized.append(value)
if verbose:
self.print("\t {}, {}, {}".format(key, value.numel(), value.shape))
return params_optimized
def save_everything(self, fpath: str):
"""Saving and loading the model, optimizer, RNG generators, and the GradScaler."""
if not self.accelerator.is_main_process:
return
self.accelerator.save_state(fpath)
def load_save_everything(self, fpath: str):
"""Loading the model, optimizer, RNG generators, and the GradScaler."""
self.accelerator.load_state(fpath)
def save(self, milestone: Union[str, float, int], checkpoint: object) -> None:
if not self.accelerator.is_main_process:
return
torch.save(checkpoint, self.result_path / f'model-{milestone}.pt')
def save_in(self, root: Union[str, Path], checkpoint: object) -> None:
if not self.accelerator.is_main_process:
return
torch.save(checkpoint, root)
def load_ckpt_model_only(self, model: torch.nn.Module, path: Union[str, Path], rm_module_prefix: bool = False):
ckpt = torch.load(path, map_location=self.device)
unwrapped_model = self.accelerator.unwrap_model(model)
if rm_module_prefix:
unwrapped_model.load_state_dict({k.replace('module.', ''): v for k, v in ckpt.items()})
else:
unwrapped_model.load_state_dict(ckpt)
return unwrapped_model
def load_shared_weights(self, model: torch.nn.Module, path: Union[str, Path]):
ckpt = torch.load(path, map_location=self.accelerator.device)
self.print(f"pretrained_dict len: {len(ckpt)}")
unwrapped_model = self.accelerator.unwrap_model(model)
model_dict = unwrapped_model.state_dict()
pretrained_dict = {k: v for k, v in ckpt.items() if k in model_dict}
model_dict.update(pretrained_dict)
unwrapped_model.load_state_dict(model_dict, strict=False)
self.print(f"selected pretrained_dict: {len(model_dict)}")
return unwrapped_model
def print(self, *args, **kwargs):
"""Use in replacement of `print()` to only print once per server."""
self.accelerator.print(*args, **kwargs)
def pretty_print(self, msg):
if self.accelerator.is_main_process:
pprint(dict(msg))
def close_tracker(self):
self.accelerator.end_training()
def free_memory(self):
self.accelerator.clear()
def close(self, msg: str = "Training complete."):
"""Use in end of training."""
self.free_memory()
if torch.cuda.is_available():
self.print(f'\nGPU memory usage: {torch.cuda.max_memory_reserved() / 1024 ** 3:.2f} GB')
if len(self.log_with) > 0:
self.close_tracker()
self.print(msg)
|