Skip to content

Log memory_info in save code path. #1789

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions checkpoint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ handlers for checkpointables using V1 APIs.
- `ocp.PLACEHOLDER` for partial restoration.
- `tree_trim` utility function for matching PyTree structures.
- #v1 Add `ProtoHandler`.
- Log memory_info in save code path.

### Changed

Expand Down
1 change: 1 addition & 0 deletions checkpoint/orbax/checkpoint/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ py_library(
"//checkpoint/orbax/checkpoint/_src/path:step",
"//checkpoint/orbax/checkpoint/_src/path:utils",
"//orbax/checkpoint/_src:threading",
"//orbax/checkpoint/_src/logging:perf_logging",
],
)

Expand Down
6 changes: 6 additions & 0 deletions checkpoint/orbax/checkpoint/_src/logging/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ py_library(
srcs = ["composite_logger.py"],
deps = [":abstract_logger"],
)

py_library(
name = "perf_logging",
srcs = ["perf_logging.py"],
deps = ["//third_party/py/psutil"],
)
32 changes: 32 additions & 0 deletions checkpoint/orbax/checkpoint/_src/logging/perf_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.

"""Performance logging."""

from absl import logging
import jax
import psutil


def log_memory_info(label: str | None = None):
"""Logs memory usage of the current os process."""
jax_process_index = jax.process_index()
process = psutil.Process()
memory_info = process.memory_info()
logging.info(
'[process=%s] %s memory_info: %s',
jax_process_index,
label or '',
memory_info,
)
15 changes: 5 additions & 10 deletions checkpoint/orbax/checkpoint/checkpoint_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from orbax.checkpoint._src.handlers import json_checkpoint_handler
from orbax.checkpoint._src.handlers import proto_checkpoint_handler
from orbax.checkpoint._src.logging import abstract_logger
from orbax.checkpoint._src.logging import perf_logging
from orbax.checkpoint._src.logging import standard_logger
from orbax.checkpoint._src.logging import step_statistics
from orbax.checkpoint._src.metadata import checkpoint
Expand Down Expand Up @@ -1397,9 +1398,7 @@ def save(
timeout=multihost.DIRECTORY_DELETION_TIMEOUT,
processes=self._multiprocessing_options.active_processes,
)
logging.info(
'[process=%s] Saving checkpoint at step %d', process_index, step
)
perf_logging.log_memory_info(f'[step={step}] Saving checkpoint.')
step_stats.checkpointer_blocking_start_time = time.time()
self._checkpointer.save(
save_directory, args=args, custom_metadata=custom_metadata
Expand Down Expand Up @@ -1470,6 +1469,7 @@ def launch_finalize_thread() -> _FinalizeThread:
step,
)

perf_logging.log_memory_info(f'[step={step}] After sync phase save.')
step_stats.synchronous = not is_async_checkpointer(self._checkpointer)
step_stats.checkpoint_manager_blocking_duration_secs = (
time.time() - step_stats.checkpoint_manager_blocking_start_time
Expand Down Expand Up @@ -2080,6 +2080,7 @@ def _finalize_checkpoint(self, step: int):

def _finalize(self, step: int, steps_to_remove: List[int]):
"""Finalizes individual items and starts garbage collection."""
perf_logging.log_memory_info(f'[step={step}] Before finalize phase save.')
process_index = multihost.process_index()
current_thread = threading.current_thread()
self._non_blocking_metadata_store.wait_until_finished()
Expand Down Expand Up @@ -2108,13 +2109,7 @@ def _finalize(self, step: int, steps_to_remove: List[int]):
suffix=str(step),
)
)
logging.info(
'[process=%s][thread=%s][step=%s] CheckpointManager Save Finalize is'
' done on all hosts.',
process_index,
current_thread.name,
step,
)
perf_logging.log_memory_info(f'[step={step}] After finalize phase save.')

def close(self):
"""Waits for outstanding operations to finish and closes internal objects."""
Expand Down
1 change: 1 addition & 0 deletions checkpoint/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies = [
'protobuf',
'humanize',
'simplejson >= 3.16.0',
'psutil',
]

dynamic = ['version']
Expand Down
Loading