-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining.py
More file actions
282 lines (245 loc) · 12.3 KB
/
training.py
File metadata and controls
282 lines (245 loc) · 12.3 KB
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
import time
from shared_types import Modality
import torch
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
from attack import Attack
from perf.profiling import GpuMemoryTracker, ProfileModelMemory
from perf.debug import log_grad
from model import MODALITY_MAP, Model, ForwardMode
from meter import AverageMeter
from transform import unnormalize_inplace, normalize_inplace
from loss import l2_loss, ce_loss
import torch.distributed as dist
from utils.utils import gen_label, loss_fun, all_gather_batch_with_grad, all_gather_batch
from imagebind.imagebind_model import ModalityType
def train_alignment_epoch(
logger,
device,
model,
data_loader,
optimizer,
scheduler,
epoch: int,
total_epochs: int,
writer: SummaryWriter,
modality: Modality,
align_temperature: float = 1.0,
align_symmetric: bool = False,
align_all_gather: bool = False,
align_label_smoothing: float = 0.0,
align_mask_same_label: bool = False,
):
epoch_start_time = time.time()
step_base = epoch * len(data_loader)
total_samples = 0
model.train()
for batch_idx, batch in enumerate(data_loader):
batch_start_time = time.time()
step_total = step_base + batch_idx + 1
inputs_tensor = batch['inputs']
descriptions = batch['descriptions']
_ = batch['labels']
# to device
inputs_tensor = inputs_tensor.to(device, non_blocking=True)
descriptions = descriptions.to(device, non_blocking=True)
labels_tensor = batch.get('labels', None)
if labels_tensor is not None:
labels_tensor = labels_tensor.to(device, non_blocking=True)
merged_inputs = {
MODALITY_MAP[modality]: inputs_tensor,
MODALITY_MAP[Modality.TEXT]: descriptions
}
with GpuMemoryTracker(logger):
text_embeddings, modality_embeddings = model(merged_inputs)
# Optionally gather negatives across GPUs for stronger contrast
if align_all_gather and dist.is_initialized() and dist.get_world_size() > 1:
text_embeddings, modality_embeddings = all_gather_batch_with_grad([
text_embeddings, modality_embeddings
])
# Keep labels in sync with embeddings if masking is enabled
if align_mask_same_label and labels_tensor is not None:
(labels_tensor,) = all_gather_batch([labels_tensor])
with GpuMemoryTracker(logger):
logits = modality_embeddings @ text_embeddings.t()
# Temperature scaling (divide by tau)
if align_temperature is not None and align_temperature > 0:
logits = logits / align_temperature
labels = gen_label(logits, device)
# Optionally mask negatives that share the same class label
if align_mask_same_label and labels_tensor is not None:
B = logits.size(0)
diag = torch.eye(B, dtype=torch.bool, device=device)
same = labels_tensor.unsqueeze(1) == labels_tensor.unsqueeze(0)
neg_same = same & (~diag)
logits = logits.masked_fill(neg_same, -1e9)
if align_symmetric:
# Text->modality direction
logits_t2m = text_embeddings @ modality_embeddings.t()
if align_temperature is not None and align_temperature > 0:
logits_t2m = logits_t2m / align_temperature
# Use label smoothing if provided; else default loss_fun
if align_label_smoothing and align_label_smoothing > 0.0:
loss_m2t = F.cross_entropy(logits, labels, reduction='mean', label_smoothing=align_label_smoothing)
loss_t2m = F.cross_entropy(logits_t2m, labels, reduction='mean', label_smoothing=align_label_smoothing)
else:
loss_m2t = loss_fun(logits, labels)
loss_t2m = loss_fun(logits_t2m, labels)
loss_val = 0.5 * (loss_m2t + loss_t2m)
else:
if align_label_smoothing and align_label_smoothing > 0.0:
loss_val = F.cross_entropy(logits, labels, reduction='mean', label_smoothing=align_label_smoothing)
else:
loss_val = loss_fun(logits, labels)
optimizer.zero_grad(set_to_none=True)
loss_val.backward()
optimizer.step()
scheduler.step()
n_samples = inputs_tensor.size(0)
total_samples += n_samples
lr = optimizer.param_groups[0]['lr']
logger.info(
f"[ALIGN] Epoch={epoch+1}/{total_epochs}, Step={batch_idx+1}/{len(data_loader)}, "
f"LR={lr:.6f}, Loss={loss_val.item():.6f}, tau={align_temperature:.4f}, sym={align_symmetric}, gather={align_all_gather}, ls={align_label_smoothing}, mask_same={align_mask_same_label}"
)
if dist.get_rank() == 0 and writer is not None:
writer.add_scalar("alignment/loss", loss_val.item(), step_total)
writer.add_scalar("alignment/lr", lr, step_total)
del inputs_tensor, descriptions, text_embeddings, modality_embeddings, logits, labels, loss_val
torch.cuda.empty_cache()
batch_end_time = time.time()
logger.info(f"[ALIGN] Batch {batch_idx+1} time: {batch_end_time - batch_start_time:.2f} seconds")
sample_tensor = torch.tensor(total_samples, dtype=torch.float64, device=device)
dist.all_reduce(sample_tensor, op=dist.ReduceOp.SUM)
epoch_end_time = time.time()
logger.info(f"[ALIGN] Epoch {epoch+1}/{total_epochs} time: {epoch_end_time - epoch_start_time:.2f} seconds")
logger.info(f"[ALIGN] Total samples processed (all ranks): {sample_tensor.item()}")
def train_robust_epoch(
logger,
device,
model_train: Model, # UniBindClassifier
model_original: Model, # UniBindClassifier
mean,
std,
data_loader,
optimizer,
scheduler,
attack: Attack,
train_loss_type,
epoch: int,
total_epochs: int,
loss_meter: AverageMeter,
cos_sim_meter: AverageMeter,
rcos_sim_meter: AverageMeter,
acc_meter: AverageMeter,
racc_meter: AverageMeter,
writer: SummaryWriter,
):
epoch_start_time = time.time()
step_base = epoch * len(data_loader)
total_samples = 0
for batch_idx, batch in enumerate(data_loader):
inputs_tensor = batch['inputs']
lbl = batch['labels']
batch_size = inputs_tensor.size(0)
total_samples += batch_size
batch_start_time = time.time()
step_total = step_base + batch_idx + 1
logger.info(f"[ROBUST] Epoch {epoch+1}/{total_epochs}, batch {batch_idx+1}/{len(data_loader)}, batch size={batch_size}")
inputs_tensor = inputs_tensor.to(device, non_blocking=True)
lbl = lbl.to(device, non_blocking=True)
model_train.eval()
# Unnormalized copy for attack
inp_unorm = inputs_tensor.clone().detach()
unnormalize_inplace(inp_unorm, mean, std)
emb_orig = None
if train_loss_type == 'l2':
with torch.no_grad():
emb_orig = model_original(inputs_tensor, mode=ForwardMode.EMBEDDINGS)
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
adv_inp = attack.perturb(inp_unorm, lbl, emb_orig)
normalize_inplace(adv_inp, mean, std)
# ===== Initial metrics (before update) =====
if train_loss_type == 'l2':
with torch.no_grad():
clean_emb = model_train(inputs_tensor, mode=ForwardMode.EMBEDDINGS)
robust_emb = model_train(adv_inp, mode=ForwardMode.EMBEDDINGS)
clean_cos = F.cosine_similarity(clean_emb, emb_orig, dim=1).mean()
robust_cos = F.cosine_similarity(robust_emb, emb_orig, dim=1).mean()
logger.info(f"[ROBUST][Init] Step={step_total}, CleanCos={clean_cos.item():.4f}, RobustCos={robust_cos.item():.4f}")
elif train_loss_type == 'ce':
with torch.no_grad():
logits_clean, _ = model_train(inputs_tensor, mode=ForwardMode.LOGITS)
logits_robust, _ = model_train(adv_inp, mode=ForwardMode.LOGITS)
clean_acc = compute_acc(logits_clean, lbl)
robust_acc = compute_acc(logits_robust, lbl)
logger.info(f"[ROBUST][Init] Step={step_total}, CleanAcc={clean_acc:.2f}, RobustAcc={robust_acc:.2f}")
# ===== Training step =====
model_train.train()
optimizer.zero_grad()
adv_inp.requires_grad = True
if train_loss_type == 'l2':
emb_adv = model_train(adv_inp, mode=ForwardMode.EMBEDDINGS)
loss_val = l2_loss(emb_adv, emb_orig)
elif train_loss_type == 'ce':
logits_adv, _ = model_train(adv_inp, mode=ForwardMode.LOGITS)
loss_val = ce_loss(logits_adv, lbl)
else:
raise ValueError(f"Unknown loss type: {train_loss_type}")
loss_val.backward()
optimizer.step()
scheduler.step()
n_samples = batch_size
loss_meter.update(loss_val.item(), n_samples)
# ===== Final metrics (after update) =====
model_train.eval()
lr = optimizer.param_groups[0]['lr']
with torch.no_grad():
if train_loss_type == 'l2':
final_clean_emb = model_train(inputs_tensor, mode=ForwardMode.EMBEDDINGS)
final_robust_emb = model_train(adv_inp, mode=ForwardMode.EMBEDDINGS)
clean_cos = F.cosine_similarity(final_clean_emb, emb_orig, dim=1).mean()
robust_cos = F.cosine_similarity(final_robust_emb, emb_orig, dim=1).mean()
cos_sim_meter.update(clean_cos.item(), n_samples)
rcos_sim_meter.update(robust_cos.item(), n_samples)
logger.info(
f"[ROBUST][Final] Step={step_total}, LR={lr:.6f}, Loss={loss_val.item():.6f}, "
f"CleanCos={clean_cos.item():.4f}, RobustCos={robust_cos.item():.4f}, "
f"AvgCleanCos={cos_sim_meter.avg:.4f}, AvgRobustCos={rcos_sim_meter.avg:.4f}"
)
if dist.get_rank() == 0 and writer is not None:
writer.add_scalar("train/loss", loss_val.item(), step_total)
writer.add_scalar("train/clean_cos", clean_cos.item(), step_total)
writer.add_scalar("train/robust_cos", robust_cos.item(), step_total)
writer.add_scalar("train/lr", lr, step_total)
elif train_loss_type == 'ce':
final_logits_clean, _ = model_train(inputs_tensor, mode=ForwardMode.LOGITS)
final_logits_robust, _ = model_train(adv_inp, mode=ForwardMode.LOGITS)
clean_acc = compute_acc(final_logits_clean, lbl)
robust_acc = compute_acc(final_logits_robust, lbl)
acc_meter.update(clean_acc, n_samples)
racc_meter.update(robust_acc, n_samples)
logger.info(
f"[ROBUST][Final] Step={step_total}, LR={lr:.6f}, Loss={loss_val.item():.6f}, "
f"CleanAcc={clean_acc:.2f}, RobustAcc={robust_acc:.2f}, "
f"AvgCleanAcc={acc_meter.avg:.2f}, AvgRobustAcc={racc_meter.avg:.2f}"
)
if dist.get_rank() == 0 and writer is not None:
writer.add_scalar("train/loss", loss_val.item(), step_total)
writer.add_scalar("train/clean_acc", clean_acc, step_total)
writer.add_scalar("train/robust_acc", robust_acc, step_total)
writer.add_scalar("train/lr", lr, step_total)
del inputs_tensor, lbl, inp_unorm, adv_inp, loss_val
torch.cuda.empty_cache()
batch_end_time = time.time()
logger.info(f"[ROBUST] Batch {batch_idx+1} time: {batch_end_time - batch_start_time:.2f} seconds")
sample_tensor = torch.tensor(total_samples, dtype=torch.float64, device=device)
dist.all_reduce(sample_tensor, op=dist.ReduceOp.SUM)
epoch_end_time = time.time()
logger.info(f"[ROBUST] Epoch {epoch+1}/{total_epochs} time: {epoch_end_time - epoch_start_time:.2f} seconds")
logger.info(f"[ROBUST] Total samples processed: {sample_tensor.item()}")
def predict(logits):
return logits.argmax(dim=1)
def compute_acc(logits, targets):
correct = (predict(logits) == targets).float().sum().item()
return 100.0 * correct / targets.size(0)