-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d59f8d
commit 42d8c22
Showing
43 changed files
with
5,268 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,8 @@ | |
|
||
# pre-commit config | ||
./.pre-commit-config.yaml | ||
|
||
# checkpoint | ||
*.checkpoint_dir | ||
*.distcp | ||
.metadata |
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,48 @@ | ||
# vescale.checkpoint | ||
|
||
`vescale.checkpoint` is an automatic distributed checkpointing system for LLM training and inference. | ||
|
||
## Why `vescale.checkpoint`? | ||
|
||
1. Manually managing distributed checkpointing, such as writing model saving/loading/resharding scripts under complex distributed environments, is painful and error-prone. | ||
|
||
2. `torch.save` and `torch.load` lacks the capability of managing checkpointing in distributed settings, let alone resharding checkpoints for different distributed settings. | ||
Although existing systems extend `torch.save` for saving checkpoints on multiple GPUs or machines, the saved checkpoints are heavily coupled with a single distributed setting like the degrees of data, tensor and pipeline parallelism. Consequently, existing systems with `torch.load` fail to load checkpoints with varying degrees of parallelism, which is common in elastic training or switching between training and fine-tuning. | ||
|
||
3. `PyTorch Distirbuted Checkpoint` indeed supports checkpoint resharding to some extent. Nonetheless, it currently only supports resharding for the simplest data parallelism, but not for the complex tensor nor pipeline parallelism, which are commonly used in 3D parallelism of LLM training. Furthermore, it does not support load-time resharding for Distributed Optimizer, nor provide decent performance optimizations. | ||
|
||
## What is `vescale.checkpoint`? | ||
|
||
`vescale.checkpoint` offers simple and straightforward APIs, | ||
enabling users to load and save distributed model (e.g., `DModule`) and optimizer (e.g., `DistributedOptimizer`) seamlessly, | ||
abstracting away the complexities of underlying details such as process rank and device mesh. | ||
|
||
`vescale.checkpoint` supports load-time checkpoint resharding when varying the degrees of data, tensor, or pipeline (TODO) parallelism for both veScale model (e.g., `DModule`) and optimizer (e.g., `DistributedOptimizer`). | ||
|
||
`vescale.checkpoint` incorporates [fast checkpointing](https://arxiv.org/abs/2402.15627) and various I/O optimization techinques, enhancing I/O efficiency during LLM training. | ||
|
||
`vescale.checkpoint` will be a part of `OmniStore` project, a new open-source project coming soon. | ||
|
||
`vescale.checkpoint` is built on top of `PyTorch Distributed Checkpoint` with significant differences as discussed above. | ||
|
||
## How to use `vescale.checkpoint`? | ||
|
||
- Saving checkpoint: | ||
|
||
``` | ||
# prepare checkpoint state for the model and optimizer | ||
checkpoint_state = { "model": distributed_model, "optimizer": distributed_optimizer } | ||
# save the checkpoint | ||
vescale.checkpoint.save("/user/vescale/gpt/", checkpoint_state) | ||
``` | ||
- Loading checkpoint (under different world size or 3D parallelism degrees): | ||
``` | ||
# 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) | ||
``` | ||
|
||
- More examples can be found under `<repo>/test/checkpoint` and `<repo>/python/example`. | ||
|
||
- Original examples can be found in PyTorch [Distributed Checkpoint](https://github.com/pytorch/pytorch/tree/main/torch/distributed/checkpoint) |
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,44 @@ | ||
################################################################################ | ||
# 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. | ||
################################################################################ | ||
# The "checkpoint" folder is ONLY USED for "open source" version veScale | ||
# If you use veScale in ByteDance, please use OmniStore | ||
|
||
from .api.vescale_checkpointer import VeScaleCheckpointer | ||
from .api.meta_type import CheckpointState | ||
|
||
|
||
def save(path: str, checkpoint_state: CheckpointState): | ||
""" | ||
Save a checkpoint to a given path | ||
Args: | ||
path: Defines the storage path for checkpoint. | ||
checkpoint_state: A dictionary contains key-value pairs for model and optimizer. | ||
- Model: Identified by 'model' key, value should be a model instance. | ||
- Optimizer: Identified by 'optimizer' key, value should be an optimizer instance. | ||
Example: | ||
>>> checkpoint_state = { "model": distributd_model, "optimizer": distributed_optimizer } | ||
>>> vescale.checkpoint.save("/user/vescale/gpt/", checkpoint_state) | ||
""" | ||
VeScaleCheckpointer.save(path, checkpoint_state) | ||
|
||
|
||
def load(path: str, checkpoint_state: CheckpointState): | ||
""" | ||
Load a checkpoint from a given path | ||
Args: | ||
path: Defines the storage path for checkpoint. | ||
checkpoint_state: A dictionary contains key-value pairs for model and optimizer. | ||
- Model: Identified by 'model' key, value should be a model instance. | ||
- Optimizer: Identified by 'optimizer' key, value should be an optimizer instance. | ||
Example: | ||
>>> checkpoint_state = { "model": distributd_model, "optimizer": distributed_optimizer } | ||
>>> vescale.checkpoint.load("/user/vescale/gpt/", checkpoint_state) | ||
""" | ||
VeScaleCheckpointer.load(path, checkpoint_state) |
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,47 @@ | ||
################################################################################ | ||
# | ||
# Copyright 2023 ByteDance Ltd. and/or its affiliates. All rights reserved. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
################################################################################ | ||
from .meta_type import CheckpointState | ||
|
||
|
||
class BaseCheckpointer: | ||
""" | ||
The Checkpointer class offers APIs that enable users to save and load state dictionarie. | ||
It is designed for extension across various training frameworks. | ||
""" | ||
|
||
@classmethod | ||
def save(cls, path: str, checkpoint_state: CheckpointState): | ||
""" | ||
A Method for saving checkpoint | ||
Args: | ||
path: Defines the storage path for checkpoint. | ||
checkpoint_state: A dictionary contains key-value pairs for model, optimizer and dataloader(TODO). | ||
- Model: Identified by 'model' key, value should be a model instance. | ||
- Optimizer: Identified by 'optimizer' key, value should be an optimizer instance. | ||
""" | ||
pass | ||
|
||
def load(cls, path: str, checkpoint_state: CheckpointState): | ||
""" | ||
A Method for loading checkpoint | ||
Args: | ||
path: Defines the storage path for checkpoint. | ||
checkpoint_state: A dictionary contains key-value pairs for model, optimizer and dataloader(TODO). | ||
- Model: Identified by 'model' key, value should be a model instance. | ||
- Optimizer: Identified by 'optimizer' key, value should be an optimizer instance. | ||
""" | ||
pass |
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,38 @@ | ||
################################################################################ | ||
# 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. | ||
################################################################################ | ||
# meta_type.py saves all constants and data types commonly used in omnistore | ||
|
||
from enum import Enum | ||
from typing import Dict, Any, TypeVar | ||
from typing_extensions import Protocol, runtime_checkable | ||
|
||
|
||
STATE_DICT_TYPE = Dict[str, Any] | ||
|
||
MODEL_STR = "model" | ||
OPTIMIZER_STR = "optimizer" | ||
SHM_PATH = "/dev/shm" | ||
|
||
|
||
class SupportedStrategy(Enum): | ||
Megatron = 0 | ||
FSDP = 1 | ||
VeScale = 2 | ||
|
||
|
||
@runtime_checkable | ||
class Stateful(Protocol): | ||
def state_dict(self) -> Dict[str, Any]: ... | ||
|
||
def load_state_dict(self, state_dict: Dict[str, Any]) -> None: ... | ||
|
||
|
||
T = TypeVar("T", bound=Stateful) | ||
CheckpointState = Dict[str, T] |
Oops, something went wrong.