-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwandb_update.py
387 lines (338 loc) · 12.6 KB
/
wandb_update.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
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
"""
Delete files from old Wandb projects to clean up space.
Author:
Collin McCarthy
https://github.com/collinmccarthy/wandb-scripts
Examples:
- Delete all checkpoints
```
python wandb_update.py \
--wandb-entity=$WANDB_ENTITY \
--wandb-project=$WANDB_PROJECT \
--delete_filename_regex=".*\.pth"
```
- Delete all bbox and segm checkpoints for runs matching 'm2f_.*_city_.*'
```
python wandb_update.py \
--wandb-entity=$WANDB_ENTITY \
--wandb-project=$WANDB_PROJECT \
--delete_filename_regex '.*_segm_.*\.pth' '.*_bbox_.*\.pth' \
--select-run-names-regex 'm2f_.*_city.*'
```
- Delete all files in folder 'predictions/', except for runs matching 'm2f_.*_city_.*'
```
python wandb_update.py \
--wandb-entity=$WANDB_ENTITY \
--wandb-project=$WANDB_PROJECT \
--delete_filename_regex="^predictions/.*" \
--skip-run-names-regex=$SKIP_RUN
```
- Delete all artifacts ending in '_pred_final', except for run $SKIP_RUN
```
python wandb_update.py \
--wandb-entity=$WANDB_ENTITY \
--wandb-project=$WANDB_PROJECT \
--delete-artifact-regex=".*_pred_final" \
--skip-run-names-regex=$SKIP_RUN
```
"""
import os
import argparse
import re
import pprint
from argparse import Namespace
from datetime import datetime
from collections import namedtuple
from typing import Union, Sequence
import wandb
from wandb.apis.public.runs import Run, Runs
from wandb.apis.public.files import File, Files
from wandb.apis.public.artifacts import RunArtifacts
from wandb.sdk.artifacts.artifact import Artifact
from tqdm import tqdm
def parse_args() -> Namespace:
parser = argparse.ArgumentParser(description="Clean up Wandb runs remotely")
parser.add_argument(
"--wandb-entity",
"--wandb_entity",
default=os.environ.get("WANDB_ENTITY", None),
help="Wandb entity name (e.g. username or team name)",
)
parser.add_argument(
"--wandb-project",
"--wandb_project",
default=os.environ.get("WANDB_PROJECT", None),
help="Wandb project name",
)
parser.add_argument("--username", help="Username to filter / select runs")
parser.add_argument(
"--delete-filename-regex",
"--delete_filename_regex",
nargs="+",
default=list(),
help="Delete all files in all runs in the project that match this regular expression",
)
parser.add_argument(
"--delete-keep-latest",
"--delete_keep_latest",
action="store_true",
help="If delete_filename_regex or delete_artifact_regex are used, keep the objects with"
" the last modified time",
)
parser.add_argument(
"--delete-artifact-regex",
"--delete_artifact_regex",
nargs="+",
default=list(),
help="Delete all artifacts in all runs in the project that match this regular expression",
)
parser.add_argument(
"--find-filename-regex",
"--find_filename_regex",
nargs="+",
default=list(),
help="Print all files in all runs in the project that match this regular expression",
)
parser.add_argument(
"--find-artifact-regex",
"--find_artifact_regex",
nargs="+",
default=list(),
help="Print all artifacts in all runs in the project that match this regular expression",
)
parser.add_argument(
"--skip-run-ids",
"--skip_run_ids",
nargs="+",
default=list(),
help="Run ids to skip",
)
parser.add_argument(
"--select-run-ids",
"--select_run_ids",
nargs="+",
default=list(),
help="Run ids to filter / select",
)
parser.add_argument(
"--skip-run-names-regex",
"--skip_run_names_regex",
nargs="+",
default=list(),
help="Run names to skip",
)
parser.add_argument(
"--select-run-names-regex",
"--select_run_names_regex",
nargs="+",
default=list(),
help="Run names to filter / select",
)
parser.add_argument(
"--dry-run",
"--dry_run",
action="store_true",
help="Print all files that will be deleted but do not delete them",
)
args = parser.parse_args()
if args.wandb_entity is None:
raise RuntimeError(f"Missing --wandb-entity (default: $WANDB_ENTITY)")
if args.wandb_project is None:
raise RuntimeError(f"Missing --wandb-project (default: $WANDB_PROJECT)")
if len(args.wandb_entity) == 0:
raise RuntimeError(f"Found empty string for --wandb-entity")
if len(args.wandb_project) == 0:
raise RuntimeError(f"Found empty string for --wandb-project")
return args
def remove_latest_file_or_artifact(
delete_objs: Union[Sequence[File], Sequence[Artifact]]
) -> Union[Sequence[File], Sequence[Artifact]]:
if len(delete_objs) == 1:
last_mod_file = delete_objs[0]
else:
DatetimeTup = namedtuple("DatetimeTup", ["file", "datetime"])
datetimes = []
for f in delete_objs:
if isinstance(f, File):
updated_attr = "updatedAt"
elif isinstance(f, Artifact):
updated_attr = "updated_at"
else:
assert False, "Expected File or Artifact"
dt = DatetimeTup(
file=f,
datetime=datetime.strptime(
getattr(f, updated_attr),
"%Y-%m-%dT%H:%M:%SZ", # For version 0.18.6; throws exception if incorrect
),
)
datetimes.append(dt)
datetimes = sorted(datetimes, key=lambda x: x.datetime, reverse=True)
last_mod_file = datetimes[0].file
print(f"Removing last modified file from deletion list: {last_mod_file.name}")
num_delete_prev = len(delete_objs)
delete_objs = [
f for f in delete_objs if f != last_mod_file
] # pyright: ignore[reportAssignmentType]
assert len(delete_objs) == num_delete_prev - 1
return delete_objs
def filter_runs(args: Namespace) -> Sequence[Run]:
api = wandb.Api()
print(f"Querying Wandb entity: {args.wandb_entity}, project: {args.wandb_project}")
runs: Runs = api.runs(f"{args.wandb_entity}/{args.wandb_project}")
if len(args.delete_filename_regex) > 0:
print(
f"Searching {len(runs)} runs for filenames matching {args.delete_filename_regex}"
)
if len(args.delete_artifact_regex) > 0:
print(
f"Searching {len(runs)} runs for artifacts matching {args.delete_artifact_regex}"
)
current_runs: Sequence[Run] = []
for run in runs:
run: Run
if len(args.skip_run_ids) > 0 and run.id in args.skip_run_ids:
# print(f"Skipping run id {run.id} (found in --skip-run-ids)")
continue
if len(args.select_run_ids) > 0 and run.id not in args.select_run_ids:
# print(f"Skipping run id {run.id} (not found in --select-run-ids)")
continue
if len(args.skip_run_names_regex) > 0 and any(
[re.search(regex, run.name) for regex in args.skip_run_names_regex]
):
# print(f"Skipping run name {run.name} (matched with --skip-run-names-regex)")
continue
if len(args.select_run_names_regex) > 0 and not any(
[re.search(regex, run.name) for regex in args.select_run_names_regex]
):
# print(f"Skipping run name {run.name} (not matched with --select-run-names-regex)")
continue
if args.username is not None and run.user.username != args.username:
continue
current_runs.append(run)
return current_runs
def find_objects(
run: Run,
objects: Union[Files, RunArtifacts],
regex: str,
remove_latest: bool,
verbose: bool = True,
) -> Union[Sequence[File], Sequence[Artifact]]:
found_objs: Union[Sequence[File], Sequence[Artifact]] = [
f for f in objects if re.search(regex, f.name)
]
if len(found_objs) > 0:
found_filenames = [f.name for f in found_objs]
found_filenames_str = "\n " + "\n ".join(sorted(found_filenames))
if verbose:
print(
f"Found {len(found_objs)} matching objects from {run.name}:"
f"{found_filenames_str}"
)
if remove_latest:
found_objs = remove_latest_file_or_artifact(found_objs)
return found_objs
def delete_objects(args: Namespace):
runs = filter_runs(args)
print(
f"Using delete_filename_regex={args.delete_filename_regex},"
f" delete_artifact_regex={args.delete_artifact_regex}"
)
all_delete_files = []
all_delete_artifacts = []
for run in tqdm(runs, desc="Run"):
for regex in args.delete_filename_regex:
files: Files = run.files()
delete_files: Sequence[File] = find_objects(
run=run,
objects=files,
regex=regex,
remove_latest=args.delete_keep_latest,
) # pyright: ignore[reportAssignmentType]
all_delete_files.extend(delete_files)
for regex in args.delete_artifact_regex:
artifacts = run.logged_artifacts()
delete_artifacts: Sequence[File] = find_objects(
run=run,
objects=artifacts,
regex=regex,
remove_latest=args.delete_keep_latest,
) # pyright: ignore[reportAssignmentType]
all_delete_artifacts.extend(delete_artifacts)
if len(all_delete_files + all_delete_artifacts) == 0:
print(f"Found no matching files or artifacts to delete")
else:
all_obj_strs = []
all_obj_strs.extend([f.url for f in all_delete_files])
all_obj_strs.extend([f.qualified_name for f in all_delete_artifacts])
print("-" * 80)
print(
f"Found {len(all_obj_strs)} files / artifacts:"
f"\n{pprint.pformat(all_obj_strs)}"
)
if not args.dry_run:
response = input(
f"Delete {len(all_delete_files + all_delete_artifacts)} files/artifacts? (y/N): "
)
if response.lower() == "y":
print(f"Deleting files (response=y)")
if len(all_delete_files) > 0: # For tqdm output
for f in tqdm(all_delete_files, desc="Files"):
f.delete()
if len(all_delete_artifacts) > 0: # For tqdm output
for f in tqdm(all_delete_artifacts, desc="Artifacts"):
f.delete(delete_aliases=True)
else:
print(f"Skipping deletion (response=n)")
def find_all_objects(args: Namespace):
runs = filter_runs(args)
print(
f"Using find_filename_regex={args.find_filename_regex},"
f" find_artifact_regex={args.find_artifact_regex}"
)
all_found_files: list[File] = []
all_found_artifacts: list[Artifact] = []
for run in tqdm(runs, desc="Run"):
for regex in args.find_filename_regex:
files: Files = run.files()
found_files: Sequence[File] = find_objects(
run=run,
objects=files,
regex=regex,
remove_latest=False,
verbose=False,
) # pyright: ignore[reportAssignmentType]
all_found_files.extend(found_files)
for regex in args.find_artifact_regex:
artifacts = run.logged_artifacts()
found_artifacts: Sequence[Artifact] = find_objects(
run=run,
objects=artifacts,
regex=regex,
remove_latest=False,
verbose=False,
) # pyright: ignore[reportAssignmentType]
all_found_artifacts.extend(found_artifacts)
if len(all_found_files + all_found_artifacts) == 0:
print(f"Found no matching files or artifacts")
else:
all_obj_strs = []
all_obj_strs.extend([f.url for f in all_found_files])
all_obj_strs.extend([f.qualified_name for f in all_found_artifacts])
print("-" * 80)
print(
f"Found {len(all_obj_strs)} files / artifacts:"
f"\n{pprint.pformat(all_obj_strs)}"
)
unique_names = list(set(f.name for f in all_found_files + all_found_artifacts))
print("-" * 80)
print(
f"Found {len(unique_names)} unique files / artifacts:"
f"\n{pprint.pformat(unique_names)}"
)
if __name__ == "__main__":
args = parse_args()
if len(args.find_filename_regex) > 0 or len(args.find_artifact_regex) > 0:
find_all_objects(args)
if len(args.delete_filename_regex) > 0 or len(args.delete_artifact_regex) > 0:
delete_objects(args)