-
Notifications
You must be signed in to change notification settings - Fork 9k
[v1] support training with fsdp2 #9773
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6d74326
add fsdp2 demo
1b4033c
add fsdp2 meta device support
3dd5bfe
add fsdp2 meta device support
6ed7fae
Merge remote-tracking branch 'upstream/main' into main-fsdp2
a175150
dev fsdp2
9f7c723
dev fsdp2
frozenleaves f01873c
Merge remote-tracking branch 'origin/main' into main-fsdp2
frozenleaves 2bf35cc
Merge remote-tracking branch 'origin/main' into main-fsdp2
frozenleaves 9bd7f7e
add fsdp2 support and training example
frozenleaves 335c447
add fsdp2 support and training example
frozenleaves d8f946c
del collator
frozenleaves 6357aa4
add max_steps support, add e2e sft test
frozenleaves edf537e
fix
frozenleaves 1905a0f
fix check license to support 2026
frozenleaves 215702f
fix pin memeory on npu
frozenleaves 83492ac
fix
frozenleaves 41f549b
fix
frozenleaves 3c96b42
fix
frozenleaves 6afff5e
fix
frozenleaves b1d42c1
fix
frozenleaves 46842a3
add ddp fallback
frozenleaves a4a36b1
fix
frozenleaves aa25884
fix ut
frozenleaves cee60c9
fix comment
frozenleaves 46a9c28
Update cli.py
hiyouga 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # convert_hf_to_dcp.py | ||
| import argparse | ||
|
|
||
| import torch | ||
| import torch.distributed.checkpoint as dcp | ||
| from transformers import AutoModelForCausalLM | ||
|
|
||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--hf_path", type=str, required=True) | ||
| parser.add_argument("--dcp_path", type=str, required=True) | ||
| args = parser.parse_args() | ||
|
|
||
| def convert(model_path, save_path): | ||
| print(f"Loading HF model from {model_path}...") | ||
| model = AutoModelForCausalLM.from_pretrained(model_path, device_map="cpu", torch_dtype=torch.bfloat16) | ||
|
|
||
| print(f"Saving to DCP format at {save_path}...") | ||
| dcp.save(model.state_dict(), checkpoint_id=save_path) | ||
| print("Done!") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| convert(args.hf_path, args.dcp_path) | ||
|
frozenleaves marked this conversation as resolved.
|
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,33 @@ | ||
| model: Qwen/Qwen3-0.6B | ||
| trust_remote_code: true | ||
| model_class: llm | ||
|
|
||
| template: qwen3_nothink | ||
|
|
||
| kernel_config: | ||
| name: auto | ||
| include_kernels: auto # choice: null/true/false/auto/kernel_id1,kernel_id2,kernel_id3, default is null | ||
|
|
||
| quant_config: null | ||
|
|
||
| dist_config: | ||
| name: fsdp2 | ||
| dcp_path: null # /mnt/f/pretrain_models/Qwen3-1.7B-dcp | ||
|
|
||
| init_config: | ||
| name: init_on_meta | ||
|
|
||
| ### data | ||
| train_dataset: data/v1_sft_demo.yaml | ||
|
|
||
| ### training | ||
| output_dir: outputs/test_run | ||
| micro_batch_size: 1 | ||
| global_batch_size: 1 | ||
| cutoff_len: 2048 | ||
| learning_rate: 1.0e-4 | ||
| bf16: false | ||
|
|
||
| ### sample | ||
| sample_backend: hf | ||
| max_new_tokens: 128 |
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
|
frozenleaves marked this conversation as resolved.
|
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,36 @@ | ||
| # Copyright 2025 the LlamaFactory team. | ||
| # | ||
| # 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 __future__ import annotations | ||
|
|
||
| from ....config.arg_utils import PluginConfig | ||
| from ....utils.plugin import BasePlugin | ||
| from ....utils.types import HFModel | ||
|
|
||
|
|
||
| class DistributedPlugin(BasePlugin): | ||
| def __call__(self, model: HFModel, dist_config: PluginConfig, **kwargs) -> HFModel: | ||
| return super().__call__(model, dist_config, **kwargs) | ||
|
|
||
|
|
||
| @DistributedPlugin("fsdp2").register() | ||
| def shard_model_fsdp2(model: HFModel, dist_config: PluginConfig) -> HFModel: | ||
| from .fsdp2 import FSDP2Engine | ||
|
|
||
| return FSDP2Engine(dist_config).shard_model(model) | ||
|
|
||
|
|
||
| @DistributedPlugin("deepspeed").register() | ||
| def shard_model_deepspeed(model: HFModel, dist_config: PluginConfig) -> HFModel: | ||
| return model |
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.
Uh oh!
There was an error while loading. Please reload this page.