forked from tensorflow/tensorboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatching_provider_test.py
324 lines (291 loc) · 11.1 KB
/
dispatching_provider_test.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
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Unit tests for `tensorboard.data.dispatching_provider`."""
import base64
from tensorboard import errors
from tensorboard import context
from tensorboard import test as tb_test
from tensorboard.data import dispatching_provider
from tensorboard.data import provider
class PlaceholderDataProvider(provider.DataProvider):
"""Data provider with parameterized test data."""
def __init__(self, name, eids):
self._name = name
self._eids = eids
def _validate_eid(self, eid):
if eid not in self._eids:
raise errors.NotFoundError("%r not in %r" % (eid, self._eids))
def data_location(self, ctx, *, experiment_id):
self._validate_eid(experiment_id)
return "%s://%s" % (self._name, experiment_id)
def experiment_metadata(self, ctx, *, experiment_id):
return None
def list_plugins(self, ctx, *, experiment_id):
self._validate_eid(experiment_id)
return ["%s_a" % experiment_id, "%s_b" % experiment_id]
def list_runs(self, ctx, *, experiment_id):
self._validate_eid(experiment_id)
return ["%s/train" % experiment_id, "%s/test" % experiment_id]
def list_scalars(
self, ctx, *, experiment_id, plugin_name, run_tag_filter=None
):
self._validate_eid(experiment_id)
run_name = "%s/train" % experiment_id
tag_name = "loss.%s" % plugin_name
return {
run_name: {
tag_name: provider.ScalarTimeSeries(
max_step=2,
max_wall_time=0.5,
plugin_content=b"",
description="Hello from %s" % self._name,
display_name="loss",
)
}
}
def read_scalars(
self,
ctx,
*,
experiment_id,
plugin_name,
downsample=None,
run_tag_filter=None,
):
self._validate_eid(experiment_id)
if run_tag_filter is None:
run_tag_filter = provider.RunTagFilter()
rtf = run_tag_filter
expected_run = "%s/train" % experiment_id
expected_tag = "loss.%s" % plugin_name
if rtf.runs is not None and expected_run not in rtf.runs:
return {}
if rtf.tags is not None and expected_tag not in rtf.tags:
return {}
return {
expected_run: {
expected_tag: [
provider.ScalarDatum(
step=0, wall_time=0.0, value=float(len(plugin_name))
),
provider.ScalarDatum(
step=1, wall_time=0.5, value=float(len(experiment_id))
),
]
}
}
def list_tensors(
self, ctx, *, experiment_id, plugin_name, run_tag_filter=None
):
# We bravely assume that `list_tensors` and `read_tensors` work
# the same as their scalar counterparts.
raise NotImplementedError()
def read_tensors(
self,
ctx,
*,
experiment_id,
plugin_name,
downsample=None,
run_tag_filter=None,
):
raise NotImplementedError()
def list_blob_sequences(
self, ctx, *, experiment_id, plugin_name, run_tag_filter=None
):
self._validate_eid(experiment_id)
run_name = "%s/test" % experiment_id
tag_name = "input.%s" % plugin_name
return {
run_name: {
tag_name: provider.BlobSequenceTimeSeries(
max_step=0,
max_wall_time=0.0,
max_length=2,
plugin_content=b"",
description="Greetings via %s" % self._name,
display_name="input",
)
}
}
def read_blob_sequences(
self,
ctx,
*,
experiment_id,
plugin_name,
downsample=None,
run_tag_filter=None,
):
self._validate_eid(experiment_id)
if run_tag_filter is None:
run_tag_filter = provider.RunTagFilter()
rtf = run_tag_filter
expected_run = "%s/test" % experiment_id
expected_tag = "input.%s" % plugin_name
if rtf.runs is not None and expected_run not in rtf.runs:
return {}
if rtf.tags is not None and expected_tag not in rtf.tags:
return {}
return {
expected_run: {
expected_tag: [
provider.BlobSequenceDatum(
step=0,
wall_time=0.0,
values=[
self._make_blob_reference(
"experiment: %s" % experiment_id
),
self._make_blob_reference("name: %s" % self._name),
],
),
]
}
}
def _make_blob_reference(self, text):
key = base64.urlsafe_b64encode(
("%s:%s" % (self._name, text)).encode("utf-8")
).decode("ascii")
return provider.BlobReference(key)
def read_blob(self, ctx, *, blob_key):
payload = base64.urlsafe_b64decode(blob_key)
prefix = ("%s:" % self._name).encode("utf-8")
if not payload.startswith(prefix):
raise errors.NotFound("not %r.startswith(%r)" % (payload, prefix))
return payload[len(prefix) :]
class DispatchingDataProviderTest(tb_test.TestCase):
def setUp(self):
self.foo_provider = PlaceholderDataProvider("foo", ["123", "456"])
self.bar_provider = PlaceholderDataProvider("Bar", ["a:b:c", "@xyz@"])
self.baz_provider = PlaceholderDataProvider("BAZ", ["baz"])
providers = {"foo": self.foo_provider, "bar": self.bar_provider}
unprefixed = self.baz_provider
self.with_unpfx = dispatching_provider.DispatchingDataProvider(
providers, unprefixed_provider=unprefixed
)
self.without_unpfx = dispatching_provider.DispatchingDataProvider(
providers
)
def test_data_location(self):
self.assertEqual(
self.with_unpfx.data_location(_ctx(), experiment_id="foo:123"),
self.foo_provider.data_location(_ctx(), experiment_id="123"),
)
self.assertEqual(
self.with_unpfx.data_location(_ctx(), experiment_id="bar:a:b:c"),
self.bar_provider.data_location(_ctx(), experiment_id="a:b:c"),
)
self.assertEqual(
self.with_unpfx.data_location(_ctx(), experiment_id="baz"),
self.baz_provider.data_location(_ctx(), experiment_id="baz"),
)
with self.assertRaisesRegex(
errors.NotFoundError, "Unknown prefix in experiment ID: 'quux:hmm'"
):
self.with_unpfx.data_location(_ctx(), experiment_id="quux:hmm")
with self.assertRaisesRegex(
errors.NotFoundError,
"No data provider found for unprefixed experiment ID: 'quux'",
):
self.without_unpfx.data_location(_ctx(), experiment_id="quux")
def test_scalars(self):
listing = self.with_unpfx.list_scalars(
_ctx(), experiment_id="foo:123", plugin_name="scalars"
)
self.assertEqual(
listing,
self.foo_provider.list_scalars(
_ctx(), experiment_id="123", plugin_name="scalars"
),
)
reading = self.with_unpfx.read_scalars(
_ctx(),
experiment_id="foo:123",
plugin_name="scalars",
downsample=1000,
run_tag_filter=provider.RunTagFilter(
["123/train"], ["loss.scalars"]
),
)
expected_reading = self.foo_provider.read_scalars(
_ctx(),
experiment_id="123",
plugin_name="scalars",
downsample=1000,
run_tag_filter=provider.RunTagFilter(
["123/train"], ["loss.scalars"]
),
)
self.assertNotEmpty(expected_reading)
self.assertEqual(reading, expected_reading)
def _get_blobs(self, data_provider, experiment_id):
"""Read and fetch all blobs for an experiment."""
reading = data_provider.read_blob_sequences(
_ctx(),
experiment_id=experiment_id,
plugin_name="images",
downsample=10,
run_tag_filter=provider.RunTagFilter(),
)
result = {}
for run in reading:
result[run] = {}
for tag in reading[run]:
result[run][tag] = []
for datum in reading[run][tag]:
blob_values = [
data_provider.read_blob(_ctx(), blob_key=ref.blob_key)
for ref in datum.values
]
result[run][tag].append(blob_values)
return result
def test_blob_sequences_prefixed(self):
listing = self.with_unpfx.list_blob_sequences(
_ctx(), experiment_id="bar:a:b:c", plugin_name="images"
)
expected_listing = self.bar_provider.list_blob_sequences(
_ctx(), experiment_id="a:b:c", plugin_name="images"
)
self.assertEqual(listing, expected_listing)
blobs = self._get_blobs(self.with_unpfx, "bar:a:b:c")
expected_blobs = self._get_blobs(self.bar_provider, "a:b:c")
self.assertEqual(blobs, expected_blobs)
def test_blob_sequences_unprefixed(self):
listing = self.with_unpfx.list_blob_sequences(
_ctx(), experiment_id="baz", plugin_name="images"
)
expected_listing = self.baz_provider.list_blob_sequences(
_ctx(), experiment_id="baz", plugin_name="images"
)
self.assertEqual(listing, expected_listing)
blobs = self._get_blobs(self.with_unpfx, "baz")
expected_blobs = self._get_blobs(self.baz_provider, "baz")
self.assertEqual(blobs, expected_blobs)
def test_blobs_error_cases(self):
with self.assertRaisesRegex(
errors.NotFoundError,
"Unknown prefix in experiment ID: 'quux:hmm'",
):
self._get_blobs(self.with_unpfx, "quux:hmm")
with self.assertRaisesRegex(
errors.NotFoundError,
"No data provider found for unprefixed experiment ID: 'baz'",
):
result = self._get_blobs(self.without_unpfx, "baz")
def _ctx():
return context.RequestContext()
if __name__ == "__main__":
tb_test.main()