-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgep_utils.py
221 lines (179 loc) · 8.64 KB
/
gep_utils.py
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
import gc
from typing import Optional, Tuple
import numpy as np
import torch
from numpy.core.defchararray import translate
# from sklearn.decomposition import PCA
from torch import Tensor
# def flatten_tensor(tensor_list) -> torch.Tensor:
#
# # for i in range(len(tensor_list)):
# # tensor_list[i] = tensor_list[i].reshape([tensor_list[i].shape[0], -1])
# # # tensor_list[i] = tensor_list[i].reshape(1, -1)
# flatten_param = torch.stack(tensor_list)
# flatten_param = flatten_param.reshape(flatten_param.shape[0], -1)
# return flatten_param
def flatten_tensor(tensor_list) -> torch.Tensor:
"""
Taken from https://github.com/dayu11/Gradient-Embedding-Perturbation
"""
for i in range(len(tensor_list)):
tensor_list[i] = tensor_list[i].reshape([tensor_list[i].shape[0], -1])
# tensor_list[i] = tensor_list[i].reshape(1, -1)
flatten_param = torch.cat(tensor_list, dim=1)
del tensor_list
return flatten_param
@torch.no_grad()
def check_approx_error(L, target) -> float:
L = L.to(target.device)
encode = torch.matmul(target, L) # n x k
decode = torch.matmul(encode, L.T)
error = torch.sum(torch.square(target - decode))
target = torch.sum(torch.square(target))
return -1.0 if target.item() == 0 else error.item() / target.item()
# GEP UTILS numpy variants
# *************************
# def get_bases(pub_grad, num_bases):
# num_k = pub_grad.shape[0]
# num_p = pub_grad.shape[1]
#
# num_bases = min(num_bases, min(num_p, num_k))
#
# pca = PCA(n_components=num_bases)
# pca.fit(pub_grad.cpu().detach().numpy())
#
# # error_rate = check_approx_error(torch.from_numpy(pca.components_).T, pub_grad)
#
# return num_bases, pca
# # return num_bases, error_rate, pca
#
#
# def compute_subspace(basis_gradients: torch.Tensor, num_basis_elements: int) -> PCA:
# num_bases: int
# pub_error: float
# pca: PCA
# num_bases, pca = get_bases(basis_gradients, num_basis_elements)
# # num_bases, pub_error, pca = get_bases(basis_gradients, num_basis_elements)
# return pca
#
#
# def embed_grad(grad: torch.Tensor, pca: PCA) -> torch.Tensor:
# grad_np: np.ndarray = grad.cpu().detach().numpy()
# embedding: np.ndarray = pca.transform(grad_np)
# return torch.from_numpy(embedding)
#
#
# def project_back_embedding(embedding: torch.Tensor, pca: PCA, device: torch.device) -> torch.Tensor:
# embedding_np: np.ndarray = embedding.cpu().detach().numpy()
# grad_np: np.ndarray = pca.inverse_transform(embedding_np)
# return torch.from_numpy(grad_np).to(device)
# End of GEP UTILS numpy variants
# *************************
# GEP UTILS torch variants
# *************************
@torch.no_grad()
def get_bases(pub_grad, num_bases):
num_samples = pub_grad.shape[0]
num_features = pub_grad.shape[1]
# print(f'num samples: {num_samples} num features: {num_features} num bases: {num_bases}')
num_bases = min(num_bases, min(num_samples, num_features))
# print(f'num bases: {num_bases} to compute')
mean = torch.mean(pub_grad, dim=0, keepdim=True)
std = torch.std(pub_grad, dim=0, keepdim=True)
mx,_ = torch.max(pub_grad, dim=0, keepdim=True)
mn,_ = torch.min(pub_grad, dim=0, keepdim=True)
# translate_transform = mn
# translate_transform = float(mn.mean())
translate_transform = pub_grad.mean(dim=(-2,), keepdim=True)
# scale_transform = torch.max(torch.tensor(.0001), mx - mn)
scale_transform = max(0.1, float(mx.mean()) - float(mn.mean()))
X = (pub_grad - translate_transform) / scale_transform
# U, S, V = torch.pca_lowrank(X, q=num_bases, niter=2, center=True)
U, S, Vh = torch.linalg.svd(X, full_matrices=False)
# print(f'translate_transform {translate_transform}\nscale_transform {scale_transform}')
# for nb in [n for n in [10, 20, 50, 100, 500, 750, 1000] if n <= num_bases]:
# for nb in [n for n in [5, 10, 15, 18, 19, 20]]:
# err = torch.dist(X, U[:, :nb] @ torch.diag(S[:nb]) @ Vh[:nb, :])
# print(f'Reconstruction Error for num bases {nb}: {err}')
if torch.any(torch.isnan(Vh)):
raise Exception(
f'NaNs in V: {torch.sum(torch.any(torch.isnan(Vh)))} NaNs')
explained_variance_ = ((S ** 2) / (num_samples - 1)).squeeze()
total_var = torch.sum(explained_variance_)
explained_variance_ratio_ = explained_variance_ / total_var
# for obj in [U,S,Vh,explained_variance_, explained_variance_ratio_]:
# obj.detach().cpu()
explained_variance_ratio_cumsum = torch.cumsum(explained_variance_ratio_, dim=0)
num_components_explained_variance_ratio_dict = {}
for th in [0.5, 0.75, 0.9, 0.95, 0.99]:
over_th = torch.argwhere(explained_variance_ratio_cumsum > th)
over_th_idx = over_th[0] if len(over_th) > 0 else len(explained_variance_ratio_cumsum) - 1
num_components_explained_variance_ratio_dict[th] = int(over_th_idx)
# pca = torch.linalg.qr(pub_grad.t())
# print(f'Q shape {pca[0].shape}')
# print(f'R shape {pca[1].shape}')
S=S.cpu()
explained_variance_=explained_variance_.cpu()
explained_variance_ratio_=explained_variance_ratio_.cpu()
explained_variance_ratio_cumsum=explained_variance_ratio_cumsum.cpu()
del S,explained_variance_,explained_variance_ratio_, explained_variance_ratio_cumsum
gc.collect()
torch.cuda.empty_cache()
# The principal directions are the transpose of Vh
V = Vh.t()[:,:num_bases]
return V, translate_transform, scale_transform, num_components_explained_variance_ratio_dict
@torch.no_grad()
def embed_grad(grad: torch.Tensor, pca, device=torch.device('cuda')) -> torch.Tensor:
# embedding: torch.Tensor = torch.matmul(grad, pca)
# return embedding
V, translate_transform, scale_transform, _ = pca
# V, grad = V.to(device), grad.to(device)
with torch.amp.autocast('cuda', enabled=False):
grad = (grad - translate_transform) / scale_transform
embedding: torch.Tensor = torch.matmul(grad, V)
if torch.any(torch.isnan(embedding)):
raise Exception(
f'NaNs in embedding: {torch.sum(torch.any(torch.isnan(embedding)))} NaNs')
# V, grad, embedding = V.detach().cpu(), grad.detach().cpu(), embedding.detach().cpu()
return embedding
@torch.no_grad()
def project_back_embedding(embedding: torch.Tensor, pca, device) -> torch.Tensor:
# reconstructed = torch.matmul(embedding, pca.t())
# return reconstructed
V, translate_transform, scale_transform, _ = pca
V, embedding = V.to(device), embedding.to(device)
if torch.any(torch.isnan(embedding)):
raise Exception(
f'NaNs in embedding: {torch.sum(torch.any(torch.isnan(embedding)))} NaNs')
with torch.amp.autocast('cuda', enabled=False):
reconstructed = torch.matmul(embedding, V.t())
if torch.any(torch.isnan(reconstructed)):
raise Exception(
f'NaNs in reconstructed: {torch.sum(torch.any(torch.isnan(reconstructed)))} NaNs')
reconstructed = (reconstructed * scale_transform) + translate_transform
# V, embedding, reconstructed = V.detach().cpu(), embedding.detach().cpu(), reconstructed.detach().cpu()
return reconstructed
@torch.no_grad()
def compute_subspace(basis_gradients: torch.Tensor, num_basis_elements: int, device=torch.device('cuda')):
pca = get_bases(basis_gradients, num_basis_elements)
return pca
# End of GEP UTILS torch variants
# *************************
@torch.no_grad()
def add_new_gradients_to_history(new_gradients: torch.Tensor,
basis_gradients: Optional[torch.Tensor],
basis_gradients_cpu: Optional[torch.Tensor],
gradients_history_size: int) -> Tuple[Tensor, Tensor, int]:
# print(f'\n\t\t\t\t\t\t\t\t1 - basis gradients shape {basis_gradients.shape if basis_gradients is not None else None}')
basis_gradients_cpu = torch.cat((basis_gradients_cpu, new_gradients), dim=0) \
if basis_gradients_cpu is not None \
else new_gradients
# print(f'\n\t\t\t\t\t\t\t\t2 - basis gradients shape {basis_gradients.shape}')
basis_gradients_cpu = basis_gradients_cpu[-gradients_history_size:] \
if gradients_history_size < basis_gradients_cpu.shape[0] \
else basis_gradients_cpu
# print(f'\n\t\t\t\t\t\t\t\t3 - basis gradients shape {basis_gradients.shape}')
# basis_gradients = basis_gradients_cpu.to('cuda', non_blocking=True)
basis_gradients = basis_gradients_cpu.to('cuda', non_blocking=True) if new_gradients.device == torch.device('cuda') else basis_gradients_cpu
filled_history_size = basis_gradients_cpu.shape[0]
return basis_gradients, basis_gradients_cpu, filled_history_size