Skip to content

Commit 0627ad5

Browse files
authored
Relax protobuf<5.0.0 restriction (#6888)
Instead of restricting `protobuf<5.0.0` due to the removal to `including_default_value_fields` kwarg in `json_format.MessageToJson` (replaced by `always_print_fields_with_nopresence`), we can instead detect the currently installed protobuf version and use the appropriate kwarg. Contributes to #6808 and should resolve #6887.
1 parent 32e9e95 commit 0627ad5

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

tensorboard/pip_package/requirements.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ absl-py >= 0.4
2020
grpcio >= 1.48.2
2121
markdown >= 2.6.8
2222
numpy >= 1.12.0
23+
# NOTE: The packaging dependency was introduced in order to compare installed
24+
# package versions and conditionally use different supported kwargs
25+
# (specifically the protobuf dependency). If we restrict protobuf >= 5.0.0 we
26+
# can get rid of the packaging dependency.
27+
packaging
2328
# NOTE: this version must be >= the protoc version in our WORKSPACE file.
2429
# At the same time, any constraints we specify here must allow at least some
2530
# version to be installed that is also compatible with TensorFlow's constraints:
2631
# https://github.com/tensorflow/tensorflow/blob/25adc4fccb4b0bb5a933eba1d246380e7b87d7f7/tensorflow/tools/pip_package/setup.py#L101
2732
# 4.24.0 had an issue that broke our tests, so we should avoid that release:
2833
# https://github.com/protocolbuffers/protobuf/issues/13485
29-
# 5.26.0 introduced a breaking change, so we restricted it for now. See issue #6808 for details.
30-
protobuf >= 3.19.6, != 4.24.0, < 5.0.0
34+
protobuf >= 3.19.6, != 4.24.0
3135
setuptools >= 41.0.0 # Note: provides pkg_resources as well as setuptools
3236
# A dependency of our vendored packages. This lower bound has not been correctly
3337
# vetted, but I wanted to be the least restrictive we can, since it's not a new

tensorboard/plugin_util.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# ==============================================================================
1515
"""Provides utilities that may be especially useful to plugins."""
1616

17-
17+
from google.protobuf import json_format
18+
from importlib import metadata
19+
from packaging import version
1820
import threading
1921

2022
from bleach.sanitizer import Cleaner
@@ -193,6 +195,24 @@ def experiment_id(environ):
193195
return environ.get(_experiment_id.WSGI_ENVIRON_KEY, "")
194196

195197

198+
def proto_to_json(proto):
199+
"""Utility method to convert proto to JSON, accounting for different version support.
200+
201+
Args:
202+
proto: The proto to convert to JSON.
203+
"""
204+
current_version = metadata.version("protobuf")
205+
if version.parse(current_version) >= version.parse("5.0.0"):
206+
return json_format.MessageToJson(
207+
proto,
208+
always_print_fields_with_no_presence=True,
209+
)
210+
return json_format.MessageToJson(
211+
proto,
212+
including_default_value_fields=True,
213+
)
214+
215+
196216
class _MetadataVersionChecker:
197217
"""TensorBoard-internal utility for warning when data is too new.
198218

tensorboard/plugins/custom_scalar/custom_scalars_plugin.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
this plugin.
2222
"""
2323

24-
2524
import re
2625

27-
from google.protobuf import json_format
28-
2926
from werkzeug import wrappers
3027

3128
from tensorboard import plugin_util
@@ -318,9 +315,7 @@ def layout_impl(self, ctx, experiment):
318315
title_to_category[category.title] = category
319316

320317
if merged_layout:
321-
return json_format.MessageToJson(
322-
merged_layout, including_default_value_fields=True
323-
)
318+
return plugin_util.proto_to_json(merged_layout)
324319
else:
325320
# No layout was found.
326321
return {}

tensorboard/plugins/hparams/hparams_plugin.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
this plugin.
1919
"""
2020

21-
2221
import json
2322

24-
2523
import werkzeug
2624
from werkzeug import wrappers
2725

@@ -116,14 +114,16 @@ def get_experiment_route(self, request):
116114
request_proto = _parse_request_argument(
117115
request, api_pb2.GetExperimentRequest
118116
)
117+
response_proto = get_experiment.Handler(
118+
ctx,
119+
self._context,
120+
experiment_id,
121+
request_proto,
122+
).run()
123+
response = plugin_util.proto_to_json(response_proto)
119124
return http_util.Respond(
120125
request,
121-
json_format.MessageToJson(
122-
get_experiment.Handler(
123-
ctx, self._context, experiment_id, request_proto
124-
).run(),
125-
including_default_value_fields=True,
126-
),
126+
response,
127127
"application/json",
128128
)
129129
except error.HParamsError as e:
@@ -139,14 +139,16 @@ def list_session_groups_route(self, request):
139139
request_proto = _parse_request_argument(
140140
request, api_pb2.ListSessionGroupsRequest
141141
)
142+
response_proto = list_session_groups.Handler(
143+
ctx,
144+
self._context,
145+
experiment_id,
146+
request_proto,
147+
).run()
148+
response = plugin_util.proto_to_json(response_proto)
142149
return http_util.Respond(
143150
request,
144-
json_format.MessageToJson(
145-
list_session_groups.Handler(
146-
ctx, self._context, experiment_id, request_proto
147-
).run(),
148-
including_default_value_fields=True,
149-
),
151+
response,
150152
"application/json",
151153
)
152154
except error.HParamsError as e:

0 commit comments

Comments
 (0)