2020-12-05 13:45:51 +08:00
|
|
|
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
import os
|
2021-08-17 15:29:30 +08:00
|
|
|
|
2020-12-05 13:45:51 +08:00
|
|
|
import paddle
|
|
|
|
from paddle import distributed as dist
|
2020-12-09 15:58:39 +08:00
|
|
|
|
2020-12-05 13:45:51 +08:00
|
|
|
from parakeet.utils import mp_tools
|
|
|
|
|
2020-12-09 15:58:39 +08:00
|
|
|
__all__ = ["load_parameters", "save_parameters"]
|
|
|
|
|
2020-12-05 13:45:51 +08:00
|
|
|
|
2020-12-09 15:58:39 +08:00
|
|
|
def _load_latest_checkpoint(checkpoint_dir: str) -> int:
|
|
|
|
"""Get the iteration number corresponding to the latest saved checkpoint.
|
2020-12-05 13:45:51 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
checkpoint_dir (str): the directory where checkpoint is saved.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int: the latest iteration number.
|
|
|
|
"""
|
|
|
|
checkpoint_record = os.path.join(checkpoint_dir, "checkpoint")
|
|
|
|
if (not os.path.isfile(checkpoint_record)):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# Fetch the latest checkpoint index.
|
2020-12-09 15:58:39 +08:00
|
|
|
with open(checkpoint_record, "rt") as handle:
|
2020-12-05 13:45:51 +08:00
|
|
|
latest_checkpoint = handle.readline().split()[-1]
|
|
|
|
iteration = int(latest_checkpoint.split("-")[-1])
|
|
|
|
|
|
|
|
return iteration
|
|
|
|
|
2020-12-20 13:15:07 +08:00
|
|
|
|
2020-12-09 15:58:39 +08:00
|
|
|
def _save_checkpoint(checkpoint_dir: str, iteration: int):
|
2020-12-05 13:45:51 +08:00
|
|
|
"""Save the iteration number of the latest model to be checkpointed.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
checkpoint_dir (str): the directory where checkpoint is saved.
|
|
|
|
iteration (int): the latest iteration number.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
checkpoint_record = os.path.join(checkpoint_dir, "checkpoint")
|
|
|
|
# Update the latest checkpoint index.
|
2020-12-09 15:58:39 +08:00
|
|
|
with open(checkpoint_record, "wt") as handle:
|
2020-12-05 13:45:51 +08:00
|
|
|
handle.write("model_checkpoint_path: step-{}".format(iteration))
|
|
|
|
|
2020-12-20 13:15:07 +08:00
|
|
|
|
2020-12-05 13:45:51 +08:00
|
|
|
def load_parameters(model,
|
|
|
|
optimizer=None,
|
|
|
|
checkpoint_dir=None,
|
|
|
|
checkpoint_path=None):
|
2021-08-17 15:29:30 +08:00
|
|
|
"""Load a specific model checkpoint from disk.
|
2020-12-05 13:45:51 +08:00
|
|
|
|
|
|
|
Args:
|
2020-12-09 15:58:39 +08:00
|
|
|
model (Layer): model to load parameters.
|
|
|
|
optimizer (Optimizer, optional): optimizer to load states if needed.
|
2020-12-05 13:45:51 +08:00
|
|
|
Defaults to None.
|
|
|
|
checkpoint_dir (str, optional): the directory where checkpoint is saved.
|
|
|
|
checkpoint_path (str, optional): if specified, load the checkpoint
|
2021-08-17 15:29:30 +08:00
|
|
|
stored in the checkpoint_path and the argument 'checkpoint_dir' will
|
|
|
|
be ignored. Defaults to None.
|
2020-12-05 13:45:51 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
iteration (int): number of iterations that the loaded checkpoint has
|
|
|
|
been trained.
|
|
|
|
"""
|
|
|
|
if checkpoint_path is not None:
|
|
|
|
iteration = int(os.path.basename(checkpoint_path).split("-")[-1])
|
|
|
|
elif checkpoint_dir is not None:
|
|
|
|
iteration = _load_latest_checkpoint(checkpoint_dir)
|
|
|
|
if iteration == 0:
|
|
|
|
return iteration
|
|
|
|
checkpoint_path = os.path.join(checkpoint_dir,
|
|
|
|
"step-{}".format(iteration))
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"At least one of 'checkpoint_dir' and 'checkpoint_path' should be specified!"
|
|
|
|
)
|
|
|
|
|
|
|
|
local_rank = dist.get_rank()
|
|
|
|
|
|
|
|
params_path = checkpoint_path + ".pdparams"
|
|
|
|
model_dict = paddle.load(params_path)
|
|
|
|
model.set_state_dict(model_dict)
|
2020-12-20 13:15:07 +08:00
|
|
|
print("[checkpoint] Rank {}: loaded model from {}".format(local_rank,
|
|
|
|
params_path))
|
|
|
|
|
2020-12-05 13:45:51 +08:00
|
|
|
optimizer_path = checkpoint_path + ".pdopt"
|
|
|
|
if optimizer and os.path.isfile(optimizer_path):
|
|
|
|
optimizer_dict = paddle.load(optimizer_path)
|
|
|
|
optimizer.set_state_dict(optimizer_dict)
|
2020-12-20 13:15:07 +08:00
|
|
|
print("[checkpoint] Rank {}: loaded optimizer state from {}".format(
|
|
|
|
local_rank, optimizer_path))
|
2020-12-05 13:45:51 +08:00
|
|
|
|
|
|
|
return iteration
|
|
|
|
|
2020-12-20 13:15:07 +08:00
|
|
|
|
2020-12-05 13:45:51 +08:00
|
|
|
@mp_tools.rank_zero_only
|
|
|
|
def save_parameters(checkpoint_dir, iteration, model, optimizer=None):
|
|
|
|
"""Checkpoint the latest trained model parameters.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
checkpoint_dir (str): the directory where checkpoint is saved.
|
|
|
|
iteration (int): the latest iteration number.
|
2020-12-09 15:58:39 +08:00
|
|
|
model (Layer): model to be checkpointed.
|
|
|
|
optimizer (Optimizer, optional): optimizer to be checkpointed.
|
2020-12-05 13:45:51 +08:00
|
|
|
Defaults to None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
checkpoint_path = os.path.join(checkpoint_dir, "step-{}".format(iteration))
|
2020-12-20 13:15:07 +08:00
|
|
|
|
2020-12-05 13:45:51 +08:00
|
|
|
model_dict = model.state_dict()
|
|
|
|
params_path = checkpoint_path + ".pdparams"
|
|
|
|
paddle.save(model_dict, params_path)
|
|
|
|
print("[checkpoint] Saved model to {}".format(params_path))
|
|
|
|
|
|
|
|
if optimizer:
|
|
|
|
opt_dict = optimizer.state_dict()
|
|
|
|
optimizer_path = checkpoint_path + ".pdopt"
|
|
|
|
paddle.save(opt_dict, optimizer_path)
|
2021-08-17 15:29:30 +08:00
|
|
|
print("[checkpoint] Saved optimzier state to {}".format(optimizer_path))
|
2020-12-05 13:45:51 +08:00
|
|
|
|
|
|
|
_save_checkpoint(checkpoint_dir, iteration)
|