Debian Codegraph @

docs/source/usage_guides/checkpoint.md

raw
1 <!--Copyright 2022 The HuggingFace Team. All rights reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4 the License. You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9 an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11
12 ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
13 rendered properly in your Markdown viewer.
14 -->
15
16 # Checkpointing
17
18 When training a PyTorch model with Accelerate, you may often want to save and continue a state of training. Doing so requires
19 saving and loading the model, optimizer, RNG generators, and the GradScaler. Inside Accelerate are two convenience functions to achieve this quickly:
20 - Use [`~Accelerator.save_state`] for saving everything mentioned above to a folder location
21 - Use [`~Accelerator.load_state`] for loading everything stored from an earlier `save_state`
22
23 To further customize where and how states are saved through [`~Accelerator.save_state`] the [`~utils.ProjectConfiguration`] class can be used. For example
24 if `automatic_checkpoint_naming` is enabled each saved checkpoint will be located then at `Accelerator.project_dir/checkpoints/checkpoint_{checkpoint_number}`.
25
26 It should be noted that the expectation is that those states come from the same training script, they should not be from two separate scripts.
27
28 - By using [`~Accelerator.register_for_checkpointing`], you can register custom objects to be automatically stored or loaded from the two prior functions,
29 so long as the object has a `state_dict` **and** a `load_state_dict` functionality. This could include objects such as a learning rate scheduler.
30
31
32 Below is a brief example using checkpointing to save and reload a state during training:
33
34 ```python
35 from accelerate import Accelerator
36 import torch
37
38 accelerator = Accelerator(project_dir="my/save/path")
39
40 my_scheduler = torch.optim.lr_scheduler.StepLR(my_optimizer, step_size=1, gamma=0.99)
41 my_model, my_optimizer, my_training_dataloader = accelerator.prepare(my_model, my_optimizer, my_training_dataloader)
42
43 # Register the LR scheduler
44 accelerator.register_for_checkpointing(my_scheduler)
45
46 # Save the starting state
47 accelerator.save_state()
48
49 device = accelerator.device
50 my_model.to(device)
51
52 # Perform training
53 for epoch in range(num_epochs):
54 for batch in my_training_dataloader:
55 my_optimizer.zero_grad()
56 inputs, targets = batch
57 inputs = inputs.to(device)
58 targets = targets.to(device)
59 outputs = my_model(inputs)
60 loss = my_loss_function(outputs, targets)
61 accelerator.backward(loss)
62 my_optimizer.step()
63 my_scheduler.step()
64
65 # Restore the previous state
66 accelerator.load_state("my/save/path/checkpointing/checkpoint_0")
67 ```
68
69 ## Restoring the state of the DataLoader
70
71 After resuming from a checkpoint, it may also be desirable to resume from a particular point in the active `DataLoader` if
72 the state was saved during the middle of an epoch. You can use [`~Accelerator.skip_first_batches`] to do so.
73
74 ```python
75 from accelerate import Accelerator
76
77 accelerator = Accelerator(project_dir="my/save/path")
78
79 train_dataloader = accelerator.prepare(train_dataloader)
80 accelerator.load_state("my_state")
81
82 # Assume the checkpoint was saved 100 steps into the epoch
83 skipped_dataloader = accelerator.skip_first_batches(train_dataloader, 100)
84
85 # After the first iteration, go back to `train_dataloader`
86
87 # First epoch
88 for batch in skipped_dataloader:
89 # Do something
90 pass
91
92 # Second epoch
93 for batch in train_dataloader:
94 # Do something
95 pass
96 ```