-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
SaveDecisionPolicy
to better encapsulate various options around…
… choosing whether or not to perform a save at a particular step. Added a policy that allows for checkpointing as often as possible, as long as a save is not already in progress (continuous checkpointing). PiperOrigin-RevId: 718984622
- Loading branch information
1 parent
302c2c1
commit d7c1cfd
Showing
7 changed files
with
281 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
checkpoint/orbax/checkpoint/_src/checkpoint_managers/save_decision_policy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# Copyright 2024 The Orbax Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Defines policies for when a checkpoint is saved.""" | ||
|
||
import dataclasses | ||
import typing | ||
from typing import Container, Protocol, Sequence | ||
|
||
|
||
@dataclasses.dataclass(kw_only=True) | ||
class StepInfo: | ||
"""Relevant information about a checkpoint step.""" | ||
step: int | ||
|
||
|
||
@dataclasses.dataclass(kw_only=True) | ||
class DecisionContext: | ||
"""Additional properties for making a save decision.""" | ||
|
||
is_saving_in_progress: bool | ||
reached_preemption: bool | ||
|
||
|
||
@typing.runtime_checkable | ||
class SaveDecisionPolicy(Protocol): | ||
"""A policy that defines when to save a checkpoint. | ||
Implementations should return True from `should_save` when saving a checkpoint | ||
is desired at the given step. | ||
""" | ||
|
||
def should_save( | ||
self, | ||
step: StepInfo, | ||
previous_steps: Sequence[StepInfo], | ||
*, | ||
context: DecisionContext | ||
) -> bool: | ||
... | ||
|
||
|
||
@dataclasses.dataclass | ||
class FixedIntervalPolicy(SaveDecisionPolicy): | ||
"""Checkpoint at a fixed interval.""" | ||
|
||
interval: int | ||
|
||
def should_save( | ||
self, | ||
step: StepInfo, | ||
previous_steps: Sequence[StepInfo], | ||
*, | ||
context: DecisionContext | ||
) -> bool: | ||
del previous_steps | ||
del context | ||
return step.step % self.interval == 0 | ||
|
||
|
||
@dataclasses.dataclass | ||
class SpecificStepsPolicy(SaveDecisionPolicy): | ||
"""Checkpoint at specific steps.""" | ||
|
||
steps: Container[int] | ||
|
||
def should_save( | ||
self, | ||
step: StepInfo, | ||
previous_steps: Sequence[StepInfo], | ||
*, | ||
context: DecisionContext | ||
) -> bool: | ||
del previous_steps | ||
del context | ||
return step.step in self.steps | ||
|
||
|
||
class ContinuousCheckpointingPolicy(SaveDecisionPolicy): | ||
"""Checkpoint as often as possible, as long as a save is not ongoing.""" | ||
|
||
def should_save( | ||
self, | ||
step: StepInfo, | ||
previous_steps: Sequence[StepInfo], | ||
*, | ||
context: DecisionContext | ||
) -> bool: | ||
del step | ||
del previous_steps | ||
return not context.is_saving_in_progress | ||
|
||
|
||
class PreemptionCheckpointingPolicy(SaveDecisionPolicy): | ||
"""Save a checkpoint when a preemption is detected.""" | ||
|
||
def should_save( | ||
self, | ||
step: StepInfo, | ||
previous_steps: Sequence[StepInfo], | ||
*, | ||
context: DecisionContext | ||
) -> bool: | ||
del step | ||
del previous_steps | ||
return context.reached_preemption | ||
|
||
|
||
class InitialSavePolicy(SaveDecisionPolicy): | ||
"""Checkpoint as soon as possible if no checkpoints already exist.""" | ||
|
||
def should_save( | ||
self, | ||
step: StepInfo, | ||
previous_steps: Sequence[StepInfo], | ||
*, | ||
context: DecisionContext | ||
) -> bool: | ||
del step | ||
del context | ||
return not previous_steps | ||
|
||
|
||
@dataclasses.dataclass | ||
class AnySavePolicy(SaveDecisionPolicy): | ||
"""Evaluates all policies and saves if any of them returns True. | ||
Each policy is evaluated in order, and if all are False, the final result is | ||
False. If at least one is True, the final result is True. | ||
""" | ||
|
||
policies: Sequence[SaveDecisionPolicy] | ||
|
||
def should_save( | ||
self, | ||
step: StepInfo, | ||
previous_steps: Sequence[StepInfo], | ||
*, | ||
context: DecisionContext | ||
) -> bool: | ||
return any( | ||
policy.should_save(step, previous_steps=previous_steps, context=context) | ||
for policy in self.policies | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2024 The Orbax Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Public symbols for checkpoint_managers module.""" | ||
|
||
# pylint: disable=g-importing-member, g-bad-import-order, unused-import, g-multiple-import | ||
|
||
from orbax.checkpoint._src.checkpoint_managers import save_decision_policy | ||
from orbax.checkpoint._src.checkpoint_managers.save_decision_policy import ( | ||
StepInfo, | ||
SaveDecisionPolicy, | ||
FixedIntervalPolicy, | ||
InitialSavePolicy, | ||
SpecificStepsPolicy, | ||
ContinuousCheckpointingPolicy, | ||
AnySavePolicy, | ||
) | ||
|
||
from orbax.checkpoint.checkpoint_manager import CheckpointManagerOptions | ||
from orbax.checkpoint.checkpoint_manager import CheckpointManager | ||
|
||
from orbax.checkpoint.abstract_checkpoint_manager import AbstractCheckpointManager |