-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_utils.py
More file actions
468 lines (358 loc) · 17.8 KB
/
analysis_utils.py
File metadata and controls
468 lines (358 loc) · 17.8 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
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
import os
import numpy as np
import torch
import torch.nn.functional as F
from datasets import load_dataset, concatenate_datasets
from tqdm import tqdm
from nnsight import NNsight
from transformers import WhisperForAudioClassification, WhisperFeatureExtractor
from peft import PeftModel
import matplotlib.pyplot as plt
import seaborn as sns
import threading
model_lock = threading.Lock()
processor = WhisperFeatureExtractor.from_pretrained("openai/whisper-large-v2")
id2label = {0: "anger", 1: "happiness", 2: "neutral", 3: "sadness"}
label2id = {v: k for k, v in id2label.items()}
def analyze_norms(nmodel, test_data, processor=processor, num_samples=10):
num_layers = nmodel.config.encoder_layers
att_cos_all = 0
mlp_cos_all = 0
layer_cos_all = 0
mean_relative_contribution_att = 0
mean_relative_contribution_mlp = 0
mean_relative_contribution_layer = 0
total_sequence_length = 0
for i in tqdm(range(len(test_data.select(range(num_samples))))):
sample = test_data[i]
labels = torch.tensor([sample['labels']],device=nmodel.device)
input_features = processor(
sample['audio'],
sampling_rate=16000,
return_tensors="pt"
).input_features.to(nmodel.device)
att_cos = []
mlp_cos = []
layer_cos = []
relative_contribution_att = []
relative_contribution_mlp = []
relative_contribution_layer = []
with torch.no_grad():
with nmodel.trace(input_features):
seq_len = nmodel.encoder.layers[0].output[0].shape[1].save()
total_sequence_length += seq_len
with torch.no_grad():
with nmodel.trace(input_features):
for layer_idx in range(num_layers):
layer_inputs = nmodel.encoder.layers[layer_idx].input
self_attn_output = nmodel.encoder.layers[layer_idx].self_attn.output[0]
layer_outputs = nmodel.encoder.layers[layer_idx].output[0]
attn_contribution = self_attn_output.detach()
mlp_input = (self_attn_output + layer_inputs).detach()
mlp_contribution = (layer_outputs - mlp_input).detach()
residual_norm = layer_inputs.detach().norm(dim=-1).float().clamp(min=1e-6)
relative_contribution_att.append(
(attn_contribution.norm(dim=-1).float() / residual_norm).sum(1).cpu()
)
relative_contribution_mlp.append(
(mlp_contribution.norm(dim=-1).float() / residual_norm).sum(1).cpu()
)
total_layer_contribution = attn_contribution + mlp_contribution
relative_contribution_layer.append(
(total_layer_contribution.norm(dim=-1).float() / residual_norm).sum(1).cpu()
)
att_cos.append(F.cosine_similarity(attn_contribution, layer_inputs.detach(), dim=-1).sum(1).cpu().float())
mlp_cos.append(F.cosine_similarity(mlp_contribution, layer_inputs.detach(), dim=-1).sum(1).cpu().float())
layer_cos.append(F.cosine_similarity(total_layer_contribution, layer_inputs.detach(), dim=-1).sum(1).cpu().float())
mean_relative_contribution_att += torch.cat(relative_contribution_att, dim=0)
mean_relative_contribution_mlp += torch.cat(relative_contribution_mlp, dim=0)
mean_relative_contribution_layer += torch.cat(relative_contribution_layer, dim=0)
att_cos_all += torch.cat(att_cos, dim=0)
mlp_cos_all += torch.cat(mlp_cos, dim=0)
layer_cos_all += torch.cat(layer_cos, dim=0)
att_cos_all = att_cos_all / total_sequence_length
mlp_cos_all = mlp_cos_all / total_sequence_length
layer_cos_all = layer_cos_all / total_sequence_length
mean_relative_contribution_att = mean_relative_contribution_att / total_sequence_length
mean_relative_contribution_mlp = mean_relative_contribution_mlp / total_sequence_length
mean_relative_contribution_layer = mean_relative_contribution_layer / total_sequence_length
return (mean_relative_contribution_att, mean_relative_contribution_mlp, mean_relative_contribution_layer,
att_cos_all, mlp_cos_all, layer_cos_all)
def logit_lens(nmodel, test_data):
device = nmodel.device
res_kl_divs = 0
res_overlaps = 0
cnt = 0
all_layer_logits = []
all_final_logits = []
all_labels = []
for sample in tqdm(test_data):
kl_divs = []
overlaps = []
layer_logits = []
n_layers = nmodel.config.encoder_layers
# Process input features
input_features = processor(
sample['audio'],
sampling_rate=16000,
return_tensors="pt"
).input_features.to(device)
labels = sample['labels']
all_labels.append(labels)
encoder_layers_outputs = []
with torch.no_grad():
with nmodel.trace(input_features):
for l in range(n_layers):
encoder_layers_outputs.append(nmodel.encoder.layers[l].output[0].save())
final_encoder_output = nmodel.encoder.output.last_hidden_state
pooled_output = final_encoder_output.mean(dim=1)
projected = nmodel.projector(pooled_output)
final_logits = nmodel.classifier(projected)
all_final_logits.append(final_logits.save())
for layer_idx, layer_output in enumerate(encoder_layers_outputs):
pooled = layer_output.mean(dim=1)
with torch.no_grad():
projected = nmodel.projector(pooled)
logits = nmodel.classifier(projected)
layer_logits.append(logits)
all_layer_logits.append(layer_logits)
final_probs = F.softmax(final_logits, dim=-1)
for layer_idx, layer_logit in enumerate(layer_logits):
layer_probs = F.softmax(layer_logit, dim=-1)
kl_div = F.kl_div(layer_probs.log(), final_probs, reduction='batchmean')
kl_divs.append(kl_div.item())
layer_pred = layer_logit.argmax(dim=-1)
final_pred = final_logits.argmax(dim=-1)
overlap = (layer_pred == final_pred).float().mean().item()
overlaps.append(overlap)
res_kl_divs += np.array(kl_divs)
res_overlaps += np.array(overlaps)
cnt += 1
avg_kl_divs = res_kl_divs / cnt
avg_overlaps = res_overlaps / cnt
return {
'avg_kl_divs': avg_kl_divs,
'avg_overlaps': avg_overlaps,
'all_layer_logits': all_layer_logits,
'all_final_logits': all_final_logits,
'all_labels': all_labels
}
def residual_erasure(nmodel, test_data):
device = next(nmodel.parameters()).device
n_layers = nmodel.config.encoder_layers
test_samples = test_data.shuffle().select(range(50))
layer_contributions = []
accuracy_drops = []
loss_increases = []
confidence_drops = []
print("Calculating original model performance...")
original_acc, original_loss, original_conf = evaluate_model(nmodel, test_samples)
print(f"Original model - Accuracy: {original_acc:.3f}, Loss: {original_loss:.3f}, Confidence: {original_conf:.3f}")
for layer_idx in tqdm(range(n_layers), desc="Residual Erasure Experiment"):
erased_acc, erased_loss, erased_conf = evaluate_model_with_erasure(
nmodel, test_samples, layer_idx
)
acc_drop = original_acc - erased_acc
loss_increase = erased_loss - original_loss
conf_drop = original_conf - erased_conf
contribution = acc_drop + (conf_drop * 0.5) - (loss_increase * 0.1)
layer_contributions.append(contribution)
accuracy_drops.append(acc_drop)
loss_increases.append(loss_increase)
confidence_drops.append(conf_drop)
print(f"Layer {layer_idx+1} - Accuracy Drop: {acc_drop:.3f}, Loss Increase: {loss_increase:.3f}, Confidence Drop: {conf_drop:.3f}")
print(f"Layer {layer_idx+1} - Contribution Score: {contribution:.3f}")
return {
'layer_contributions': np.array(layer_contributions),
'accuracy_drops': np.array(accuracy_drops),
'loss_increases': np.array(loss_increases),
'confidence_drops': np.array(confidence_drops),
'original_metrics': (original_acc, original_loss, original_conf)
}
def evaluate_model(nmodel, test_samples, batch_size=8):
"""Batch evaluation of model performance"""
device = next(nmodel.parameters()).device
correct = 0
total_loss = 0
total_confidence = 0
total_samples = len(test_samples)
# Process data in batches
for i in tqdm(range(0, total_samples, batch_size), desc="Batch Model Evaluation"):
batch_end = min(i + batch_size, total_samples)
# Correct way: use select method to get batch data
batch_indices = list(range(i, batch_end))
batch_dataset = test_samples.select(batch_indices)
# Process batch audio features
batch_features = []
batch_labels = []
# Iterate through each sample in batch
for idx in range(len(batch_dataset)):
sample = batch_dataset[idx] # Get single sample this way
input_features = processor(
sample['audio'],
sampling_rate=16000,
return_tensors="pt"
).input_features
batch_features.append(input_features.squeeze(0))
batch_labels.append(sample['labels'])
# Stack into batch tensors
batch_features = torch.stack(batch_features).to(device)
batch_labels = torch.tensor(batch_labels).to(device)
with torch.no_grad():
outputs = nmodel(batch_features)
logits = outputs.logits
# Calculate loss
loss = F.cross_entropy(logits, batch_labels, reduction='sum')
total_loss += loss.item()
# Calculate accuracy
pred = logits.argmax(dim=-1)
correct += (pred == batch_labels).sum().item()
# Calculate confidence
probs = F.softmax(logits, dim=-1)
confidence = probs.max(dim=-1)[0].sum().item()
total_confidence += confidence
accuracy = correct / total_samples
avg_loss = total_loss / total_samples
avg_confidence = total_confidence / total_samples
return accuracy, avg_loss, avg_confidence
def evaluate_model_with_erasure(nmodel, test_samples, erase_layer_idx, batch_size=4):
"""Batch evaluation of model performance with layer erasure"""
device = next(nmodel.parameters()).device
correct = 0
total_loss = 0.0
total_confidence = 0
total_samples = len(test_samples)
for i in tqdm(range(0, total_samples, batch_size), desc=f"Evaluating model (erasing layer {erase_layer_idx})"):
batch_end = min(i + batch_size, total_samples)
# Correct way: use select method to get batch data
batch_indices = list(range(i, batch_end))
batch_dataset = test_samples.select(batch_indices)
# Prepare batch data
batch_features = []
batch_labels = []
for idx in range(len(batch_dataset)):
sample = batch_dataset[idx]
input_features = processor(
sample['audio'],
sampling_rate=16000,
return_tensors="pt"
).input_features
batch_features.append(input_features.squeeze(0))
batch_labels.append(sample['labels'])
batch_features = torch.stack(batch_features).to(device)
batch_labels = torch.tensor(batch_labels).to(device)
with torch.no_grad():
with nmodel.trace() as tracer:
with tracer.invoke(batch_features):
# Get input of layer to be erased
layer_input = nmodel.encoder.layers[erase_layer_idx].input.clone()
# Set layer output to its input (skip layer computation)
nmodel.encoder.layers[erase_layer_idx].output = (layer_input, None, None)
# Get final encoder output
final_output = nmodel.encoder.output[0]
pooled_output = final_output.mean(dim=1)
projected = nmodel.projector(pooled_output)
logits = nmodel.classifier(projected).save()
# Calculate metrics
loss = F.cross_entropy(logits, batch_labels, reduction='sum')
total_loss += loss.item()
# Calculate accuracy
pred = logits.argmax(dim=-1)
correct += (pred == batch_labels).sum().item()
# Calculate confidence
probs = F.softmax(logits, dim=-1)
confidence = probs.max(dim=-1)[0].sum().item()
total_confidence += confidence
accuracy = correct / total_samples
avg_loss = total_loss / total_samples
avg_confidence = total_confidence / total_samples
return accuracy, avg_loss, avg_confidence
def progressive_residual_erasure(nmodel, test_data, num_samples=50):
"""
Progressive Residual Erasure Experiment: Cumulatively remove multiple residual connections
Starting from layer 0, gradually increase number of erased layers to observe cumulative effects
"""
device = next(nmodel.parameters()).device
n_layers = nmodel.config.encoder_layers
# Get test samples
test_samples = test_data.shuffle().select(range(min(num_samples, len(test_data))))
# Store cumulative effects
cumulative_results = []
# Get original performance
print("Calculating original model performance...")
original_acc, original_loss, original_conf = evaluate_model(nmodel, test_samples)
cumulative_results.append({
'num_erased_layers': 0,
'accuracy': original_acc,
'loss': original_loss,
'confidence': original_conf,
'acc_drop': 0.0,
'loss_increase': 0.0,
'conf_drop': 0.0
})
print(f"Original model performance - Accuracy: {original_acc:.3f}, Loss: {original_loss:.3f}, Confidence: {original_conf:.3f}")
# Gradually remove more layers
for num_erased in tqdm(range(1, n_layers + 1), desc="Progressive Residual Erasure"):
print(f"\nRemoving first {num_erased} layers' residual connections...")
acc, loss, conf = evaluate_model_with_multiple_erasure(
nmodel, test_samples, list(range(num_erased))
)
acc_drop = original_acc - acc
loss_increase = loss - original_loss
conf_drop = original_conf - conf
cumulative_results.append({
'num_erased_layers': num_erased,
'accuracy': acc,
'loss': loss,
'confidence': conf,
'acc_drop': acc_drop,
'loss_increase': loss_increase,
'conf_drop': conf_drop
})
print(f"After removing {num_erased} layers - Accuracy: {acc:.3f}, Loss: {loss:.3f}, Confidence: {conf:.3f}")
print(f"Performance drops - Accuracy: {acc_drop:.3f}, Loss increase: {loss_increase:.3f}, Confidence drop: {conf_drop:.3f}")
return cumulative_results
def evaluate_model_with_multiple_erasure(nmodel, test_samples, erase_layer_indices, batch_size=4):
device = next(nmodel.parameters()).device
correct = 0
total_loss = 0
total_confidence = 0
total_samples = len(test_samples)
for i in tqdm(range(0, total_samples, batch_size), desc=f"eval_model(erase_layer_indices={erase_layer_indices})"):
batch_end = min(i + batch_size, total_samples)
batch_indices = list(range(i, batch_end))
batch_dataset = test_samples.select(batch_indices)
batch_features = []
batch_labels = []
for idx in range(len(batch_dataset)):
sample = batch_dataset[idx]
input_features = processor(
sample['audio'],
sampling_rate=16000,
return_tensors="pt"
).input_features
batch_features.append(input_features.squeeze(0))
batch_labels.append(sample['labels'])
batch_features = torch.stack(batch_features).to(device)
batch_labels = torch.tensor(batch_labels).to(device)
with torch.no_grad():
with nmodel.trace() as tracer:
with tracer.invoke(batch_features):
for layer_idx in erase_layer_indices:
layer_input = nmodel.encoder.layers[layer_idx].input.clone()
nmodel.encoder.layers[layer_idx].output = (layer_input, None, None)
final_output = nmodel.encoder.output[0]
pooled_output = final_output.mean(dim=1)
projected = nmodel.projector(pooled_output)
logits = nmodel.classifier(projected).save()
loss = F.cross_entropy(logits, batch_labels, reduction='sum')
total_loss += loss.item()
pred = logits.argmax(dim=-1)
correct += (pred == batch_labels).sum().item()
probs = F.softmax(logits, dim=-1)
confidence = probs.max(dim=-1)[0].sum().item()
total_confidence += confidence
accuracy = correct / total_samples
avg_loss = total_loss / total_samples
avg_confidence = total_confidence / total_samples
return accuracy, avg_loss, avg_confidence