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

[Trainer] ignore_save_lr_and_optim #7978

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 28 additions & 26 deletions paddlenlp/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2100,36 +2100,38 @@
else:
self.save_model(output_dir)

optimizer_name = _add_variant(OPTIMIZER_NAME, self.args.optimizer_name_suffix)
# only save model state dict, ignore optimizer and scheduler
if not self.args.ignore_save_lr_and_optim:
optimizer_name = _add_variant(OPTIMIZER_NAME, self.args.optimizer_name_suffix)

if self.args.use_hybrid_parallel:
if self.dp_group.rank <= 0:
os.makedirs(output_dir, exist_ok=True)
logger.info("Saving optimizer files.")
if self.args.unified_checkpoint:
save_unified_optimizer(
self.args,
self.model,
self.optimizer,
output_dir,
safe_serialization=True,
)
else:
self._save_ckpt_func(
self.optimizer.state_dict(),
os.path.join(output_dir, optimizer_name),
)
if self.args.use_hybrid_parallel:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZHUI 帮忙确认下这个改动是否对自动并行生肖

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不影响

if self.dp_group.rank <= 0:
os.makedirs(output_dir, exist_ok=True)
logger.info("Saving optimizer files.")
if self.args.unified_checkpoint:
save_unified_optimizer(

Check warning on line 2112 in paddlenlp/trainer/trainer.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/trainer/trainer.py#L2108-L2112

Added lines #L2108 - L2112 were not covered by tests
self.args,
self.model,
self.optimizer,
output_dir,
safe_serialization=True,
)
else:
self._save_ckpt_func(

Check warning on line 2120 in paddlenlp/trainer/trainer.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/trainer/trainer.py#L2120

Added line #L2120 was not covered by tests
self.optimizer.state_dict(),
os.path.join(output_dir, optimizer_name),
)

if self.args.should_save:
if not self.args.use_hybrid_parallel:
logger.info("Saving optimizer files.")
self._save_ckpt_func(self.optimizer.state_dict(), os.path.join(output_dir, OPTIMIZER_NAME))
if self.args.should_save:
if not self.args.use_hybrid_parallel:
logger.info("Saving optimizer files.")
self._save_ckpt_func(self.optimizer.state_dict(), os.path.join(output_dir, OPTIMIZER_NAME))

# FIXME: maybe only save one copy
paddle.save(self.lr_scheduler.state_dict(), os.path.join(output_dir, SCHEDULER_NAME))
# FIXME: maybe only save one copy
paddle.save(self.lr_scheduler.state_dict(), os.path.join(output_dir, SCHEDULER_NAME))

if self.do_grad_scaling:
paddle.save(self.scaler.state_dict(), os.path.join(output_dir, SCALER_NAME))
if self.do_grad_scaling:
paddle.save(self.scaler.state_dict(), os.path.join(output_dir, SCALER_NAME))

Check warning on line 2134 in paddlenlp/trainer/trainer.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/trainer/trainer.py#L2134

Added line #L2134 was not covered by tests

self.runtime_timer.stop()
# Determine the new best metric / best model checkpoint
Expand Down
4 changes: 4 additions & 0 deletions paddlenlp/trainer/training_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,10 @@ class TrainingArguments:
default=False,
metadata={"help": "whether to ignore load optimizer and scheduler."},
)
ignore_save_lr_and_optim: Optional[bool] = field(
default=False,
metadata={"help": "whether to ignore save optimizer and scheduler."},
)
force_reshard_pp: Optional[bool] = field(
default=False,
metadata={"help": "reshard pp even if pp degree in the model and pp degree in script match"},
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/llm/finetune.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ finetune:
save_total_limit: 1
tensor_parallel_degree: 1
pipeline_parallel_degree: 1
ignore_save_lr_and_optim: 1
JunnYu marked this conversation as resolved.
Show resolved Hide resolved

default:
llama:
model_name_or_path: __internal_testing__/tiny-random-llama
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/llm/lora.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ lora:
save_total_limit: 1
tensor_parallel_degree: 1
pipeline_parallel_degree: 1
ignore_save_lr_and_optim: 1
lora: true

default:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/llm/prefix_tuning.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ prefix_tuning:
save_total_limit: 1
tensor_parallel_degree: 1
pipeline_parallel_degree: 1
ignore_save_lr_and_optim: 1
prefix_tuning: true

default:
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/llm/pretrain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pretrain:
use_flash_attention: 0
use_fused_rms_norm: 0
continue_training: 1
ignore_save_lr_and_optim: 1

default:
llama:
model_name_or_path: __internal_testing__/tiny-random-llama
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/llm/ptq.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ ptq:
eval_with_do_generation: false
do_ptq: true
ptq_step: 4
ignore_save_lr_and_optim: 1

default:
llama:
model_name_or_path: __internal_testing__/tiny-fused-llama-inference5.2
Expand Down
Loading