Spaces:
Sleeping
Sleeping
File size: 11,652 Bytes
c64c726 a29f249 c64c726 a29f249 c64c726 a29f249 |
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 337 338 339 340 341 342 343 344 345 346 |
from argparse import Namespace
from collections import OrderedDict
from dataclasses import dataclass
from functools import partial
import json
from pathlib import Path
import random
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from omegaconf import OmegaConf
import numpy as np
import torch
import torch.distributed as dist
from torch import Tensor
from torch.optim.lr_scheduler import LambdaLR
import torch.nn as nn
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.optim import AdamW
try:
import wandb
WANDB_AVAILABLE = True
except ImportError:
# wandb not available, set to None for graceful fallback
wandb = None
WANDB_AVAILABLE = False
ATARI_100K_GAMES = [
"Alien",
"Amidar",
"Assault",
"Asterix",
"BankHeist",
"BattleZone",
"Boxing",
"Breakout",
"ChopperCommand",
"CrazyClimber",
"DemonAttack",
"Freeway",
"Frostbite",
"Gopher",
"Hero",
"Jamesbond",
"Kangaroo",
"Krull",
"KungFuMaster",
"MsPacman",
"Pong",
"PrivateEye",
"Qbert",
"RoadRunner",
"Seaquest",
"UpNDown",
]
Logs = List[Dict[str, float]]
LossAndLogs = Tuple[Tensor, Dict[str, Any]]
class StateDictMixin:
def _init_fields(self) -> None:
def has_sd(x: str) -> bool:
return callable(getattr(x, "state_dict", None)) and callable(getattr(x, "load_state_dict", None))
self._all_fields = {k for k in vars(self) if not k.startswith("_")}
self._fields_sd = {k for k in self._all_fields if has_sd(getattr(self, k))}
def _get_field(self, k: str) -> Any:
return getattr(self, k).state_dict() if k in self._fields_sd else getattr(self, k)
def _set_field(self, k: str, v: Any) -> None:
getattr(self, k).load_state_dict(v) if k in self._fields_sd else setattr(self, k, v)
def state_dict(self) -> Dict[str, Any]:
if not hasattr(self, "_all_fields"):
self._init_fields()
return {k: self._get_field(k) for k in self._all_fields}
def load_state_dict(self, state_dict: Dict[str, Any]) -> None:
if not hasattr(self, "_all_fields"):
self._init_fields()
assert set(list(state_dict.keys())) == self._all_fields
for k, v in state_dict.items():
self._set_field(k, v)
@dataclass
class CommonTools(StateDictMixin):
denoiser: Any
upsampler: Optional[Any] = None
rew_end_model: Optional[Any] = None
actor_critic: Optional[Any] = None
def get(self, name: str) -> Any:
return getattr(self, name)
def set(self, name: str, value: Any):
return setattr(self, name, value)
def broadcast_if_needed(*args):
objects = list(args)
if dist.is_initialized():
dist.broadcast_object_list(objects, src=0)
# the list `objects` now contains the version of rank 0
return objects
def build_ddp_wrapper(**modules_dict: Dict[str, nn.Module]) -> Namespace:
return Namespace(**{name: DDP(module) for name, module in modules_dict.items()})
def compute_classification_metrics(confusion_matrix: Tensor) -> Tuple[Tensor, Tensor, Tensor]:
num_classes = confusion_matrix.size(0)
precision = torch.zeros(num_classes)
recall = torch.zeros(num_classes)
f1_score = torch.zeros(num_classes)
for i in range(num_classes):
true_positive = confusion_matrix[i, i].item()
false_positive = confusion_matrix[:, i].sum().item() - true_positive
false_negative = confusion_matrix[i, :].sum().item() - true_positive
precision[i] = true_positive / (true_positive + false_positive) if (true_positive + false_positive) != 0 else 0
recall[i] = true_positive / (true_positive + false_negative) if (true_positive + false_negative) != 0 else 0
f1_score[i] = (
2 * (precision[i] * recall[i]) / (precision[i] + recall[i]) if (precision[i] + recall[i]) != 0 else 0
)
return precision, recall, f1_score
def configure_opt(model: nn.Module, lr: float, weight_decay: float, eps: float, *blacklist_module_names: str) -> AdamW:
"""Credits to https://github.com/karpathy/minGPT"""
# separate out all parameters to those that will and won't experience regularizing weight decay
decay = set()
no_decay = set()
whitelist_weight_modules = (nn.Linear, nn.Conv1d, nn.Conv2d, nn.LSTMCell, nn.LSTM)
blacklist_weight_modules = (nn.LayerNorm, nn.Embedding, nn.GroupNorm)
for mn, m in model.named_modules():
for pn, p in m.named_parameters():
fpn = "%s.%s" % (mn, pn) if mn else pn # full param name
if any([fpn.startswith(module_name) for module_name in blacklist_module_names]):
no_decay.add(fpn)
elif "bias" in pn:
# all biases will not be decayed
no_decay.add(fpn)
elif (pn.endswith("weight") or pn.startswith("weight_")) and isinstance(m, whitelist_weight_modules):
# weights of whitelist modules will be weight decayed
decay.add(fpn)
elif (pn.endswith("weight") or pn.startswith("weight_")) and isinstance(m, blacklist_weight_modules):
# weights of blacklist modules will NOT be weight decayed
no_decay.add(fpn)
# validate that we considered every parameter
param_dict = {pn: p for pn, p in model.named_parameters()}
inter_params = decay & no_decay
union_params = decay | no_decay
assert len(inter_params) == 0, f"parameters {str(inter_params)} made it into both decay/no_decay sets!"
assert (
len(param_dict.keys() - union_params) == 0
), f"parameters {str(param_dict.keys() - union_params)} were not separated into either decay/no_decay set!"
# create the pytorch optimizer object
optim_groups = [
{"params": [param_dict[pn] for pn in sorted(list(decay))], "weight_decay": weight_decay},
{"params": [param_dict[pn] for pn in sorted(list(no_decay))], "weight_decay": 0.0},
]
optimizer = AdamW(optim_groups, lr=lr, eps=eps)
return optimizer
def count_parameters(model: nn.Module) -> int:
return sum(p.numel() for p in model.parameters())
def extract_state_dict(state_dict: OrderedDict, module_name: str) -> OrderedDict:
return OrderedDict({k.split(".", 1)[1]: v for k, v in state_dict.items() if k.startswith(module_name)})
def get_lr_sched(opt: torch.optim.Optimizer, num_warmup_steps: int) -> LambdaLR:
def lr_lambda(current_step: int):
return 1 if current_step >= num_warmup_steps else current_step / max(1, num_warmup_steps)
return LambdaLR(opt, lr_lambda, last_epoch=-1)
def init_lstm(model: nn.Module) -> None:
for name, p in model.named_parameters():
if "weight_ih" in name:
nn.init.xavier_uniform_(p.data)
elif "weight_hh" in name:
nn.init.orthogonal_(p.data)
elif "bias_ih" in name:
p.data.fill_(0)
# Set forget-gate bias to 1
n = p.size(0)
p.data[(n // 4) : (n // 2)].fill_(1)
elif "bias_hh" in name:
p.data.fill_(0)
def get_path_agent_ckpt(path_ckpt_dir: Union[str, Path], epoch: int, num_zeros: int = 5) -> Path:
d = Path(path_ckpt_dir) / "agent_versions"
if epoch >= 0:
return d / f"agent_epoch_{epoch:0{num_zeros}d}.pt"
else:
all_ = sorted(list(d.iterdir()))
assert len(all_) >= -epoch
return all_[epoch]
def keep_agent_copies_every(
agent_sd: Dict[str, Any],
epoch: int,
path_ckpt_dir: Path,
every: int,
num_to_keep: Optional[int],
) -> None:
assert every > 0
assert num_to_keep is None or num_to_keep > 0
get_path = partial(get_path_agent_ckpt, path_ckpt_dir)
get_path(0).parent.mkdir(parents=False, exist_ok=True)
# Save agent
save_with_backup(agent_sd, get_path(epoch))
# Clean oldest
if (num_to_keep is not None) and (epoch % every == 0):
get_path(max(0, epoch - num_to_keep * every)).unlink(missing_ok=True)
# Clean previous
if (epoch - 1) % every != 0:
get_path(max(0, epoch - 1)).unlink(missing_ok=True)
def move_opt_to(opt: AdamW, device: torch.device):
for optimizer_metrics in opt.state.values():
for metric_name, metric in optimizer_metrics.items():
if torch.is_tensor(metric) and metric_name != "step":
optimizer_metrics[metric_name] = metric.to(device)
def process_confusion_matrices_if_any_and_compute_classification_metrics(logs: Logs) -> None:
cm = [x.pop("confusion_matrix") for x in logs if "confusion_matrix" in x]
if len(cm) > 0:
confusion_matrices = {k: sum([d[k] for d in cm]) for k in cm[0]} # accumulate confusion matrices
metrics = {}
for key, confusion_matrix in confusion_matrices.items():
precision, recall, f1_score = compute_classification_metrics(confusion_matrix)
metrics.update(
{
**{f"classification_metrics/{key}_precision_class_{i}": v for i, v in enumerate(precision)},
**{f"classification_metrics/{key}_recall_class_{i}": v for i, v in enumerate(recall)},
**{f"classification_metrics/{key}_f1_score_class_{i}": v for i, v in enumerate(f1_score)},
}
)
logs.append(metrics) # Append the obtained metrics to logs (in place)
def prompt_atari_game():
for i, game in enumerate(ATARI_100K_GAMES):
print(f"{i:2d}: {game}")
while True:
x = input("\nEnter a number: ")
if not x.isdigit():
print("Invalid.")
continue
x = int(x)
if x < 0 or x > 25:
print("Invalid.")
continue
break
game = ATARI_100K_GAMES[x]
return game
def prompt_run_name(game):
cfg_file = Path("config/trainer.yaml")
try:
cfg_name = OmegaConf.load(cfg_file).wandb.name
suffix = f"-{cfg_name}" if cfg_name is not None else ""
except:
# If wandb config not available, use empty suffix
suffix = ""
name = game + suffix
name_ = input(f"Confirm run name by pressing Enter (or enter a new name): {name}\n")
if name_ != "":
name = name_
return name
def save_info_for_import_script(epoch: int, run_name: str, path_ckpt_dir: Path) -> None:
with (path_ckpt_dir / "info_for_import_script.json").open("w") as f:
json.dump({"epoch": epoch, "name": run_name}, f)
def save_with_backup(obj: Any, path: Path):
bk = path.with_suffix(".bk")
if path.is_file():
path.rename(bk)
torch.save(obj, path)
bk.unlink(missing_ok=True)
def set_seed(seed: int) -> None:
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
random.seed(seed)
def skip_if_run_is_over(func: Callable) -> Callable:
def inner(*args, **kwargs):
path_run_is_over = Path(".run_is_over")
if not path_run_is_over.is_file():
func(*args, **kwargs)
path_run_is_over.touch()
else:
print(f"Run is marked as finished. To unmark, remove '{str(path_run_is_over)}'.")
return inner
def try_until_no_except(func: Callable) -> None:
while True:
try:
func()
except KeyboardInterrupt:
break
except Exception:
continue
else:
break
def wandb_log(logs: Logs, epoch: int):
if WANDB_AVAILABLE and wandb is not None:
for d in logs:
wandb.log({"epoch": epoch, **d})
# If wandb not available, silently skip logging
|