-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[checkpoint] feat: open source fast checkpoint system (#38)
## Summary We improved `vescale.checkpoint` with the following new features for fast checkpointing (where front three features are built-in techniques without necessitating manual activation): - **Saving Plan Caching**: During training, the program may save model and optimizer checkpoints every n steps. Once a saving plan is created, it remains unchanged as long as the model does. We implemented plan caching to avoid regenerating the plan when checkpointing a model or optimizer multiple times, reducing unnecessary compute and communication costs. As of 05/30/2024, PyTorch DCP does not support plan caching. - **Saving Plan Load-Balancing**: In data parallel training, models are replicated across GPUs with different data parallel ranks but the same pipeline and tensor parallel ranks. Existing PyTorch DCP (as of 05/30/2024) deduplicates replicated tensors using a simple algorithm, causing GPUs with data parallel rank 0 to save the entire model, leading to load imbalance. We implemented a load-balancing algorithm to address this issue when deduplicating model tensors. - **D2H Tensor Copying via Pinned Memory**: When copying tensors from GPU to host memory, `vescale.checkpoint` uses pinned host memory, reducing memory allocation costs each time a checkpoint is saved. As of 05/30/2024, PyTorch DCP does not support pinned memory. - **Checkpoint Broadcasting**: In data parallel training, models are replicated across GPUs with different data parallel ranks but the same pipeline and tensor parallel ranks. If `broadcast_checkpoint` is enabled, `vescale.checkpoint.load` lets GPUs with data parallel rank 0 to load the model and broadcast it to other GPUs with higher data parallel ranks. If GPUs are connected with NCCL and I/O bandwidth is fully utilized, broadcasting model tensors speeds up checkpoint loading compared to all GPUs loading models from persistent storage. E.g.: ```python # prepare checkpoint state for the model and optimizer checkpoint_state = { "model": distributed_model, "optimizer": distributed_optimizer } # load the checkpoint vescale.checkpoint.load("/user/vescale/gpt/", checkpoint_state, broadcast_checkpoint=True) ``` - **Asynchronous Checkpointing**: When `vescale.checkpoint.save` is called, it first generates a saving plan and then synchronously copies tensors from GPU to host memory. If `async_checkpoint` is enabled, the training program can continue after the D2H copying, while `vescale.checkpoint.save` continues to serialize tensors and dump the checkpoint to persistent storage asynchronously without blocking training. As of 05/30/2024, PyTorch DCP does not support asynchronous checkpointing. E.g.: ```python # prepare checkpoint state for the model and optimizer checkpoint_state = { "model": distributed_model, "optimizer": distributed_optimizer } # save the checkpoint asynchronuously vescale.checkpoint.save("/user/vescale/gpt/", checkpoint_state, async_checkpoint=True) ``` ## Acknowledgement We sincerely appreciate all contributors including but not limited to @shanesyy-1992 @raywan-110 @lazychao @AHEADer @MingjiHan99
- Loading branch information
1 parent
55c7f8a
commit c4afc72
Showing
67 changed files
with
2,977 additions
and
1,044 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ optree | |
accelerate | ||
transformers==4.37.2 | ||
flash_attn | ||
mmh3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# shut up pylint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
################################################################################ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
################################################################################ | ||
# Modification Copyright 2023 ByteDance Ltd. and/or its affiliates. | ||
################################################################################ | ||
|
||
import itertools | ||
from common_dtensor import ( | ||
DTensorTestBase, | ||
with_comms, | ||
) | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
from torch.testing._internal.common_utils import run_tests | ||
from vescale import distribute_tensor | ||
from vescale.dtensor.placement_types import Shard | ||
from vescale.dtensor.loss import loss_parallel | ||
|
||
|
||
class DistLossParallelTest(DTensorTestBase): | ||
@with_comms | ||
def test_loss_parallel(self): | ||
device_mesh = self.build_device_mesh() | ||
|
||
channel_size, channel_dim = 16, 1 | ||
test_setup = [ | ||
(2, (8, channel_size), (8,)), # calling aten.nll_loss_forward | ||
(3, (8, channel_size, 12), (8, 12)), # calling aten.nll_loss2d_forward | ||
] | ||
weight = torch.rand(channel_size, device=self.device_type) | ||
for input_ndim, input_size, target_size in test_setup: | ||
x = torch.rand(*input_size, device=self.device_type, requires_grad=True) | ||
target = torch.randint(channel_size, target_size, device=self.device_type) | ||
|
||
shard_dims = list(range(input_ndim)) | ||
reductions = ["none", "mean", "sum"] | ||
for shard_dim, reduction in itertools.product(shard_dims, reductions): | ||
dist_x = distribute_tensor(x, device_mesh, [Shard(shard_dim)]) | ||
y = F.cross_entropy(x, target, weight, reduction=reduction) | ||
with loss_parallel(): | ||
if shard_dim == channel_dim: | ||
dist_y = F.cross_entropy(dist_x, target, weight, reduction=reduction) | ||
|
||
self.assertTrue(dist_y.placements[0].is_replicate()) | ||
self.assertEqual(dist_y.to_local(), y) | ||
|
||
if reduction == "none": | ||
y.sum().backward() | ||
dist_y.sum().backward() | ||
else: | ||
y.backward() | ||
dist_y.backward() | ||
self.assertTrue(dist_x.grad.placements[0].is_shard(shard_dim)) | ||
self.assertEqual(dist_x.grad.full_tensor(), x.grad) | ||
x.grad.zero_() | ||
else: | ||
with self.assertRaisesRegex( | ||
ValueError, | ||
"loss_parallel", | ||
): | ||
dist_y = F.cross_entropy(dist_x, target, reduction=reduction) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.