Skip to content

Commit 61ef807

Browse files
committed
[monarch] refactor monarch/ callsites to use monarch.actor
Pull Request resolved: #438 as title ghstack-source-id: 294724000 @exported-using-ghexport Differential Revision: [D77823401](https://our.internmc.facebook.com/intern/diff/D77823401/)
1 parent 60258de commit 61ef807

File tree

13 files changed

+48
-34
lines changed

13 files changed

+48
-34
lines changed

examples/grpo_actor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
import torch
1414
import torch.nn as nn
1515
import torch.optim as optim
16-
from monarch.actor_mesh import Actor, ActorMeshRef, endpoint
1716

18-
from monarch.proc_mesh import proc_mesh
17+
from monarch.actor import Actor, ActorMeshRef, endpoint, proc_mesh
1918
from monarch.rdma import RDMABuffer
2019
from torch.distributions import Categorical, kl_divergence
2120

examples/notebooks/ping_pong.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"metadata": {},
1717
"source": [
1818
"## Hello World\n",
19-
"Actors are spawned in Process meshes via the `monarch.proc_mesh` API. For those familiar with distributed systems, it can be helpful to think of each Actor as a server with endpoints that can be called."
19+
"Actors are spawned in Process meshes via the `monarch.actor` API. For those familiar with distributed systems, it can be helpful to think of each Actor as a server with endpoints that can be called."
2020
]
2121
},
2222
{
@@ -43,8 +43,8 @@
4343
"source": [
4444
"import asyncio\n",
4545
"\n",
46-
"from monarch.proc_mesh import proc_mesh, ProcMesh\n",
47-
"from monarch.actor_mesh import Actor, endpoint, current_rank\n",
46+
"from monarch.actor import proc_mesh, ProcMesh\n",
47+
"from monarch.actor import Actor, endpoint, current_rank\n",
4848
"\n",
4949
"NUM_ACTORS=4\n",
5050
"\n",
@@ -168,8 +168,8 @@
168168
"source": [
169169
"import asyncio\n",
170170
"\n",
171-
"from monarch.proc_mesh import proc_mesh, ProcMesh\n",
172-
"from monarch.actor_mesh import Actor, endpoint, current_rank\n",
171+
"from monarch.actor import proc_mesh, ProcMesh\n",
172+
"from monarch.actor import Actor, endpoint, current_rank\n",
173173
"\n",
174174
"class ExampleActor(Actor):\n",
175175
" def __init__(self, actor_name):\n",

examples/notebooks/spmd_ddp.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"import torch.nn as nn\n",
2525
"import torch.optim as optim\n",
2626
"\n",
27-
"from monarch.proc_mesh import proc_mesh\n",
28-
"from monarch.actor_mesh import Actor, current_rank, endpoint\n",
27+
"from monarch.actor import proc_mesh\n",
28+
"from monarch.actor import Actor, current_rank, endpoint\n",
2929
"\n",
3030
"from torch.nn.parallel import DistributedDataParallel as DDP\n",
3131
"\n",

python/monarch/_src/actor/proc_mesh.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
TypeVar,
2323
)
2424

25-
from monarch._rust_bindings import has_tensor_engine
26-
2725
from monarch._rust_bindings.hyperactor_extension.alloc import ( # @manual=//monarch/monarch_extension:monarch_extension # @manual=//monarch/monarch_extension:monarch_extension
2826
Alloc,
2927
AllocConstraints,
@@ -44,11 +42,21 @@
4442
from monarch._src.actor.future import Future
4543
from monarch._src.actor.shape import MeshTrait
4644

45+
HAS_TENSOR_ENGINE = False
46+
try:
47+
# TODO: while the tensor_engine submodule doesn't exist yet, use the
48+
# available of monarch.rdma as a proxy.
49+
# type: ignore
50+
from monarch.rdma import RDMAManager # @manual
51+
52+
HAS_TENSOR_ENGINE = True
53+
except ImportError:
54+
pass
55+
4756

4857
if TYPE_CHECKING:
4958
Tensor = Any
5059
DeviceMesh = Any
51-
RDMAManager = Any
5260

5361

5462
T = TypeVar("T")
@@ -83,10 +91,7 @@ def __init__(
8391
self._rsync_mesh_client: Optional[RsyncMeshClient] = None
8492
self._auto_reload_actor: Optional[AutoReloadActor] = None
8593
self._maybe_device_mesh: Optional["DeviceMesh"] = _device_mesh
86-
if _mock_shape is None and has_tensor_engine():
87-
# type: ignore[21]
88-
from monarch.rdma import RDMAManager # @manual
89-
94+
if _mock_shape is None and HAS_TENSOR_ENGINE:
9095
# type: ignore[21]
9196
self._rdma_manager = self._spawn_blocking("rdma_manager", RDMAManager)
9297

@@ -191,7 +196,7 @@ async def _spawn_nonblocking(
191196

192197
@property
193198
def _device_mesh(self) -> "DeviceMesh":
194-
if not has_tensor_engine():
199+
if not HAS_TENSOR_ENGINE:
195200
raise RuntimeError(
196201
"DeviceMesh is not available because tensor_engine was not compiled (USE_TENSOR_ENGINE=0)"
197202
)
@@ -280,7 +285,7 @@ def local_proc_mesh(*, gpus: Optional[int] = None, hosts: int = 1) -> Future[Pro
280285
)
281286

282287

283-
_BOOTSTRAP_MAIN = "monarch.bootstrap_main"
288+
_BOOTSTRAP_MAIN = "monarch._src.actor.bootstrap_main"
284289

285290

286291
def _get_bootstrap_args() -> tuple[str, Optional[list[str]], dict[str, str]]:

python/monarch/_testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
import monarch_supervisor
1616
from monarch._src.actor.shape import NDSlice
17+
from monarch.actor import proc_mesh, ProcMesh
1718
from monarch.common.client import Client
1819
from monarch.common.device_mesh import DeviceMesh
1920
from monarch.common.invocation import DeviceException, RemoteException
2021
from monarch.controller.backend import ProcessBackend
2122
from monarch.mesh_controller import spawn_tensor_engine
22-
from monarch.proc_mesh import proc_mesh, ProcMesh
2323
from monarch.python_local_mesh import PythonLocalContext
2424
from monarch.rust_local_mesh import (
2525
local_mesh,

python/monarch/actor/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,34 @@
1212
Accumulator,
1313
Actor,
1414
ActorError,
15+
ActorMeshRef,
1516
current_actor_name,
1617
current_rank,
1718
current_size,
1819
endpoint,
1920
MonarchContext,
21+
Point,
22+
send,
2023
ValueMesh,
2124
)
2225
from monarch._src.actor.future import Future
23-
from monarch._src.actor.proc_mesh import proc_mesh, ProcMesh
26+
from monarch._src.actor.proc_mesh import local_proc_mesh, proc_mesh, ProcMesh
2427

2528
__all__ = [
2629
"Accumulator",
2730
"Actor",
2831
"ActorError",
32+
"ActorMeshRef",
2933
"current_actor_name",
3034
"current_rank",
3135
"current_size",
3236
"endpoint",
37+
"Future",
38+
"local_proc_mesh",
3339
"MonarchContext",
34-
"ValueMesh",
40+
"Point",
3541
"proc_mesh",
3642
"ProcMesh",
37-
"Future",
43+
"send",
44+
"ValueMesh",
3845
]

python/monarch/mesh_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
from monarch._rust_bindings.monarch_hyperactor.proc import ( # @manual=//monarch/monarch_extension:monarch_extension
3535
ActorId,
3636
)
37+
from monarch._src.actor.actor_mesh import Port, PortTuple
3738
from monarch._src.actor.shape import NDSlice
38-
from monarch.actor_mesh import Port, PortTuple
3939
from monarch.common import messages
4040
from monarch.common.controller_api import TController
4141
from monarch.common.invocation import Seq

python/tests/error_test_binary.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
from monarch._rust_bindings.monarch_extension.panic import panicking_function
1515

16-
from monarch.actor_mesh import Actor, endpoint, send
17-
from monarch.proc_mesh import proc_mesh
16+
from monarch.actor import Actor, endpoint, proc_mesh, send
1817

1918

2019
class ErrorActor(Actor):

python/tests/test_actor_error.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
import pytest
1212
from monarch._rust_bindings.monarch_hyperactor.proc_mesh import ProcEvent
13-
from monarch.actor_mesh import Actor, ActorError, endpoint, send
14-
from monarch.proc_mesh import local_proc_mesh, proc_mesh
13+
from monarch.actor import Actor, ActorError, endpoint, local_proc_mesh, proc_mesh
1514

1615

1716
class ExceptionActor(Actor):

python/tests/test_allocator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@
3838
StaticRemoteAllocInitializer,
3939
TorchXRemoteAllocInitializer,
4040
)
41-
from monarch.actor_mesh import Actor, current_rank, current_size, endpoint, ValueMesh
42-
from monarch.proc_mesh import ProcMesh
41+
from monarch.actor import (
42+
Actor,
43+
current_rank,
44+
current_size,
45+
endpoint,
46+
ProcMesh,
47+
ValueMesh,
48+
)
4349
from monarch.tools.mesh_spec import MeshSpec, ServerSpec
4450
from monarch.tools.network import get_sockaddr
4551

0 commit comments

Comments
 (0)