Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hparams: Treat Nan/Infinity numeric hparam values as unset values. #6496

Merged
merged 2 commits into from
Jul 18, 2023

Conversation

bmd3k
Copy link
Contributor

@bmd3k bmd3k commented Jul 17, 2023

Motivation for features / changes

There are cases where users might log hparam numeric values that are NaN or Infinity. Recall, we use proto's Value message for logging and storing hparam values. While processing these values in the python layer of the Hparams plugin, we may run these Value message through json_format.MessageToJson, at which point the json_format library raises an error.

The limitation is documented at https://protobuf.dev/reference/protobuf/google.protobuf/#value. "[A]ttempting to serialize NaN or Infinity results in error."

Technical description of changes

We now will try to avoid calling MessageToJson on these problematic hparam values. We instead chose to treat these as "unset" values -- equivalent to having not set an hparam value at all. It will mean the values will not contribute to the calculation of discrete domains. And it means that the values will appear blank in the hparams UI rather than appearing as "NaN".

Alternate designs / implementations considered (or N/A)

Given infinite amount of time:

  • Fix json_format to handle NaN and Infinity :O.
  • Change the hparams api.proto to use something other than proto's Value for representing hparam values.

Other reasonable options:

  • Change the NaN numeric value to "NaN" string. This was my first choice but it unfortunately leads to other complications - for instance the plugin will raise errors when it encounters a "NaN" string for an hparam that is supposed to have a numeric value.

@bmd3k bmd3k requested a review from arcra July 17, 2023 16:26
@@ -304,6 +300,11 @@ def _compute_hparam_info_from_values(self, name, values):
return None

if result.type == api_pb2.DATA_TYPE_STRING:
distinct_values = set()
for value in values:
if _protobuf_value_type(value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider extracting into a separate named function, where you can use early returns, to reduce nesting, and so you can use a set comprehension here, like it was before.

Optional. This is fine too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

False, otherwise.
"""
return not value.HasField("number_value") or (
not math.isnan(value.number_value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider using early returns, so the "short circuit" evaluation is more evident:

if not value.HasField("..."):
    return True

val = value.number_value
return not math.isnan(val) and not math.isinf(val)

Again, optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

request = """
start_index: 0
slice_size: 10
allowed_statuses: [STATUS_UNKNOWN]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just an arbitrary value that is irrelevant for the test? Can it be omitted, or is it a required field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya unfortunately it is necessary for the query to work. (allowed_status: [] will return no results).

@bmd3k bmd3k merged commit 07d74ac into tensorflow:master Jul 18, 2023
14 checks passed
bmd3k added a commit that referenced this pull request Jul 18, 2023
#6496 broke the internal import due to missing BUILD dependency for
json_format_compat_test.py.
rileyajones pushed a commit to rileyajones/tensorboard that referenced this pull request Jul 18, 2023
…ensorflow#6496)

## Motivation for features / changes

There are cases where users might log hparam numeric values that are NaN
or Infinity. Recall, we use proto's Value message for logging and
storing hparam values. While processing these values in the python layer
of the Hparams plugin, we may run these Value message through
json_format.MessageToJson, at which point the json_format library raises
an error.

The limitation is documented at
https://protobuf.dev/reference/protobuf/google.protobuf/#value.
"[A]ttempting to serialize NaN or Infinity results in error."

## Technical description of changes

We now will try to avoid calling MessageToJson on these problematic
hparam values. We instead chose to treat these as "unset" values --
equivalent to having not set an hparam value at all. It will mean the
values will not contribute to the calculation of discrete domains. And
it means that the values will appear blank in the hparams UI rather than
appearing as "NaN".

## Alternate designs / implementations considered (or N/A)

Given infinite amount of time:
* Fix json_format to handle NaN and Infinity :O.
* Change the hparams api.proto to use something other than proto's Value
for representing hparam values.

Other reasonable options:
* Change the NaN numeric value to "NaN" string. This was my first choice
but it unfortunately leads to other complications - for instance the
plugin will raise errors when it encounters a "NaN" string for an hparam
that is supposed to have a numeric value.
rileyajones pushed a commit to rileyajones/tensorboard that referenced this pull request Jul 18, 2023
tensorflow#6496 broke the internal import due to missing BUILD dependency for
json_format_compat_test.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants