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

Fix PeftSavingCallback #24

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
17 changes: 13 additions & 4 deletions tuning/sft_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from tuning.aim_loader import get_aimstack_callback
from transformers.utils import logging
from transformers.modeling_utils import unwrap_model, PreTrainedModel
from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR
from dataclasses import asdict
from typing import Optional, Union

Expand All @@ -22,11 +24,18 @@

class PeftSavingCallback(TrainerCallback):
def on_save(self, args, state, control, **kwargs):
checkpoint_path = os.path.join(args.output_dir, f"checkpoint-{state.global_step}")
kwargs["model"].save_pretrained(checkpoint_path)

if "pytorch_model.bin" in os.listdir(checkpoint_path):
os.remove(os.path.join(checkpoint_path, "pytorch_model.bin"))
# in case of peft_config=None, (i.e. full parameter tuning), there is no adapter needed to save separately
if isinstance(kwargs["model"], PreTrainedModel) or isinstance(unwrap_model(kwargs["model"]), PreTrainedModel):
return

checkpoint_folder = os.path.join(args.output_dir, f"{PREFIX_CHECKPOINT_DIR}-{state.global_step}")
peft_model_path = os.path.join(checkpoint_folder, "adapter_model")
kwargs["model"].save_pretrained(peft_model_path)

pytorch_model_path = os.path.join(checkpoint_folder, "pytorch_model.bin")
if os.path.exists(pytorch_model_path):
os.remove(pytorch_model_path)


def train(
Expand Down