|
| 1 | +# Copyright 2024 Bytedance Ltd. and/or its affiliates |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from unittest.mock import patch |
| 17 | + |
| 18 | +from verl.utils.tracking import _MlflowLoggingAdapter |
| 19 | + |
| 20 | + |
| 21 | +class TestMlflowLoggingAdapter(unittest.TestCase): |
| 22 | + def test_sanitize_key_and_warning(self): |
| 23 | + adapter = _MlflowLoggingAdapter() |
| 24 | + data = {"valid_key": 1.0, "invalid@key!": 2.0, "another/valid-key": 3.0, "bad key#": 4.0} |
| 25 | + # Patch mlflow.log_metrics to capture the metrics actually sent |
| 26 | + with ( |
| 27 | + patch("mlflow.log_metrics") as mock_log_metrics, |
| 28 | + patch.object(adapter, "logger") as mock_logger, |
| 29 | + ): |
| 30 | + adapter.log(data, step=5) |
| 31 | + # Check that keys are sanitized |
| 32 | + sent_metrics = mock_log_metrics.call_args[1]["metrics"] |
| 33 | + self.assertIn("invalid_at_key_", sent_metrics) # @ becomes _at_, ! becomes _ |
| 34 | + self.assertIn("bad key_", sent_metrics) # # becomes _, space remains |
| 35 | + self.assertNotIn("invalid@key!", sent_metrics) |
| 36 | + self.assertNotIn("bad key#", sent_metrics) |
| 37 | + # Check that a warning was logged for each sanitized key |
| 38 | + warning_msgs = [str(call) for call in mock_logger.warning.call_args_list] |
| 39 | + self.assertTrue(any("invalid@key!" in msg and "invalid_at_key_" in msg for msg in warning_msgs)) |
| 40 | + self.assertTrue(any("bad key#" in msg and "bad key_" in msg for msg in warning_msgs)) |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + unittest.main() |
0 commit comments