Skip to content

Commit

Permalink
Add HandlerAwaitableSignal class to be used for signaling when creati…
Browse files Browse the repository at this point in the history
…ng step and item directories asynchronously during checkpointing.

PiperOrigin-RevId: 716129191
  • Loading branch information
Orbax Authors committed Jan 17, 2025
1 parent 6ed02f3 commit c49685c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
57 changes: 57 additions & 0 deletions checkpoint/orbax/checkpoint/_src/futures/synchronization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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.

"""Synchronization utilities for futures."""

import enum
import itertools
from orbax.checkpoint._src.multihost import multihost


class HandlerAwaitableSignal(enum.Enum):
"""Defines signals that may be awaited by a `CheckpointHandler`.
Signals may be passed from `CheckpointManager` or `Checkpointer` layers to
`CheckpointHandler or below.`
Attributes:
STEP_DIRECOTRY_CREATION: When recieved, indicates that the step directory
has been created. The handler should not attemp to write files before the
directory is created.
ITEM_DIRECTORY_CREATION: When recieved, indicates that the item directory
has been created. The handler should not attemp to write files before the
directory is created.
"""

STEP_DIRECTORY_CREATION = 'step_directory_creation'
ITEM_DIRECTORY_CREATION = 'item_directory_creation'


class HandlerAwaitableSignalBarrierKeyGenerator:
"""A unique barrier key generator for a `HandlerAwaitableSignal`."""

_barrier_key_counter = itertools.count()
_barrier_key_counter_value = None

@classmethod
def step_barrier_key_counter(cls) -> int:
cls._barrier_key_counter_value = next(cls._barrier_key_counter)
return cls._barrier_key_counter_value

@classmethod
def get_unique_barrier_key(cls, signal: HandlerAwaitableSignal) -> str:
"""Returns a unique barrier key for the signal."""
return multihost.unique_barrier_key(
signal.value, suffix=str(cls._barrier_key_counter_value)
)
41 changes: 41 additions & 0 deletions checkpoint/orbax/checkpoint/_src/futures/synchronization_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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.

from absl.testing import absltest
from absl.testing import parameterized
from orbax.checkpoint._src.futures import synchronization
from orbax.checkpoint._src.multihost import multihost


class HandlerAwaitableSignalBarrierKeyGeneratorTest(parameterized.TestCase):

@parameterized.parameters("0", "1", "2")
def test_get_unique_barrier_key(self, suffix):
step_directory_creation_signal = (
synchronization.HandlerAwaitableSignal.STEP_DIRECTORY_CREATION
)
expected_barrier_key = multihost.unique_barrier_key(
step_directory_creation_signal.value, suffix=suffix
)

synchronization.HandlerAwaitableSignalBarrierKeyGenerator.step_barrier_key_counter()
barrier_key = synchronization.HandlerAwaitableSignalBarrierKeyGenerator.get_unique_barrier_key(
step_directory_creation_signal
)

self.assertEqual(barrier_key, expected_barrier_key)


if __name__ == "__main__":
absltest.main()

0 comments on commit c49685c

Please sign in to comment.