Skip to content

Commit 97d048a

Browse files
authored
hparams: account for proto nondeterminism in tests (#2235)
Summary: This test seems to be fine in open-source land, but failed 18/32 times within Google, because the wire format order for maps is undefined: <https://developers.google.com/protocol-buffers/docs/proto3#maps> Test Plan: Within google3, cherry-pick this patch onto <http://cl/247655537>, then run `:summary_v2_notf_test` with `--runs_per_test=128` to check for flakiness. wchargin-branch: hparams-test-deflake
1 parent cb4b1ba commit 97d048a

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

tensorboard/plugins/hparams/summary_v2_test.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ def test_pb_is_tensorboard_copy_of_proto(self):
149149
if tf is not None:
150150
self.assertNotIsInstance(result, tf.compat.v1.Summary)
151151

152+
def assert_hparams_summaries_equal(self, summary_1, summary_2):
153+
def canonical(summary):
154+
"""Return a canonical form for `summary`.
155+
156+
The result is such that `canonical(a) == canonical(b)` if and only
157+
if `a` and `b` are logically equivalent.
158+
159+
Args:
160+
summary: A `summary_pb2.Summary` containing hparams plugin data.
161+
"""
162+
new_summary = summary_pb2.Summary()
163+
new_summary.MergeFrom(summary)
164+
values = new_summary.value
165+
self.assertEqual(len(values), 1, values)
166+
value = values[0]
167+
raw_content = value.metadata.plugin_data.content
168+
value.metadata.plugin_data.content = b"<snipped>"
169+
content = plugin_data_pb2.HParamsPluginData.FromString(raw_content)
170+
return (new_summary, content)
171+
172+
self.assertEqual(canonical(summary_1), canonical(summary_2))
173+
152174
def test_consistency_across_string_key_and_object_key(self):
153175
hparams_1 = {
154176
hp.HParam("optimizer", hp.Discrete(["adam", "sgd"])): "adam",
@@ -158,7 +180,7 @@ def test_consistency_across_string_key_and_object_key(self):
158180
"optimizer": "adam",
159181
hp.HParam("learning_rate", hp.RealInterval(1e-2, 1e-1)): 0.02,
160182
}
161-
self.assertEqual(
183+
self.assert_hparams_summaries_equal(
162184
hp.hparams_pb(hparams_1, start_time_secs=self.start_time_secs),
163185
hp.hparams_pb(hparams_2, start_time_secs=self.start_time_secs),
164186
)
@@ -191,7 +213,7 @@ def test_invariant_under_permutation(self):
191213
"learning_rate": 0.02,
192214
"optimizer": "adam",
193215
}
194-
self.assertEqual(
216+
self.assert_hparams_summaries_equal(
195217
hp.hparams_pb(hparams_1, start_time_secs=self.start_time_secs),
196218
hp.hparams_pb(hparams_2, start_time_secs=self.start_time_secs),
197219
)

0 commit comments

Comments
 (0)