File size: 12,510 Bytes
7aefe45 |
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
import os
from functools import partial
from typing import Union, List
from pathlib import Path
from datetime import datetime, timedelta
from omegaconf import DictConfig
from pprint import pprint
import torch
from accelerate.utils import LoggerType
from accelerate import (
Accelerator,
GradScalerKwargs,
DistributedDataParallelKwargs,
InitProcessGroupKwargs
)
from ..modules.ema import EMA
from ..utils.logging import get_logger
class ModelState:
"""
Handling logger and `hugging face` accelerate training
features:
- Mixed Precision
- Gradient Scaler
- Gradient Accumulation
- Optimizer
- EMA
- Logger (default: python print)
- Monitor (default: wandb, tensorboard)
"""
def __init__(
self,
args,
log_path_suffix: str = None,
ignore_log=False, # whether to create log file or not
) -> None:
self.args: DictConfig = args
"""check valid"""
mixed_precision = self.args.get("mixed_precision")
# Bug: omegaconf convert 'no' to false
mixed_precision = "no" if type(mixed_precision) == bool else mixed_precision
split_batches = self.args.get("split_batches", False)
gradient_accumulate_step = self.args.get("gradient_accumulate_step", 1)
assert gradient_accumulate_step >= 1, f"except gradient_accumulate_step >= 1, get {gradient_accumulate_step}"
"""create working space"""
# rule: ['./config'. 'method_name', 'exp_name.yaml']
# -> results_path: ./runs/{method_name}-{exp_name}, as a base folder
# config_prefix, config_name = str(self.args.get("config")).split('/')
# config_name_only = str(config_name).split(".")[0]
config_name_only = str(self.args.get("config")).split(".")[0]
results_folder = self.args.get("results_path", None)
if results_folder is None:
# self.results_path = Path("./workdir") / f"{config_prefix}-{config_name_only}"
self.results_path = Path("./workdir")
else:
# self.results_path = Path(results_folder) / f"{config_prefix}-{config_name_only}"
self.results_path = Path(os.path.join(results_folder, self.args.get("edit_type"), ))
# update results_path: ./runs/{method_name}-{exp_name}/{log_path_suffix}
# noting: can be understood as "results dir / methods / ablation study / your result"
if log_path_suffix is not None:
self.results_path = self.results_path / log_path_suffix
kwargs_handlers = []
"""mixed precision training"""
if args.mixed_precision == "no":
scaler_handler = GradScalerKwargs(
init_scale=args.init_scale,
growth_factor=args.growth_factor,
backoff_factor=args.backoff_factor,
growth_interval=args.growth_interval,
enabled=True
)
kwargs_handlers.append(scaler_handler)
"""distributed training"""
ddp_handler = DistributedDataParallelKwargs(
dim=0,
broadcast_buffers=True,
static_graph=False,
bucket_cap_mb=25,
find_unused_parameters=False,
check_reduction=False,
gradient_as_bucket_view=False
)
kwargs_handlers.append(ddp_handler)
init_handler = InitProcessGroupKwargs(timeout=timedelta(seconds=1200))
kwargs_handlers.append(init_handler)
"""init visualized tracker"""
log_with = []
self.args.visual = False
if args.use_wandb:
log_with.append(LoggerType.WANDB)
if args.tensorboard:
log_with.append(LoggerType.TENSORBOARD)
"""hugging face Accelerator"""
self.accelerator = Accelerator(
device_placement=True,
split_batches=split_batches,
mixed_precision=mixed_precision,
gradient_accumulation_steps=args.gradient_accumulate_step,
cpu=True if args.use_cpu else False,
log_with=None if len(log_with) == 0 else log_with,
project_dir=self.results_path / "vis",
kwargs_handlers=kwargs_handlers,
)
"""logs"""
if self.accelerator.is_local_main_process:
# for logging results in a folder periodically
self.results_path.mkdir(parents=True, exist_ok=True)
if not ignore_log:
now_time = datetime.now().strftime('%Y-%m-%d-%H-%M')
# self.logger = get_logger(
# logs_dir=self.results_path.as_posix(),
# file_name=f"log.txt"
# )
print("==> command line args: ")
print(args.cmd_args)
print("==> yaml config args: ")
print(args.yaml_config)
print("\n***** Model State *****")
if self.accelerator.distributed_type != "NO":
print(f"-> Distributed Type: {self.accelerator.distributed_type}")
print(f"-> Split Batch Size: {split_batches}, Total Batch Size: {self.actual_batch_size}")
print(f"-> Mixed Precision: {mixed_precision}, AMP: {self.accelerator.native_amp},"
f" Gradient Accumulate Step: {gradient_accumulate_step}")
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()}")
if args.use_wandb:
print(f"-> Init trackers: 'wandb' ")
self.args.visual = True
self.__init_tracker(project_name="my_project", tags=None, entity="")
print(f"-> Working Space: '{self.results_path}'")
"""EMA"""
self.use_ema = args.get('ema', False)
self.ema_wrapper = self.__build_ema_wrapper()
"""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")
def __init_tracker(self, project_name, tags, entity):
self.accelerator.init_trackers(
project_name=project_name,
config=dict(self.args),
init_kwargs={
"wandb": {
"notes": "accelerate trainer pipeline",
"tags": [
f"total batch_size: {self.actual_batch_size}"
],
"entity": entity,
}}
)
def __build_ema_wrapper(self):
if self.use_ema:
self.print(f"-> EMA: {self.use_ema}, decay: {self.args.ema_decay}, "
f"update_after_step: {self.args.ema_update_after_step}, "
f"update_every: {self.args.ema_update_every}")
ema_wrapper = partial(
EMA, beta=self.args.ema_decay,
update_after_step=self.args.ema_update_after_step,
update_every=self.args.ema_update_every
)
else:
ema_wrapper = None
return ema_wrapper
@property
def device(self):
return self.accelerator.device
@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 actual_batch_size(self):
if self.accelerator.split_batches is False:
actual_batch_size = self.args.batch_size * self.accelerator.num_processes * self.accelerator.gradient_accumulation_steps
else:
assert self.actual_batch_size % self.accelerator.num_processes == 0
actual_batch_size = self.args.batch_size
return actual_batch_size
@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:
>>> self.params_optimized = self.optimized_params(uvit, verbose=True)
>>> optimizer = torch.optim.AdamW(self.params_optimized, lr=args.lr)
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.results_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.accelerator.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_local_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 self.args.visual:
self.close_tracker()
self.print(msg)
|