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

[memories] Overlap shard transfers in async_serialize #22169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gspschmid
Copy link
Contributor

Builds upon #22114, which introduces a fast-path for async checkpoint saving in which each array shard is copied through a single device-to-pinned-host transfer. So far, all of these transfers are serialized.

The present PR allows overlapping the transfers of a single array's shards. This is achieved by inserting asyncio.sleep(0) right after the transfer has been started, thereby permitting the _write_array(shard) coroutine to yield.

Why do we need the sleep? Python's await doesn't actually guarantee that control will be yielded to the eventloop so that other coroutines can be scheduled. Python ties coroutine semantics to that of generators. As I understand it, the only thing that causes control to pass to the eventloop (and thus allows other coroutines to be scheduled) is a yield. What we therefore need to achieve overlap in our situation, is something like

import types

@types.coroutine
def noop():
  yield

async def transfer_shard_to_host(shard):
  ...
  await noop() # behaves like asyncio.sleep(0)
  ...

This is, in fact, what asyncio.sleep(0) does.

An alternative, more roundabout way to achieve overlap would be to split _write_array(shard) into a first phase that initiates all the transfers, and provides the resulting host-resident arrays to a second phase, in which we invoke t.write(data) as before.

cc @yashk2810

@gspschmid gspschmid force-pushed the gschmid/async_serialize-overlap-shard-copies branch from e18ab9f to 50d1474 Compare July 1, 2024 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant