-
Notifications
You must be signed in to change notification settings - Fork 618
/
test_wandb.py
617 lines (474 loc) 路 17.8 KB
/
test_wandb.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
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
"""These test the high level sdk methods by mocking out the backend.
See wandb_integration_test.py for tests that launch a real backend against
a live backend server.
"""
import glob
import inspect
import os
import tempfile
from contextlib import contextmanager
from pathlib import Path
from unittest import mock
import pytest
import wandb
from wandb.sdk.wandb_init import init as real_wandb_init
from wandb.viz import custom_chart
def test_wandb_init_fixture_args(wandb_init):
"""Test that the fixture args are in sync with the real wandb.init()."""
# comparing lists of args as order also matters
assert (
inspect.getfullargspec(real_wandb_init).args
== inspect.getfullargspec(wandb_init).args
)
def test_sagemaker_key():
with open("secrets.env", "w") as f:
f.write("WANDB_API_KEY={}".format("S" * 40))
assert wandb.api.api_key == "S" * 40
@pytest.mark.wandb_args(
tf_config={
"cluster": {"master": ["trainer-4dsl7-master-0:2222"]},
"task": {"type": "master", "index": 0},
"environment": "cloud",
}
)
@pytest.mark.skip(
reason="TF_CONFIG parsing not yet implemented, see wandb.util.parse_tfjob_config"
)
def test_simple_tfjob(wandb_init):
assert wandb.run.group is None
assert wandb.run.job_type == "master"
@pytest.mark.wandb_args(
tf_config={
"cluster": {
"master": ["trainer-sj2hp-master-0:2222"],
"ps": ["trainer-sj2hp-ps-0:2222"],
"worker": ["trainer-sj2hp-worker-0:2222"],
},
"task": {"type": "worker", "index": 0},
"environment": "cloud",
}
)
@pytest.mark.skip(
reason="TF_CONFIG parsing not yet implemented, see wandb.util.parse_tfjob_config"
)
def test_distributed_tfjob(wandb_init):
assert wandb.run.group == "trainer-sj2hp"
assert wandb.run.job_type == "worker"
@pytest.mark.wandb_args(tf_config={"cluster": {"corrupt": ["bad"]}})
@pytest.mark.skip(
reason="TF_CONFIG parsing not yet implemented, see wandb.util.parse_tfjob_config"
)
def test_corrupt_tfjob(wandb_init):
assert wandb.run.group is None
@pytest.mark.wandb_args(env={"TF_CONFIG": "garbage"})
@pytest.mark.skip(
reason="TF_CONFIG parsing not yet implemented, see wandb.util.parse_tfjob_config"
)
def test_bad_json_tfjob(wandb_init):
assert wandb.run.group is None
def test_custom_dir(wandb_init):
with tempfile.TemporaryDirectory() as tmpdir:
run = wandb_init(dir=tmpdir, mode="offline")
run.finish()
assert len(glob.glob(os.path.join(tmpdir, "wandb", "offline-*"))) > 0
def test_custom_dir_env(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_DIR": tempfile.gettempdir()}):
run = wandb_init(mode="offline")
run.finish()
assert len(glob.glob(os.path.join(tempfile.gettempdir(), "wandb", "offline-*"))) > 0
@pytest.mark.xfail(reason="Backend race condition")
def test_anonymous_mode(wandb_init, capsys, local_settings):
copied_env = os.environ.copy()
copied_env.pop("WANDB_API_KEY")
copied_env.pop("WANDB_USERNAME")
copied_env.pop("WANDB_ENTITY")
with mock.patch.dict("os.environ", copied_env, clear=True):
run = wandb_init(anonymous="must")
run.log({"something": 1})
run.finish()
_, err = capsys.readouterr()
assert (
"Do NOT share these links with anyone. They can be used to claim your runs."
in err
)
@pytest.mark.xfail(reason="Backend race condition")
def test_anonymous_mode_artifact(wandb_init, capsys, local_settings):
copied_env = os.environ.copy()
copied_env.pop("WANDB_API_KEY")
copied_env.pop("WANDB_USERNAME")
copied_env.pop("WANDB_ENTITY")
with mock.patch.dict("os.environ", copied_env, clear=True):
run = wandb_init(anonymous="must")
run.log_artifact(wandb.Artifact("my-arti", type="dataset"))
run.finish()
_, err = capsys.readouterr()
assert (
"Artifacts logged anonymously cannot be claimed and expire after 7 days." in err
)
def test_run_id(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_RUN_ID": "123456"}):
run = wandb_init()
run.finish()
assert run.id == "123456"
def test_run_name(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_NAME": "coolio"}):
run = wandb_init()
run.finish()
assert run.name == "coolio"
def test_run_setname(wandb_init):
run = wandb_init()
run.name = "name1"
run.finish()
assert run.name == "name1"
def test_run_notes(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_NOTES": "these are my notes"}):
run = wandb_init()
run.finish()
assert run.notes == "these are my notes"
def test_run_setnotes(wandb_init):
run = wandb_init()
run.notes = "notes1"
run.finish()
assert run.notes == "notes1"
def test_run_tags(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_TAGS": "tag1,tag2"}):
run = wandb_init()
run.finish()
assert run.tags == ("tag1", "tag2")
def test_run_settags(wandb_init):
run = wandb_init()
run.tags = ("tag1", "tag2")
run.finish()
assert run.tags == ("tag1", "tag2")
def test_run_mode(wandb_init):
run = wandb_init(mode="dryrun")
run.finish()
assert run.mode == "dryrun"
def test_run_offline(wandb_init):
run = wandb_init(mode="offline")
run.finish()
assert run.offline is True
def test_run_entity(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_ENTITY": "ent1"}):
run = wandb_init(mode="offline")
run.finish()
assert run.entity == "ent1"
def test_run_project(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_PROJECT": "proj1"}):
run = wandb_init()
run.finish()
assert run.project == "proj1"
assert run.project_name() == "proj1"
def test_run_group(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_RUN_GROUP": "group1"}):
run = wandb_init()
run.finish()
assert run.group == "group1"
def test_run_jobtype(wandb_init):
with mock.patch.dict("os.environ", {"WANDB_JOB_TYPE": "job1"}):
run = wandb_init()
run.finish()
assert run.job_type == "job1"
def test_run_resumed(wandb_init):
run = wandb_init()
run.finish()
assert run.resumed is False
def test_run_sweepid(wandb_init):
run = wandb_init()
run.finish()
assert run.sweep_id is None
def test_run_configstatic(wandb_init):
run = wandb_init()
run.config.update(dict(this=2, that=3))
assert dict(run.config_static) == dict(this=2, that=3)
run.finish()
def test_run_path(wandb_init):
with mock.patch.dict(
"os.environ",
{"WANDB_ENTITY": "ent1", "WANDB_PROJECT": "proj1", "WANDB_RUN_ID": "run1"},
):
run = wandb_init(mode="offline")
run.finish()
assert run.path == "ent1/proj1/run1"
def test_run_projecturl(wandb_init):
run = wandb_init(settings={"mode": "offline"})
run.finish()
# URL is not available offline
assert run.get_project_url() is None
def test_run_sweepurl(wandb_init):
run = wandb_init(settings={"mode": "offline"})
run.finish()
# URL is not available offline
assert run.get_sweep_url() is None
def test_run_url(wandb_init):
run = wandb_init(settings={"mode": "offline"})
run.finish()
# URL is not available offline
assert run.get_url() is None
assert run.url is None
# ----------------------------------
# wandb.log
# ----------------------------------
def test_nice_log_error():
with pytest.raises(wandb.Error):
wandb.log({"no": "init"})
def test_nice_log_error_config():
with pytest.raises(
wandb.Error, match=r"You must call wandb.init\(\) before wandb.config.update"
):
wandb.config.update({"foo": 1})
with pytest.raises(
wandb.Error, match=r"You must call wandb.init\(\) before wandb.config.foo"
):
wandb.config.foo = 1
def test_nice_log_error_summary():
with pytest.raises(
wandb.Error,
match=r'You must call wandb.init\(\) before wandb.summary\["great"\]',
):
wandb.summary["great"] = 1
with pytest.raises(
wandb.Error, match=r"You must call wandb.init\(\) before wandb.summary.bam"
):
wandb.summary.bam = 1
def test_log_only_strings_as_keys(mock_run):
run = mock_run()
with pytest.raises(ValueError):
run.log({1: 1000})
with pytest.raises(ValueError):
run.log({("tup", "idx"): 1000})
def test_log_not_dict(mock_run):
run = mock_run()
with pytest.raises(ValueError):
run.log(10)
def test_log_step(relay_server, wandb_init):
with relay_server() as relay:
run = wandb_init()
run.log({"acc": 1}, step=5, commit=True)
run.finish()
assert relay.context.history["_step"][0] == 5
def test_log_custom_chart(relay_server, wandb_init):
with relay_server() as relay:
run = wandb_init()
my_custom_chart = custom_chart(
"test_spec", wandb.Table(data=[[1, 2], [3, 4]], columns=["A", "B"]), {}, {}
)
run.log({"my_custom_chart": my_custom_chart})
run.finish()
assert relay.context.history["my_custom_chart_table"][0]
def test_log_silent(wandb_init, capsys):
with mock.patch.dict("os.environ", {"WANDB_SILENT": "true"}):
run = wandb_init()
run.log({"acc": 1})
run.finish()
_, err = capsys.readouterr()
assert "wandb: " not in err
def test_log_multiple_cases_example(relay_server, wandb_init):
with relay_server() as relay:
run = wandb_init()
run.log(dict(n=1))
run.log(dict(n=11), commit=False)
run.log(dict(n=2), step=100)
run.log(dict(n=3), step=100)
run.log(dict(n=8), step=101)
run.log(dict(n=5), step=102)
run.log(dict(cool=2), step=2)
run.log(dict(cool=2), step=4)
run.finish()
assert relay.context.history["n"].tolist() == [1, 11, 3, 8, 5]
assert relay.context.history["_step"].tolist() == [0, 1, 100, 101, 102]
def test_log_step_uncommited(relay_server, wandb_init):
with relay_server() as relay:
run = wandb_init()
run.log(dict(cool=2), step=2, commit=False)
run.log(dict(cool=2), step=4)
run.finish()
assert len(relay.context.history) == 2
def test_log_step_committed(relay_server, wandb_init):
with relay_server() as relay:
run = wandb_init()
run.log(dict(cool=2), step=2)
run.log(dict(cool=2), step=4, commit=True)
run.finish()
assert len(relay.context.history) == 2
def test_log_step_committed_same(relay_server, wandb_init):
with relay_server() as relay:
run = wandb_init()
run.log(dict(cool=2), step=1)
run.log(dict(cool=2), step=4)
run.log(dict(bad=3), step=4, commit=True)
run.finish()
history = relay.context.get_run_history(run.id)
assert len(history) == 2
assert history.cool[1] == 2
assert history.bad[1] == 3
assert len(history.columns) == 2
def test_log_step_committed_same_dropped(relay_server, wandb_init):
with relay_server() as relay:
run = wandb_init()
run.log(dict(cool=2), step=1)
run.log(dict(cool=2), step=4, commit=True)
run.log(dict(bad=3), step=4, commit=True)
run.finish()
history = relay.context.get_run_history(run.id)
assert len(history) == 2
assert history["cool"][1] == 2
# filter all the columns that don't start with `_`
assert len(history.columns) == 1
# ----------------------------------
# wandb.save
# ----------------------------------
@pytest.mark.xfail(reason="This test is flaky")
def test_save_invalid_path(wandb_init):
run = wandb_init()
root = tempfile.gettempdir()
test_path = os.path.join(root, "tmp", "test.txt")
wandb.util.mkdir_exists_ok(os.path.dirname(test_path))
with open(test_path, "w") as f:
f.write("something")
with pytest.raises(ValueError):
run.save(os.path.join(root, os.pardir, os.pardir, "*.txt"), base_path=root)
run.finish()
@pytest.mark.xfail(reason="This test is flaky")
def test_save_policy_symlink(mock_run, parse_records, record_q):
run = mock_run()
with open("test.rad", "w") as f:
f.write("something")
run.save("test.rad")
assert os.path.exists(os.path.join(run.dir, "test.rad"))
parsed = parse_records(record_q)
file_record = parsed.files[0].files[0]
assert file_record.path == "test.rad"
assert file_record.policy == 2
@pytest.mark.xfail(reason="This test is flaky")
def test_save_policy_glob_symlink(mock_run, parse_records, record_q, capsys):
run = mock_run()
with open("test.rad", "w") as f:
f.write("something")
with open("foo.rad", "w") as f:
f.write("something")
run.save("*.rad")
_, err = capsys.readouterr()
assert "Symlinked 2 files" in err
assert os.path.exists(os.path.join(run.dir, "test.rad"))
assert os.path.exists(os.path.join(run.dir, "foo.rad"))
# test_save_policy_glob_symlink
parsed = parse_records(record_q)
file_record = parsed.files[0].files[0]
assert file_record.path == "*.rad"
assert file_record.policy == 2
@pytest.mark.xfail(reason="This test is flaky")
def test_save_absolute_path(mock_run, parse_records, record_q, capsys):
run = mock_run()
root = tempfile.gettempdir()
test_path = os.path.join(root, "test.txt")
with open(test_path, "w") as f:
f.write("something")
run.save(test_path)
_, err = capsys.readouterr()
assert "Saving files without folders" in err
assert os.path.exists(os.path.join(run.dir, "test.txt"))
parsed = parse_records(record_q)
file_record = parsed.files[0].files[0]
assert file_record.path == "test.txt"
assert file_record.policy == 2
@pytest.mark.xfail(reason="This test is flaky")
def test_save_relative_path(mock_run, parse_records, record_q):
run = mock_run()
root = tempfile.gettempdir()
test_path = os.path.join(root, "tmp", "test.txt")
print("DAMN", os.path.dirname(test_path))
wandb.util.mkdir_exists_ok(os.path.dirname(test_path))
with open(test_path, "w") as f:
f.write("something")
run.save(test_path, base_path=root, policy="now")
assert os.path.exists(os.path.join(run.dir, test_path))
parsed = parse_records(record_q)
file_record = parsed.files[0].files[0]
assert file_record.path == os.path.relpath(test_path, root)
assert file_record.policy == 0
# ----------------------------------
# wandb.restore
# ----------------------------------
@pytest.fixture
def create_run_with_file(wandb_init):
@contextmanager
def create_file_for_run_fn(file_name, file_content):
run = wandb_init(settings={"save_code": True})
try:
file = Path(run.dir) / file_name
file.touch(exist_ok=True)
file.write_text(file_content)
yield run, file
finally:
run.finish()
file.unlink()
return create_file_for_run_fn
def test_restore_no_path():
with pytest.raises(ValueError, match="run_path required"):
wandb.restore("weights.h5")
@pytest.mark.xfail(reason="Public API might not return the correct value")
def test_restore_name_not_found(wandb_init):
with pytest.raises(ValueError):
run = wandb_init()
wandb.restore("no_file.h5")
run.finish()
@pytest.mark.xfail(reason="Public API might not return the correct value")
def test_restore_no_init(create_run_with_file):
with create_run_with_file("weights.h5", "content") as (run, file):
file_size = os.path.getsize(file)
res = wandb.restore("weights.h5", run_path=run.path)
assert os.path.getsize(res.name) == file_size
@pytest.mark.xfail(reason="Public API might not return the correct value")
def test_restore(create_run_with_file, test_settings):
with create_run_with_file("weights.h5", "content") as (run, file):
file_size = os.path.getsize(file)
with wandb.init(settings=test_settings()):
res = wandb.restore("weights.h5", run_path=run.path)
assert os.path.getsize(res.name) == file_size
# ----------------------------------
# wandb.attach
# ----------------------------------
def test_attach_usage_errors(wandb_init):
run = wandb_init()
if not os.environ.get("WANDB_REQUIRE_SERVICE"):
with pytest.raises(wandb.UsageError) as e:
wandb._attach(run=run)
assert (
"Either `attach_id` or `run_id` must be specified or `run` must have `_attach_id`"
in str(e.value)
)
with pytest.raises(wandb.UsageError) as e:
wandb._attach()
assert "Either (`attach_id` or `run_id`) or `run` must be specified" in str(e.value)
run.finish()
# ----------------------------------
# wandb.teardown
# ----------------------------------
# In a notebook environment we might get into a situation where the service process will be removed
# but the singleton setup instance still exists, hence it will try to do the teardown.
# Howeverwandb.teardown will encounter an error because the service process is already gone.
# but since we have an error handle logic in the teardown, we don't see the error
# only informational message about the error.
def test_teardown_error_path(capsys):
with mock.patch.dict(
os.environ, {wandb.env.SERVICE: "2-96604-tcp-localhost-57337"}
):
with mock.patch.object(
wandb.sdk.wandb_manager._Manager,
"_get_service_interface",
return_value=mock.MagicMock(),
):
wandb.setup()
assert wandb.wandb_sdk.wandb_setup._WandbSetup._instance
wandb.teardown()
assert wandb.env.SERVICE not in os.environ
assert not wandb.wandb_sdk.wandb_setup._WandbSetup._instance
_, err = capsys.readouterr()
assert (
"While tearing down the service manager. The following error has occured:"
in err
)
# TODO: test these or make sure they are tested somewhere
# run.use_artifact()
# run.log_artifact()