File size: 24,012 Bytes
e841b45 | 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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | """
SGWI + Dual Fisher Trainer (Contribution 2)
============================================
Inherits from SRT_Trainer. Adds:
- SGWI (SRT-Guided Warm Initialization): warm-init LoRA from similar past tasks
- Dual Fisher: SRT-weighted embedding Fisher penalty during training
Usage in run_t5.py:
from sgwi_trainer import SGWI_DualFisher_Trainer
trainer = SGWI_DualFisher_Trainer(
...,
sgwi_mode='sgwi', # 'sgwi', 'inflora', 'random', 'sgwi+inflora'
lambda_emb=0.01, # Dual Fisher strength (0 = disabled)
)
"""
import os
import math
import logging
import numpy as np
import torch
import torch.nn as nn
from typing import Optional, Dict, List, Tuple
from cl_trainer_srt import SRT_Trainer
logger = logging.getLogger(__name__)
class SGWI_DualFisher_Trainer(SRT_Trainer):
"""SRT_Trainer + SGWI initialization + Dual Fisher regularization."""
def __init__(
self,
model,
args,
train_dataset,
cur_task_id,
task_order,
# SGWI params
sgwi_mode: str = 'sgwi', # 'sgwi', 'inflora', 'random', 'sgwi+inflora'
lambda_emb: float = 0.0, # Dual Fisher Ξ» (0 = disabled)
# SRT params (passed to SRT_Trainer)
srt_metric_mode='hard',
srt_shrink=True,
srt_shrink_factor=0.1,
srt_max_emb_samples=500,
srt_load_path=None,
srt_skip_forward=False,
# Standard trainer params
data_collator_replay=None,
replay_dataset_dict=None,
replay_label_dict=None,
eval_dataset=None,
tokenizer=None,
data_collator=None,
compute_metrics=None,
callbacks=None,
**kwargs,
):
super().__init__(
model=model,
args=args,
train_dataset=train_dataset,
cur_task_id=cur_task_id,
task_order=task_order,
srt_metric_mode=srt_metric_mode,
srt_shrink=srt_shrink,
srt_shrink_factor=srt_shrink_factor,
srt_max_emb_samples=srt_max_emb_samples,
srt_load_path=srt_load_path,
srt_skip_forward=srt_skip_forward,
data_collator_replay=data_collator_replay,
replay_dataset_dict=replay_dataset_dict,
replay_label_dict=replay_label_dict,
eval_dataset=eval_dataset,
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics,
callbacks=callbacks,
**kwargs,
)
self.sgwi_mode = sgwi_mode
self.lambda_emb = lambda_emb
self.theta_stars: Dict[int, Dict[str, torch.Tensor]] = {}
self._current_task_mu = None # cached for Dual Fisher
logger.info(f"[SGWI] mode={sgwi_mode}, lambda_emb={lambda_emb}, task_id={cur_task_id}")
# =========================================================================
# SGWI: Override get_reg_matrix
# =========================================================================
def get_reg_matrix(self):
"""Override InfLoRA initialization with SGWI or other modes.
6 Ablation Configs:
Config 1 'inflora': InfLoRA baseline (null-space + GPM)
Config 2 'random': No GPM, freeze A(kaiming), B=0
Config 3 'full_lora': No GPM, train A(kaiming)+B(0)
Config 5 'sgwi_freeze_a': No GPM, SGWIβA(frozen), B=0
Config 6 'sgwi_train_a': No GPM, SGWIβA(trainable), B=0
Config 4 'sgwi_full': No GPM, SGWIβA(trainable)+B(warm)
"""
mode = self.sgwi_mode
logger.info(f"[SGWI] get_reg_matrix: mode={mode}, task_id={self.cur_task_id}")
# ββ Config 1: InfLoRA baseline ββββββββββββββββββββββββββββββ
if mode == 'inflora':
super().get_reg_matrix()
return
# ββ All other configs: NO GPM, NO null-space ββββββββββββββββ
self._init_gpm_attrs_skip()
# Task 0: no prior tasks β nothing to warm-init
if self.cur_task_id == 0:
logger.info(f"[SGWI] Task 0, mode={mode} β standard init (no prior tasks)")
return
# ββ Config 2: random (freeze A kaiming, B=0) ββββββββββββββββ
if mode == 'random':
logger.info("[SGWI] Config 2: random β no warm init")
return
# ββ Config 3: full_lora (train A kaiming, B=0) ββββββββββββββ
if mode == 'full_lora':
logger.info("[SGWI] Config 3: full_lora β no warm init, A will be unfrozen by run_t5.py")
return
# ββ Configs 4,5,6: need SGWI weights ββββββββββββββββββββββββ
srt_weights = self._compute_sgwi_weights()
if not srt_weights:
logger.warning("[SGWI] No SRT weights. Falling back to random init.")
return
logger.info(f"[SGWI] SRT weights: {srt_weights}")
# ββ Config 5: sgwi_freeze_a (SGWIβA frozen, B=0) ββββββββββββ
if mode == 'sgwi_freeze_a':
logger.info("[SGWI] Config 5: warm-init A only (frozen)")
self._sgwi_init_a(srt_weights)
return
# ββ Config 6: sgwi_train_a (SGWIβA trainable, B=0) ββββββββββ
if mode == 'sgwi_train_a':
logger.info("[SGWI] Config 6: warm-init A only (will be unfrozen by run_t5.py)")
self._sgwi_init_a(srt_weights)
return
# ββ Config 4: sgwi_full (SGWIβA+B, both trainable) ββββββββββ
if mode == 'sgwi_full':
logger.info("[SGWI] Config 4: warm-init both A and B")
self._sgwi_init_a(srt_weights)
self._fuse_past_lora_adapters(srt_weights) # warm-init B
return
# Legacy modes
if mode == 'sgwi':
self._sgwi_init()
return
if mode == 'sgwi+inflora':
self._sgwi_init()
super().get_reg_matrix()
return
raise ValueError(f"Unknown sgwi_mode: {mode}")
def _init_gpm_attrs_skip(self):
"""Initialize GPM attributes to safe defaults so _inner_training_loop doesn't crash.
Sets _cur_task=0 (falsy) so GPM projection is skipped during training.
Use for sgwi-only and random modes."""
self._cur_task = 0
self.feature_list = []
self.feature_trans_list = []
self.feature_mat = []
self.feature_trans_mat = []
logger.info("[SGWI] GPM attrs initialized (skip mode): _cur_task=0, feature_list=[]")
def _sgwi_init_a(self, srt_weights: Dict[int, float]):
"""SGWI warm-init for lora_A only, via SVD of weighted past ΞW.
delta_W = Ξ£ w_s * (B_s @ A_s) [out_dim, in_dim]
SVD: delta_W = U @ S @ V^T
A_new = sqrt(S[:r]) * V^T[:r, :] β captures the important input directions
"""
model = self.model
device = next(model.parameters()).device
lora_r = self.args.lora_r if hasattr(self.args, 'lora_r') else 8
fused_count = 0
skipped_count = 0
for name, module in model.named_modules():
if not (hasattr(module, 'lora_q') and hasattr(module, 'lora_v')):
continue
if not hasattr(module, 'previous_lora_weights_q'):
continue
prev_q = getattr(module, 'previous_lora_weights_q', None)
prev_v = getattr(module, 'previous_lora_weights_v', None)
if prev_q is None or len(prev_q) == 0:
continue
for lora_tag, lora_cur, prev_list in [
('lora_q', module.lora_q, prev_q),
('lora_v', module.lora_v, prev_v),
]:
if prev_list is None or len(prev_list) == 0:
continue
# Compute delta_W from past tasks
delta_W = None
for past_task_id, w_s in srt_weights.items():
if isinstance(past_task_id, str):
try:
pos = self.task_order.index(past_task_id)
idx = (self.cur_task_id - 1) - pos
except ValueError:
skipped_count += 1
continue
else:
idx = int(past_task_id)
if idx < 0 or idx >= len(prev_list):
skipped_count += 1
continue
past_lora = prev_list[idx]
BA = past_lora.lora_B.data.float() @ past_lora.lora_A.data.float()
delta_W = w_s * BA if delta_W is None else delta_W + w_s * BA
if delta_W is None or delta_W.norm().item() < 1e-10:
skipped_count += 1
continue
# SVD decomposition β extract top-r input directions for A
try:
U, S, Vt = torch.linalg.svd(delta_W.to(device), full_matrices=False)
current_r = lora_cur.lora_A.data.shape[0]
r = min(current_r, len(S))
S_sqrt = torch.sqrt(S[:r] + 1e-12)
A_new = S_sqrt.unsqueeze(1) * Vt[:r, :] # [r, in_dim]
old_norm = lora_cur.lora_A.data.norm().item()
lora_cur.lora_A.data[:r, :].copy_(A_new.to(lora_cur.lora_A.data.device))
new_norm = lora_cur.lora_A.data.norm().item()
if fused_count < 3:
logger.info(f"[SGWI-A] {name}.{lora_tag}: A_new norm={new_norm:.4f} (was {old_norm:.4f})")
fused_count += 1
except Exception as e:
logger.warning(f"[SGWI-A] SVD failed for {name}.{lora_tag}: {e}")
skipped_count += 1
logger.info(f"[SGWI-A] Warm-init A for {fused_count} modules, skipped {skipped_count}")
def _sgwi_init(self):
"""SRT-Guided Warm Initialization for current task's LoRA."""
logger.info(f"[SGWI] Initializing task {self.cur_task_id} from past LoRA adapters...")
# Step 1: Get SRT weights from router signatures
srt_weights = self._compute_sgwi_weights()
if not srt_weights:
logger.warning("[SGWI] No past task signatures found. Falling back to standard init.")
super().get_reg_matrix()
return
logger.info(f"[SGWI] SRT weights: {srt_weights}")
# Step 2: Load past LoRA weights and compute weighted fusion
self._fuse_past_lora_adapters(srt_weights)
def _compute_sgwi_weights(self) -> Dict[int, float]:
"""Compute softmax weights from SRT distances to past tasks."""
if self.srt_router is None or len(self.srt_router.signatures) == 0:
return {}
# Extract current task embeddings to compute signature
logger.info("[SGWI] Extracting current task embeddings for distance computation...")
result = self._extract_task_embeddings(max_samples=self.srt_max_emb_samples)
# _extract_task_embeddings returns (h_train, task_ids) tuple
if isinstance(result, tuple):
h_train, _ = result
else:
h_train = result
if h_train is None or (hasattr(h_train, '__len__') and len(h_train) == 0):
return {}
current_mu = h_train.mean(dim=0).cpu().numpy()
self._current_task_mu = current_mu
# Compute distances to all past tasks
distances = {}
for task_id, sig in self.srt_router.signatures.items():
diff = current_mu - sig.mu
# Use pooled covariance if available, else L2
if hasattr(self.srt_router, 'pooled_cov') and self.srt_router.pooled_cov is not None:
try:
cov_inv = np.linalg.inv(self.srt_router.pooled_cov + 1e-6 * np.eye(len(diff)))
d = float(diff @ cov_inv @ diff)
except np.linalg.LinAlgError:
d = float(np.sum(diff ** 2))
else:
d = float(np.sum(diff ** 2))
distances[task_id] = d
if len(distances) == 0:
return {}
# Trivial case: only 1 past task
if len(distances) == 1:
return {k: 1.0 for k in distances}
# Softmax with Ο = median heuristic
d_values = list(distances.values())
tau = float(np.median(d_values)) + 1e-8
weights = {}
for k, d in distances.items():
weights[k] = math.exp(-d / tau)
Z = sum(weights.values()) + 1e-12
weights = {k: w / Z for k, w in weights.items()}
return weights
def _fuse_past_lora_adapters(self, srt_weights: Dict[int, float]):
"""Weighted fusion of past LoRA adapters β warm-init current task's lora_B.
Architecture note (GainLoRA-InfLoRA):
- lora_A / lora_B: SINGLE nn.Parameter per module = CURRENT task's adapter
- previous_lora_weights_q[i] / previous_lora_weights_v[i]: past task i's LoRALayer
with .lora_A (nn.Parameter) and .lora_B (nn.Parameter)
Strategy:
1. For each past task s, get B_s and A_s from previous_lora_weights
2. Compute delta_W = Ξ£ w_s * (B_s @ A_s)
3. Warm-init current lora_B via least-squares: B_new = delta_W @ A_cur^+
"""
model = self.model
device = next(model.parameters()).device
fused_count = 0
skipped_count = 0
for name, module in model.named_modules():
# Look for T5Attention modules with previous_lora_weights
if not (hasattr(module, 'lora_q') and hasattr(module, 'lora_v')):
continue
if not hasattr(module, 'previous_lora_weights_q'):
continue
prev_q = getattr(module, 'previous_lora_weights_q', None)
prev_v = getattr(module, 'previous_lora_weights_v', None)
if prev_q is None or len(prev_q) == 0:
continue
for lora_tag, lora_cur, prev_list in [
('lora_q', module.lora_q, prev_q),
('lora_v', module.lora_v, prev_v),
]:
if prev_list is None or len(prev_list) == 0:
continue
# Compute weighted ΞW = Ξ£ w_s * (B_s @ A_s)
delta_W = None
for past_task_id, w_s in srt_weights.items():
# past_task_id may be a string task name (from srt_router.signatures)
# or an int. Convert to previous_lora_weights index.
# In run_t5.py: previous_lora_path is "t1,t2,...,t_{n-1}" in task_order,
# then reversed β prev_list[0] = most recent = task_order[cur_task_id-1]
if isinstance(past_task_id, str):
try:
pos = self.task_order.index(past_task_id)
idx = (self.cur_task_id - 1) - pos
except ValueError:
logger.debug(f"[SGWI] task name '{past_task_id}' not in task_order, skipping")
skipped_count += 1
continue
else:
idx = int(past_task_id)
if idx < 0 or idx >= len(prev_list):
logger.debug(f"[SGWI] idx={idx} out of range for prev_list len={len(prev_list)}, skipping")
skipped_count += 1
continue
past_lora = prev_list[idx]
A_s = past_lora.lora_A.data.float() # [r, in_features]
B_s = past_lora.lora_B.data.float() # [out_features, r]
BA = B_s @ A_s # [out_features, in_features]
if delta_W is None:
delta_W = w_s * BA
else:
delta_W = delta_W + w_s * BA
if delta_W is None:
continue
delta_W_norm = delta_W.norm().item()
if delta_W_norm < 1e-10:
logger.debug(f"[SGWI] delta_W β 0 for {name}.{lora_tag} (norm={delta_W_norm:.2e}), skipping")
skipped_count += 1
continue
# Warm-init lora_B (trainable) via least-squares given frozen lora_A:
# B_warm = delta_W @ A_cur^T @ (A_cur @ A_cur^T + Ξ΅I)^{-1}
try:
A_cur = lora_cur.lora_A.data.float().to(device) # [r, in_features]
AtA = A_cur @ A_cur.T # [r, r]
eps_mat = 1e-4 * torch.eye(A_cur.shape[0], device=device)
B_warm = delta_W.to(device) @ A_cur.T @ torch.linalg.inv(AtA + eps_mat) # [out_features, r]
old_norm = lora_cur.lora_B.data.norm().item()
lora_cur.lora_B.data.copy_(B_warm.to(lora_cur.lora_B.data.device))
new_norm = lora_cur.lora_B.data.norm().item()
if fused_count < 3:
logger.info(f"[SGWI] {name}.{lora_tag}: delta_W={delta_W_norm:.4f}, "
f"B_warm={new_norm:.4f} (was {old_norm:.4f})")
fused_count += 1
except Exception as e:
logger.warning(f"[SGWI] lora_B warm init failed for {name}.{lora_tag}: {e}")
continue
logger.info(f"[SGWI] Fused {fused_count} LoRA modules, skipped {skipped_count}")
def _get_lora_weight(self, lora_module, attr_name, task_id):
"""Safely get LoRA weight tensor for a given task_id."""
attr = getattr(lora_module, attr_name, None)
if attr is None:
return None
# Case 1: nn.ModuleDict or dict keyed by task_id
if isinstance(attr, (nn.ModuleDict, dict)):
key = str(task_id) if isinstance(attr, nn.ModuleDict) else task_id
if key in attr:
m = attr[key]
return m.weight.data if hasattr(m, 'weight') else m.data
# Try integer key
if task_id in attr:
m = attr[task_id]
return m.weight.data if hasattr(m, 'weight') else m.data
# Case 2: nn.ModuleList indexed by task_id
if isinstance(attr, nn.ModuleList):
if task_id < len(attr):
m = attr[task_id]
return m.weight.data if hasattr(m, 'weight') else m.data
# Case 3: Single nn.Linear (only for current task)
if isinstance(attr, nn.Linear):
return attr.weight.data
# Case 4: raw tensor
if isinstance(attr, torch.Tensor):
return attr
return None
def _set_lora_weight(self, lora_module, attr_name, task_id, new_weight):
"""Safely set LoRA weight tensor for current task_id."""
attr = getattr(lora_module, attr_name, None)
if attr is None:
return
try:
if isinstance(attr, (nn.ModuleDict, dict)):
key = str(task_id) if isinstance(attr, nn.ModuleDict) else task_id
if key in attr:
m = attr[key]
if hasattr(m, 'weight'):
m.weight.data.copy_(new_weight.to(m.weight.device))
else:
attr[key] = new_weight.to(next(lora_module.parameters()).device)
elif isinstance(attr, nn.ModuleList):
if task_id < len(attr):
m = attr[task_id]
if hasattr(m, 'weight'):
m.weight.data.copy_(new_weight.to(m.weight.device))
elif isinstance(attr, nn.Linear):
attr.weight.data.copy_(new_weight.to(attr.weight.device))
except Exception as e:
logger.debug(f"[SGWI] Could not set weight for {attr_name}[{task_id}]: {e}")
# =========================================================================
# Dual Fisher: Override compute_loss
# =========================================================================
def compute_loss(self, model, inputs, return_outputs=False):
"""Standard loss + Dual Fisher penalty."""
outputs = model(**inputs)
loss = outputs["loss"] if isinstance(outputs, dict) else outputs[0]
if self.lambda_emb > 0 and self.cur_task_id > 0 and len(self.theta_stars) > 0:
fisher_loss = self._dual_fisher_penalty()
loss = loss + fisher_loss
return (loss, outputs) if return_outputs else loss
def _dual_fisher_penalty(self) -> torch.Tensor:
"""SRT-weighted L2 penalty around past task parameters."""
total = torch.tensor(0.0, device=next(self.model.parameters()).device)
# Get SRT weights (cached or recompute)
srt_weights = {}
if self.srt_router and len(self.srt_router.signatures) > 0:
for task_id in self.theta_stars:
if task_id in self.srt_router.signatures:
srt_weights[task_id] = 1.0 # uniform for now
# Normalize
if srt_weights:
Z = sum(srt_weights.values())
srt_weights = {k: v / Z for k, v in srt_weights.items()}
if not srt_weights:
return total
for task_id, w_s in srt_weights.items():
if task_id not in self.theta_stars:
continue
for name, param in self.model.named_parameters():
if not param.requires_grad:
continue
if 'lora_' not in name:
continue
if name not in self.theta_stars[task_id]:
continue
theta_star = self.theta_stars[task_id][name].to(param.device)
delta = param - theta_star
total = total + w_s * (delta ** 2).sum()
return self.lambda_emb * total
# =========================================================================
# Override on_task_end to save ΞΈ* for Dual Fisher
# =========================================================================
def on_task_end(self, task_id):
"""Save ΞΈ* for Dual Fisher, then do standard SRT task end."""
# Save current task's final LoRA weights for future Dual Fisher
logger.info(f"[SGWI] Saving ΞΈ* for task {task_id} (Dual Fisher)")
self.theta_stars[task_id] = {}
for name, param in self.model.named_parameters():
if 'lora_' in name and param.requires_grad:
self.theta_stars[task_id][name] = param.detach().clone().cpu()
saved_count = len(self.theta_stars[task_id])
logger.info(f"[SGWI] Saved {saved_count} parameters for task {task_id}")
# Save theta_stars to disk for persistence
theta_stars_path = os.path.join(self.args.output_dir, 'saved_weights', 'theta_stars.pt')
os.makedirs(os.path.dirname(theta_stars_path), exist_ok=True)
torch.save(self.theta_stars, theta_stars_path)
# Standard SRT: compute signature, wire router
super().on_task_end(task_id)
def _load_theta_stars(self, srt_load_path: str):
"""Load previously saved ΞΈ* from checkpoint."""
theta_path = os.path.join(srt_load_path, 'theta_stars.pt')
if os.path.exists(theta_path):
self.theta_stars = torch.load(theta_path, map_location='cpu')
logger.info(f"[SGWI] Loaded ΞΈ* for {len(self.theta_stars)} past tasks from {theta_path}")
else:
logger.info(f"[SGWI] No ΞΈ* found at {theta_path}")
|