Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_wandb.py 4.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. import argparse
  2. import pytest
  3. import os
  4. import sys
  5. import os
  6. import textwrap
  7. import yaml
  8. import mock
  9. import glob
  10. import socket
  11. from .api_mocks import *
  12. import wandb
  13. @pytest.fixture
  14. def wandb_init_run(request, tmpdir, request_mocker, upsert_run, query_run_resume_status, upload_logs, monkeypatch):
  15. """Fixture that calls wandb.init(), yields the run that
  16. gets created, then cleans up afterward.
  17. """
  18. # save the environment so we can restore it later. pytest
  19. # may actually do this itself. didn't check.
  20. orig_environ = dict(os.environ)
  21. try:
  22. if request.node.get_marker('jupyter'):
  23. upsert_run(request_mocker)
  24. query_run_resume_status(request_mocker)
  25. def get_ipython():
  26. class Jupyter(object):
  27. __module__ = "jupyter"
  28. def __init__(self):
  29. class Hook(object):
  30. def register(self, what, where):
  31. pass
  32. self.events = Hook()
  33. def register_magics(self, magic):
  34. pass
  35. return Jupyter()
  36. wandb.get_ipython = get_ipython
  37. # no i/o wrapping - it breaks pytest
  38. os.environ['WANDB_MODE'] = 'clirun'
  39. if not request.node.get_marker('unconfigured'):
  40. os.environ['WANDB_API_KEY'] = 'test'
  41. os.environ['WANDB_ENTITY'] = 'test'
  42. os.environ['WANDB_PROJECT'] = 'unit-test-project'
  43. os.environ['WANDB_RUN_DIR'] = str(tmpdir)
  44. assert wandb.run is None
  45. assert wandb.config is None
  46. orig_namespace = vars(wandb)
  47. if request.node.get_marker('args'):
  48. kwargs = request.node.get_marker('args').kwargs
  49. # Unfortunate to enable the test to work
  50. if kwargs.get("dir"):
  51. del os.environ['WANDB_RUN_DIR']
  52. if kwargs.get("error"):
  53. err = kwargs["error"]
  54. del kwargs['error']
  55. if err == "io":
  56. @classmethod
  57. def error(cls):
  58. raise IOError
  59. monkeypatch.setattr(
  60. 'wandb.wandb_run.Run.from_environment_or_defaults', error)
  61. elif err == "socket":
  62. class Error(object):
  63. def listen(self, secs):
  64. return False, None
  65. monkeypatch.setattr("wandb.wandb_socket.Server", Error)
  66. else:
  67. kwargs = {}
  68. try:
  69. run = wandb.init(**kwargs)
  70. upload_logs(request_mocker, run)
  71. assert run is wandb.run
  72. assert run.config is wandb.config
  73. except wandb.LaunchError as e:
  74. run = e
  75. yield run
  76. wandb.uninit()
  77. assert vars(wandb) == orig_namespace
  78. finally:
  79. # restore the original environment
  80. os.environ.clear()
  81. os.environ.update(orig_environ)
  82. def test_log(wandb_init_run):
  83. history_row = {'stuff': 5}
  84. wandb.log(history_row)
  85. assert len(wandb.run.history.rows) == 1
  86. assert set(history_row.items()) <= set(wandb.run.history.rows[0].items())
  87. def test_log_step(wandb_init_run):
  88. history_row = {'stuff': 5}
  89. wandb.log(history_row, step=5)
  90. wandb.log()
  91. assert len(wandb.run.history.rows) == 1
  92. assert wandb.run.history.rows[0]['_step'] == 5
  93. @pytest.mark.args(error="io")
  94. def test_io_error(wandb_init_run):
  95. assert isinstance(wandb_init_run, wandb.LaunchError)
  96. @pytest.mark.skip("Need to figure out the headless fun")
  97. @pytest.mark.args(error="socket")
  98. def test_io_error(wandb_init_run):
  99. assert isinstance(wandb_init_run, wandb.LaunchError)
  100. @pytest.mark.args(dir="/tmp")
  101. def test_custom_dir(wandb_init_run):
  102. assert len(glob.glob("/tmp/wandb/run-*")) > 0
  103. @pytest.mark.jupyter
  104. def test_jupyter_init(wandb_init_run, capfd):
  105. assert os.getenv("WANDB_JUPYTER")
  106. wandb.log({"stat": 1})
  107. out, err = capfd.readouterr()
  108. assert "Resuming" in out
  109. # TODO: saw some global state issues here...
  110. # assert "" == err
  111. @pytest.mark.skip("Can't figure out how to make the test handle input :(")
  112. @pytest.mark.jupyter
  113. @pytest.mark.unconfigured
  114. @mock.patch.object(wandb.Api, 'api_key', None)
  115. @mock.patch(
  116. 'getpass.getpass', lambda *args: '0123456789012345678901234567890123456789\n')
  117. @mock.patch('six.moves.input', lambda *args: 'foo/bar\n')
  118. def test_jupyter_manual_configure(wandb_init_run, capsys):
  119. out, err = capsys.readouterr()
  120. assert "Not authenticated" in err
  121. assert "No W&B project configured" in err
  122. assert "Wrap your training" in out
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...