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

Enable optional checkpoint at loading #819

Merged
merged 12 commits into from
Feb 7, 2025

Conversation

mori360
Copy link
Contributor

@mori360 mori360 commented Feb 4, 2025

Add argument "--checkpoint.exclude" to provide users to exclude specific keys from being loaded from the checkpoint.

  1. if checkpoint.exclude contains "dataloder", users could load with different dp_degree as dataloader is excluded without resharding
  2. if checkpoint.exclude contains "lr_scheduler", lr_scheduler would count from step 0

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 4, 2025
@mori360 mori360 force-pushed the optional_checkpoint branch from 4491e62 to 58466d5 Compare February 4, 2025 23:02
@@ -169,12 +169,6 @@ def __init__(
into one state dict before saving/loading. We rely on the individual state_dicts to not collide,
which is gauranteed for the model by correct pipeline splitting and for the optimizer by the flattening
support described in (1).

3. LR schedulers also index model states like optimizers and would need to be flattened properly to support
Copy link
Contributor Author

@mori360 mori360 Feb 4, 2025

Choose a reason for hiding this comment

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

lr_scheduler flatten at #794

Copy link
Contributor

Choose a reason for hiding this comment

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

We should add a comment here to say the lr_scheduler resharding assumes that all lr_schedulers are the same.

@@ -511,6 +511,16 @@ def __init__(self):
default=-1,
help="Load the checkpoint at the specified step. If -1, load the latest checkpoint.",
)
self.parser.add_argument(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

currently checkpoint.exclude only support excluding at loading, shall we use argument like exclude_from_loading?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, exclude_from_loading is more explicit.

],
"Optional checkpoint",
"optional_checkpoint",
),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

add integration test here, especially for that optional checkpoint at dataloader could avoid dp_degree mismatch error before and after checkpoint

@mori360 mori360 requested review from tianyu-l and fegin February 5, 2025 00:29
@mori360 mori360 marked this pull request as ready for review February 5, 2025 00:29
Copy link
Contributor

@fegin fegin left a comment

Choose a reason for hiding this comment

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

LGTM, but please change the comments

@@ -169,12 +169,6 @@ def __init__(
into one state dict before saving/loading. We rely on the individual state_dicts to not collide,
which is gauranteed for the model by correct pipeline splitting and for the optimizer by the flattening
support described in (1).

3. LR schedulers also index model states like optimizers and would need to be flattened properly to support
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add a comment here to say the lr_scheduler resharding assumes that all lr_schedulers are the same.

shadow_states = {k: v for k, v in states.items() if k not in self.exclude}
for exclude_key in self.exclude:
if exclude_key not in states:
logger.warning(f"{exclude_key} not found in state_dict, skipping")
Copy link
Contributor

Choose a reason for hiding this comment

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

We should just raise an exception. So a better way to do this is

if not set(self.exclude).issubset(set(states.keys()):
     raise ValueError("...")

@@ -511,6 +511,16 @@ def __init__(self):
default=-1,
help="Load the checkpoint at the specified step. If -1, load the latest checkpoint.",
)
self.parser.add_argument(
Copy link
Contributor

Choose a reason for hiding this comment

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

yes, exclude_from_loading is more explicit.

Copy link
Contributor

@tianyu-l tianyu-l left a comment

Choose a reason for hiding this comment

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

In general looks good. Had several comments on details.
Plus we need to document the usage in https://github.com/pytorch/torchtitan/blob/main/docs/checkpoint.md
including the proper use cases mentioned in #809 (comment)

optimizers do, so it's hard to write a generic 'flattener' utility.

TODO: This is currently unsolved and needs a fix.
3. LR schedulers also index model states like optimizers. Here we flatten the lr_schedulers by the ssumption that
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
3. LR schedulers also index model states like optimizers. Here we flatten the lr_schedulers by the ssumption that
3. LR schedulers also index model states like optimizers. Here we flatten the lr_schedulers with the assumption that

@@ -203,6 +200,11 @@ def __init__(

self.model_weights_only = ckpt_config.model_weights_only
self.export_dtype = TORCH_DTYPE_MAP[ckpt_config.export_dtype]
self.exclude_from_loading = (
[item.strip() for item in ckpt_config.exclude_from_loading]
Copy link
Contributor

Choose a reason for hiding this comment

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

can we do this strip in the definition of string_list?

self.exclude_from_loading = (
[item.strip() for item in ckpt_config.exclude_from_loading]
if ckpt_config.exclude_from_loading
else []
Copy link
Contributor

Choose a reason for hiding this comment

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

why this branch? Isn't it already a list after split in string_list?

self.parser.add_argument(
"--checkpoint.exclude_from_loading",
type=string_list,
default="",
Copy link
Contributor

Choose a reason for hiding this comment

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

The default should be []? as in


If default is "", you'll always end up with [""] after string_split.
See https://docs.python.org/3.3/library/stdtypes.html

@@ -418,6 +418,22 @@ def build_test_list():
"test_generate",
ngpu=2,
),
OverrideDefinitions(
Copy link
Contributor

Choose a reason for hiding this comment

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

There are two tests missing:

  1. unit tests (on CPU). Passing the test here doesn't mean the behavior is correct. We should add a unit test similar to https://github.com/pytorch/torchtitan/blob/690f299d37c5f6d34273762c0d650888a754d3c0/tests/unit_tests/test_dataset_checkpointing.py
  2. The test here only covers the cmd line arg override, but it could be problematic if specified in toml. We need to add a test similar to
    def test_parse_pp_split_points(self):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add test here with comments. In the optional checkpoint, we save at [dp:4] and load at [dp:2, tp:2], dataloader should be excluded in loading, otherwise would raise error for dp_degree mismatch

k: v for k, v in states.items() if k not in self.exclude_from_loading
}
for exclude_key in self.exclude_from_loading:
if exclude_key != "" and exclude_key not in states:
Copy link
Contributor

Choose a reason for hiding this comment

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

we should filter "" (and any empty space) out in string_list

}
for exclude_key in self.exclude_from_loading:
if exclude_key != "" and exclude_key not in states:
raise ValueError(f"{exclude_key} not found in state_dict, skipping")
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you mean by "skipping" when you raise an exception. Technically it should be "failing"?

@@ -435,10 +437,17 @@ def load(self, step: int = -1) -> bool:
}
logger.info(f"Loading the checkpoint at step {step}.")
begin = time.monotonic()
shadow_states = {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you explain more about the naming? I'd call it states_to_load

@@ -665,6 +682,9 @@ def parse_args_from_command_line(
# since the inferred type is just 'list' and it ends up flattening
# e.g. from ["layers.0", "layers.1"] into ["l", "a", "y", "e", "r", "s", ".0", ...]
aux_parser.add_argument("--" + arg, type=string_list)
elif arg == "checkpoint.exclude_from_loading":
# same as above for checkpoint.exclude_from_loading
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# same as above for checkpoint.exclude_from_loading
# similar to the case above

@mori360 mori360 marked this pull request as draft February 5, 2025 05:09
@fegin
Copy link
Contributor

fegin commented Feb 5, 2025

Can you publish the PR?

@mori360 mori360 requested a review from tianyu-l February 6, 2025 18:30
Copy link
Contributor

@tianyu-l tianyu-l left a comment

Choose a reason for hiding this comment

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

lgtm, thank you!
please address remaining comments before merging.

@@ -435,10 +433,17 @@ def load(self, step: int = -1) -> bool:
}
logger.info(f"Loading the checkpoint at step {step}.")
begin = time.monotonic()
state_to_load = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
state_to_load = {
states_to_load = {

@@ -511,6 +511,17 @@ def __init__(self):
default=-1,
help="Load the checkpoint at the specified step. If -1, load the latest checkpoint.",
)
self.parser.add_argument(
"--checkpoint.exclude_from_loading",
type=string_list,
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we still do .strip and empty check in string_list?

Comment on lines 121 to 124
toml_splits = ["optimizer", "lr_scheduler", "dataloader"]
toml_split_str = ",".join(toml_splits)
cmdline_splits = ["optimizer", "lr_scheduler", "dataloader"]
cmdline_split_str = ",".join(cmdline_splits)
Copy link
Contributor

Choose a reason for hiding this comment

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

the point of having two sets is that we can test override, e.g. in "toml has split points, cmdline overrides them".
So we need to make them different to test robustness.

@mori360 mori360 merged commit 49c6d6f into pytorch:main Feb 7, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Meta Open Source bot.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FSDP checkpoints don't load when run is restarted with greater world size
4 participants