Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
14 changes: 3 additions & 11 deletions fms_fsdp/utils/checkpointing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ def get_latest(targdir, qualifier=lambda x: True):
If directory is empty or nonexistent or no items qualify, return None."""
if os.path.exists(targdir) and len(os.listdir(targdir)) > 0:
latest = max(
[
os.path.join(targdir, x)
for x in os.listdir(targdir)
if qualifier(os.path.join(targdir, x))
],
key=lambda path: int(path.split("/")[-1].split("_")[1]),
[x for x in os.listdir(targdir) if qualifier(os.path.join(targdir, x))],
key=lambda path: int(path.split("_")[1]),
Copy link
Contributor

Choose a reason for hiding this comment

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

probably the same issue below with get_oldest ?

maybe we could add a unit test?

Copy link
Author

Choose a reason for hiding this comment

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

Ok, removed os.path.join in the get_oldest function in commit 4a2b4b8, and added unit tests in commit f082164.

Copy link
Contributor

Choose a reason for hiding this comment

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

looks better, though now the ctime key fails in get_oldest

Copy link
Author

Choose a reason for hiding this comment

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

Ok, have pushed a fix, can you run the tests again to see if it works?

)
return os.path.join(targdir, latest)
return None
Expand All @@ -41,11 +37,7 @@ def get_oldest(targdir, qualifier=lambda x: True):
If directory is empty or nonexistent or no items qualify, return None."""
if os.path.exists(targdir) and len(os.listdir(targdir)) > 0:
oldest = min(
[
os.path.join(targdir, x)
for x in os.listdir(targdir)
if qualifier(os.path.join(targdir, x))
],
[x for x in os.listdir(targdir) if qualifier(os.path.join(targdir, x))],
key=os.path.getctime,
)
return os.path.join(targdir, oldest)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_utils_checkpointing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
Tests for functions in utils/checkpointing_utils.py
"""

import os
import tempfile

from fms_fsdp.utils.checkpointing_utils import get_latest, get_oldest


def test_get_oldest():
"""
Ensure that the get_oldest function returns the name of the file with the oldest
timestamp (i.e. that was created first).
"""
with tempfile.TemporaryDirectory() as tempdir:
for i in range(3):
filename = os.path.join(tempdir, f"file_{i}")
print("random content", file=open(file=filename, mode="w"))

oldest_filename = get_oldest(targdir=tempdir)
assert oldest_filename.endswith("file_0")


def test_get_latest():
"""
Ensure that the get_latest function returns the name of the file with the latest
integer suffix (i.e. that was created last).
"""
with tempfile.TemporaryDirectory() as tempdir:
for i in range(3):
filename = os.path.join(tempdir, f"file_{i}")
print("random content", file=open(file=filename, mode="w"))

latest_filename = get_latest(targdir=tempdir)
assert latest_filename.endswith("file_2")