|
1 | 1 | # Owner(s): ["oncall: distributed checkpointing"] |
2 | 2 |
|
| 3 | +import os |
3 | 4 | import sys |
4 | 5 | from unittest.mock import patch |
5 | 6 |
|
6 | 7 | import torch |
| 8 | +import torch.testing._internal.common_utils as common |
7 | 9 | from torch import distributed as dist |
8 | 10 | from torch.distributed.checkpoint._async_process_executor import ( |
9 | 11 | _ProcessBasedAsyncCheckpointExecutor, |
| 12 | + _ProcessGroupInitInfo, |
10 | 13 | ) |
| 14 | +from torch.distributed.checkpoint.api import CheckpointException |
11 | 15 | from torch.distributed.checkpoint.storage import StorageWriter |
12 | 16 | from torch.distributed.elastic.utils.distributed import get_free_port |
13 | | -from torch.testing._internal.common_utils import run_tests, TEST_WITH_DEV_DBG_ASAN |
| 17 | +from torch.testing._internal.common_distributed import skip_if_win32 |
| 18 | +from torch.testing._internal.common_utils import ( |
| 19 | + retry_on_connect_failures, |
| 20 | + run_tests, |
| 21 | + TEST_WITH_DEV_DBG_ASAN, |
| 22 | + TestCase, |
| 23 | +) |
14 | 24 | from torch.testing._internal.distributed._tensor.common_dtensor import ( |
15 | 25 | DTensorTestBase, |
16 | 26 | with_comms, |
@@ -110,47 +120,184 @@ def test_checkpoint_save_failure_continues_serving(self) -> None: |
110 | 120 | "epoch": 5, |
111 | 121 | } |
112 | 122 |
|
113 | | - # 1. Simulate a failure in creating PG in background process. |
114 | | - with patch( |
115 | | - "torch.distributed.checkpoint._async_process_executor.get_free_port", |
116 | | - return_value=-1, |
| 123 | + with patch.dict(os.environ, {}, clear=False): |
| 124 | + os.environ.pop("DCP_USE_PREFIX_STORE", None) |
| 125 | + |
| 126 | + # 1. Simulate a failure in creating PG in background process. |
| 127 | + with patch( |
| 128 | + "torch.distributed.checkpoint._async_process_executor.get_free_port", |
| 129 | + return_value=-1, |
| 130 | + ): |
| 131 | + with self.assertRaises(ValueError) as _: |
| 132 | + proc_executor = _ProcessBasedAsyncCheckpointExecutor() |
| 133 | + fut = proc_executor.execute_save( |
| 134 | + staging_future_or_state_dict=test_state_dict, |
| 135 | + ) |
| 136 | + fut.result() |
| 137 | + |
| 138 | + # 2. Attempt save with failing storage writer |
| 139 | + with patch( |
| 140 | + "torch.distributed.checkpoint._async_process_executor.get_free_port", |
| 141 | + return_value=get_free_port(), |
| 142 | + ) as mock_get_free_port: |
| 143 | + proc_executor = _ProcessBasedAsyncCheckpointExecutor() |
| 144 | + fut = proc_executor.execute_save( |
| 145 | + staging_future_or_state_dict=test_state_dict, |
| 146 | + storage_writer=TestStorageWriter(behavior="fail_once"), |
| 147 | + ) |
| 148 | + self.assertIn( |
| 149 | + "fail_once policy triggered failure", str(fut.exception()) |
| 150 | + ) |
| 151 | + # Verify new process was created for this attempt |
| 152 | + if dist.get_rank() == 0: |
| 153 | + mock_get_free_port.assert_called_once() |
| 154 | + |
| 155 | + # 3. Second save attempt with successful storage writer - process should still be alive |
| 156 | + with patch( |
| 157 | + "torch.distributed.checkpoint._async_process_executor.get_free_port", |
| 158 | + ) as mock_get_free_port: |
| 159 | + proc_executor = _ProcessBasedAsyncCheckpointExecutor() |
| 160 | + fut = proc_executor.execute_save( |
| 161 | + staging_future_or_state_dict=test_state_dict, |
| 162 | + storage_writer=TestStorageWriter(behavior="success"), |
| 163 | + ) |
| 164 | + result = fut.result() |
| 165 | + # Verify process is still alive |
| 166 | + mock_get_free_port.assert_not_called() |
| 167 | + # Verify successful save |
| 168 | + self.assertIsNotNone(result) |
| 169 | + |
| 170 | + |
| 171 | +class TestAsyncProcessExecutorPrefixStore(TestCase): |
| 172 | + @skip_if_win32() |
| 173 | + @retry_on_connect_failures |
| 174 | + def test_checkpoint_save_with_prefix_store_enabled(self) -> None: |
| 175 | + """Test that checkpoint save works when DCP_USE_PREFIX_STORE is enabled.""" |
| 176 | + |
| 177 | + test_state_dict = { |
| 178 | + "model": {"weight": torch.randn(4, 4), "bias": torch.randn(4)}, |
| 179 | + "optimizer": {"param_groups": [{"lr": 0.01}]}, |
| 180 | + "epoch": 5, |
| 181 | + } |
| 182 | + |
| 183 | + master_addr = "localhost" |
| 184 | + master_port = str(common.find_free_port()) |
| 185 | + |
| 186 | + with patch.dict( |
| 187 | + os.environ, |
| 188 | + { |
| 189 | + "DCP_USE_PREFIX_STORE": "1", |
| 190 | + "MASTER_ADDR": master_addr, |
| 191 | + "MASTER_PORT": master_port, |
| 192 | + }, |
117 | 193 | ): |
118 | | - with self.assertRaises(ValueError) as _: |
| 194 | + with patch( |
| 195 | + "torch.distributed.checkpoint._async_process_executor.get_free_port" |
| 196 | + ) as mock_get_free_port: |
| 197 | + dist.init_process_group( |
| 198 | + backend=dist.Backend.GLOO, |
| 199 | + rank=0, |
| 200 | + world_size=1, |
| 201 | + ) |
| 202 | + |
119 | 203 | proc_executor = _ProcessBasedAsyncCheckpointExecutor() |
120 | 204 | fut = proc_executor.execute_save( |
121 | 205 | staging_future_or_state_dict=test_state_dict, |
| 206 | + storage_writer=TestStorageWriter(behavior="success"), |
122 | 207 | ) |
123 | | - fut.result() |
124 | | - |
125 | | - # 2. Attempt save with failing storage writer |
126 | | - with patch( |
127 | | - "torch.distributed.checkpoint._async_process_executor.get_free_port", |
128 | | - return_value=get_free_port(), |
129 | | - ) as mock_get_free_port: |
130 | | - proc_executor = _ProcessBasedAsyncCheckpointExecutor() |
131 | | - fut = proc_executor.execute_save( |
132 | | - staging_future_or_state_dict=test_state_dict, |
133 | | - storage_writer=TestStorageWriter(behavior="fail_once"), |
134 | | - ) |
135 | | - self.assertIn("fail_once policy triggered failure", str(fut.exception())) |
136 | | - # Verify new process was created for this attempt |
137 | | - if dist.get_rank() == 0: |
138 | | - mock_get_free_port.assert_called_once() |
139 | | - |
140 | | - # 3. Second save attempt with successful storage writer - process should still be alive |
141 | | - with patch( |
142 | | - "torch.distributed.checkpoint._async_process_executor.get_free_port", |
143 | | - ) as mock_get_free_port: |
144 | | - proc_executor = _ProcessBasedAsyncCheckpointExecutor() |
145 | | - fut = proc_executor.execute_save( |
146 | | - staging_future_or_state_dict=test_state_dict, |
147 | | - storage_writer=TestStorageWriter(behavior="success"), |
148 | | - ) |
149 | | - result = fut.result() |
150 | | - # Verify process is still alive |
151 | | - mock_get_free_port.assert_not_called() |
152 | | - # Verify successful save |
153 | | - self.assertIsNotNone(result) |
| 208 | + result = fut.result() |
| 209 | + self.assertIsNotNone(result) |
| 210 | + mock_get_free_port.assert_not_called() |
| 211 | + |
| 212 | + |
| 213 | +class TestProcessGroupInitInfo(DTensorTestBase): |
| 214 | + """Test suite for _ProcessGroupInitInfo.""" |
| 215 | + |
| 216 | + @with_comms |
| 217 | + def test_process_group_init_info_with_default_pg(self) -> None: |
| 218 | + """Test that ProcessGroupInitInfo correctly initializes.""" |
| 219 | + with patch.dict(os.environ, {}, clear=False): |
| 220 | + os.environ.pop("DCP_USE_PREFIX_STORE", None) |
| 221 | + |
| 222 | + pg_init_info = _ProcessGroupInitInfo() |
| 223 | + |
| 224 | + self.assertEqual(pg_init_info.global_rank, dist.get_rank()) |
| 225 | + self.assertEqual(pg_init_info.world_size, dist.get_world_size()) |
| 226 | + self.assertIsNotNone(pg_init_info.tcp_store_master_addr) |
| 227 | + self.assertGreater(pg_init_info.tcp_store_master_port, 0) |
| 228 | + self.assertEqual(pg_init_info.use_prefix_store, False) |
| 229 | + |
| 230 | + @with_comms |
| 231 | + def test_process_group_init_info_with_prefix_store_env_var(self) -> None: |
| 232 | + """Test that ProcessGroupInitInfo handles DCP_USE_PREFIX_STORE environment variable.""" |
| 233 | + |
| 234 | + # Flag enabled, addr/port correctly defined |
| 235 | + with patch.dict( |
| 236 | + os.environ, |
| 237 | + { |
| 238 | + "DCP_USE_PREFIX_STORE": "1", |
| 239 | + "MASTER_ADDR": "localhost", |
| 240 | + "MASTER_PORT": "12345", |
| 241 | + }, |
| 242 | + ): |
| 243 | + pg_init_info = _ProcessGroupInitInfo() |
| 244 | + self.assertTrue(pg_init_info.use_prefix_store) |
| 245 | + |
| 246 | + # Missing port |
| 247 | + with patch.dict( |
| 248 | + os.environ, {"DCP_USE_PREFIX_STORE": "1", "MASTER_ADDR": "localhost"} |
| 249 | + ): |
| 250 | + with self.assertRaises(CheckpointException): |
| 251 | + pg_init_info = _ProcessGroupInitInfo() |
| 252 | + # Missing addr |
| 253 | + with patch.dict( |
| 254 | + os.environ, {"DCP_USE_PREFIX_STORE": "1", "MASTER_PORT": "12345"} |
| 255 | + ): |
| 256 | + with self.assertRaises(CheckpointException): |
| 257 | + pg_init_info = _ProcessGroupInitInfo() |
| 258 | + # Invalid port |
| 259 | + with patch.dict( |
| 260 | + os.environ, |
| 261 | + { |
| 262 | + "DCP_USE_PREFIX_STORE": "1", |
| 263 | + "MASTER_ADDR": "localhost", |
| 264 | + "MASTER_PORT": "a", |
| 265 | + }, |
| 266 | + ): |
| 267 | + with self.assertRaises(CheckpointException): |
| 268 | + pg_init_info = _ProcessGroupInitInfo() |
| 269 | + |
| 270 | + @with_comms |
| 271 | + def test_process_group_init_info_without_prefix_store_env_var(self) -> None: |
| 272 | + """Test that ProcessGroupInitInfo defaults to not using prefix store.""" |
| 273 | + |
| 274 | + # Env var set to 0 |
| 275 | + with patch.dict(os.environ, {"DCP_USE_PREFIX_STORE": "0"}): |
| 276 | + pg_init_info = _ProcessGroupInitInfo() |
| 277 | + self.assertFalse(pg_init_info.use_prefix_store) |
| 278 | + |
| 279 | + # Missing env var |
| 280 | + with patch.dict(os.environ, {}, clear=False): |
| 281 | + os.environ.pop("DCP_USE_PREFIX_STORE", None) |
| 282 | + pg_init_info = _ProcessGroupInitInfo() |
| 283 | + self.assertFalse(pg_init_info.use_prefix_store) |
| 284 | + |
| 285 | + # Invalid env var |
| 286 | + with patch.dict(os.environ, {"DCP_USE_PREFIX_STORE": "2"}): |
| 287 | + pg_init_info = _ProcessGroupInitInfo() |
| 288 | + self.assertFalse(pg_init_info.use_prefix_store) |
| 289 | + |
| 290 | + with patch.dict(os.environ, {"DCP_USE_PREFIX_STORE": "true"}): |
| 291 | + pg_init_info = _ProcessGroupInitInfo() |
| 292 | + self.assertFalse(pg_init_info.use_prefix_store) |
| 293 | + |
| 294 | + with patch.dict(os.environ, {"DCP_USE_PREFIX_STORE": "false"}): |
| 295 | + pg_init_info = _ProcessGroupInitInfo() |
| 296 | + self.assertFalse(pg_init_info.use_prefix_store) |
| 297 | + |
| 298 | + with patch.dict(os.environ, {"DCP_USE_PREFIX_STORE": ""}): |
| 299 | + pg_init_info = _ProcessGroupInitInfo() |
| 300 | + self.assertFalse(pg_init_info.use_prefix_store) |
154 | 301 |
|
155 | 302 |
|
156 | 303 | if __name__ == "__main__": |
|
0 commit comments