Skip to content

[6.5/n] Small refactor of _call_on_shard_and_fetch #291

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 3 commits into
base: gh/zdevito/18/base
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
38 changes: 35 additions & 3 deletions python/monarch/common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
from monarch.common.borrows import Borrow, StorageAliases
from monarch.common.controller_api import LogMessage, MessageResult, TController
from monarch.common.device_mesh import DeviceMesh

from monarch.common.future import Future
from monarch.common.invocation import DeviceException, RemoteException, Seq
from monarch.common.recording import flatten_messages, Recording

Expand All @@ -52,9 +54,6 @@

from . import _coalescing

if TYPE_CHECKING:
from monarch.common.future import Future


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -447,6 +446,39 @@ def no_coalescing(self, reason):
def mesh_state(self) -> WorldState:
return self.inner.worker_world_state()

def fetch(
self,
mesh: "DeviceMesh",
stream: "StreamRef",
shard,
preprocess_message,
args,
kwargs,
defs: Tuple["Tensor", ...],
uses: Tuple["Tensor", ...],
) -> "Future":
fut = Future(self)
ident = self.new_node(defs, uses, fut)
process = mesh._process(shard)
self.send(
process,
messages.SendValue(
ident,
None,
defs,
preprocess_message,
args,
kwargs,
stream,
),
)
# we have to ask for status updates
# from workers to be sure they have finished
# enough work to count this future as finished,
# and all potential errors have been reported
self._request_status()
return fut


def tree_map_refs(first_ref: int, tree):
def translate_id(ref: int) -> int:
Expand Down
35 changes: 14 additions & 21 deletions python/monarch/common/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
overload,
Protocol,
Tuple,
TYPE_CHECKING,
TypeVar,
)

Expand All @@ -30,6 +31,9 @@

from monarch.common import _coalescing, device_mesh, messages, stream

if TYPE_CHECKING:
from monarch.common.client import Client

from monarch.common.device_mesh import RemoteProcessGroup
from monarch.common.fake import fake_call

Expand Down Expand Up @@ -173,30 +177,19 @@ def _call_on_shard_and_fetch(
propagator, rfunction, args, kwargs, ambient_mesh, stream._active
)

client = mesh.client
client: "Client" = mesh.client
if _coalescing.is_active(client):
raise NotImplementedError("NYI: fetching results during a coalescing block")
fut = Future(client)
ident = client.new_node(mutates, dtensors, fut)
process = mesh._process(shard)
client.send(
process,
messages.SendValue(
ident,
None,
mutates,
preprocess_message,
args,
kwargs,
stream._active._to_ref(client),
),
return client.fetch(
mesh,
stream._active._to_ref(client),
shard,
preprocess_message,
args,
kwargs,
mutates,
dtensors,
)
# we have to ask for status updates
# from workers to be sure they have finished
# enough work to count this future as finished,
# and all potential errors have been reported
client._request_status()
return fut


@remote
Expand Down