You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
299 lines
13 KiB
299 lines
13 KiB
4 years ago
|
# Copyright (c) 2021 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.
|
||
4 years ago
|
import glob
|
||
4 years ago
|
import json
|
||
4 years ago
|
import os
|
||
4 years ago
|
import re
|
||
4 years ago
|
from pathlib import Path
|
||
4 years ago
|
from typing import Text
|
||
4 years ago
|
from typing import Union
|
||
4 years ago
|
|
||
|
import paddle
|
||
|
from paddle import distributed as dist
|
||
|
from paddle.optimizer import Optimizer
|
||
|
|
||
3 years ago
|
from paddlespeech.s2t.utils import mp_tools
|
||
|
from paddlespeech.s2t.utils.log import Log
|
||
4 years ago
|
|
||
4 years ago
|
logger = Log(__name__).getlog()
|
||
4 years ago
|
|
||
4 years ago
|
__all__ = ["Checkpoint"]
|
||
4 years ago
|
|
||
4 years ago
|
|
||
4 years ago
|
class Checkpoint():
|
||
4 years ago
|
def __init__(self, kbest_n: int=5, latest_n: int=1):
|
||
4 years ago
|
self.best_records: Mapping[Path, float] = {}
|
||
4 years ago
|
self.latest_records = []
|
||
|
self.kbest_n = kbest_n
|
||
|
self.latest_n = latest_n
|
||
|
self._save_all = (kbest_n == -1)
|
||
4 years ago
|
|
||
3 years ago
|
def save_parameters(self,
|
||
|
checkpoint_dir,
|
||
|
tag_or_iteration: Union[int, Text],
|
||
|
model: paddle.nn.Layer,
|
||
|
optimizer: Optimizer=None,
|
||
|
infos: dict=None,
|
||
|
metric_type="val_loss"):
|
||
4 years ago
|
"""Save checkpoint in best_n and latest_n.
|
||
|
|
||
|
Args:
|
||
|
checkpoint_dir (str): the directory where checkpoint is saved.
|
||
|
tag_or_iteration (int or str): the latest iteration(step or epoch) number or tag.
|
||
4 years ago
|
model (Layer): model to be checkpointed.
|
||
4 years ago
|
optimizer (Optimizer, optional): optimizer to be checkpointed.
|
||
|
infos (dict or None)): any info you want to save.
|
||
|
metric_type (str, optional): metric type. Defaults to "val_loss".
|
||
|
"""
|
||
4 years ago
|
if (metric_type not in infos.keys()):
|
||
4 years ago
|
self._save_parameters(checkpoint_dir, tag_or_iteration, model,
|
||
4 years ago
|
optimizer, infos)
|
||
4 years ago
|
return
|
||
|
|
||
|
#save best
|
||
4 years ago
|
if self._should_save_best(infos[metric_type]):
|
||
|
self._save_best_checkpoint_and_update(
|
||
4 years ago
|
infos[metric_type], checkpoint_dir, tag_or_iteration, model,
|
||
|
optimizer, infos)
|
||
4 years ago
|
#save latest
|
||
4 years ago
|
self._save_latest_checkpoint_and_update(
|
||
|
checkpoint_dir, tag_or_iteration, model, optimizer, infos)
|
||
4 years ago
|
|
||
4 years ago
|
if isinstance(tag_or_iteration, int):
|
||
4 years ago
|
self._save_checkpoint_record(checkpoint_dir, tag_or_iteration)
|
||
4 years ago
|
|
||
4 years ago
|
def load_parameters(self,
|
||
4 years ago
|
model,
|
||
|
optimizer=None,
|
||
|
checkpoint_dir=None,
|
||
|
checkpoint_path=None,
|
||
|
record_file="checkpoint_latest"):
|
||
|
"""Load a last model checkpoint from disk.
|
||
4 years ago
|
Args:
|
||
|
model (Layer): model to load parameters.
|
||
|
optimizer (Optimizer, optional): optimizer to load states if needed.
|
||
|
Defaults to None.
|
||
|
checkpoint_dir (str, optional): the directory where checkpoint is saved.
|
||
|
checkpoint_path (str, optional): if specified, load the checkpoint
|
||
4 years ago
|
stored in the checkpoint_path(prefix) and the argument 'checkpoint_dir' will
|
||
|
be ignored. Defaults to None.
|
||
4 years ago
|
record_file "checkpoint_latest" or "checkpoint_best"
|
||
|
Returns:
|
||
|
configs (dict): epoch or step, lr and other meta info should be saved.
|
||
|
"""
|
||
|
configs = {}
|
||
|
|
||
|
if checkpoint_path is not None:
|
||
|
pass
|
||
|
elif checkpoint_dir is not None and record_file is not None:
|
||
|
# load checkpint from record file
|
||
|
checkpoint_record = os.path.join(checkpoint_dir, record_file)
|
||
|
iteration = self._load_checkpoint_idx(checkpoint_record)
|
||
|
if iteration == -1:
|
||
|
return configs
|
||
|
checkpoint_path = os.path.join(checkpoint_dir,
|
||
|
"{}".format(iteration))
|
||
|
else:
|
||
|
raise ValueError(
|
||
|
"At least one of 'checkpoint_path' or 'checkpoint_dir' should be specified!"
|
||
|
)
|
||
|
|
||
|
rank = dist.get_rank()
|
||
|
|
||
|
params_path = checkpoint_path + ".pdparams"
|
||
|
model_dict = paddle.load(params_path)
|
||
|
model.set_state_dict(model_dict)
|
||
3 years ago
|
logger.info("Rank {}: Restore model from {}".format(rank, params_path))
|
||
4 years ago
|
|
||
|
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)
|
||
3 years ago
|
logger.info("Rank {}: Restore optimizer state from {}".format(
|
||
4 years ago
|
rank, optimizer_path))
|
||
|
|
||
|
info_path = re.sub('.pdparams$', '.json', params_path)
|
||
|
if os.path.exists(info_path):
|
||
|
with open(info_path, 'r') as fin:
|
||
|
configs = json.load(fin)
|
||
|
return configs
|
||
4 years ago
|
|
||
4 years ago
|
def load_latest_parameters(self,
|
||
4 years ago
|
model,
|
||
|
optimizer=None,
|
||
|
checkpoint_dir=None,
|
||
|
checkpoint_path=None):
|
||
4 years ago
|
"""Load a last model checkpoint from disk.
|
||
4 years ago
|
Args:
|
||
|
model (Layer): model to load parameters.
|
||
|
optimizer (Optimizer, optional): optimizer to load states if needed.
|
||
|
Defaults to None.
|
||
|
checkpoint_dir (str, optional): the directory where checkpoint is saved.
|
||
|
checkpoint_path (str, optional): if specified, load the checkpoint
|
||
4 years ago
|
stored in the checkpoint_path(prefix) and the argument 'checkpoint_dir' will
|
||
|
be ignored. Defaults to None.
|
||
4 years ago
|
Returns:
|
||
|
configs (dict): epoch or step, lr and other meta info should be saved.
|
||
|
"""
|
||
4 years ago
|
return self.load_parameters(model, optimizer, checkpoint_dir,
|
||
|
checkpoint_path, "checkpoint_latest")
|
||
4 years ago
|
|
||
|
def load_best_parameters(self,
|
||
4 years ago
|
model,
|
||
|
optimizer=None,
|
||
|
checkpoint_dir=None,
|
||
|
checkpoint_path=None):
|
||
4 years ago
|
"""Load a last model checkpoint from disk.
|
||
4 years ago
|
Args:
|
||
|
model (Layer): model to load parameters.
|
||
|
optimizer (Optimizer, optional): optimizer to load states if needed.
|
||
|
Defaults to None.
|
||
|
checkpoint_dir (str, optional): the directory where checkpoint is saved.
|
||
|
checkpoint_path (str, optional): if specified, load the checkpoint
|
||
4 years ago
|
stored in the checkpoint_path(prefix) and the argument 'checkpoint_dir' will
|
||
|
be ignored. Defaults to None.
|
||
4 years ago
|
Returns:
|
||
|
configs (dict): epoch or step, lr and other meta info should be saved.
|
||
|
"""
|
||
4 years ago
|
return self.load_parameters(model, optimizer, checkpoint_dir,
|
||
|
checkpoint_path, "checkpoint_best")
|
||
4 years ago
|
|
||
|
def _should_save_best(self, metric: float) -> bool:
|
||
|
if not self._best_full():
|
||
|
return True
|
||
|
|
||
|
# already full
|
||
|
worst_record_path = max(self.best_records, key=self.best_records.get)
|
||
|
# worst_record_path = max(self.best_records.iteritems(), key=operator.itemgetter(1))[0]
|
||
|
worst_metric = self.best_records[worst_record_path]
|
||
|
return metric < worst_metric
|
||
|
|
||
|
def _best_full(self):
|
||
|
return (not self._save_all) and len(self.best_records) == self.kbest_n
|
||
|
|
||
|
def _latest_full(self):
|
||
|
return len(self.latest_records) == self.latest_n
|
||
|
|
||
4 years ago
|
def _save_best_checkpoint_and_update(self, metric, checkpoint_dir,
|
||
4 years ago
|
tag_or_iteration, model, optimizer,
|
||
|
infos):
|
||
4 years ago
|
# remove the worst
|
||
4 years ago
|
if self._best_full():
|
||
4 years ago
|
worst_record_path = max(self.best_records,
|
||
|
key=self.best_records.get)
|
||
|
self.best_records.pop(worst_record_path)
|
||
4 years ago
|
if (worst_record_path not in self.latest_records):
|
||
|
logger.info(
|
||
|
"remove the worst checkpoint: {}".format(worst_record_path))
|
||
4 years ago
|
self._del_checkpoint(checkpoint_dir, worst_record_path)
|
||
4 years ago
|
|
||
|
# add the new one
|
||
4 years ago
|
self._save_parameters(checkpoint_dir, tag_or_iteration, model,
|
||
|
optimizer, infos)
|
||
4 years ago
|
self.best_records[tag_or_iteration] = metric
|
||
4 years ago
|
|
||
4 years ago
|
def _save_latest_checkpoint_and_update(
|
||
4 years ago
|
self, checkpoint_dir, tag_or_iteration, model, optimizer, infos):
|
||
4 years ago
|
# remove the old
|
||
4 years ago
|
if self._latest_full():
|
||
4 years ago
|
to_del_fn = self.latest_records.pop(0)
|
||
4 years ago
|
if (to_del_fn not in self.best_records.keys()):
|
||
|
logger.info(
|
||
|
"remove the latest checkpoint: {}".format(to_del_fn))
|
||
4 years ago
|
self._del_checkpoint(checkpoint_dir, to_del_fn)
|
||
4 years ago
|
self.latest_records.append(tag_or_iteration)
|
||
4 years ago
|
|
||
4 years ago
|
self._save_parameters(checkpoint_dir, tag_or_iteration, model,
|
||
|
optimizer, infos)
|
||
4 years ago
|
|
||
4 years ago
|
def _del_checkpoint(self, checkpoint_dir, tag_or_iteration):
|
||
4 years ago
|
checkpoint_path = os.path.join(checkpoint_dir,
|
||
4 years ago
|
"{}".format(tag_or_iteration))
|
||
|
for filename in glob.glob(checkpoint_path + ".*"):
|
||
4 years ago
|
os.remove(filename)
|
||
4 years ago
|
logger.info("delete file: {}".format(filename))
|
||
4 years ago
|
|
||
4 years ago
|
def _load_checkpoint_idx(self, checkpoint_record: str) -> int:
|
||
4 years ago
|
"""Get the iteration number corresponding to the latest saved checkpoint.
|
||
|
Args:
|
||
4 years ago
|
checkpoint_path (str): the saved path of checkpoint.
|
||
4 years ago
|
Returns:
|
||
|
int: the latest iteration number. -1 for no checkpoint to load.
|
||
|
"""
|
||
|
if not os.path.isfile(checkpoint_record):
|
||
|
return -1
|
||
|
|
||
|
# Fetch the latest checkpoint index.
|
||
|
with open(checkpoint_record, "rt") as handle:
|
||
|
latest_checkpoint = handle.readlines()[-1].strip()
|
||
|
iteration = int(latest_checkpoint.split(":")[-1])
|
||
|
return iteration
|
||
|
|
||
4 years ago
|
def _save_checkpoint_record(self, checkpoint_dir: str, iteration: int):
|
||
4 years ago
|
"""Save the iteration number of the latest model to be checkpoint record.
|
||
|
Args:
|
||
|
checkpoint_dir (str): the directory where checkpoint is saved.
|
||
|
iteration (int): the latest iteration number.
|
||
|
Returns:
|
||
|
None
|
||
|
"""
|
||
4 years ago
|
checkpoint_record_latest = os.path.join(checkpoint_dir,
|
||
|
"checkpoint_latest")
|
||
4 years ago
|
checkpoint_record_best = os.path.join(checkpoint_dir, "checkpoint_best")
|
||
4 years ago
|
|
||
4 years ago
|
with open(checkpoint_record_best, "w") as handle:
|
||
|
for i in self.best_records.keys():
|
||
|
handle.write("model_checkpoint_path:{}\n".format(i))
|
||
4 years ago
|
with open(checkpoint_record_latest, "w") as handle:
|
||
|
for i in self.latest_records:
|
||
4 years ago
|
handle.write("model_checkpoint_path:{}\n".format(i))
|
||
|
|
||
|
@mp_tools.rank_zero_only
|
||
4 years ago
|
def _save_parameters(self,
|
||
4 years ago
|
checkpoint_dir: str,
|
||
|
tag_or_iteration: Union[int, str],
|
||
|
model: paddle.nn.Layer,
|
||
|
optimizer: Optimizer=None,
|
||
|
infos: dict=None):
|
||
4 years ago
|
"""Checkpoint the latest trained model parameters.
|
||
|
Args:
|
||
|
checkpoint_dir (str): the directory where checkpoint is saved.
|
||
|
tag_or_iteration (int or str): the latest iteration(step or epoch) number.
|
||
|
model (Layer): model to be checkpointed.
|
||
|
optimizer (Optimizer, optional): optimizer to be checkpointed.
|
||
|
Defaults to None.
|
||
|
infos (dict or None): any info you want to save.
|
||
|
Returns:
|
||
|
None
|
||
|
"""
|
||
|
checkpoint_path = os.path.join(checkpoint_dir,
|
||
4 years ago
|
"{}".format(tag_or_iteration))
|
||
4 years ago
|
|
||
|
model_dict = model.state_dict()
|
||
|
params_path = checkpoint_path + ".pdparams"
|
||
|
paddle.save(model_dict, params_path)
|
||
|
logger.info("Saved model to {}".format(params_path))
|
||
|
|
||
|
if optimizer:
|
||
|
opt_dict = optimizer.state_dict()
|
||
|
optimizer_path = checkpoint_path + ".pdopt"
|
||
|
paddle.save(opt_dict, optimizer_path)
|
||
|
logger.info("Saved optimzier state to {}".format(optimizer_path))
|
||
|
|
||
|
info_path = re.sub('.pdparams$', '.json', params_path)
|
||
|
infos = {} if infos is None else infos
|
||
|
with open(info_path, 'w') as fout:
|
||
|
data = json.dumps(infos)
|
||
|
fout.write(data)
|