-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnn_influence_utils.py
More file actions
463 lines (397 loc) · 16.5 KB
/
nn_influence_utils.py
File metadata and controls
463 lines (397 loc) · 16.5 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
# Copyright (c) 2020, salesforce.com, inc.
# All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
import torch
import torch.nn as nn
import numpy as np
from tqdm import tqdm
from utils import expected_gradient, expected_grad_grad
from typing import Dict, List, Union, Optional, Tuple, Iterator, Any
def count_parameters(model: torch.nn.Module) -> int:
return sum(p.numel() for p in model.parameters())
def get_loss_with_weight_decay(
device: torch.device,
n_gpu: int,
model: torch.nn.Module,
inputs: Dict[str, torch.Tensor],
weight_decay: Optional[float],
weight_decay_ignores: Optional[List[str]]) -> float:
criterion = nn.CrossEntropyLoss()
input, label, _ = inputs
label = torch.tensor([label]) if type(label) is not torch.Tensor else label
outputs = model(input)
loss = criterion(outputs, label)
if n_gpu > 1:
# mean() to average on multi-gpu parallel training
loss = loss.mean()
# In PyTorch, weight-decay loss and gradients are calculated in
# optimizers rather in nn.Module, so we have to manually specify
# this for the loss here.
if weight_decay is not None:
no_decay = (
weight_decay_ignores
if weight_decay_ignores
is not None else [])
weight_decay_loss = torch.cat([
p.square().view(-1)
for n, p in model.named_parameters()
if not any(nd in n for nd in no_decay)
]).sum() * weight_decay
loss = loss + weight_decay_loss
return loss
def compute_gradients(
device: torch.device,
n_gpu: int,
model: torch.nn.Module,
inputs: Dict[str, torch.Tensor],
params_filter: Optional[List[str]],
weight_decay: Optional[float],
weight_decay_ignores: Optional[List[str]]
) -> List[torch.FloatTensor]:
if params_filter is None:
params_filter = []
model.zero_grad()
loss = get_loss_with_weight_decay(
device=device, n_gpu=n_gpu,
model=model, inputs=inputs,
weight_decay=weight_decay,
weight_decay_ignores=weight_decay_ignores)
return torch.autograd.grad(
outputs=loss,
inputs=[
param for name, param
in model.named_parameters()
if name not in params_filter],
create_graph=True)
def compute_hessian_vector_products(
device: torch.device,
n_gpu: int,
model: torch.nn.Module,
inputs: Dict[str, torch.Tensor],
vectors: torch.FloatTensor,
params_filter: Optional[List[str]],
weight_decay: Optional[float],
weight_decay_ignores: Optional[List[str]]
) -> List[torch.FloatTensor]:
if params_filter is None:
params_filter = []
model.zero_grad()
loss = get_loss_with_weight_decay(
model=model, n_gpu=n_gpu,
device=device, inputs=inputs,
weight_decay=weight_decay,
weight_decay_ignores=weight_decay_ignores)
grad_tuple = torch.autograd.grad(
outputs=loss,
inputs=[
param for name, param
in model.named_parameters()
if name not in params_filter],
create_graph=True)
model.zero_grad()
grad_grad_tuple = torch.autograd.grad(
outputs=grad_tuple,
inputs=[
param for name, param
in model.named_parameters()
if name not in params_filter],
grad_outputs=vectors,
only_inputs=True
)
return grad_grad_tuple
def compute_s_test(
n_gpu: int,
device: torch.device,
model: torch.nn.Module,
test_inputs: Dict[str, torch.Tensor],
train_data_loaders: List[torch.utils.data.DataLoader],
params_filter: Optional[List[str]],
weight_decay: Optional[float],
weight_decay_ignores: Optional[List[str]],
damp: float,
scale: float,
num_samples: Optional[int] = None,
verbose: bool = True,
) -> List[torch.FloatTensor]:
v = compute_gradients(
model=model,
n_gpu=n_gpu,
device=device,
inputs=test_inputs,
params_filter=params_filter,
weight_decay=weight_decay,
weight_decay_ignores=weight_decay_ignores)
# Technically, it's hv^-1
last_estimate = list(v).copy()
cumulative_num_samples = 0
with tqdm(total=num_samples) as pbar:
for data_loader in train_data_loaders:
for i, inputs in enumerate(data_loader):
this_estimate = compute_hessian_vector_products(
model=model,
n_gpu=n_gpu,
device=device,
vectors=last_estimate,
inputs=inputs,
params_filter=params_filter,
weight_decay=weight_decay,
weight_decay_ignores=weight_decay_ignores)
# Recursively caclulate h_estimate
# https://github.com/dedeswim/pytorch_influence_functions/blob/master/pytorch_influence_functions/influence_functions/hvp_grad.py#L118
with torch.no_grad():
new_estimate = [
a + (1 - damp) * b - c / scale
for a, b, c in zip(v, last_estimate, this_estimate)
]
pbar.update(1)
if verbose is True:
new_estimate_norm = new_estimate[0].norm().item()
last_estimate_norm = last_estimate[0].norm().item()
estimate_norm_diff = new_estimate_norm - last_estimate_norm
pbar.set_description(f"{new_estimate_norm:.2f} | {estimate_norm_diff:.2f}")
cumulative_num_samples += 1
last_estimate = new_estimate
if num_samples is not None and i > num_samples:
break
# References:
# https://github.com/kohpangwei/influence-release/blob/master/influence/genericNeuralNet.py#L475
# Do this for each iteration of estimation
# Since we use one estimation, we put this at the end
inverse_hvp = [X / scale for X in last_estimate]
# Sanity check
# Note that in parallel settings, we should have `num_samples`
# whereas in sequential settings we would have `num_samples + 2`.
# This is caused by some loose stop condition. In parallel settings,
# We only allocate `num_samples` data to reduce communication overhead.
# Should probably make this more consistent sometime.
if cumulative_num_samples not in [num_samples, num_samples + 2]:
raise ValueError(f"cumulative_num_samples={cumulative_num_samples} f"
f"but num_samples={num_samples}: Untested Territory")
return inverse_hvp
def compute_grad_zs(
n_gpu: int,
device: torch.device,
model: torch.nn.Module,
data_loader: torch.utils.data.DataLoader,
params_filter: Optional[List[str]] = None,
weight_decay: Optional[float] = None,
weight_decay_ignores: Optional[List[str]] = None,
) -> List[List[torch.FloatTensor]]:
if weight_decay_ignores is None:
weight_decay_ignores = [
"bias",
"LayerNorm.weight"]
grad_zs = []
for inputs in data_loader:
grad_z = compute_gradients(
n_gpu=n_gpu, device=device,
model=model, inputs=inputs,
params_filter=params_filter,
weight_decay=weight_decay,
weight_decay_ignores=weight_decay_ignores)
with torch.no_grad():
grad_zs.append([X.cpu() for X in grad_z])
return grad_zs
def compute_influences(
n_gpu: int,
device: torch.device,
model: torch.nn.Module,
test_inputs: Dict[str, torch.Tensor],
batch_train_data_loader: torch.utils.data.DataLoader,
instance_train_data_loader: torch.utils.data.DataLoader,
params_filter: Optional[List[str]] = None,
weight_decay: Optional[float] = None,
weight_decay_ignores: Optional[List[str]] = None,
s_test_damp: float = 3e-5,
s_test_scale: float = 1e4,
s_test_num_samples: Optional[int] = None,
s_test_iterations: int = 1,
precomputed_s_test: Optional[List[torch.FloatTensor]] = None,
train_indices_to_include: Optional[Union[np.ndarray, List[int]]] = None,
) -> Tuple[Dict[int, float], Dict[int, Dict], List[torch.FloatTensor]]:
if s_test_iterations < 1:
raise ValueError("`s_test_iterations` must >= 1")
if weight_decay_ignores is None:
# https://github.com/huggingface/transformers/blob/v3.0.2/src/transformers/trainer.py#L325
weight_decay_ignores = [
"bias",
"LayerNorm.weight"]
if precomputed_s_test is not None:
s_test = precomputed_s_test
else:
s_test = None
for _ in range(s_test_iterations):
_s_test = compute_s_test(
n_gpu=n_gpu,
device=device,
model=model,
test_inputs=test_inputs,
train_data_loaders=[batch_train_data_loader],
params_filter=params_filter,
weight_decay=weight_decay,
weight_decay_ignores=weight_decay_ignores,
damp=s_test_damp,
scale=s_test_scale,
num_samples=s_test_num_samples)
# Sum the values across runs
if s_test is None:
s_test = _s_test
else:
s_test = [
a + b for a, b in zip(s_test, _s_test)
]
# Do the averaging
s_test = [a / s_test_iterations for a in s_test]
influences = {}
# train_inputs_collections = {}
for train_inputs in tqdm(instance_train_data_loader):
index = train_inputs[2]
# Skip indices when a subset is specified to be included
if (train_indices_to_include is not None) and (
index not in train_indices_to_include):
continue
grad_z = compute_gradients(
n_gpu=n_gpu,
device=device,
model=model,
inputs=train_inputs,
params_filter=params_filter,
weight_decay=weight_decay,
weight_decay_ignores=weight_decay_ignores)
with torch.no_grad():
influence = [
- torch.sum(x * y)
for x, y in zip(grad_z, s_test)]
influences[index] = sum(influence).item()
# train_inputs_collections[index] = train_inputs
return influences#, train_inputs_collections, s_test
def compute_s_test_from_expected_grads(
n_gpu: int,
device: torch.device,
num_ckpts: int,
test_inputs: Dict[str, torch.Tensor],
train_data_loaders: List[torch.utils.data.DataLoader],
params_filter: Optional[List[str]],
weight_decay: Optional[float],
weight_decay_ignores: Optional[List[str]],
damp: float,
scale: float,
num_samples: Optional[int] = None,
verbose: bool = True,
) -> List[torch.FloatTensor]:
v = expected_gradient(num_ckpts=num_ckpts,
input=test_inputs)
# for saving the history of HVP estimation to see if it converges
estimate_hist = []
# Technically, it's hv^-1
last_estimate = list(v).copy()
cumulative_num_samples = 0
with tqdm(total=num_samples) as pbar:
for data_loader in train_data_loaders:
for i, inputs in enumerate(data_loader):
this_estimate = expected_grad_grad(
num_ckpts=num_ckpts,
input=inputs)
# Recursively caclulate h_estimate
# https://github.com/dedeswim/pytorch_influence_functions/blob/master/pytorch_influence_functions/influence_functions/hvp_grad.py#L118
with torch.no_grad():
batch_size = this_estimate.shape[0]
for idx in range(batch_size):
new_estimate = [
a + (1 - damp) * b - c / scale
for a, b, c in zip(v, last_estimate, this_estimate[idx])
]
estimate_hist.append(new_estimate[0].norm().item())
pbar.update(batch_size)
if verbose is True:
new_estimate_norm = new_estimate[0].norm().item()
last_estimate_norm = last_estimate[0].norm().item()
estimate_norm_diff = new_estimate_norm - last_estimate_norm
pbar.set_description(f"{new_estimate_norm:.2f} | {estimate_norm_diff:.2f}")
cumulative_num_samples += batch_size
last_estimate = new_estimate
if num_samples is not None and i > num_samples:
break
# References:
# https://github.com/kohpangwei/influence-release/blob/master/influence/genericNeuralNet.py#L475
# Do this for each iteration of estimation
# Since we use one estimation, we put this at the end
inverse_hvp = [X / scale for X in last_estimate]
# Sanity check
# Note that in parallel settings, we should have `num_samples`
# whereas in sequential settings we would have `num_samples + 2`.
# This is caused by some loose stop condition. In parallel settings,
# We only allocate `num_samples` data to reduce communication overhead.
# Should probably make this more consistent sometime.
if cumulative_num_samples not in [num_samples, num_samples + 2]:
raise ValueError(f"cumulative_num_samples={cumulative_num_samples} f"
f"but num_samples={num_samples}: Untested Territory")
return inverse_hvp, estimate_hist
def compute_influences_from_expected_grads(
n_gpu: int,
device: torch.device,
num_ckpts: int,
test_inputs: Dict[str, torch.Tensor],
batch_train_data_loader: torch.utils.data.DataLoader,
instance_train_data_loader: torch.utils.data.DataLoader,
params_filter: Optional[List[str]] = None,
weight_decay: Optional[float] = None,
weight_decay_ignores: Optional[List[str]] = None,
s_test_damp: float = 3e-5,
s_test_scale: float = 1e4,
s_test_num_samples: Optional[int] = None,
s_test_iterations: int = 1,
precomputed_s_test: Optional[List[torch.FloatTensor]] = None,
train_indices_to_include: Optional[Union[np.ndarray, List[int]]] = None,
) -> Tuple[Dict[int, float], Dict[int, Dict], List[torch.FloatTensor]]:
if s_test_iterations < 1:
raise ValueError("`s_test_iterations` must >= 1")
if weight_decay_ignores is None:
# https://github.com/huggingface/transformers/blob/v3.0.2/src/transformers/trainer.py#L325
weight_decay_ignores = [
"bias",
"LayerNorm.weight"]
if precomputed_s_test is not None:
s_test = precomputed_s_test
else:
s_test = None
for _ in range(s_test_iterations):
_s_test = compute_s_test_from_expected_grads(
n_gpu=n_gpu,
device=device,
num_ckpts=num_ckpts,
test_inputs=test_inputs,
train_data_loaders=[batch_train_data_loader],
params_filter=params_filter,
weight_decay=weight_decay,
weight_decay_ignores=weight_decay_ignores,
damp=s_test_damp,
scale=s_test_scale,
num_samples=s_test_num_samples)
# Sum the values across runs
if s_test is None:
s_test = _s_test
else:
s_test = [
a + b for a, b in zip(s_test, _s_test)
]
# Do the averaging
s_test = [a / s_test_iterations for a in s_test]
influences = {}
# train_inputs_collections = {}
for train_inputs in tqdm(instance_train_data_loader):
index = train_inputs[2].item()
# Skip indices when a subset is specified to be included
if (train_indices_to_include is not None) and (
index not in train_indices_to_include):
continue
grad_z = expected_gradient(num_ckpts=num_ckpts,
input=train_inputs)
with torch.no_grad():
influence = [
- torch.sum(x * y)
for x, y in zip(grad_z, s_test)]
influences[index] = sum(influence).item()
# train_inputs_collections[index] = train_inputs
return influences#, train_inputs_collections, s_test