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

Support saving and loading of step while saving and loading state #2765

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/accelerate/accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,7 @@ def _inner(folder):
schedulers,
dataloaders,
self.state.process_index,
self.step,
self.scaler,
save_on_each_node=self.project_configuration.save_on_each_node,
safe_serialization=safe_serialization,
Expand Down Expand Up @@ -3099,7 +3100,7 @@ def _inner(folder):
else:
map_location = "cpu"

load_accelerator_state(
override_attributes = load_accelerator_state(
input_dir,
models,
optimizers,
Expand All @@ -3110,6 +3111,7 @@ def _inner(folder):
map_location,
**load_model_func_kwargs,
)
self.step = override_attributes["step"]
custom_checkpoints = [
f for f in os.listdir(input_dir) if re.search(r"^custom_checkpoint_\d+\.pkl$", f) is not None
]
Expand Down
9 changes: 9 additions & 0 deletions src/accelerate/checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def save_accelerator_state(
schedulers: list,
dataloaders: list,
process_index: int,
step: int,
scaler: GradScaler = None,
save_on_each_node: bool = False,
safe_serialization: bool = True,
Expand Down Expand Up @@ -82,6 +83,8 @@ def save_accelerator_state(
A list of dataloader instances to save their sampler states
process_index (`int`):
The current process index in the Accelerator state
step (`int`):
The current step in the internal step tracker
scaler (`torch.cuda.amp.GradScaler`, *optional*):
An optional gradient scaler instance to save
save_on_each_node (`bool`, *optional*):
Expand Down Expand Up @@ -134,6 +137,7 @@ def save_accelerator_state(
# Random number generator states
states = {}
states_name = f"{RNG_STATE_NAME}_{process_index}.pkl"
states["step"] = step
states["random_state"] = random.getstate()
states["numpy_random_seed"] = np.random.get_state()
states["torch_manual_seed"] = torch.get_rng_state()
Expand Down Expand Up @@ -181,6 +185,8 @@ def load_accelerator_state(
load_model_func_kwargs (`dict`, *optional*):
Additional arguments that can be passed to the model's `load_state_dict` method.
"""
# stores the `Accelerator` attributes to override
override_attributes = dict()
if map_location not in [None, "cpu", "on_device"]:
raise TypeError(
"Unsupported optimizer map location passed, please choose one of `None`, `'cpu'`, or `'on_device'`"
Expand Down Expand Up @@ -240,6 +246,7 @@ def load_accelerator_state(
# Random states
try:
states = torch.load(input_dir.joinpath(f"{RNG_STATE_NAME}_{process_index}.pkl"))
override_attributes["step"] = states["step"]
Copy link

Choose a reason for hiding this comment

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

This is a breaking change for people upgrading accelerate. If previously saved pickled states objects don't contain a "step" key, then random states fail to initialize correctly when you resume a training that was checkpointed from a prior version of accelerate.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Correct, we've fixed this in a patch release

Copy link

Choose a reason for hiding this comment

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

Ah I see this now. Thanks!

random.setstate(states["random_state"])
np.random.set_state(states["numpy_random_seed"])
torch.set_rng_state(states["torch_manual_seed"])
Expand All @@ -253,6 +260,8 @@ def load_accelerator_state(
except Exception:
logger.info("Could not load random states")

return override_attributes


def save_custom_state(obj, path, index: int = 0, save_on_each_node: bool = False):
"""
Expand Down
Loading