Update continual_learning.py
Browse files- continual_learning.py +260 -293
continual_learning.py
CHANGED
|
@@ -1,294 +1,261 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
import
|
| 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 |
-
self.modality_configs
|
| 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 |
-
def
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
self.
|
| 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 |
-
self.
|
| 260 |
-
|
| 261 |
-
self.device = next(model.parameters()).device
|
| 262 |
-
|
| 263 |
-
self.params = {}
|
| 264 |
-
self.fisher = {}
|
| 265 |
-
self.task_count = 0
|
| 266 |
-
|
| 267 |
-
def update_fisher(self, dataloader: DataLoader):
|
| 268 |
-
"""更新Fisher信息矩阵"""
|
| 269 |
-
print(f"Updating Online EWC Fisher Matrix (Task {self.task_count + 1})...")
|
| 270 |
-
new_fisher = self._compute_fisher(dataloader)
|
| 271 |
-
|
| 272 |
-
if self.task_count == 0:
|
| 273 |
-
self.fisher = new_fisher
|
| 274 |
-
else:
|
| 275 |
-
for n in self.fisher:
|
| 276 |
-
if n in new_fisher:
|
| 277 |
-
# 移动平均更新 Fisher 信息
|
| 278 |
-
self.fisher[n] = self.gamma * self.fisher[n] + new_fisher[n]
|
| 279 |
-
|
| 280 |
-
# 更新参考参数为当前任务训练后的参数
|
| 281 |
-
self.params = {
|
| 282 |
-
n: p.clone().detach()
|
| 283 |
-
for n, p in self.model.named_parameters()
|
| 284 |
-
if p.requires_grad
|
| 285 |
-
}
|
| 286 |
-
|
| 287 |
-
self.task_count += 1
|
| 288 |
-
print(f"Online EWC regularizer updated.")
|
| 289 |
-
|
| 290 |
-
def penalty(self, model: Optional[nn.Module] = None) -> torch.Tensor:
|
| 291 |
-
"""计算EWC惩罚项"""
|
| 292 |
-
if self.task_count == 0:
|
| 293 |
-
return torch.tensor(0.0, device=self.device)
|
| 294 |
return super().penalty(model)
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import numpy as np
|
| 5 |
+
from torch.utils.data import DataLoader
|
| 6 |
+
from collections import deque
|
| 7 |
+
from typing import List, Dict, Any, Optional, Union
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
from dataclasses import dataclass
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class ModalityConfig:
|
| 14 |
+
name: str
|
| 15 |
+
modality_id: int
|
| 16 |
+
|
| 17 |
+
class UnifiedMultiModalPreprocessor(nn.Module):
|
| 18 |
+
def __init__(self, model_dim: int = 2048):
|
| 19 |
+
super().__init__()
|
| 20 |
+
self.modality_configs = {
|
| 21 |
+
'text': ModalityConfig('text', 0),
|
| 22 |
+
'image': ModalityConfig('image', 1),
|
| 23 |
+
'audio': ModalityConfig('audio', 2),
|
| 24 |
+
'video': ModalityConfig('video', 3)
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def process_batch(self, batch_data: Union[torch.Tensor, List[Any]], modality_type: str) -> List[Dict]:
|
| 28 |
+
processed_segments = []
|
| 29 |
+
if modality_type not in self.modality_configs:
|
| 30 |
+
return processed_segments
|
| 31 |
+
|
| 32 |
+
config = self.modality_configs[modality_type]
|
| 33 |
+
|
| 34 |
+
if isinstance(batch_data, list):
|
| 35 |
+
# 过滤 None
|
| 36 |
+
valid_data = [x for x in batch_data if x is not None]
|
| 37 |
+
if not valid_data:
|
| 38 |
+
return []
|
| 39 |
+
# 假设 list 中全是 Tensor,且维度一致,进行堆叠
|
| 40 |
+
# 如果是 list of tensor (B, C, H, W) -> stack -> (B, C, H, W)
|
| 41 |
+
try:
|
| 42 |
+
data_tensor = torch.stack(valid_data)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error stacking modality data: {e}")
|
| 45 |
+
return []
|
| 46 |
+
elif isinstance(batch_data, torch.Tensor):
|
| 47 |
+
data_tensor = batch_data
|
| 48 |
+
else:
|
| 49 |
+
return []
|
| 50 |
+
|
| 51 |
+
processed_segments.append({
|
| 52 |
+
'type': modality_type,
|
| 53 |
+
'data': data_tensor,
|
| 54 |
+
'modality_id': config.modality_id
|
| 55 |
+
})
|
| 56 |
+
return processed_segments
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class ExperienceReplayBuffer:
|
| 60 |
+
def __init__(self, max_size: int = 10000):
|
| 61 |
+
self.buffer = deque(maxlen=max_size)
|
| 62 |
+
|
| 63 |
+
def add(self, sample: Dict[str, Any]):
|
| 64 |
+
safe_sample = {}
|
| 65 |
+
for k, v in sample.items():
|
| 66 |
+
if isinstance(v, torch.Tensor):
|
| 67 |
+
safe_sample[k] = v.detach().cpu()
|
| 68 |
+
elif isinstance(v, list):
|
| 69 |
+
# 递归处理 list 中的 tensor
|
| 70 |
+
safe_sample[k] = [x.detach().cpu() if isinstance(x, torch.Tensor) else x for x in v]
|
| 71 |
+
else:
|
| 72 |
+
safe_sample[k] = v
|
| 73 |
+
self.buffer.append(safe_sample)
|
| 74 |
+
|
| 75 |
+
def sample(self, batch_size: int) -> List[Any]:
|
| 76 |
+
"""从buffer中采样"""
|
| 77 |
+
if not self.buffer:
|
| 78 |
+
return []
|
| 79 |
+
|
| 80 |
+
indices = np.random.choice(
|
| 81 |
+
len(self.buffer),
|
| 82 |
+
min(len(self.buffer), batch_size),
|
| 83 |
+
replace=False
|
| 84 |
+
)
|
| 85 |
+
return [self.buffer[i] for i in indices]
|
| 86 |
+
|
| 87 |
+
def __len__(self):
|
| 88 |
+
return len(self.buffer)
|
| 89 |
+
|
| 90 |
+
def clear(self):
|
| 91 |
+
"""清空buffer"""
|
| 92 |
+
self.buffer.clear()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class EWC:
|
| 96 |
+
"""弹性权重固化 (Elastic Weight Consolidation)"""
|
| 97 |
+
def __init__(
|
| 98 |
+
self,
|
| 99 |
+
model: nn.Module,
|
| 100 |
+
dataloader: DataLoader,
|
| 101 |
+
preprocessor: UnifiedMultiModalPreprocessor,
|
| 102 |
+
importance: float = 1000.0
|
| 103 |
+
):
|
| 104 |
+
self.model = model
|
| 105 |
+
self.preprocessor = preprocessor
|
| 106 |
+
self.importance = importance
|
| 107 |
+
self.device = next(model.parameters()).device
|
| 108 |
+
|
| 109 |
+
# 冻结当前参数作为参考
|
| 110 |
+
self.params = {
|
| 111 |
+
n: p.clone().detach()
|
| 112 |
+
for n, p in model.named_parameters()
|
| 113 |
+
if p.requires_grad
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
self.fisher = self._compute_fisher(dataloader)
|
| 117 |
+
|
| 118 |
+
def _compute_fisher(self, dataloader: DataLoader) -> Dict[str, torch.Tensor]:
|
| 119 |
+
"""计算Fisher信息矩阵 (使用 Empirical Fisher)"""
|
| 120 |
+
fisher = {
|
| 121 |
+
n: torch.zeros_like(p)
|
| 122 |
+
for n, p in self.model.named_parameters()
|
| 123 |
+
if p.requires_grad
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
self.model.eval()
|
| 127 |
+
num_samples = 0
|
| 128 |
+
|
| 129 |
+
# 使用 tqdm 稍微简化输出
|
| 130 |
+
pbar = tqdm(dataloader, desc="Computing Fisher Matrix", leave=False)
|
| 131 |
+
for batch in pbar:
|
| 132 |
+
if batch is None: continue
|
| 133 |
+
|
| 134 |
+
self.model.zero_grad()
|
| 135 |
+
|
| 136 |
+
# 1. 准备文本输入
|
| 137 |
+
instruction_ids = batch['instruction'].to(self.device)
|
| 138 |
+
response_ids = batch['response'].to(self.device)
|
| 139 |
+
# 拼接: [Instruction, Response]
|
| 140 |
+
input_ids = torch.cat([instruction_ids, response_ids], dim=1)
|
| 141 |
+
|
| 142 |
+
# 2. 准备多模态输入结构
|
| 143 |
+
input_data = {'segments': []}
|
| 144 |
+
|
| 145 |
+
# 处理额外的模态数据
|
| 146 |
+
raw_modality_data = batch.get('modality_data')
|
| 147 |
+
if raw_modality_data is not None:
|
| 148 |
+
modality_type = batch.get('modality_type', 'image')
|
| 149 |
+
if isinstance(modality_type, list): modality_type = modality_type[0]
|
| 150 |
+
|
| 151 |
+
mod_segments = self.preprocessor.process_batch(raw_modality_data, modality_type)
|
| 152 |
+
for seg in mod_segments:
|
| 153 |
+
seg['data'] = seg['data'].to(self.device)
|
| 154 |
+
input_data['segments'].append(seg)
|
| 155 |
+
|
| 156 |
+
input_data['segments'].append({
|
| 157 |
+
'type': 'text',
|
| 158 |
+
'data': input_ids,
|
| 159 |
+
'modality_id': 0
|
| 160 |
+
})
|
| 161 |
+
|
| 162 |
+
output = self.model(input_data)
|
| 163 |
+
logits = output['logits']
|
| 164 |
+
|
| 165 |
+
# 4. 计算 Loss (Standard Causal LM Loss)
|
| 166 |
+
# Shift logits and labels
|
| 167 |
+
# input_ids: [I1, I2, R1, R2]
|
| 168 |
+
# labels: [I2, R1, R2, EOS]
|
| 169 |
+
shift_logits = logits[:, :-1, :].contiguous()
|
| 170 |
+
shift_labels = input_ids[:, 1:].contiguous()
|
| 171 |
+
|
| 172 |
+
# 创建 Mask: 只在 Response 部分计算梯度
|
| 173 |
+
# Instruction 长度
|
| 174 |
+
inst_len = instruction_ids.shape[1]
|
| 175 |
+
loss_mask = torch.ones_like(shift_labels, dtype=torch.float)
|
| 176 |
+
if inst_len > 1:
|
| 177 |
+
loss_mask[:, :inst_len-1] = 0.0
|
| 178 |
+
|
| 179 |
+
# 计算逐个 Token 的 Loss
|
| 180 |
+
loss_fct = nn.CrossEntropyLoss(reduction='none')
|
| 181 |
+
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
|
| 182 |
+
|
| 183 |
+
# 应用 Mask 并求平均
|
| 184 |
+
loss = (loss * loss_mask.view(-1)).sum() / (loss_mask.sum() + 1e-6)
|
| 185 |
+
|
| 186 |
+
# 5. 反向传播累积梯度平方
|
| 187 |
+
loss.backward()
|
| 188 |
+
|
| 189 |
+
for n, p in self.model.named_parameters():
|
| 190 |
+
if p.grad is not None and n in fisher:
|
| 191 |
+
fisher[n] += p.grad.detach() ** 2
|
| 192 |
+
|
| 193 |
+
num_samples += input_ids.size(0)
|
| 194 |
+
|
| 195 |
+
# 平均化
|
| 196 |
+
if num_samples > 0:
|
| 197 |
+
for n in fisher:
|
| 198 |
+
fisher[n] /= num_samples
|
| 199 |
+
|
| 200 |
+
self.model.train()
|
| 201 |
+
return fisher
|
| 202 |
+
|
| 203 |
+
def penalty(self, model: Optional[nn.Module] = None) -> torch.Tensor:
|
| 204 |
+
target_model = model if model is not None else self.model
|
| 205 |
+
|
| 206 |
+
loss = torch.tensor(0.0, device=self.device)
|
| 207 |
+
|
| 208 |
+
for n, p in target_model.named_parameters():
|
| 209 |
+
if n in self.params and p.requires_grad:
|
| 210 |
+
if n in self.fisher:
|
| 211 |
+
loss += (self.fisher[n] * (p - self.params[n]) ** 2).sum()
|
| 212 |
+
|
| 213 |
+
return self.importance * loss
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
class OnlineEWC(EWC):
|
| 217 |
+
def __init__(
|
| 218 |
+
self,
|
| 219 |
+
model: nn.Module,
|
| 220 |
+
preprocessor: UnifiedMultiModalPreprocessor,
|
| 221 |
+
importance: float = 1000.0,
|
| 222 |
+
gamma: float = 0.9
|
| 223 |
+
):
|
| 224 |
+
self.model = model
|
| 225 |
+
self.preprocessor = preprocessor
|
| 226 |
+
self.importance = importance
|
| 227 |
+
self.gamma = gamma
|
| 228 |
+
self.device = next(model.parameters()).device
|
| 229 |
+
|
| 230 |
+
self.params = {}
|
| 231 |
+
self.fisher = {}
|
| 232 |
+
self.task_count = 0
|
| 233 |
+
|
| 234 |
+
def update_fisher(self, dataloader: DataLoader):
|
| 235 |
+
"""更新Fisher信息矩阵"""
|
| 236 |
+
print(f"Updating Online EWC Fisher Matrix (Task {self.task_count + 1})...")
|
| 237 |
+
new_fisher = self._compute_fisher(dataloader)
|
| 238 |
+
|
| 239 |
+
if self.task_count == 0:
|
| 240 |
+
self.fisher = new_fisher
|
| 241 |
+
else:
|
| 242 |
+
for n in self.fisher:
|
| 243 |
+
if n in new_fisher:
|
| 244 |
+
# 移动平均更新 Fisher 信息
|
| 245 |
+
self.fisher[n] = self.gamma * self.fisher[n] + new_fisher[n]
|
| 246 |
+
|
| 247 |
+
# 更新参考参数为当前任务训练后的参数
|
| 248 |
+
self.params = {
|
| 249 |
+
n: p.clone().detach()
|
| 250 |
+
for n, p in self.model.named_parameters()
|
| 251 |
+
if p.requires_grad
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
self.task_count += 1
|
| 255 |
+
print(f"Online EWC regularizer updated.")
|
| 256 |
+
|
| 257 |
+
def penalty(self, model: Optional[nn.Module] = None) -> torch.Tensor:
|
| 258 |
+
"""计算EWC惩罚项"""
|
| 259 |
+
if self.task_count == 0:
|
| 260 |
+
return torch.tensor(0.0, device=self.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
return super().penalty(model)
|