File size: 9,805 Bytes
9299fff | 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | """
VicAI Utilities
Helper functions for training and evaluation.
"""
import json
import logging
import math
import os
import sys
from pathlib import Path
from typing import Dict, Optional
import torch
import torch.distributed as dist
from torch.optim import AdamW
def get_logger(name: str, log_file: Optional[Path] = None) -> logging.Logger:
"""Create a logger with file and console handlers."""
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
# Clear existing handlers
logger.handlers = []
# Formatter
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# Console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
# File handler
if log_file:
log_file.parent.mkdir(parents=True, exist_ok=True)
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
def save_checkpoint(
model,
optimizer,
scaler,
step: int,
loss: float,
path: Path,
):
"""Save model checkpoint."""
path.parent.mkdir(parents=True, exist_ok=True)
# Unwrap model if using DDP/FSDP
state_dict = model.state_dict()
if hasattr(model, 'module'):
state_dict = model.module.state_dict()
checkpoint = {
'model': state_dict,
'optimizer': optimizer.state_dict(),
'scaler': scaler.state_dict() if scaler else None,
'step': step,
'loss': loss,
}
torch.save(checkpoint, path)
def load_checkpoint(
model,
optimizer,
scaler,
path: str,
device,
):
"""Load model checkpoint."""
checkpoint = torch.load(path, map_location=device)
# Handle both wrapped and unwrapped models
state_dict = checkpoint['model']
if hasattr(model, 'module'):
model.module.load_state_dict(state_dict)
else:
model.load_state_dict(state_dict)
optimizer.load_state_dict(checkpoint['optimizer'])
if scaler and checkpoint.get('scaler'):
scaler.load_state_dict(checkpoint['scaler'])
return checkpoint.get('step', 0)
def get_lr_scheduler(optimizer, args):
"""Create learning rate scheduler with warmup and cosine decay."""
def lr_lambda(current_step):
if current_step < args.warmup_steps:
# Linear warmup
return current_step / args.warmup_steps
else:
# Cosine decay
progress = (current_step - args.warmup_steps) / (args.max_steps - args.warmup_steps)
progress = min(1.0, progress)
cosine_decay = 0.5 * (1 + math.cos(math.pi * progress))
return args.min_lr / args.learning_rate + (1 - args.min_lr / args.learning_rate) * cosine_decay
from torch.optim.lr_scheduler import LambdaLR
return LambdaLR(optimizer, lr_lambda)
def configure_optimizers(model, args):
"""Configure optimizer with weight decay."""
# Separate parameters that should and shouldn't have weight decay
decay_params = []
no_decay_params = []
for name, param in model.named_parameters():
if not param.requires_grad:
continue
# Don't apply weight decay to bias and normalization parameters
if 'bias' in name or 'norm' in name or 'embedding' in name:
no_decay_params.append(param)
else:
decay_params.append(param)
param_groups = [
{'params': decay_params, 'weight_decay': args.weight_decay},
{'params': no_decay_params, 'weight_decay': 0.0},
]
optimizer = AdamW(
param_groups,
lr=args.learning_rate,
betas=(args.beta1, args.beta2),
eps=1e-8,
)
return optimizer
def estimate_loss(model, data_loader, device, num_batches=10):
"""Estimate loss on a data loader."""
model.eval()
total_loss = 0
with torch.no_grad():
for i, batch in enumerate(data_loader):
if i >= num_batches:
break
input_ids = batch['input_ids'].to(device)
labels = batch['labels'].to(device)
outputs = model(input_ids, targets=labels)
total_loss += outputs['loss'].item()
model.train()
return total_loss / num_batches
def get_grad_norm(model):
"""Calculate gradient norm."""
total_norm = 0.0
for p in model.parameters():
if p.grad is not None:
total_norm += p.grad.data.norm(2).item() ** 2
return total_norm ** 0.5
def clip_gradients(model, max_norm):
"""Clip gradients by norm."""
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
class AverageMeter:
"""Track running average of metrics."""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class EarlyStopping:
"""Early stopping to prevent overfitting."""
def __init__(self, patience=5, min_delta=0.0):
self.patience = patience
self.min_delta = min_delta
self.counter = 0
self.best_loss = None
self.early_stop = False
def __call__(self, val_loss):
if self.best_loss is None:
self.best_loss = val_loss
elif val_loss > self.best_loss - self.min_delta:
self.counter += 1
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_loss = val_loss
self.counter = 0
return self.early_stop
def count_parameters(model):
"""Count trainable parameters."""
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def format_num_parameters(num_params):
"""Format parameter count for display."""
if num_params >= 1e9:
return f"{num_params / 1e9:.2f}B"
elif num_params >= 1e6:
return f"{num_params / 1e6:.2f}M"
elif num_params >= 1e3:
return f"{num_params / 1e3:.2f}K"
else:
return str(num_params)
def get_device_info():
"""Get information about available GPUs."""
if not torch.cuda.is_available():
return "No CUDA available"
info = []
for i in range(torch.cuda.device_count()):
props = torch.cuda.get_device_properties(i)
info.append(
f"GPU {i}: {props.name} ({props.total_memory / 1e9:.1f} GB)"
)
return "\n".join(info)
def print_model_summary(model):
"""Print a summary of the model architecture."""
print("\n" + "=" * 60)
print("MODEL SUMMARY")
print("=" * 60)
total_params = 0
trainable_params = 0
print(f"\n{'Layer':<40} {'Parameters':>15} {'Trainable':>10}")
print("-" * 70)
for name, param in model.named_parameters():
num_params = param.numel()
total_params += num_params
if param.requires_grad:
trainable_params += num_params
trainable = "Yes"
else:
trainable = "No"
print(f"{name:<40} {num_params:>15,} {trainable:>10}")
print("-" * 70)
print(f"{'Total':<40} {total_params:>15,}")
print(f"{'Trainable':<40} {trainable_params:>15,}")
print(f"{'Non-trainable':<40} {total_params - trainable_params:>15,}")
print("=" * 60 + "\n")
def save_training_config(args, output_path: Path):
"""Save training configuration to JSON."""
config = vars(args)
with open(output_path, 'w') as f:
json.dump(config, f, indent=2)
def load_training_config(config_path: Path):
"""Load training configuration from JSON."""
with open(config_path, 'r') as f:
return json.load(f)
def all_reduce_dict(data: Dict, device):
"""All reduce dictionary values across processes."""
if not dist.is_initialized():
return data
reduced_data = {}
for key, value in data.items():
if isinstance(value, (int, float)):
tensor = torch.tensor([value], device=device)
dist.all_reduce(tensor, op=dist.ReduceOp.AVG)
reduced_data[key] = tensor.item()
else:
reduced_data[key] = value
return reduced_data
def set_seed(seed: int):
"""Set random seed for reproducibility."""
import random
import numpy as np
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# For deterministic operations (may be slower)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def get_memory_usage():
"""Get current memory usage."""
if torch.cuda.is_available():
allocated = torch.cuda.memory_allocated() / 1e9
reserved = torch.cuda.memory_reserved() / 1e9
max_allocated = torch.cuda.max_memory_allocated() / 1e9
return {
'allocated_gb': allocated,
'reserved_gb': reserved,
'max_allocated_gb': max_allocated,
}
return {'allocated_gb': 0, 'reserved_gb': 0, 'max_allocated_gb': 0}
if __name__ == "__main__":
# Test utilities
logger = get_logger("test")
logger.info("Testing logger")
print(get_device_info())
meter = AverageMeter()
for i in range(10):
meter.update(i)
print(f"Average: {meter.avg}")
|