|
| 1 | +# Copyright 2024 The Scenic Authors. |
| 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 | +"""Unit tests for training utility functions in train_lib.train_utils. |
| 16 | +
|
| 17 | +This file covers tests for the Chrono context manager. |
| 18 | +""" |
| 19 | + |
| 20 | +from unittest import mock |
| 21 | + |
| 22 | +from absl.testing import absltest |
| 23 | +from scenic.train_lib import train_utils |
| 24 | + |
| 25 | + |
| 26 | +class ChronoPausedTest(absltest.TestCase): |
| 27 | + """Tests the Chrono.paused context manager for correct behavior.""" |
| 28 | + |
| 29 | + @mock.patch("jax.block_until_ready", autospec=True) |
| 30 | + @mock.patch("time.monotonic") |
| 31 | + def test_paused_context_manager_waits_executes_the_code_block_and_resumes( |
| 32 | + self, mock_monotonic, mock_block_until_ready |
| 33 | + ): |
| 34 | + """Tests the Chrono.paused context manager in a normal flow.""" |
| 35 | + chrono = train_utils.Chrono() |
| 36 | + before_pause, after_pause, after_resume = 100.0, 101.1, 105.5 |
| 37 | + mock_monotonic.side_effect = [before_pause, after_pause, after_resume] |
| 38 | + wait_for_ops = [mock.MagicMock()] # Dummy operations to await. |
| 39 | + |
| 40 | + with chrono.paused(wait_for=wait_for_ops): |
| 41 | + mock_block_until_ready.assert_called_once_with(wait_for_ops) |
| 42 | + self.assertEqual(chrono.pause_start, before_pause) |
| 43 | + |
| 44 | + self.assertIsNone(chrono.pause_start) # Should be reset by resume |
| 45 | + self.assertEqual(chrono.paused_time, after_pause - before_pause) |
| 46 | + self.assertEqual(mock_monotonic.call_count, 3) # init, pause, and resume |
| 47 | + |
| 48 | + @mock.patch("jax.block_until_ready", autospec=True) |
| 49 | + @mock.patch("time.monotonic") |
| 50 | + def test_paused_context_manager_with_exception_calls_resume( |
| 51 | + self, mock_monotonic, mock_block_until_ready |
| 52 | + ): |
| 53 | + """Tests that Chrono.resume is called even if an exception occurs.""" |
| 54 | + chrono = train_utils.Chrono() |
| 55 | + before_pause, after_pause, after_resume = 100.0, 101.1, 105.5 |
| 56 | + mock_monotonic.side_effect = [before_pause, after_pause, after_resume] |
| 57 | + wait_for_ops = ("dummy_op",) |
| 58 | + custom_exception = ValueError("Test exception inside context") |
| 59 | + |
| 60 | + # Disable linting since the assertion against the exception must be done |
| 61 | + # within the context manager. The assertions below the context blocks are |
| 62 | + # not affected by the exception, despite the highlighting (or dimming). |
| 63 | + with self.assertRaises(ValueError) as context: # pylint: disable=g-error-prone-assert-raises |
| 64 | + with chrono.paused(wait_for=wait_for_ops): |
| 65 | + mock_block_until_ready.assert_called_once_with(wait_for_ops) |
| 66 | + self.assertEqual(chrono.pause_start, before_pause) |
| 67 | + raise custom_exception |
| 68 | + self.assertEqual(context.exception, custom_exception) |
| 69 | + |
| 70 | + self.assertIsNone(chrono.pause_start) # Should be reset by resume |
| 71 | + self.assertEqual(chrono.paused_time, after_pause - before_pause) |
| 72 | + self.assertEqual(mock_monotonic.call_count, 3) # init, pause, and resume |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + absltest.main() |
0 commit comments