|
| 1 | +########################################## |
| 2 | +Saving and Loading Distributed Checkpoints |
| 3 | +########################################## |
| 4 | + |
| 5 | +Generally, the bigger your model is, the longer it takes to save a checkpoint to disk. |
| 6 | +With distributed checkpoints (sometimes called sharded checkpoints), you can save and load the state of your training script with multiple GPUs or nodes more efficiently, avoiding memory issues. |
| 7 | + |
| 8 | + |
| 9 | +---- |
| 10 | + |
| 11 | + |
| 12 | +***************************** |
| 13 | +Save a distributed checkpoint |
| 14 | +***************************** |
| 15 | + |
| 16 | +The distributed checkpoint format is the default when you train with the :doc:`FSDP strategy <../../advanced/model_parallel/fsdp>`. |
| 17 | + |
| 18 | +.. code-block:: python |
| 19 | +
|
| 20 | + import lightning as L |
| 21 | + from lightning.fabric.strategies import FSDPStrategy |
| 22 | +
|
| 23 | + # 1. Select the FSDP strategy |
| 24 | + strategy = FSDPStrategy( |
| 25 | + # Default: sharded/distributed checkpoint |
| 26 | + state_dict_type="sharded", |
| 27 | + # Full checkpoint (not distributed) |
| 28 | + # state_dict_type="full", |
| 29 | + ) |
| 30 | +
|
| 31 | + fabric = L.Fabric(devices=2, strategy=strategy, ...) |
| 32 | + fabric.launch() |
| 33 | + ... |
| 34 | + model, optimizer = fabric.setup(model, optimizer) |
| 35 | +
|
| 36 | + # 2. Define model, optimizer, and other training loop state |
| 37 | + state = {"model": model, "optimizer": optimizer, "iter": iteration} |
| 38 | +
|
| 39 | + # DON'T do this (inefficient): |
| 40 | + # state = {"model": model.state_dict(), "optimizer": optimizer.state_dict(), ...} |
| 41 | +
|
| 42 | + # 3. Save using Fabric's method |
| 43 | + fabric.save("path/to/checkpoint/file", state) |
| 44 | +
|
| 45 | + # DON'T do this (inefficient): |
| 46 | + # torch.save("path/to/checkpoint/file", state) |
| 47 | +
|
| 48 | +With ``state_dict_type="sharded"``, each process/GPU will save its own file into a folder at the given path. |
| 49 | +This reduces memory peaks and speeds up the saving to disk. |
| 50 | + |
| 51 | +.. collapse:: Full example |
| 52 | + |
| 53 | + .. code-block:: python |
| 54 | +
|
| 55 | + import time |
| 56 | + import torch |
| 57 | + import torch.nn.functional as F |
| 58 | +
|
| 59 | + import lightning as L |
| 60 | + from lightning.fabric.strategies import FSDPStrategy |
| 61 | + from lightning.pytorch.demos import Transformer, WikiText2 |
| 62 | +
|
| 63 | + strategy = FSDPStrategy(state_dict_type="sharded") |
| 64 | + fabric = L.Fabric(accelerator="cuda", devices=4, strategy=strategy) |
| 65 | + fabric.launch() |
| 66 | +
|
| 67 | + with fabric.rank_zero_first(): |
| 68 | + dataset = WikiText2() |
| 69 | +
|
| 70 | + # 1B parameters |
| 71 | + model = Transformer(vocab_size=dataset.vocab_size, nlayers=32, nhid=4096, ninp=1024, nhead=64) |
| 72 | + optimizer = torch.optim.Adam(model.parameters(), lr=0.1) |
| 73 | +
|
| 74 | + model, optimizer = fabric.setup(model, optimizer) |
| 75 | +
|
| 76 | + state = {"model": model, "optimizer": optimizer, "iteration": 0} |
| 77 | +
|
| 78 | + for i in range(10): |
| 79 | + input, target = fabric.to_device(dataset[i]) |
| 80 | + output = model(input.unsqueeze(0), target.unsqueeze(0)) |
| 81 | + loss = F.nll_loss(output, target.view(-1)) |
| 82 | + fabric.backward(loss) |
| 83 | + optimizer.step() |
| 84 | + optimizer.zero_grad() |
| 85 | + fabric.print(loss.item()) |
| 86 | +
|
| 87 | + fabric.print("Saving checkpoint ...") |
| 88 | + t0 = time.time() |
| 89 | + fabric.save("my-checkpoint.ckpt", state) |
| 90 | + fabric.print(f"Took {time.time() - t0:.2f} seconds.") |
| 91 | +
|
| 92 | + Check the contents of the checkpoint folder: |
| 93 | + |
| 94 | + .. code-block:: bash |
| 95 | +
|
| 96 | + ls -a my-checkpoint.ckpt/ |
| 97 | +
|
| 98 | + .. code-block:: |
| 99 | +
|
| 100 | + my-checkpoint.ckpt/ |
| 101 | + ├── __0_0.distcp |
| 102 | + ├── __1_0.distcp |
| 103 | + ├── __2_0.distcp |
| 104 | + ├── __3_0.distcp |
| 105 | + ├── .metadata |
| 106 | + └── meta.pt |
| 107 | +
|
| 108 | + The ``.distcp`` files contain the tensor shards from each process/GPU. You can see that the size of these files |
| 109 | + is roughly 1/4 of the total size of the checkpoint since the script distributes the model across 4 GPUs. |
| 110 | + |
| 111 | + |
| 112 | +---- |
| 113 | + |
| 114 | + |
| 115 | +***************************** |
| 116 | +Load a distributed checkpoint |
| 117 | +***************************** |
| 118 | + |
| 119 | +You can easily load a distributed checkpoint in Fabric if your script uses :doc:`FSDP <../../advanced/model_parallel/fsdp>`. |
| 120 | + |
| 121 | +.. code-block:: python |
| 122 | +
|
| 123 | + import lightning as L |
| 124 | + from lightning.fabric.strategies import FSDPStrategy |
| 125 | +
|
| 126 | + # 1. Select the FSDP strategy |
| 127 | + fabric = L.Fabric(devices=2, strategy=FSDPStrategy(), ...) |
| 128 | + fabric.launch() |
| 129 | + ... |
| 130 | + model, optimizer = fabric.setup(model, optimizer) |
| 131 | +
|
| 132 | + # 2. Define model, optimizer, and other training loop state |
| 133 | + state = {"model": model, "optimizer": optimizer, "iter": iteration} |
| 134 | +
|
| 135 | + # 3. Load using Fabric's method |
| 136 | + fabric.load("path/to/checkpoint/file", state) |
| 137 | +
|
| 138 | + # DON'T do this (inefficient): |
| 139 | + # model.load_state_dict(torch.load("path/to/checkpoint/file")) |
| 140 | +
|
| 141 | +Note that you can load the distributed checkpoint even if the world size has changed, i.e., you are running on a different number of GPUs than when you saved the checkpoint. |
| 142 | + |
| 143 | +.. collapse:: Full example |
| 144 | + |
| 145 | + .. code-block:: python |
| 146 | +
|
| 147 | + import torch |
| 148 | +
|
| 149 | + import lightning as L |
| 150 | + from lightning.fabric.strategies import FSDPStrategy |
| 151 | + from lightning.pytorch.demos import Transformer, WikiText2 |
| 152 | +
|
| 153 | + strategy = FSDPStrategy(state_dict_type="sharded") |
| 154 | + fabric = L.Fabric(accelerator="cuda", devices=2, strategy=strategy) |
| 155 | + fabric.launch() |
| 156 | +
|
| 157 | + with fabric.rank_zero_first(): |
| 158 | + dataset = WikiText2() |
| 159 | +
|
| 160 | + # 1B parameters |
| 161 | + model = Transformer(vocab_size=dataset.vocab_size, nlayers=32, nhid=4096, ninp=1024, nhead=64) |
| 162 | + optimizer = torch.optim.Adam(model.parameters(), lr=0.1) |
| 163 | +
|
| 164 | + model, optimizer = fabric.setup(model, optimizer) |
| 165 | +
|
| 166 | + state = {"model": model, "optimizer": optimizer, "iteration": 0} |
| 167 | +
|
| 168 | + fabric.print("Loading checkpoint ...") |
| 169 | + fabric.load("my-checkpoint.ckpt", state) |
| 170 | +
|
| 171 | +
|
| 172 | +.. important:: |
| 173 | + |
| 174 | + If you want to load a distributed checkpoint into a script that doesn't use FSDP (or Fabric at all), then you will have to :ref:`convert it to a single-file checkpoint first <Convert dist-checkpoint>`. |
| 175 | + |
| 176 | + |
| 177 | +---- |
| 178 | + |
| 179 | + |
| 180 | +.. _Convert dist-checkpoint: |
| 181 | + |
| 182 | +******************************** |
| 183 | +Convert a distributed checkpoint |
| 184 | +******************************** |
| 185 | + |
| 186 | +Coming soon. |
0 commit comments