Skip to content
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

[WIP] Fixe FSDP saving error #593

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion fastchat/train/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# 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.
import warnings

import copy
from dataclasses import dataclass, field
Expand All @@ -24,6 +25,8 @@
import transformers
from transformers import Trainer
from transformers.trainer_pt_utils import LabelSmoother
from torch.distributed.fsdp.api import ShardedOptimStateDictConfig, ShardedStateDictConfig, StateDictType
import torch.distributed.fsdp.fully_sharded_data_parallel as FSDP

from fastchat.conversation import get_default_conv_template, SeparatorStyle

Expand Down Expand Up @@ -237,7 +240,18 @@ def train():
trainer = Trainer(
model=model, tokenizer=tokenizer, args=training_args, **data_module
)


# Hao: fix the FSDP save error
if int(torch.__version__[0]) < 2:
warnings.warn("You might run out of GPU memory when you call `safe_save_model_for_hf_trainer()` "
"at the end of your training if you are running on a GPU with less than 40GB memory. "
"Please refer to https://github.com/tatsu-lab/stanford_alpaca/issues/81 about how to "
"work around it.")
else:
FSDP.FullyShardedDataParallel.set_state_dict_type(trainer.model,
StateDictType.SHARDED_STATE_DICT,
ShardedStateDictConfig(offload_to_cpu=True),
ShardedOptimStateDictConfig(offload_to_cpu=True))
if list(pathlib.Path(training_args.output_dir).glob("checkpoint-*")):
trainer.train(resume_from_checkpoint=True)
else:
Expand Down