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

Fix some zero-overhead checkpointing bugs #602

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
23 changes: 14 additions & 9 deletions torchtitan/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ def __init__(
self.mp.start()
self.cpu_offload_state_dict = None
self.staging = False
self.staging_state_dict = None
self.staging_id = None
self.staging_stream = torch.cuda.Stream()
else:
Expand Down Expand Up @@ -384,7 +383,7 @@ def _async_with_pinned_memory(self, checkpoint_id: str) -> None:
if self.cpu_offload_state_dict is None:
logger.debug(f"Preparing the CPU memory, {time.monotonic()=}.:.2f")
self.cpu_offload_state_dict = _create_cpu_state_dict(
state_dict, pin_memory=True
state_dict, pin_memory=True, share_memory=True
)

logger.debug(f"Staging the state_dict, {time.monotonic()=}.:.2f")
Expand All @@ -395,7 +394,6 @@ def _async_with_pinned_memory(self, checkpoint_id: str) -> None:
non_blocking=True,
)
self.staging = True
self.staging_state_dict = state_dict
self.staging_id = checkpoint_id

def save(self, curr_step: int, force: bool = False) -> None:
Expand Down Expand Up @@ -435,12 +433,19 @@ def maybe_wait_for_staging(self) -> None:
and self.async_mode == AsyncMode.ASYNC_WITH_PINNED_MEM
and self.staging
):
logger.debug(f"Waiting for staging, {time.monotonic()=:.2f}.")
self.staging_stream.synchronize()
logger.debug(
f"Sending the state dict to the background process, {time.monotonic()=:.2f}."
)
self.mp_queue_send.put((self.staging_state_dict, self.staging_id))
if not self.staging_stream.query():
self.staging_stream.synchronize()

def sync_func():
self.mp_queue_send.put_nowait(
(self.cpu_offload_state_dict, self.staging_id)
)

# This may be a faster way to do zero-overhead checkpointing staging
# checkpointing but we need more thorough investigation before
# swithing to this method.
# self.my_thread = threading.Thread(target=func).start()
sync_func()
self.staging = False

def load(self, step: int = -1) -> bool:
Expand Down
Loading