-
Notifications
You must be signed in to change notification settings - Fork 69
Feat/checkpoint resharding #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JYMiracle305
wants to merge
3
commits into
master
Choose a base branch
from
feat/checkpoint_resharding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,407
−92
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <map> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "infini_train/include/checkpoint/checkpoint.h" | ||
| #include "infini_train/include/checkpoint/shard_spec.h" | ||
| #include "infini_train/include/datatype.h" | ||
|
|
||
| namespace infini_train::checkpoint { | ||
|
|
||
| // One storage-region transfer from a saved shard into a target local tensor. | ||
| struct ReadItem { | ||
| std::string key; | ||
| std::string filename; | ||
| DataType dtype = DataType::kFLOAT32; | ||
| std::vector<int64_t> global_shape; | ||
| uint64_t byte_size = 0; | ||
| uint64_t data_offset = 0; | ||
| int shard_dim = -1; | ||
| int64_t source_offset = 0; | ||
| int64_t target_offset = 0; | ||
| int64_t length = 0; | ||
| std::vector<int64_t> source_shape; | ||
| }; | ||
|
|
||
| // All reads required to materialize one target local tensor. | ||
| struct TargetTensorPlan { | ||
| std::string key; | ||
| DataType dtype = DataType::kFLOAT32; | ||
| std::vector<int64_t> global_shape; | ||
| std::vector<int64_t> target_shape; | ||
| int shard_dim = -1; | ||
| int64_t trailing_zero_fill = 0; | ||
| std::vector<ReadItem> reads; | ||
| }; | ||
|
|
||
| // Complete load plan for one rank. | ||
| struct LoadPlan { | ||
| std::map<std::string, TargetTensorPlan> tensors; | ||
| }; | ||
|
|
||
| class LoadPlanner { | ||
| public: | ||
| // Compute saved-to-target overlaps from explicit global shard coordinates. | ||
| static LoadPlan PlanReshard(const Checkpoint::CheckpointMetadata &metadata, | ||
| const ShardedStateDict &target_state_dict); | ||
| }; | ||
|
|
||
| } // namespace infini_train::checkpoint |
This file contains hidden or 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,30 @@ | ||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <unordered_map> | ||
|
|
||
| #include "infini_train/include/checkpoint/load_planner.h" | ||
|
|
||
| namespace infini_train { | ||
| class Tensor; | ||
| } | ||
|
|
||
| namespace infini_train::checkpoint { | ||
|
|
||
| using LoadedStateDict = std::unordered_map<std::string, std::shared_ptr<Tensor>>; | ||
|
|
||
| class LoadStrategy { | ||
| public: | ||
| virtual ~LoadStrategy() = default; | ||
| virtual LoadedStateDict Execute(const std::filesystem::path &checkpoint_dir, const LoadPlan &plan) = 0; | ||
| }; | ||
|
|
||
| /// Reads source regions directly from metadata offsets while caching one open stream per file. | ||
| class IndexedRegionLoadStrategy final : public LoadStrategy { | ||
| public: | ||
| LoadedStateDict Execute(const std::filesystem::path &checkpoint_dir, const LoadPlan &plan) override; | ||
| }; | ||
|
|
||
| } // namespace infini_train::checkpoint |
This file contains hidden or 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,22 @@ | ||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
|
|
||
| #include "infini_train/include/checkpoint/checkpoint.h" | ||
|
|
||
| namespace infini_train { | ||
| class LRScheduler; | ||
| class Optimizer; | ||
| namespace nn { | ||
| class Module; | ||
| } | ||
| } // namespace infini_train | ||
|
|
||
| namespace infini_train::checkpoint { | ||
|
|
||
| // Restore this rank's target model shards from a distributed checkpoint. | ||
| void LoadDistributedCheckpoint(const std::filesystem::path &checkpoint_dir, nn::Module &model, Optimizer *optimizer, | ||
| TrainerState &state, LRScheduler *lr_scheduler, | ||
| const Checkpoint::CheckpointMetadata &metadata); | ||
|
|
||
| } // namespace infini_train::checkpoint |
This file contains hidden or 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,79 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "infini_train/include/checkpoint/shard_spec.h" | ||
| #include "infini_train/include/datatype.h" | ||
|
|
||
| namespace infini_train { | ||
| class Tensor; | ||
| } | ||
|
|
||
| namespace infini_train::checkpoint { | ||
|
|
||
| // Physical write description for one local tensor shard. | ||
| struct WriteItem { | ||
| std::string key; | ||
| std::string filename; // "model.ckpt" or "optimizer.ckpt" | ||
| uint64_t offset = 0; // Planned byte offset in the checkpoint file. | ||
| uint64_t byte_size = 0; // Tensor payload size in bytes. | ||
| DataType dtype = DataType::kFLOAT32; | ||
| std::vector<int64_t> local_shape; | ||
| std::vector<int64_t> global_offset; | ||
| std::vector<int> axis_fragmentations; | ||
| int rank = 0; | ||
| }; | ||
|
|
||
| // Build the local tensor write layout from a ShardedStateDict. | ||
| class SavePlanner { | ||
| public: | ||
| static std::vector<WriteItem> Plan(const ShardedStateDict &sd, int rank); | ||
| }; | ||
|
|
||
| ShardedStateDict | ||
| BuildOptimizerShardedStateDict(const ShardedStateDict &model_state, | ||
| const std::unordered_map<std::string, std::shared_ptr<Tensor>> &optimizer_state); | ||
|
|
||
| // Return the number of payload bytes required by a tensor. | ||
| inline uint64_t TensorByteSize(DataType dtype, const std::vector<int64_t> &shape) { | ||
| uint64_t numel = 1; | ||
| for (auto d : shape) { numel *= static_cast<uint64_t>(d); } | ||
| switch (dtype) { | ||
| case DataType::kBFLOAT16: | ||
| case DataType::kFLOAT16: | ||
| return numel * 2; | ||
| case DataType::kFLOAT32: | ||
| return numel * 4; | ||
| case DataType::kFLOAT64: | ||
| case DataType::kINT64: | ||
| case DataType::kUINT64: | ||
| return numel * 8; | ||
| case DataType::kINT32: | ||
| case DataType::kUINT32: | ||
| return numel * 4; | ||
| case DataType::kINT16: | ||
| case DataType::kUINT16: | ||
| return numel * 2; | ||
| case DataType::kINT8: | ||
| case DataType::kUINT8: | ||
| case DataType::kBOOL: | ||
| return numel; | ||
| default: | ||
| return numel * 4; | ||
| } | ||
| } | ||
|
|
||
| // Compute one rank's balanced interval, including non-divisible dimensions. | ||
| inline std::pair<int64_t, int64_t> GetRankSliceRange(int64_t global_size, int world_size, int rank) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个函数没有被使用,可以删掉。 |
||
| int64_t per_rank = global_size / world_size; | ||
| int64_t remainder = global_size % world_size; | ||
| int64_t start = rank * per_rank + std::min<int64_t>(rank, remainder); | ||
| int64_t local_size = per_rank + (rank < remainder ? 1 : 0); | ||
| return {start, local_size}; | ||
| } | ||
|
|
||
| } // namespace infini_train::checkpoint | ||
This file contains hidden or 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,56 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <map> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "glog/logging.h" | ||
|
|
||
| #include "infini_train/include/datatype.h" | ||
|
|
||
| namespace infini_train::checkpoint { | ||
|
|
||
| struct ShardSegment { | ||
| int64_t global_offset = 0; | ||
| int64_t local_offset = 0; | ||
| int64_t length = 0; | ||
|
|
||
| bool operator==(const ShardSegment &other) const = default; | ||
| }; | ||
|
|
||
| // Logical tensor shard metadata, aligned with Megatron-LM's ShardedTensor model. | ||
| struct ShardedTensor { | ||
| std::string key; | ||
| std::string local_key; | ||
| DataType dtype = DataType::kFLOAT32; | ||
| std::vector<int64_t> global_shape; | ||
| std::vector<int64_t> local_shape; | ||
| std::vector<int64_t> global_offset; | ||
| std::vector<int> axis_fragmentations; | ||
| // Optional disjoint regions along the single fragmented axis. This is used | ||
| // by layouts such as rank-local [Q, K, V], which are not one contiguous | ||
| // slice of the logical global [Q, K, V] tensor. | ||
| std::vector<ShardSegment> segments; | ||
|
|
||
| bool operator==(const ShardedTensor &other) const { | ||
| return key == other.key && local_key == other.local_key && dtype == other.dtype | ||
| && global_shape == other.global_shape && local_shape == other.local_shape | ||
| && global_offset == other.global_offset && axis_fragmentations == other.axis_fragmentations | ||
| && segments == other.segments; | ||
| } | ||
| }; | ||
|
|
||
| struct ShardedStateDict { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ShardedStateDict 既作为方法名又作为类型名,可以给方法改个名字 BuildShardedStateDict()。 |
||
| std::map<std::string, ShardedTensor> tensors; | ||
|
|
||
| void Merge(ShardedStateDict &&other) { | ||
| for (auto &[key, info] : other.tensors) { | ||
| const auto display_key = key; | ||
| const auto [_, inserted] = tensors.emplace(std::move(key), std::move(info)); | ||
| CHECK(inserted) << "Duplicate sharded state-dict key: " << display_key; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace infini_train::checkpoint | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
复用已有的 kDataTypeToSize 函数即可。