-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcircuit.py
More file actions
626 lines (552 loc) · 24.3 KB
/
circuit.py
File metadata and controls
626 lines (552 loc) · 24.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import argparse
import gc
import json
import math
import os
from collections import defaultdict
import torch as t
from einops import rearrange
from tqdm import tqdm
from activation_utils import SparseAct
from attribution import patching_effect, jvp
from circuit_plotting import plot_circuit, plot_circuit_posaligned
from dictionary_learning import AutoEncoder
from loading_utils import load_examples, load_examples_nopair
from nnsight import LanguageModel
###### utilities for dealing with sparse COO tensors ######
def flatten_index(idxs, shape):
"""
index : a tensor of shape [n, len(shape)]
shape : a shape
return a tensor of shape [n] where each element is the flattened index
"""
idxs = idxs.t()
# get strides from shape
strides = [1]
for i in range(len(shape)-1, 0, -1):
strides.append(strides[-1]*shape[i])
strides = list(reversed(strides))
strides = t.tensor(strides).to(idxs.device)
# flatten index
return (idxs * strides).sum(dim=1).unsqueeze(0)
def prod(l):
out = 1
for x in l: out *= x
return out
def sparse_flatten(x):
x = x.coalesce()
return t.sparse_coo_tensor(
flatten_index(x.indices(), x.shape),
x.values(),
(prod(x.shape),)
)
def reshape_index(index, shape):
"""
index : a tensor of shape [n]
shape : a shape
return a tensor of shape [n, len(shape)] where each element is the reshaped index
"""
multi_index = []
for dim in reversed(shape):
multi_index.append(index % dim)
index //= dim
multi_index.reverse()
return t.stack(multi_index, dim=-1)
def sparse_reshape(x, shape):
"""
x : a sparse COO tensor
shape : a shape
return x reshaped to shape
"""
# first flatten x
x = sparse_flatten(x).coalesce()
new_indices = reshape_index(x.indices()[0], shape)
return t.sparse_coo_tensor(new_indices.t(), x.values(), shape)
def sparse_mean(x, dim):
if isinstance(dim, int):
return x.sum(dim=dim) / x.shape[dim]
else:
return x.sum(dim=dim) / prod(x.shape[d] for d in dim)
######## end sparse tensor utilities ########
def get_circuit(
clean,
patch,
model,
embed,
attns,
mlps,
resids,
dictionaries,
metric_fn,
metric_kwargs=dict(),
aggregation='sum', # or 'none' for not aggregating across sequence position
nodes_only=False,
node_threshold=0.1,
edge_threshold=0.01,
):
all_submods = [embed] + [submod for layer_submods in zip(mlps, attns, resids) for submod in layer_submods]
# first get the patching effect of everything on y
effects, deltas, grads, total_effect = patching_effect(
clean,
patch,
model,
all_submods,
dictionaries,
metric_fn,
metric_kwargs=metric_kwargs,
method='ig' # get better approximations for early layers by using ig
)
def unflatten(tensor): # will break if dictionaries vary in size between layers
b, s, f = effects[resids[0]].act.shape
unflattened = rearrange(tensor, '(b s x) -> b s x', b=b, s=s)
return SparseAct(act=unflattened[...,:f], res=unflattened[...,f:])
features_by_submod = {
submod : (effects[submod].to_tensor().flatten().abs() > node_threshold).nonzero().flatten().tolist() for submod in all_submods
}
n_layers = len(resids)
nodes = {'y' : total_effect}
nodes['embed'] = effects[embed]
for i in range(n_layers):
nodes[f'attn_{i}'] = effects[attns[i]]
nodes[f'mlp_{i}'] = effects[mlps[i]]
nodes[f'resid_{i}'] = effects[resids[i]]
if nodes_only:
if aggregation == 'sum':
for k in nodes:
if k != 'y':
nodes[k] = nodes[k].sum(dim=1)
nodes = {k : v.mean(dim=0) for k, v in nodes.items()}
return nodes, None
edges = defaultdict(lambda:{})
edges[f'resid_{len(resids)-1}'] = { 'y' : effects[resids[-1]].to_tensor().flatten().to_sparse() }
def N(upstream, downstream):
return jvp(
clean,
model,
dictionaries,
downstream,
features_by_submod[downstream],
upstream,
grads[downstream],
deltas[upstream],
return_without_right=True,
)
# now we work backward through the model to get the edges
for layer in reversed(range(len(resids))):
resid = resids[layer]
mlp = mlps[layer]
attn = attns[layer]
MR_effect, MR_grad = N(mlp, resid)
AR_effect, AR_grad = N(attn, resid)
edges[f'mlp_{layer}'][f'resid_{layer}'] = MR_effect
edges[f'attn_{layer}'][f'resid_{layer}'] = AR_effect
if layer > 0:
prev_resid = resids[layer-1]
else:
prev_resid = embed
RM_effect, _ = N(prev_resid, mlp)
RA_effect, _ = N(prev_resid, attn)
MR_grad = MR_grad.coalesce()
AR_grad = AR_grad.coalesce()
RMR_effect = jvp(
clean,
model,
dictionaries,
mlp,
features_by_submod[resid],
prev_resid,
{feat_idx : unflatten(MR_grad[feat_idx].to_dense()) for feat_idx in features_by_submod[resid]},
deltas[prev_resid],
)
RAR_effect = jvp(
clean,
model,
dictionaries,
attn,
features_by_submod[resid],
prev_resid,
{feat_idx : unflatten(AR_grad[feat_idx].to_dense()) for feat_idx in features_by_submod[resid]},
deltas[prev_resid],
)
RR_effect, _ = N(prev_resid, resid)
if layer > 0:
edges[f'resid_{layer-1}'][f'mlp_{layer}'] = RM_effect
edges[f'resid_{layer-1}'][f'attn_{layer}'] = RA_effect
edges[f'resid_{layer-1}'][f'resid_{layer}'] = RR_effect - RMR_effect - RAR_effect
else:
edges['embed'][f'mlp_{layer}'] = RM_effect
edges['embed'][f'attn_{layer}'] = RA_effect
edges['embed'][f'resid_0'] = RR_effect - RMR_effect - RAR_effect
# rearrange weight matrices
for child in edges:
# get shape for child
bc, sc, fc = nodes[child].act.shape
for parent in edges[child]:
weight_matrix = edges[child][parent]
if parent == 'y':
weight_matrix = sparse_reshape(weight_matrix, (bc, sc, fc+1))
else:
bp, sp, fp = nodes[parent].act.shape
assert bp == bc
weight_matrix = sparse_reshape(weight_matrix, (bp, sp, fp+1, bc, sc, fc+1))
edges[child][parent] = weight_matrix
if aggregation == 'sum':
# aggregate across sequence position
for child in edges:
for parent in edges[child]:
weight_matrix = edges[child][parent]
if parent == 'y':
weight_matrix = weight_matrix.sum(dim=1)
else:
weight_matrix = weight_matrix.sum(dim=(1, 4))
edges[child][parent] = weight_matrix
for node in nodes:
if node != 'y':
nodes[node] = nodes[node].sum(dim=1)
# aggregate across batch dimension
for child in edges:
bc, fc = nodes[child].act.shape
for parent in edges[child]:
weight_matrix = edges[child][parent]
if parent == 'y':
weight_matrix = weight_matrix.sum(dim=0) / bc
else:
bp, fp = nodes[parent].act.shape
assert bp == bc
weight_matrix = weight_matrix.sum(dim=(0,2)) / bc
edges[child][parent] = weight_matrix
for node in nodes:
if node != 'y':
nodes[node] = nodes[node].mean(dim=0)
elif aggregation == 'none':
# aggregate across batch dimensions
for child in edges:
# get shape for child
bc, sc, fc = nodes[child].act.shape
for parent in edges[child]:
weight_matrix = edges[child][parent]
if parent == 'y':
weight_matrix = sparse_reshape(weight_matrix, (bc, sc, fc+1))
weight_matrix = weight_matrix.sum(dim=0) / bc
else:
bp, sp, fp = nodes[parent].act.shape
assert bp == bc
weight_matrix = sparse_reshape(weight_matrix, (bp, sp, fp+1, bc, sc, fc+1))
weight_matrix = weight_matrix.sum(dim=(0, 3)) / bc
edges[child][parent] = weight_matrix
for node in nodes:
nodes[node] = nodes[node].mean(dim=0)
else:
raise ValueError(f"Unknown aggregation: {aggregation}")
return nodes, edges
def get_circuit_cluster(dataset,
model_name="EleutherAI/pythia-70m-deduped",
d_model=512,
dict_id=10,
dict_size=32768,
max_length=64,
max_examples=100,
batch_size=2,
node_threshold=0.1,
edge_threshold=0.01,
device="cuda:0",
dict_path="dictionaries/pythia-70m-deduped/",
dataset_name="cluster_circuit",
circuit_dir="circuits/",
plot_dir="circuits/figures/",
model=None,
dictionaries=None,):
model = LanguageModel(model_name, device_map=device, dispatch=True)
embed = model.gpt_neox.embed_in
attns = [layer.attention for layer in model.gpt_neox.layers]
mlps = [layer.mlp for layer in model.gpt_neox.layers]
resids = [layer for layer in model.gpt_neox.layers]
dictionaries = {}
dictionaries[embed] = AutoEncoder.from_pretrained(
os.path.join(dict_path, f'embed/{dict_id}_{dict_size}/ae.pt'),
device=device
)
for i in range(len(model.gpt_neox.layers)):
dictionaries[attns[i]] = AutoEncoder.from_pretrained(
os.path.join(dict_path, f'attn_out_layer{i}/{dict_id}_{dict_size}/ae.pt'),
device=device
)
dictionaries[mlps[i]] = AutoEncoder.from_pretrained(
os.path.join(dict_path, f'mlp_out_layer{i}/{dict_id}_{dict_size}/ae.pt'),
device=device
)
dictionaries[resids[i]] = AutoEncoder.from_pretrained(
os.path.join(dict_path, f'resid_out_layer{i}/{dict_id}_{dict_size}/ae.pt'),
device=device
)
examples = load_examples_nopair(dataset, max_examples, model, length=max_length)
num_examples = min(len(examples), max_examples)
n_batches = math.ceil(num_examples / batch_size)
batches = [
examples[batch*batch_size:(batch+1)*batch_size] for batch in range(n_batches)
]
if num_examples < max_examples: # warn the user
print(f"Total number of examples is less than {max_examples}. Using {num_examples} examples instead.")
running_nodes = None
running_edges = None
for batch in tqdm(batches, desc="Batches"):
clean_inputs = t.cat([e['clean_prefix'] for e in batch], dim=0).to(device)
clean_answer_idxs = t.tensor([e['clean_answer'] for e in batch], dtype=t.long, device=device)
patch_inputs = None
def metric_fn(model):
return (
-1 * t.gather(
t.nn.functional.log_softmax(model.embed_out.output[:,-1,:], dim=-1), dim=-1, index=clean_answer_idxs.view(-1, 1)
).squeeze(-1)
)
nodes, edges = get_circuit(
clean_inputs,
patch_inputs,
model,
embed,
attns,
mlps,
resids,
dictionaries,
metric_fn,
aggregation="sum",
node_threshold=node_threshold,
edge_threshold=edge_threshold,
)
if running_nodes is None:
running_nodes = {k : len(batch) * nodes[k].to('cpu') for k in nodes.keys() if k != 'y'}
running_edges = { k : { kk : len(batch) * edges[k][kk].to('cpu') for kk in edges[k].keys() } for k in edges.keys()}
else:
for k in nodes.keys():
if k != 'y':
running_nodes[k] += len(batch) * nodes[k].to('cpu')
for k in edges.keys():
for v in edges[k].keys():
running_edges[k][v] += len(batch) * edges[k][v].to('cpu')
# memory cleanup
del nodes, edges
gc.collect()
nodes = {k : v.to(device) / num_examples for k, v in running_nodes.items()}
edges = {k : {kk : 1/num_examples * v.to(device) for kk, v in running_edges[k].items()} for k in running_edges.keys()}
save_dict = {
"examples" : examples,
"nodes": nodes,
"edges": edges
}
save_basename = f"{dataset_name}_dict{dict_id}_node{node_threshold}_edge{edge_threshold}_n{num_examples}_aggsum"
with open(f'{circuit_dir}/{save_basename}.pt', 'wb') as outfile:
t.save(save_dict, outfile)
nodes = save_dict['nodes']
edges = save_dict['edges']
# feature annotations
try:
annotations = {}
with open(f'annotations/{dict_id}_{dict_size}.jsonl', 'r') as f:
for line in f:
line = json.loads(line)
annotations[line['Name']] = line['Annotation']
except:
annotations = None
plot_circuit(
nodes,
edges,
layers=len(model.gpt_neox.layers),
node_threshold=node_threshold,
edge_threshold=edge_threshold,
pen_thickness=1,
annotations=annotations,
save_dir=os.path.join(plot_dir, save_basename))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', '-d', type=str, default='simple_train',
help="A subject-verb agreement dataset in data/, or a path to a cluster .json.")
parser.add_argument('--num_examples', '-n', type=int, default=100,
help="The number of examples from the --dataset over which to average indirect effects.")
parser.add_argument('--example_length', '-l', type=int, default=None,
help="The max length (if using sum aggregation) or exact length (if not aggregating) of examples.")
parser.add_argument('--model', type=str, default='EleutherAI/pythia-70m-deduped',
help="The Huggingface ID of the model you wish to test.")
parser.add_argument("--dict_path", type=str, default="dictionaries/pythia-70m-deduped/",
help="Path to all dictionaries for your language model.")
parser.add_argument('--d_model', type=int, default=512,
help="Hidden size of the language model.")
parser.add_argument('--dict_id', type=str, default=10,
help="ID of the dictionaries. Use `id` to obtain circuits on neurons/heads directly.")
parser.add_argument('--dict_size', type=int, default=32768,
help="The width of the dictionary encoder.")
parser.add_argument('--batch_size', type=int, default=32,
help="Number of examples to process at once when running circuit discovery.")
parser.add_argument('--aggregation', type=str, default='sum',
help="Aggregation across token positions. Should be one of `sum` or `none`.")
parser.add_argument('--node_threshold', type=float, default=0.2,
help="Indirect effect threshold for keeping circuit nodes.")
parser.add_argument('--edge_threshold', type=float, default=0.02,
help="Indirect effect threshold for keeping edges.")
parser.add_argument('--pen_thickness', type=float, default=1,
help="Scales the width of the edges in the circuit plot.")
parser.add_argument('--nopair', default=False, action="store_true",
help="Use if your data does not contain contrastive (minimal) pairs.")
parser.add_argument('--plot_circuit', default=False, action='store_true',
help="Plot the circuit after discovering it.")
parser.add_argument('--nodes_only', default=False, action='store_true',
help="Only search for causally implicated features; do not draw edges.")
parser.add_argument('--plot_only', action="store_true",
help="Do not run circuit discovery; just plot an existing circuit.")
parser.add_argument("--circuit_dir", type=str, default="circuits/",
help="Directory to save/load circuits.")
parser.add_argument("--plot_dir", type=str, default="circuits/figures/",
help="Directory to save figures.")
parser.add_argument('--seed', type=int, default=12)
parser.add_argument('--device', type=str, default='cuda:0')
args = parser.parse_args()
device = args.device
model = LanguageModel(args.model, device_map=device, dispatch=True)
embed = model.gpt_neox.embed_in
attns = [layer.attention for layer in model.gpt_neox.layers]
mlps = [layer.mlp for layer in model.gpt_neox.layers]
resids = [layer for layer in model.gpt_neox.layers]
dictionaries = {}
if args.dict_id == 'id':
from dictionary_learning.dictionary import IdentityDict
dictionaries[embed] = IdentityDict(args.d_model)
for i in range(len(model.gpt_neox.layers)):
dictionaries[attns[i]] = IdentityDict(args.d_model)
dictionaries[mlps[i]] = IdentityDict(args.d_model)
dictionaries[resids[i]] = IdentityDict(args.d_model)
else:
dictionaries[embed] = AutoEncoder.from_pretrained(
f'{args.dict_path}/embed/{args.dict_id}_{args.dict_size}/ae.pt',
device=device
)
for i in range(len(model.gpt_neox.layers)):
dictionaries[attns[i]] = AutoEncoder.from_pretrained(
f'{args.dict_path}/attn_out_layer{i}/{args.dict_id}_{args.dict_size}/ae.pt',
device=device
)
dictionaries[mlps[i]] = AutoEncoder.from_pretrained(
f'{args.dict_path}/mlp_out_layer{i}/{args.dict_id}_{args.dict_size}/ae.pt',
device=device
)
dictionaries[resids[i]] = AutoEncoder.from_pretrained(
f'{args.dict_path}/resid_out_layer{i}/{args.dict_id}_{args.dict_size}/ae.pt',
device=device
)
if args.nopair:
save_basename = os.path.splitext(os.path.basename(args.dataset))[0]
examples = load_examples_nopair(args.dataset, args.num_examples, model, length=args.example_length)
else:
data_path = f"data/{args.dataset}.json"
save_basename = args.dataset
if args.aggregation == "sum":
examples = load_examples(data_path, args.num_examples, model, pad_to_length=args.example_length)
else:
examples = load_examples(data_path, args.num_examples, model, length=args.example_length)
batch_size = args.batch_size
num_examples = min([args.num_examples, len(examples)])
n_batches = math.ceil(num_examples / batch_size)
batches = [
examples[batch*batch_size:(batch+1)*batch_size] for batch in range(n_batches)
]
if num_examples < args.num_examples: # warn the user
print(f"Total number of examples is less than {args.num_examples}. Using {num_examples} examples instead.")
if not args.plot_only:
running_nodes = None
running_edges = None
for batch in tqdm(batches, desc="Batches"):
clean_inputs = t.cat([e['clean_prefix'] for e in batch], dim=0).to(device)
clean_answer_idxs = t.tensor([e['clean_answer'] for e in batch], dtype=t.long, device=device)
if args.nopair:
patch_inputs = None
def metric_fn(model):
return (
-1 * t.gather(
t.nn.functional.log_softmax(model.embed_out.output[:,-1,:], dim=-1), dim=-1, index=clean_answer_idxs.view(-1, 1)
).squeeze(-1)
)
else:
patch_inputs = t.cat([e['patch_prefix'] for e in batch], dim=0).to(device)
patch_answer_idxs = t.tensor([e['patch_answer'] for e in batch], dtype=t.long, device=device)
def metric_fn(model):
return (
t.gather(model.embed_out.output[:,-1,:], dim=-1, index=patch_answer_idxs.view(-1, 1)).squeeze(-1) - \
t.gather(model.embed_out.output[:,-1,:], dim=-1, index=clean_answer_idxs.view(-1, 1)).squeeze(-1)
)
nodes, edges = get_circuit(
clean_inputs,
patch_inputs,
model,
embed,
attns,
mlps,
resids,
dictionaries,
metric_fn,
nodes_only=args.nodes_only,
aggregation=args.aggregation,
node_threshold=args.node_threshold,
edge_threshold=args.edge_threshold,
)
if running_nodes is None:
running_nodes = {k : len(batch) * nodes[k].to('cpu') for k in nodes.keys() if k != 'y'}
if not args.nodes_only: running_edges = { k : { kk : len(batch) * edges[k][kk].to('cpu') for kk in edges[k].keys() } for k in edges.keys()}
else:
for k in nodes.keys():
if k != 'y':
running_nodes[k] += len(batch) * nodes[k].to('cpu')
if not args.nodes_only:
for k in edges.keys():
for v in edges[k].keys():
running_edges[k][v] += len(batch) * edges[k][v].to('cpu')
# memory cleanup
del nodes, edges
gc.collect()
nodes = {k : v.to(device) / num_examples for k, v in running_nodes.items()}
if not args.nodes_only:
edges = {k : {kk : 1/num_examples * v.to(device) for kk, v in running_edges[k].items()} for k in running_edges.keys()}
else: edges = None
save_dict = {
"examples" : examples,
"nodes": nodes,
"edges": edges
}
with open(f'{args.circuit_dir}/{save_basename}_dict{args.dict_id}_node{args.node_threshold}_edge{args.edge_threshold}_n{num_examples}_agg{args.aggregation}.pt', 'wb') as outfile:
t.save(save_dict, outfile)
else:
with open(f'{args.circuit_dir}/{save_basename}_dict{args.dict_id}_node{args.node_threshold}_edge{args.edge_threshold}_n{num_examples}_agg{args.aggregation}.pt', 'rb') as infile:
save_dict = t.load(infile)
nodes = save_dict['nodes']
edges = save_dict['edges']
# feature annotations
try:
annotations = {}
with open(f"annotations/{args.dict_id}_{args.dict_size}.jsonl", 'r') as annotations_data:
for annotation_line in annotations_data:
annotation = json.loads(annotation_line)
annotations[annotation["Name"]] = annotation["Annotation"]
except:
annotations = None
if args.aggregation == "none":
example = model.tokenizer.batch_decode(examples[0]["clean_prefix"])[0]
plot_circuit_posaligned(
nodes,
edges,
layers=len(model.gpt_neox.layers),
length=args.example_length,
example_text=example,
node_threshold=args.node_threshold,
edge_threshold=args.edge_threshold,
pen_thickness=args.pen_thickness,
annotations=annotations,
save_dir=f'{args.plot_dir}/{save_basename}_dict{args.dict_id}_node{args.node_threshold}_edge{args.edge_threshold}_n{num_examples}_agg{args.aggregation}'
)
else:
plot_circuit(
nodes,
edges,
layers=len(model.gpt_neox.layers),
node_threshold=args.node_threshold,
edge_threshold=args.edge_threshold,
pen_thickness=args.pen_thickness,
annotations=annotations,
save_dir=f'{args.plot_dir}/{save_basename}_dict{args.dict_id}_node{args.node_threshold}_edge{args.edge_threshold}_n{num_examples}_agg{args.aggregation}'
)