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

Loader ckp fixes #119

Merged
merged 7 commits into from
Oct 11, 2024
Merged
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
32 changes: 21 additions & 11 deletions fms_fsdp/utils/checkpointing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,45 @@
from torch.distributed.fsdp import StateDictType


def get_latest(targdir, qualifier=lambda x: True):
"""Fetch the latest file or folder written to target directory, subject to name passing the qualifier fn.
If directory is empty or nonexistent or no items qualify, return None."""
def get_latest(targdir, qualifier=lambda x: True, key=os.path.getctime):
"""
Fetch the full path of the latest file or folder written to target directory,
subject to name passing the qualifier fn.
Optional key fn can be used for custom sorting.
Both functions take full path arguments.
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]),
key=key,
)
return os.path.join(targdir, latest)
return latest
return None


def get_oldest(targdir, qualifier=lambda x: True):
"""Fetch the oldest file or folder written to target directory, subject to name passing the qualifier fn.
If directory is empty or nonexistent or no items qualify, return None."""
def get_oldest(targdir, qualifier=lambda x: True, key=os.path.getctime):
"""
Fetch the full path of the oldest file or folder written to target directory,
subject to name passing the qualifier fn.
Optional key fn can be used for custom sorting.
Both functions take full path arguments.
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))
],
key=os.path.getctime,
key=key,
)
return os.path.join(targdir, oldest)
return oldest
return None


Expand Down Expand Up @@ -118,7 +128,7 @@ def _cleanup(self):
ckp_to_remove = Path(
get_oldest(self.ckp_path, qualifier=lambda x: "tmp" in x)
)
if os.path.is_file(ckp_to_remove):
if os.path.isfile(ckp_to_remove):
ckp_to_remove.unlink()
else:
shutil.rmtree(ckp_to_remove)
Expand Down
6 changes: 3 additions & 3 deletions fms_fsdp/utils/dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
rescaling (i.e. counters, RNG states), and `reshard_params`, which are lists that can be
re-distributed over workers (i.e. buffers).

Our loaders obey the following type heirarchy:
Our loaders obey the following type hierarchy:
torch.data.IterableDataset -> _StatefulDataset -> _WrapperDataset.
`_StatefulDataset` implements state and checkpointing logic. A `_WrapperDataset` holds a
single `_StatefulDataset` and iterates via calling its wrapped dataset any number of times,
Expand Down Expand Up @@ -510,8 +510,8 @@ def _validate_ckp_path(self, path: str, verbose: bool = False):
f" Dataset: No valid checkpoint detected at {path}, dataset starting from scratch."
)
return ""
# Check latest path
latest = os.path.join(path, get_latest(path))
# Check latest path, using ckp naming syntax
latest = get_latest(path, key=lambda path: int(path.split("_")[-2]))
if verbose:
self.report(f"Checkpoint detected at {latest}")
# If item is not a folder, exit early
Expand Down
9 changes: 5 additions & 4 deletions speculator/train_speculator_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import re
import time
from typing import Any, Callable, Mapping, MutableMapping, Optional, Tuple, Union
from typing import Any, Callable, List, MutableMapping, Optional, Tuple, Union

import torch
import torch.distributed as dist
Expand Down Expand Up @@ -437,11 +437,12 @@ class EmbedGPTBigCode(GPTBigCode):
# Overrides the forward function of GPTBigCode to allow returning embedding vectors
def forward(
self,
x: torch.LongTensor,
x: torch.Tensor,
mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_value_states: Optional[Tuple[torch.FloatTensor,]] = None,
position_ids: Optional[torch.Tensor] = None,
past_key_value_states: Optional[List[Tuple[torch.Tensor, torch.Tensor]]] = None,
use_cache: bool = False,
only_last_token: bool = False,
attn_algorithm: Optional[str] = None,
include_embeds: bool = False,
):
Expand Down
Loading