Skip to content
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
2 changes: 1 addition & 1 deletion projects/cope/src/utils/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def save(

path = cfg.checkpoint
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
logging.info(f"saving epoch {epoch_num} to {path}")

Expand Down
36 changes: 36 additions & 0 deletions projects/cope/tests/test_checkpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import importlib.util
import os
from pathlib import Path
import tempfile
import types
import unittest

ROOT = Path(__file__).resolve().parents[1] / "src" / "utils"


def load(name):
spec = importlib.util.spec_from_file_location("cope_checkpoint", ROOT / name)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module


class RegressionTests(unittest.TestCase):
def test_checkpoint_without_parent_directory(self):
module = load('checkpoint.py')
cfg = types.SimpleNamespace(checkpoint_freq=1, distributed=False, rank=0,
checkpoint='checkpoint.pt', checkpoint_keep=None, valid_metric=None)
stateful = types.SimpleNamespace(state_dict=lambda: {'a': 1}, get_state=lambda: {'a': 1})
old_cwd = os.getcwd()
try:
with tempfile.TemporaryDirectory() as tmp:
os.chdir(tmp)
module.save(cfg, stateful, stateful, stateful, stateful, 0)
self.assertTrue(Path('checkpoint.pt').is_file())
self.assertTrue(Path('checkpoint.pt.cfg').is_file())
finally:
os.chdir(old_cwd)


if __name__ == "__main__":
unittest.main()