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

add init_latest argument #375

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions mace/cli/run_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,21 @@ def main() -> None:
)

start_epoch = 0
if args.init_latest:
try:
opt_start_epoch = checkpoint_handler.load_latest(
state=tools.CheckpointState(model, optimizer, lr_scheduler),
swa=True,
device=device,
model_only=True,
)
except Exception: # pylint: disable=W0703
opt_start_epoch = checkpoint_handler.load_latest(
state=tools.CheckpointState(model, optimizer, lr_scheduler),
swa=False,
device=device,
model_only=True,
)
if args.restart_latest:
try:
opt_start_epoch = checkpoint_handler.load_latest(
Expand Down
9 changes: 8 additions & 1 deletion mace/tools/arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,14 @@ def build_default_arg_parser() -> argparse.ArgumentParser:
action="store_true",
default=False,
)
parser.add_argument(
parser_restart = parser.add_mutually_exclusive_group()
parser_restart.add_argument(
"--init_latest",
help="initialize model from latest checkpoint",
action="store_true",
default=False,
)
parser_restart.add_argument(
"--restart_latest",
help="restart optimizer from latest checkpoint",
action="store_true",
Expand Down
10 changes: 6 additions & 4 deletions mace/tools/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ def create_checkpoint(state: CheckpointState) -> Checkpoint:

@staticmethod
def load_checkpoint(
state: CheckpointState, checkpoint: Checkpoint, strict: bool
state: CheckpointState, checkpoint: Checkpoint, strict: bool, model_only: bool=False
) -> None:
state.model.load_state_dict(checkpoint["model"], strict=strict) # type: ignore
state.optimizer.load_state_dict(checkpoint["optimizer"])
state.lr_scheduler.load_state_dict(checkpoint["lr_scheduler"])
if not model_only:
state.optimizer.load_state_dict(checkpoint["optimizer"])
state.lr_scheduler.load_state_dict(checkpoint["lr_scheduler"])


@dataclasses.dataclass
Expand Down Expand Up @@ -206,13 +207,14 @@ def load_latest(
swa: Optional[bool] = False,
device: Optional[torch.device] = None,
strict=False,
model_only: bool = False
) -> Optional[int]:
result = self.io.load_latest(swa=swa, device=device)
if result is None:
return None

checkpoint, epochs = result
self.builder.load_checkpoint(state=state, checkpoint=checkpoint, strict=strict)
self.builder.load_checkpoint(state=state, checkpoint=checkpoint, strict=strict, model_only=model_only)
return epochs

def load(
Expand Down