|
| 1 | +# Copyright 2026 The Orbax 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 | +import time |
| 16 | +from unittest import mock |
| 17 | + |
| 18 | +from absl.testing import flagsaver |
| 19 | +from absl.testing import parameterized |
| 20 | +from etils import epath |
| 21 | +import jax |
| 22 | +import numpy as np |
| 23 | +from orbax.checkpoint import test_utils |
| 24 | +from orbax.checkpoint._src.multihost import multihost |
| 25 | +from orbax.checkpoint._src.testing import multiprocess_test |
| 26 | + |
| 27 | + |
| 28 | +class MultihostUtilsTestBase: |
| 29 | + |
| 30 | + class Test(parameterized.TestCase): |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + super().setUp() |
| 34 | + self.assertEqual(jax.device_count(), 8) |
| 35 | + self.assertEqual(jax.process_count(), 4) |
| 36 | + self.assertEqual(jax.local_device_count(), 2) |
| 37 | + |
| 38 | + if not multihost.is_runtime_to_distributed_ids_initialized(): |
| 39 | + multihost.initialize_runtime_to_distributed_ids() |
| 40 | + |
| 41 | + self.tmpdir = epath.Path( |
| 42 | + self.create_tempdir(name='multihost_test').full_path |
| 43 | + ) |
| 44 | + test_utils.sync_global_processes('setUp') |
| 45 | + |
| 46 | + def tearDown(self): |
| 47 | + test_utils.sync_global_processes('tearDown') |
| 48 | + super().tearDown() |
| 49 | + |
| 50 | + def test_process_errors(self): |
| 51 | + if multihost.process_index() == 1: |
| 52 | + with self.assertRaises(ValueError): |
| 53 | + multihost.sync_global_processes( |
| 54 | + 'test_process_errors_1', processes={0} |
| 55 | + ) |
| 56 | + |
| 57 | + def test_sync_global_processes(self): |
| 58 | + if multihost.process_index() == 0: |
| 59 | + time.sleep(2) |
| 60 | + (self.tmpdir / 'dummy').mkdir(parents=False, exist_ok=False) |
| 61 | + multihost.sync_global_processes('test_sync_global_processes') |
| 62 | + self.assertTrue((self.tmpdir / 'dummy').exists()) |
| 63 | + |
| 64 | + def test_sync_global_processes_partial(self): |
| 65 | + participating_processes = {0, 2} |
| 66 | + primary_process = 0 |
| 67 | + non_primary_process = 1 |
| 68 | + |
| 69 | + directory = self.tmpdir / 'testdir' |
| 70 | + if multihost.process_index() == primary_process: |
| 71 | + directory.mkdir(parents=False, exist_ok=False) |
| 72 | + test_utils.sync_global_processes( |
| 73 | + 'test_sync_global_processes_partial_setup' |
| 74 | + ) |
| 75 | + |
| 76 | + if multihost.process_index() == primary_process: |
| 77 | + time.sleep(2) |
| 78 | + (directory / 'dummy').mkdir(parents=False, exist_ok=False) |
| 79 | + if multihost.process_index() in participating_processes: |
| 80 | + multihost.sync_global_processes( |
| 81 | + 'test_sync_global_processes_partial', |
| 82 | + processes=participating_processes, |
| 83 | + ) |
| 84 | + if multihost.process_index() in participating_processes: |
| 85 | + self.assertTrue((directory / 'dummy').exists()) |
| 86 | + else: |
| 87 | + self.assertFalse((directory / 'dummy').exists()) |
| 88 | + |
| 89 | + if multihost.process_index() == primary_process: |
| 90 | + time.sleep(2) |
| 91 | + (directory / 'foo').mkdir(parents=False, exist_ok=False) |
| 92 | + if multihost.process_index() in participating_processes: |
| 93 | + multihost.sync_global_processes( |
| 94 | + 'test_sync_global_processes_partial_second', |
| 95 | + processes=participating_processes, |
| 96 | + ) |
| 97 | + if multihost.process_index() in participating_processes: |
| 98 | + self.assertTrue((directory / 'foo').exists()) |
| 99 | + else: |
| 100 | + self.assertFalse((directory / 'foo').exists()) |
| 101 | + |
| 102 | + multihost.sync_global_processes('test_sync_global_processes_partial_all') |
| 103 | + # If non-primary processes get past the above barrier without waiting for |
| 104 | + # all, then an error would happen for the primary process when trying to |
| 105 | + # create subdirectories. |
| 106 | + if multihost.process_index() == non_primary_process: |
| 107 | + directory.rmtree() |
| 108 | + |
| 109 | + def test_different_barriers(self): |
| 110 | + slice1 = {0, 2} |
| 111 | + slice2 = {1, 3} |
| 112 | + primary_processes = [0, 1] |
| 113 | + |
| 114 | + if multihost.process_index() in primary_processes: |
| 115 | + # Don't sleep for slice1, but do sleep for slice2, so that when slice1 |
| 116 | + # finishes waiting at the barrier, one file exists but the other does |
| 117 | + # not. |
| 118 | + time.sleep(3 * multihost.process_index()) |
| 119 | + (self.tmpdir / f'dummy_{multihost.process_index()}').mkdir( |
| 120 | + parents=False, exist_ok=False |
| 121 | + ) |
| 122 | + |
| 123 | + if multihost.process_index() in slice1: |
| 124 | + multihost.sync_global_processes( |
| 125 | + 'test_different_barriers_slice1', |
| 126 | + processes=slice1, |
| 127 | + ) |
| 128 | + else: |
| 129 | + multihost.sync_global_processes( |
| 130 | + 'test_different_barriers_slice2', |
| 131 | + processes=slice2, |
| 132 | + ) |
| 133 | + if multihost.process_index() in slice1: |
| 134 | + self.assertTrue((self.tmpdir / 'dummy_0').exists()) |
| 135 | + self.assertFalse((self.tmpdir / 'dummy_1').exists()) |
| 136 | + else: |
| 137 | + self.assertTrue((self.tmpdir / 'dummy_0').exists()) |
| 138 | + self.assertTrue((self.tmpdir / 'dummy_1').exists()) |
| 139 | + |
| 140 | + def test_broadcast_one_to_all(self): |
| 141 | + if multihost.process_index() == 0: |
| 142 | + tree = {'bar': [5, 12]} |
| 143 | + else: |
| 144 | + tree = {'bar': [0, 0]} |
| 145 | + result = multihost.broadcast_one_to_all(tree) |
| 146 | + |
| 147 | + expected = { |
| 148 | + 'bar': [np.asarray(5, dtype=np.int32), np.asarray(12, dtype=np.int32)] |
| 149 | + } |
| 150 | + test_utils.assert_tree_equal(self, expected, result) |
| 151 | + |
| 152 | + |
| 153 | + def test_sync_global_processes_with_distributed_barrier(self): |
| 154 | + with flagsaver.flagsaver( |
| 155 | + experimental_orbax_use_distributed_barrier=True |
| 156 | + ), mock.patch.object( |
| 157 | + multihost.multihost_utils, 'sync_global_devices', autospec=True |
| 158 | + ) as mock_sync_global_devices, mock.patch.object( |
| 159 | + multihost, 'get_barrier_sync_fn', autospec=True |
| 160 | + ) as mock_get_barrier_sync_fn, mock.patch.object( |
| 161 | + multihost, 'should_skip_process_sync', return_value=False |
| 162 | + ): |
| 163 | + multihost.sync_global_processes('test_barrier') |
| 164 | + |
| 165 | + mock_sync_global_devices.assert_not_called() |
| 166 | + mock_get_barrier_sync_fn.assert_called_once_with(processes=None) |
| 167 | + mock_get_barrier_sync_fn.return_value.assert_called_once_with( |
| 168 | + key='test_barrier', timeout_ms=300000 |
| 169 | + ) |
| 170 | + |
| 171 | + def test_sync_global_processes_without_distributed_barrier(self): |
| 172 | + with flagsaver.flagsaver( |
| 173 | + experimental_orbax_use_distributed_barrier=False |
| 174 | + ), mock.patch.object( |
| 175 | + multihost.multihost_utils, 'sync_global_devices', autospec=True |
| 176 | + ) as mock_sync_global_devices, mock.patch.object( |
| 177 | + multihost, 'get_barrier_sync_fn', autospec=True |
| 178 | + ) as mock_get_barrier_sync_fn, mock.patch.object( |
| 179 | + multihost, 'should_skip_process_sync', return_value=False |
| 180 | + ): |
| 181 | + multihost.sync_global_processes('test_barrier') |
| 182 | + |
| 183 | + mock_sync_global_devices.assert_called_once() |
| 184 | + mock_get_barrier_sync_fn.assert_not_called() |
| 185 | + |
| 186 | + |
| 187 | +class MultihostUtilsTestStandard(MultihostUtilsTestBase.Test): |
| 188 | + |
| 189 | + def setUp(self): |
| 190 | + self.enter_context( |
| 191 | + flagsaver.flagsaver(experimental_orbax_use_distributed_process_id=False) |
| 192 | + ) |
| 193 | + super().setUp() |
| 194 | + |
| 195 | + def test_sync_global_processes_partial(self): |
| 196 | + self.skipTest('Fix this scenario.') |
| 197 | + |
| 198 | + def test_different_barriers(self): |
| 199 | + self.skipTest('Fix this scenario.') |
| 200 | + |
| 201 | + |
| 202 | +class MultihostUtilsTestDistributedId(MultihostUtilsTestBase.Test): |
| 203 | + |
| 204 | + def setUp(self): |
| 205 | + self.enter_context( |
| 206 | + flagsaver.flagsaver(experimental_orbax_use_distributed_process_id=True) |
| 207 | + ) |
| 208 | + super().setUp() |
| 209 | + |
| 210 | + |
| 211 | +if __name__ == '__main__': |
| 212 | + multiprocess_test.main() |
0 commit comments