From 4a0e0821e964e7146eed7fcd1af2d67d9bdcc51a Mon Sep 17 00:00:00 2001 From: Ramnath0521 Date: Mon, 16 Nov 2026 19:06:25 +0530 Subject: [PATCH] hparams: strip a trailing path separator from the metric run name on Windows With an empty metric group, os.path.join(session_name, "") ends in a backslash on Windows. Only a trailing "/" was removed, so the run name did not match any run in the multiplexer and the HParams dashboard showed null for every metric. Remove a trailing os.sep as well. Fixes #7175 Co-Authored-By: Claude Sonnet 5.5 --- tensorboard/plugins/hparams/BUILD | 12 ++++ tensorboard/plugins/hparams/metrics.py | 9 +-- tensorboard/plugins/hparams/metrics_test.py | 80 +++++++++++++++++++++ 3 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 tensorboard/plugins/hparams/metrics_test.py diff --git a/tensorboard/plugins/hparams/BUILD b/tensorboard/plugins/hparams/BUILD index 43f3273a123..f82b10d2de2 100644 --- a/tensorboard/plugins/hparams/BUILD +++ b/tensorboard/plugins/hparams/BUILD @@ -89,6 +89,18 @@ py_test( ], ) +py_test( + name = "metrics_test", + size = "small", + srcs = [ + "metrics_test.py", + ], + deps = [ + ":hparams_plugin", + "//tensorboard:test", + ], +) + py_test( name = "list_metric_evals_test", size = "small", diff --git a/tensorboard/plugins/hparams/metrics.py b/tensorboard/plugins/hparams/metrics.py index 435c40321ec..500173885d6 100644 --- a/tensorboard/plugins/hparams/metrics.py +++ b/tensorboard/plugins/hparams/metrics.py @@ -32,11 +32,12 @@ def run_tag_from_session_and_metric(session_name, metric_name): """ assert isinstance(session_name, str) assert isinstance(metric_name, api_pb2.MetricName) - # os.path.join() will append a final slash if the group is empty; it seems - # like multiplexer.Tensors won't recognize paths that end with a '/' so - # we remove the final '/' in that case. + # os.path.join() will append a final separator if the group is empty; it + # seems like multiplexer.Tensors won't recognize paths that end with one, + # so we remove it in that case. The separator is '\' on Windows, and a + # session name read from a cloud path can still end with a '/' there. run = os.path.join(session_name, metric_name.group) - if run.endswith("/"): + if run.endswith(("/", os.sep)): run = run[:-1] tag = metric_name.tag return run, tag diff --git a/tensorboard/plugins/hparams/metrics_test.py b/tensorboard/plugins/hparams/metrics_test.py new file mode 100644 index 00000000000..9762b33f4b8 --- /dev/null +++ b/tensorboard/plugins/hparams/metrics_test.py @@ -0,0 +1,80 @@ +# Copyright 2026 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. +# ============================================================================== +"""Tests for the hparams plugin's `metrics` module.""" + +import ntpath +import posixpath +from unittest import mock + +from tensorboard import test as tb_test +from tensorboard.plugins.hparams import api_pb2 +from tensorboard.plugins.hparams import metrics + + +def _metric(group, tag): + return api_pb2.MetricName(group=group, tag=tag) + + +class RunTagFromSessionAndMetricTest(tb_test.TestCase): + def _run_tag(self, path_module, session_name, group, tag): + # Run names are built with the separator of the platform the logdir is + # read on, so a Windows run is "Model001\train" and a POSIX run is + # "Model001/train". Patching `os` lets both be checked on one machine. + with mock.patch.object( + metrics.os, "path", path_module + ), mock.patch.object(metrics.os, "sep", path_module.sep): + return metrics.run_tag_from_session_and_metric( + session_name, _metric(group, tag) + ) + + def test_empty_group_posix(self): + self.assertEqual( + ("Model001", "loss"), + self._run_tag(posixpath, "Model001", "", "loss"), + ) + + def test_empty_group_windows(self): + self.assertEqual( + ("Model001", "loss"), + self._run_tag(ntpath, "Model001", "", "loss"), + ) + + def test_empty_group_windows_nested_session(self): + self.assertEqual( + ("logs\\Model001", "loss"), + self._run_tag(ntpath, "logs\\Model001", "", "loss"), + ) + + def test_empty_group_session_with_trailing_forward_slash(self): + self.assertEqual( + ("gs://bucket/Model001", "loss"), + self._run_tag(ntpath, "gs://bucket/Model001/", "", "loss"), + ) + + def test_group_is_joined_posix(self): + self.assertEqual( + ("Model001/train", "loss"), + self._run_tag(posixpath, "Model001", "train", "loss"), + ) + + def test_group_is_joined_windows(self): + self.assertEqual( + ("Model001\\train", "loss"), + self._run_tag(ntpath, "Model001", "train", "loss"), + ) + + +if __name__ == "__main__": + tb_test.main()