|
| 1 | +# pylint: disable=invalid-name |
| 2 | +import os |
| 3 | +import re |
| 4 | +import time |
| 5 | + |
| 6 | +from allennlp.common.testing import AllenNlpTestCase |
| 7 | +from allennlp.training.checkpointer import Checkpointer |
| 8 | +from allennlp.common.params import Params |
| 9 | +from allennlp.training.trainer import Trainer |
| 10 | +from allennlp.common.checks import ConfigurationError |
| 11 | + |
| 12 | + |
| 13 | +class TestCheckpointer(AllenNlpTestCase): |
| 14 | + def retrieve_and_delete_saved(self): |
| 15 | + """ |
| 16 | + Helper function for the tests below. Finds the weight and training state files in |
| 17 | + self.TEST_DIR, parses their names for the epochs that were saved, deletes them, |
| 18 | + and returns the saved epochs as two lists of integers. |
| 19 | + """ |
| 20 | + serialization_files = os.listdir(self.TEST_DIR) |
| 21 | + model_checkpoints = [x for x in serialization_files if "model_state_epoch" in x] |
| 22 | + found_model_epochs = [int(re.search(r"model_state_epoch_([0-9\.\-]+)\.th", x).group(1)) |
| 23 | + for x in model_checkpoints] |
| 24 | + for f in model_checkpoints: |
| 25 | + os.remove(os.path.join(self.TEST_DIR, f)) |
| 26 | + training_checkpoints = [x for x in serialization_files if "training_state_epoch" in x] |
| 27 | + found_training_epochs = [int(re.search(r"training_state_epoch_([0-9\.\-]+)\.th", x).group(1)) |
| 28 | + for x in training_checkpoints] |
| 29 | + for f in training_checkpoints: |
| 30 | + os.remove(os.path.join(self.TEST_DIR, f)) |
| 31 | + return sorted(found_model_epochs), sorted(found_training_epochs) |
| 32 | + |
| 33 | + def test_default(self): |
| 34 | + """ |
| 35 | + Tests that the default behavior keeps just the last 20 checkpoints. |
| 36 | + """ |
| 37 | + default_num_to_keep = 20 |
| 38 | + num_epochs = 30 |
| 39 | + target = list(range(num_epochs - default_num_to_keep, num_epochs)) |
| 40 | + |
| 41 | + checkpointer = Checkpointer(serialization_dir=self.TEST_DIR) |
| 42 | + |
| 43 | + for e in range(num_epochs): |
| 44 | + checkpointer.save_checkpoint(epoch=e, |
| 45 | + model_state={"epoch": e}, |
| 46 | + training_states={"epoch": e}, |
| 47 | + is_best_so_far=False) |
| 48 | + models, training = self.retrieve_and_delete_saved() |
| 49 | + assert models == training == target |
| 50 | + |
| 51 | + def test_with_time(self): |
| 52 | + """ |
| 53 | + Tests that keep_serialized_model_every_num_seconds parameter causes a checkpoint to be saved |
| 54 | + after enough time has elapsed between epochs. |
| 55 | + """ |
| 56 | + num_to_keep = 10 |
| 57 | + num_epochs = 30 |
| 58 | + target = list(range(num_epochs - num_to_keep, num_epochs)) |
| 59 | + pauses = [5, 18, 26] |
| 60 | + target = sorted(set(target + pauses)) |
| 61 | + checkpointer = Checkpointer(serialization_dir=self.TEST_DIR, |
| 62 | + num_serialized_models_to_keep=num_to_keep, |
| 63 | + keep_serialized_model_every_num_seconds=1) |
| 64 | + for e in range(num_epochs): |
| 65 | + if e in pauses: |
| 66 | + time.sleep(2) |
| 67 | + checkpointer.save_checkpoint(epoch=e, |
| 68 | + model_state={"epoch": e}, |
| 69 | + training_states={"epoch": e}, |
| 70 | + is_best_so_far=False) |
| 71 | + models, training = self.retrieve_and_delete_saved() |
| 72 | + assert models == training == target |
| 73 | + |
| 74 | + def test_configuration_error_when_passed_as_conflicting_argument_to_trainer(self): |
| 75 | + """ |
| 76 | + Users should initialize Trainer either with an instance of Checkpointer or by specifying |
| 77 | + parameter values for num_serialized_models_to_keep and keep_serialized_model_every_num_seconds. |
| 78 | + Check that Trainer raises a ConfigurationError if both methods are used at the same time. |
| 79 | + """ |
| 80 | + with self.assertRaises(ConfigurationError): |
| 81 | + Trainer(None, None, None, None, |
| 82 | + num_serialized_models_to_keep=30, |
| 83 | + keep_serialized_model_every_num_seconds=None, |
| 84 | + checkpointer=Checkpointer(serialization_dir=self.TEST_DIR, |
| 85 | + num_serialized_models_to_keep=40, |
| 86 | + keep_serialized_model_every_num_seconds=2)) |
| 87 | + with self.assertRaises(ConfigurationError): |
| 88 | + Trainer(None, None, None, None, |
| 89 | + num_serialized_models_to_keep=20, |
| 90 | + keep_serialized_model_every_num_seconds=2, |
| 91 | + checkpointer=Checkpointer(serialization_dir=self.TEST_DIR, |
| 92 | + num_serialized_models_to_keep=40, |
| 93 | + keep_serialized_model_every_num_seconds=2)) |
| 94 | + try: |
| 95 | + Trainer(None, None, None, None, |
| 96 | + checkpointer=Checkpointer(serialization_dir=self.TEST_DIR, |
| 97 | + num_serialized_models_to_keep=40, |
| 98 | + keep_serialized_model_every_num_seconds=2)) |
| 99 | + except ConfigurationError: |
| 100 | + self.fail("Configuration Error raised for passed checkpointer") |
| 101 | + |
| 102 | + def test_registered_subclass(self): |
| 103 | + """ |
| 104 | + Tests that registering Checkpointer subclasses works correctly. |
| 105 | + """ |
| 106 | + |
| 107 | + @Checkpointer.register("checkpointer_subclass") |
| 108 | + class CheckpointerSubclass(Checkpointer): |
| 109 | + def __init__(self, x: int, y: int) -> None: |
| 110 | + super().__init__() |
| 111 | + self.x = x |
| 112 | + self.y = y |
| 113 | + |
| 114 | + sub_inst = Checkpointer.from_params(Params({"type": "checkpointer_subclass", "x": 1, "y": 3})) |
| 115 | + assert sub_inst.__class__ == CheckpointerSubclass |
| 116 | + assert sub_inst.x == 1 and sub_inst.y == 3 |
0 commit comments