-
Notifications
You must be signed in to change notification settings - Fork 618
/
test_wandb_artifacts_full.py
248 lines (192 loc) 路 8.83 KB
/
test_wandb_artifacts_full.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
import time
from datetime import datetime, timedelta, timezone
import numpy as np
import pytest
import wandb
from wandb.errors import WaitTimeoutError
sm = wandb.wandb_sdk.internal.sender.SendManager
def test_add_table_from_dataframe(wandb_init):
import pandas as pd
df_float = pd.DataFrame([[1, 2.0, 3.0]], dtype=np.float)
df_float32 = pd.DataFrame([[1, 2.0, 3.0]], dtype=np.float32)
df_bool = pd.DataFrame([[True, False, True]], dtype=np.bool)
current_time = datetime.now()
df_timestamp = pd.DataFrame(
[[current_time + timedelta(days=i)] for i in range(10)], columns=["date"]
)
wb_table_float = wandb.Table(dataframe=df_float)
wb_table_float32 = wandb.Table(dataframe=df_float32)
wb_table_float32_recast = wandb.Table(dataframe=df_float32.astype(np.float))
wb_table_bool = wandb.Table(dataframe=df_bool)
wb_table_timestamp = wandb.Table(dataframe=df_timestamp)
run = wandb_init()
artifact = wandb.Artifact("table-example", "dataset")
artifact.add(wb_table_float, "wb_table_float")
artifact.add(wb_table_float32_recast, "wb_table_float32_recast")
artifact.add(wb_table_float32, "wb_table_float32")
artifact.add(wb_table_bool, "wb_table_bool")
# check that timestamp is correctly converted to ms and not ns
json_repr = wb_table_timestamp.to_json(artifact)
assert "data" in json_repr and np.isclose(
json_repr["data"][0][0],
current_time.replace(tzinfo=timezone.utc).timestamp() * 1000,
)
artifact.add(wb_table_timestamp, "wb_table_timestamp")
run.log_artifact(artifact)
run.finish()
def test_artifact_error_for_invalid_aliases(wandb_init):
run = wandb_init()
artifact = wandb.Artifact("test-artifact", "dataset")
error_aliases = [["latest", "workflow:boom"], ["workflow/boom/test"]]
for aliases in error_aliases:
with pytest.raises(ValueError) as e_info:
run.log_artifact(artifact, aliases=aliases)
assert (
str(e_info.value)
== "Aliases must not contain any of the following characters: /, :"
)
for aliases in [["latest", "boom_test-q"]]:
run.log_artifact(artifact, aliases=aliases)
run.finish()
def test_artifact_upsert_no_id(wandb_init):
# NOTE: these tests are against a mock server so they are testing the internal flows, but
# not the actual data transfer.
artifact_name = f"distributed_artifact_{round(time.time())}"
artifact_type = "dataset"
# Upsert without a group or id should fail
run = wandb_init()
artifact = wandb.Artifact(name=artifact_name, type=artifact_type)
image = wandb.Image(np.random.randint(0, 255, (10, 10)))
artifact.add(image, "image_1")
with pytest.raises(TypeError):
run.upsert_artifact(artifact)
run.finish()
def test_artifact_upsert_group_id(wandb_init):
# NOTE: these tests are against a mock server so they are testing the internal flows, but
# not the actual data transfer.
artifact_name = f"distributed_artifact_{round(time.time())}"
group_name = f"test_group_{round(np.random.rand())}"
artifact_type = "dataset"
# Upsert with a group should succeed
run = wandb_init(group=group_name)
artifact = wandb.Artifact(name=artifact_name, type=artifact_type)
image = wandb.Image(np.random.randint(0, 255, (10, 10)))
artifact.add(image, "image_1")
run.upsert_artifact(artifact)
run.finish()
def test_artifact_upsert_distributed_id(wandb_init):
# NOTE: these tests are against a mock server so they are testing the internal flows, but
# not the actual data transfer.
artifact_name = f"distributed_artifact_{round(time.time())}"
group_name = f"test_group_{round(np.random.rand())}"
artifact_type = "dataset"
# Upsert with a distributed_id should succeed
run = wandb_init()
artifact = wandb.Artifact(name=artifact_name, type=artifact_type)
image = wandb.Image(np.random.randint(0, 255, (10, 10)))
artifact.add(image, "image_2")
run.upsert_artifact(artifact, distributed_id=group_name)
run.finish()
def test_artifact_finish_no_id(wandb_init):
# NOTE: these tests are against a mock server so they are testing the internal flows, but
# not the actual data transfer.
artifact_name = f"distributed_artifact_{round(time.time())}"
artifact_type = "dataset"
# Finish without a distributed_id should fail
run = wandb_init()
artifact = wandb.Artifact(artifact_name, type=artifact_type)
with pytest.raises(TypeError):
run.finish_artifact(artifact)
run.finish()
def test_artifact_finish_group_id(wandb_init):
# NOTE: these tests are against a mock server so they are testing the internal flows, but
# not the actual data transfer.
artifact_name = f"distributed_artifact_{round(time.time())}"
group_name = f"test_group_{round(np.random.rand())}"
artifact_type = "dataset"
# Finish with a distributed_id should succeed
run = wandb_init(group=group_name)
artifact = wandb.Artifact(artifact_name, type=artifact_type)
run.finish_artifact(artifact)
run.finish()
def test_artifact_finish_distributed_id(wandb_init):
# NOTE: these tests are against a mock server so they are testing the internal flows, but
# not the actual data transfer.
artifact_name = f"distributed_artifact_{round(time.time())}"
group_name = f"test_group_{round(np.random.rand())}"
artifact_type = "dataset"
# Finish with a distributed_id should succeed
run = wandb_init()
artifact = wandb.Artifact(artifact_name, type=artifact_type)
run.finish_artifact(artifact, distributed_id=group_name)
run.finish()
# this test hangs, which seems to be the result of incomplete mocks.
# would be worth returning to it in the future
# def test_artifact_incremental( relay_server, parse_ctx, test_settings):
#
# open("file1.txt", "w").write("hello")
# run = wandb.init(settings=test_settings)
# artifact = wandb.Artifact(type="dataset", name="incremental_test_PENDING", incremental=True)
# artifact.add_file("file1.txt")
# run.log_artifact(artifact)
# run.finish()
# manifests_created = parse_ctx(relay_server.get_ctx()).manifests_created
# assert manifests_created[0]["type"] == "INCREMENTAL"
def test_local_references(wandb_init):
run = wandb_init()
def make_table():
return wandb.Table(columns=[], data=[])
t1 = make_table()
artifact1 = wandb.Artifact("test_local_references", "dataset")
artifact1.add(t1, "t1")
assert artifact1.manifest.entries["t1.table.json"].ref is None
run.log_artifact(artifact1)
artifact2 = wandb.Artifact("test_local_references_2", "dataset")
artifact2.add(t1, "t2")
assert artifact2.manifest.entries["t2.table.json"].ref is not None
run.finish()
def test_artifact_wait_success(wandb_init):
# Test artifact wait() timeout parameter
timeout = 60
leeway = 0.50
run = wandb_init()
artifact = wandb.Artifact("art", type="dataset")
start_timestamp = time.time()
run.log_artifact(artifact).wait(timeout=timeout)
elapsed_time = time.time() - start_timestamp
assert elapsed_time < timeout * (1 + leeway)
run.finish()
@pytest.mark.parametrize("timeout", [0, 1e-6])
def test_artifact_wait_failure(wandb_init, timeout):
# Test to expect WaitTimeoutError when wait timeout is reached and large image
# wasn't uploaded yet
image = wandb.Image(np.random.randint(0, 255, (10, 10)))
run = wandb_init()
with pytest.raises(WaitTimeoutError):
artifact = wandb.Artifact("art", type="image")
artifact.add(image, "image")
run.log_artifact(artifact).wait(timeout=timeout)
run.finish()
@pytest.mark.xfail(
reason="TODO(spencerpearson): this test passes locally, but flakes in CI. After much investigation, I still have no clue.",
# examples of flakes:
# https://app.circleci.com/pipelines/github/wandb/wandb/16334/workflows/319d3e58-853e-46ec-8a3f-088cac41351c/jobs/325741/tests#failed-test-0
# https://app.circleci.com/pipelines/github/wandb/wandb/16392/workflows/b26b3e63-c8d8-45f4-b7db-00f84b11f8b8/jobs/327312
)
def test_artifact_metadata_save(wandb_init, relay_server):
# Test artifact metadata sucessfully saved for len(numpy) > 32
dummy_metadata = np.array([0] * 33)
with relay_server():
run = wandb_init()
artifact = wandb.Artifact(
name="art", type="dataset", metadata={"initMetadata": dummy_metadata}
)
run.log_artifact(artifact)
artifact.wait().metadata.update({"updateMetadata": dummy_metadata})
artifact.save()
saved_artifact = run.use_artifact("art:latest")
art_metadata = saved_artifact.metadata
assert "initMetadata" in art_metadata
assert "updateMetadata" in art_metadata
assert art_metadata["initMetadata"] == art_metadata["updateMetadata"]
run.finish()