-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.py
289 lines (222 loc) · 9.98 KB
/
analyze.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
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
from argparse import ArgumentParser
from os.path import exists, isfile
import torch as pt
from pandas import DataFrame
from tqdm import tqdm
from lib.collapse import Statistics
from lib.model import get_classifier_weights, get_model_stats, split_parts
from lib.statistics import (collect_hist, commit, create_df, triu_mean,
triu_std, update_df)
from lib.utils import identify, is_float, log_kernel, riesz_kernel
pt.set_grad_enabled(False)
LINE_SEP = "-" * 79
COL_WIDTH = 6
parser = ArgumentParser()
parser.add_argument("-dev", "--device", type=str, default="cpu")
parser.add_argument("-f", "--force", action="store_true")
parser.add_argument("-1", "--single", action="store_true")
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument(
"-total",
"--totals",
type=int,
nargs=3,
default=(469514249, 229367, 29233), # TinyStories train set, 2 workers
)
parser.add_argument("-i", "--input_files", type=str, nargs="+", default=[])
parser.add_argument("-o", "--output_file", type=str, default="analysis")
parser.add_argument("-mc", "--model_cache", type=str, default=".")
parser.add_argument("-prog", "--progress", action="store_true")
parser.add_argument("-loss", "--model_stats", action="store_true")
parser.add_argument("-snr", "-cdnv", "--inv_snr", action="store_true") # NC1 (Galanti)
parser.add_argument("-nor", "--norms", action="store_true") # (G)NC2
parser.add_argument("-etf", "--interfere", action="store_true") # NC2
parser.add_argument("-kern", "--kernel", type=str, default=None) # GNC2
parser.add_argument("-dual", "--duality", action="store_true") # NC3
parser.add_argument("-decs", "--decisions", action="store_true") # NC4
parser.add_argument("-each", "--each_model", action="store_true")
parser.add_argument("-hist", "--histograms", action="store_true")
parser.add_argument("-freq", "--frequency", action="store_true")
parser.add_argument("-mpc", "--min_per_class", type=int, default=1)
parser.add_argument("-Mpc", "--max_per_class", type=int, default=None)
parser.add_argument("-ps", "--patch_size", type=int, default=1024)
parser.add_argument("-nb", "--num_bins", type=int, default=1024)
parser.add_argument("-d", "--dims", type=int, nargs=2, default=None)
args = parser.parse_args()
if "cuda" in args.device and not pt.cuda.is_available():
print(f"W: CUDA device {args.device} unavailable; defaulting to CPU")
args.device = "cpu"
ANALYSIS = args.model_stats
ANALYSIS |= args.inv_snr | args.duality | args.decisions # NC1,3,4
ANALYSIS |= args.norms | args.interfere | (args.kernel is not None) # (G)NC2
PATHS = {}
for file in sorted(args.input_files, key=lambda x: x.split("/")[-1]):
if not exists(file) or not isfile(file):
continue
iden = identify(file)
paths = [None, None, None] if iden not in PATHS else PATHS[iden]
if "means" in file:
paths[0] = file
elif "vars" in file:
paths[1] = file
elif "decs" in file:
paths[2] = file
else:
continue
PATHS[iden] = paths
if args.single and None not in PATHS[iden]:
PATHS = {iden: PATHS[iden]}
break
def get_stats(iden):
stats = Statistics(
device=args.device,
load_means=PATHS[iden][0],
load_vars=PATHS[iden][1],
load_decs=PATHS[iden][2],
verbose=False,
)
return stats
if args.progress:
PROGRESS = {}
INCOMPLETE = []
for iden in tqdm(PATHS):
collected: Statistics = get_stats(iden)
Ns = (collected.N1, collected.N2, collected.N3)
Ns_seqs = (collected.N1_seqs, collected.N2_seqs, collected.N3_seqs)
if not (Ns[0] == Ns[1] == args.totals[0]):
INCOMPLETE.append(iden)
if args.progress:
N_unique = collected.counts_in_range(args.min_per_class).shape[0]
PROGRESS[iden] = (*Ns_seqs, N_unique)
COL_WIDTH = max(COL_WIDTH, max(len(str(n)) for n in PROGRESS[iden]))
del collected
IDENTIFIERS = sorted(PATHS.keys(), key=lambda x: split_parts(x)[1]) # sort by dim
LAST_INDEX = f"total ({len(IDENTIFIERS)})"
LONGEST_IDEN = max(len(LAST_INDEX), max([len(iden) for iden in IDENTIFIERS]))
if args.progress:
print(LINE_SEP)
head = [p.rjust(COL_WIDTH) for p in ["means", "vars", "decs", "unique"]]
print(f"model".ljust(LONGEST_IDEN + 1), *head)
for iden in IDENTIFIERS:
Ns = PROGRESS[iden]
row = [str(n).rjust(COL_WIDTH) for n in Ns]
print(iden.ljust(LONGEST_IDEN + 1), *row)
row = [args.totals[1]] * 3 + [args.totals[-1]]
row = [str(n).rjust(COL_WIDTH) for n in row]
print(LAST_INDEX.ljust(LONGEST_IDEN + 1), *row)
print(LINE_SEP)
if not ANALYSIS:
exit()
for iden in INCOMPLETE:
del PATHS[iden]
IDENTIFIERS.remove(iden)
if args.single: # run the first one for debugging purposes
IDENTIFIERS = IDENTIFIERS[0:1]
df: DataFrame = create_df(args.output_file)
missing = lambda k, i: not (k in df and i in df.index and is_float(df[k][i]))
if args.force:
missing = lambda k, i: True
def triu_stats_histogram(data: pt.Tensor, key: str):
mean = triu_mean(data)
std = triu_std(data, mean)
update_df(df, f"{key}_mean", mean, iden)
update_df(df, f"{key}_std", std, iden)
if args.histograms:
bins, edges = collect_hist(data, args.num_bins, True)
commit(args.output_file, f"{key}_bins", bins, iden)
commit(args.output_file, f"{key}_edges", edges, iden)
del bins, edges
for iden in tqdm(IDENTIFIERS):
if iden not in PATHS:
print("SKIPPING", iden)
continue
if args.verbose:
print("ANALYZE", iden)
collected: Statistics = get_stats(iden)
indices = collected.counts_in_range(args.min_per_class, args.max_per_class)
counts = collected.counts[indices]
if not exists(f"{args.output_file}.h5"):
commit(f"{args.output_file}", "counts", counts)
if args.model_stats:
train_stats = get_model_stats(f"TinyStories-{iden}", args)
for stat_key in train_stats.keys():
update_df(df, stat_key, train_stats[stat_key], iden)
if args.inv_snr and missing("cdnv_std", iden): # NC1
CDNVs = collected.compute_vars(indices, args.patch_size)
if CDNVs is not None and collected.N2 == args.totals[0]:
mean = triu_mean(CDNVs)
std = triu_std(CDNVs, mean)
update_df(df, "cdnv_mean", mean, iden)
update_df(df, "cdnv_std", std, iden)
if args.histograms:
pt.log_(CDNVs)
bins, edges = collect_hist(CDNVs, args.num_bins, True)
commit(args.output_file, "cdnv_bins", bins, iden)
commit(args.output_file, "cdnv_edges", edges, iden)
del bins, edges
del CDNVs
if args.norms and missing("norms_logscl_std", iden): # NC2 equinorm
norms = collected.mean_norms(indices, False, False)
update_df(df, "norms_mean", norms.mean(), iden)
update_df(df, "norms_std", norms.std(), iden)
norms_scaled = collected.mean_norms(indices, False, True)
update_df(df, "norms_scl_mean", norms_scaled.mean(), iden)
update_df(df, "norms_scl_std", norms_scaled.std(), iden)
norms_logged = collected.mean_norms(indices, True, False)
update_df(df, "norms_log_mean", norms_logged.mean(), iden)
update_df(df, "norms_log_std", norms_logged.std(), iden)
norms_logscaled = collected.mean_norms(indices, True, True)
update_df(df, "norms_logscl_mean", norms_logscaled.mean(), iden)
update_df(df, "norms_logscl_std", norms_logscaled.std(), iden)
if args.frequency:
commit(args.output_file, "norms", norms, iden)
if args.histograms:
bins, edges = collect_hist(norms.unsqueeze(0), args.num_bins)
commit(args.output_file, "norms_bins", bins, iden)
commit(args.output_file, "norms_edges", edges, iden)
del bins, edges
del norms
if args.interfere and missing("interfere_std", iden): # NC2 simplex ETF
interfere = collected.interference(indices, args.patch_size)
triu_stats_histogram(interfere, "interfere")
del interfere
if args.kernel and missing(f"{args.kernel}_dist_std", iden): # GNC2
kernel = riesz_kernel if "riesz" in args.kernel else log_kernel
distances = collected.kernel_distances(indices, kernel, args.patch_size)
triu_stats_histogram(distances, f"{args.kernel}_dist")
del distances
if args.duality and missing("sims_std", iden): # NC3 duality
W = get_classifier_weights(f"TinyStories-{iden}", args)
if W is None:
print("W: failed to load weights.")
else:
dists = collected.dual_dists(W, indices)
update_df(df, "dists_mean", dists.mean(), iden)
update_df(df, "dists_std", dists.std(), iden)
if args.histograms:
bins, edges = collect_hist(dists, args.num_bins)
commit(args.output_file, "dists_bins", bins, iden)
commit(args.output_file, "dists_edges", edges, iden)
del bins, edges
del dists
sims = collected.similarity(W, indices)
update_df(df, "sims_mean", sims.mean(), iden)
update_df(df, "sims_std", sims.std(), iden)
if args.histograms:
bins, edges = collect_hist(sims, args.num_bins)
commit(args.output_file, "sims_bins", bins, iden)
commit(args.output_file, "sims_edges", edges, iden)
del bins, edges
del sims
del W
if args.decisions and missing("matches_std", iden): # NC4 agreement
matches, misses = collected.matches[indices], collected.misses[indices]
update_df(df, "misses", int(misses.sum()), iden)
update_df(df, "matches", int(matches.sum()), iden)
matches = matches.to(pt.float32)
update_df(df, "matches_mean", matches.mean(), iden)
update_df(df, "matches_std", matches.std(), iden)
df.to_csv(f"{args.output_file}.csv")
del collected
df.to_csv(f"{args.output_file}.csv")
print(LINE_SEP)