已合并
test(distributed):add test for validate_checkpoint_id, reset, set_up_storage_reader from BroadcastingTorchSaveReader for v2.10.0 #33951
xh-zhan创建于 4月17日
test(distributed):add test for validate_checkpoint_id, reset, set_up_storage_reader from BroadcastingTorchSaveReader for v2.10.0 #33951
已合并
共 1 个文件变更+112-0
| @@ -0,0 +1,112 @@ | |||
| 1 | +""" | ||
| 2 | +Add validation cases for BroadcastingTorchSaveReader APIs: | ||
| 3 | +1. pytorch/test/distributed/checkpoint/test_format_utils.py from PyTorch community lacks sufficient API validations, so this file is added. | ||
| 4 | +2. This file validates the following APIs: | ||
| 5 | + torch.distributed.checkpoint.format_utils.BroadcastingTorchSaveReader.reset | ||
| 6 | + torch.distributed.checkpoint.format_utils.BroadcastingTorchSaveReader.set_up_storage_reader | ||
| 7 | + torch.distributed.checkpoint.format_utils.BroadcastingTorchSaveReader.validate_checkpoint_id | ||
| 8 | + (extendable) | ||
| 9 | +""" | ||
| 10 | + | ||
| 11 | +import os | ||
| 12 | +import tempfile | ||
| 13 | +from unittest.mock import patch | ||
| 14 | + | ||
| 15 | +import torch | ||
| 16 | +import torch_npu | ||
| 17 | +from torch.distributed.checkpoint.format_utils import BroadcastingTorchSaveReader | ||
| 18 | +from torch.testing._internal.common_utils import TestCase, run_tests | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +class TestBroadcastingTorchSaveReader(TestCase): | ||
| 22 | + """Independent unit tests for pure logic APIs of BroadcastingTorchSaveReader. | ||
| 23 | + | ||
| 24 | + This test uses mocks for distributed functions, allowing full validation | ||
| 25 | + of all APIs without starting a process group. | ||
| 26 | + """ | ||
| 27 | + | ||
| 28 | + def setUp(self): | ||
| 29 | + """Runs before each test method: creates a valid temporary checkpoint file.""" | ||
| 30 | + fd, self.temp_file_path = tempfile.mkstemp(suffix=".pt", dir=".") | ||
| 31 | + os.close(fd) | ||
| 32 | + | ||
| 33 | + tensor = torch.tensor([1, 2, 3], device="npu") | ||
| 34 | + torch.save({"dummy": tensor}, self.temp_file_path) | ||
| 35 | + self.valid_checkpoint_id = self.temp_file_path | ||
| 36 | + | ||
| 37 | + def tearDown(self): | ||
| 38 | + """Runs after each test method: deletes the temporary file to clean up.""" | ||
| 39 | + if os.path.exists(self.valid_checkpoint_id): | ||
| 40 | + os.unlink(self.valid_checkpoint_id) | ||
| 41 | + | ||
| 42 | + self.assertFalse( | ||
| 43 | + os.path.exists(self.valid_checkpoint_id), | ||
| 44 | + f"tearDown failed to cleanup file: {self.valid_checkpoint_id}" | ||
| 45 | + ) | ||
| 46 | + | ||
| 47 | + def test_validate_checkpoint_id_valid_path(self): | ||
| 48 | + """Passing a path to an existing file should return True.""" | ||
| 49 | + result = BroadcastingTorchSaveReader.validate_checkpoint_id(self.valid_checkpoint_id) | ||
| 50 | + self.assertTrue(result, "valid checkpoint file should return True") | ||
| 51 | + | ||
| 52 | + def test_validate_checkpoint_id_invalid_path(self): | ||
| 53 | + """Passing a path to a non-existent file should return False.""" | ||
| 54 | + non_existent = "does_not_exist_12345.pt" | ||
| 55 | + result = BroadcastingTorchSaveReader.validate_checkpoint_id(non_existent) | ||
| 56 | + self.assertFalse(result, "non-existent file should return False") | ||
| 57 | + | ||
| 58 | + def test_reset_updates_checkpoint_id(self): | ||
| 59 | + """Verify that the reset method correctly updates the internal checkpoint_id.""" | ||
| 60 | + reader = BroadcastingTorchSaveReader(checkpoint_id="old_path.pt") | ||
| 61 | + new_path = "new_path.pt" | ||
| 62 | + reader.reset(new_path) | ||
| 63 | + self.assertEqual(reader.checkpoint_id, new_path, "reset should update checkpoint_id") | ||
| 64 | + | ||
| 65 | + def test_reset_none(self): | ||
| 66 | + """Verify that reset with None correctly sets checkpoint_id to None.""" | ||
| 67 | + reader = BroadcastingTorchSaveReader(checkpoint_id="abc") | ||
| 68 | + reader.reset(None) | ||
| 69 | + self.assertIsNone(reader.checkpoint_id) | ||
| 70 | + | ||
| 71 | + class DummyMetadata: | ||
| 72 | + """Placeholder Metadata class used for testing.""" | ||
| 73 | + pass | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + def test_set_up_storage_reader_sets_coordinator_flag(self, mock_get_rank): | ||
| 77 | + """Verify that set_up_storage_reader correctly sets the is_coordinator attribute.""" | ||
| 78 | + mock_get_rank.return_value = 0 | ||
| 79 | + | ||
| 80 | + reader = BroadcastingTorchSaveReader(checkpoint_id=self.valid_checkpoint_id) | ||
| 81 | + metadata = self.DummyMetadata() | ||
| 82 | + | ||
| 83 | + reader.set_up_storage_reader(metadata, is_coordinator=True) | ||
| 84 | + self.assertTrue(reader.is_coordinator, "is_coordinator should be set to True") | ||
| 85 | + | ||
| 86 | + reader.set_up_storage_reader(metadata, is_coordinator=False) | ||
| 87 | + self.assertFalse(reader.is_coordinator, "is_coordinator should be set to False") | ||
| 88 | + | ||
| 89 | + | ||
| 90 | + def test_set_up_storage_reader_coordinator_rank_mismatch_raises(self, mock_get_rank): | ||
| 91 | + """Verify that an AssertionError is raised when is_coordinator=True | ||
| 92 | + but the current rank does not match coordinator_rank.""" | ||
| 93 | + mock_get_rank.return_value = 1 | ||
| 94 | + | ||
| 95 | + reader = BroadcastingTorchSaveReader(checkpoint_id=self.valid_checkpoint_id, coordinator_rank=0) | ||
| 96 | + metadata = self.DummyMetadata() | ||
| 97 | + | ||
| 98 | + with self.assertRaises(AssertionError, msg="Should raise AssertionError when coordinator rank mismatches"): | ||
| 99 | + reader.set_up_storage_reader(metadata, is_coordinator=True) | ||
| 100 | + | ||
| 101 | + def test_set_up_storage_reader_requires_checkpoint_id(self): | ||
| 102 | + """Verify that an AssertionError is raised when checkpoint_id is None.""" | ||
| 103 | + reader = BroadcastingTorchSaveReader(checkpoint_id=None) | ||
| 104 | + metadata = self.DummyMetadata() | ||
| 105 | + | ||
| 106 | + with patch("torch.distributed.get_rank", return_value=0): | ||
| 107 | + with self.assertRaises(AssertionError, msg="checkpoint_id not set should raise AssertionError"): | ||
| 108 | + reader.set_up_storage_reader(metadata, is_coordinator=True) | ||
| 109 | + | ||
| 110 | + | ||
| 111 | +if __name__ == "__main__": | ||
| 112 | + run_tests() | ||