Skip to content

Commit ee50995

Browse files
committed
[monarch] add actor_extension
Pull Request resolved: #440 Per the proposed structure, we will have a separate native extension for the actor api and the tensor_api. This is the starting point for the actor extension. Eventually the following binding-related crates will either be merged in or call from here: - hyperactor_extension - hyperactor_meta - monarch_hyperactor - monarch_meta_extension ghstack-source-id: 294788182 @exported-using-ghexport Differential Revision: [D77847588](https://our.internmc.facebook.com/intern/diff/D77847588/)
1 parent 92fccde commit ee50995

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+480
-251
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
resolver = "2"
33
members = [
4+
"actor_extension",
45
"controller",
56
"hyper",
67
"hyperactor",

actor_extension/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# @generated by autocargo from //monarch/actor_extension:actor_extension-lib
2+
3+
[package]
4+
name = "_extension"
5+
version = "0.0.0"
6+
authors = ["Meta"]
7+
edition = "2021"
8+
license = "BSD-3-Clause"
9+
10+
[lib]
11+
test = false
12+
doctest = false
13+
crate-type = ["cdylib"]
14+
15+
[dependencies]
16+
anyhow = "1.0.98"
17+
bincode = "1.3.3"
18+
hyperactor = { version = "0.0.0", path = "../hyperactor" }
19+
hyperactor_extension = { version = "0.0.0", path = "../hyperactor_extension" }
20+
hyperactor_mesh = { version = "0.0.0", path = "../hyperactor_mesh" }
21+
libc = "0.2.139"
22+
monarch_hyperactor = { version = "0.0.0", path = "../monarch_hyperactor" }
23+
pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods"] }
24+
pyo3-async-runtimes = { version = "0.24", features = ["attributes", "tokio-runtime"] }
25+
serde = { version = "1.0.185", features = ["derive", "rc"] }
26+
tokio = { version = "1.45.0", features = ["full", "test-util", "tracing"] }

monarch_extension/src/blocking.rs renamed to actor_extension/src/blocking.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ pub fn blocking_function() {
3131
/// Register Python bindings for the blocking module.
3232
pub fn register_python_bindings(module: &Bound<'_, PyModule>) -> PyResult<()> {
3333
let f = wrap_pyfunction!(blocking_function, module)?;
34-
f.setattr(
35-
"__module__",
36-
"monarch._rust_bindings.monarch_extension.blocking",
37-
)?;
34+
f.setattr("__module__", "monarch._src.actor._extension.blocking")?;
3835
module.add_function(f)?;
3936
Ok(())
4037
}

monarch_extension/src/code_sync.rs renamed to actor_extension/src/code_sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use serde::Serialize;
3131
#[pyclass(
3232
frozen,
3333
name = "WorkspaceLocation",
34-
module = "monarch._rust_bindings.monarch_extension.code_sync"
34+
module = "monarch._src.actor._extension.code_sync"
3535
)]
3636
#[derive(Clone, Debug, Serialize, Deserialize)]
3737
enum PyWorkspaceLocation {
@@ -75,7 +75,7 @@ impl PyWorkspaceLocation {
7575
#[pyclass(
7676
frozen,
7777
name = "RsyncMeshClient",
78-
module = "monarch._rust_bindings.monarch_extension.code_sync"
78+
module = "monarch._src.actor._extension.code_sync"
7979
)]
8080
pub struct RsyncMeshClient {
8181
actor_mesh: SharedCell<RootActorMesh<'static, rsync::RsyncActor>>,

actor_extension/src/lib.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
//! This module is used to expose Rust bindings for code supporting the
10+
//! `monarch.actor` module.
11+
//!
12+
//! It is imported by `monarch` as `monarch._src.actor._extension`.
13+
use pyo3::prelude::*;
14+
15+
mod blocking;
16+
mod code_sync;
17+
mod panic;
18+
19+
fn get_or_add_new_module<'py>(
20+
module: &Bound<'py, PyModule>,
21+
module_name: &str,
22+
) -> PyResult<Bound<'py, pyo3::types::PyModule>> {
23+
let mut current_module = module.clone();
24+
let mut parts = Vec::new();
25+
for part in module_name.split(".") {
26+
parts.push(part);
27+
let submodule = current_module.getattr(part).ok();
28+
if let Some(submodule) = submodule {
29+
current_module = submodule.extract()?;
30+
} else {
31+
let new_module = PyModule::new(current_module.py(), part)?;
32+
current_module.add_submodule(&new_module)?;
33+
current_module
34+
.py()
35+
.import("sys")?
36+
.getattr("modules")?
37+
.set_item(
38+
format!("monarch._src.actor._extension.{}", parts.join(".")),
39+
new_module.clone(),
40+
)?;
41+
current_module = new_module;
42+
}
43+
}
44+
Ok(current_module)
45+
}
46+
47+
#[pymodule]
48+
#[pyo3(name = "_extension")]
49+
pub fn mod_init(module: &Bound<'_, PyModule>) -> PyResult<()> {
50+
monarch_hyperactor::runtime::initialize(module.py())?;
51+
let runtime = monarch_hyperactor::runtime::get_tokio_runtime();
52+
::hyperactor::initialize(runtime.handle().clone());
53+
54+
monarch_hyperactor::shape::register_python_bindings(&get_or_add_new_module(
55+
module,
56+
"monarch_hyperactor.shape",
57+
)?)?;
58+
59+
monarch_hyperactor::selection::register_python_bindings(&get_or_add_new_module(
60+
module,
61+
"monarch_hyperactor.selection",
62+
)?)?;
63+
code_sync::register_python_bindings(&get_or_add_new_module(module, "code_sync")?)?;
64+
monarch_hyperactor::bootstrap::register_python_bindings(&get_or_add_new_module(
65+
module,
66+
"monarch_hyperactor.bootstrap",
67+
)?)?;
68+
69+
monarch_hyperactor::proc::register_python_bindings(&get_or_add_new_module(
70+
module,
71+
"monarch_hyperactor.proc",
72+
)?)?;
73+
74+
monarch_hyperactor::actor::register_python_bindings(&get_or_add_new_module(
75+
module,
76+
"monarch_hyperactor.actor",
77+
)?)?;
78+
79+
monarch_hyperactor::mailbox::register_python_bindings(&get_or_add_new_module(
80+
module,
81+
"monarch_hyperactor.mailbox",
82+
)?)?;
83+
84+
monarch_hyperactor::alloc::register_python_bindings(&get_or_add_new_module(
85+
module,
86+
"monarch_hyperactor.alloc",
87+
)?)?;
88+
monarch_hyperactor::channel::register_python_bindings(&get_or_add_new_module(
89+
module,
90+
"monarch_hyperactor.channel",
91+
)?)?;
92+
monarch_hyperactor::actor_mesh::register_python_bindings(&get_or_add_new_module(
93+
module,
94+
"monarch_hyperactor.actor_mesh",
95+
)?)?;
96+
monarch_hyperactor::proc_mesh::register_python_bindings(&get_or_add_new_module(
97+
module,
98+
"monarch_hyperactor.proc_mesh",
99+
)?)?;
100+
101+
monarch_hyperactor::runtime::register_python_bindings(&get_or_add_new_module(
102+
module,
103+
"monarch_hyperactor.runtime",
104+
)?)?;
105+
hyperactor_extension::alloc::register_python_bindings(&get_or_add_new_module(
106+
module,
107+
"hyperactor_extension.alloc",
108+
)?)?;
109+
hyperactor_extension::telemetry::register_python_bindings(&get_or_add_new_module(
110+
module,
111+
"hyperactor_extension.telemetry",
112+
)?)?;
113+
crate::panic::register_python_bindings(&get_or_add_new_module(module, "panic")?)?;
114+
115+
crate::blocking::register_python_bindings(&get_or_add_new_module(module, "blocking")?)?;
116+
Ok(())
117+
}

monarch_extension/src/panic.rs renamed to actor_extension/src/panic.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ pub fn panicking_function() {
1818
/// Register Python bindings for the panic module.
1919
pub fn register_python_bindings(module: &Bound<'_, PyModule>) -> PyResult<()> {
2020
let f = wrap_pyfunction!(panicking_function, module)?;
21-
f.setattr(
22-
"__module__",
23-
"monarch._rust_bindings.monarch_extension.panic",
24-
)?;
21+
f.setattr("__module__", "monarch._src.actor._extension.panic")?;
2522
module.add_function(f)?;
2623
Ok(())
2724
}

hyperactor_extension/src/alloc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use pyo3::types::PyDict;
2929
/// It ensures that the Alloc is only used once (i.e. moved) in rust.
3030
#[pyclass(
3131
name = "Alloc",
32-
module = "monarch._rust_bindings.hyperactor_extension.alloc"
32+
module = "monarch._src.actor._extension.hyperactor_extension.alloc"
3333
)]
3434
pub struct PyAlloc {
3535
pub inner: Arc<Mutex<Option<PyAllocWrapper>>>,
@@ -91,7 +91,7 @@ impl Alloc for PyAllocWrapper {
9191

9292
#[pyclass(
9393
name = "AllocConstraints",
94-
module = "monarch._rust_bindings.hyperactor_extension.alloc"
94+
module = "monarch._src.actor._extension.hyperactor_extension.alloc"
9595
)]
9696
pub struct PyAllocConstraints {
9797
inner: AllocConstraints,
@@ -113,7 +113,7 @@ impl PyAllocConstraints {
113113

114114
#[pyclass(
115115
name = "AllocSpec",
116-
module = "monarch._rust_bindings.hyperactor_extension.alloc"
116+
module = "monarch._src.actor._extension.hyperactor_extension.alloc"
117117
)]
118118
pub struct PyAllocSpec {
119119
pub inner: AllocSpec,

hyperactor_extension/src/telemetry.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub fn use_sim_clock() -> PyResult<()> {
113113
#[pyclass(
114114
unsendable,
115115
subclass,
116-
module = "monarch._rust_bindings.hyperactor_extension.telemetry"
116+
module = "monarch._src.actor._extension.hyperactor_extension.telemetry"
117117
)]
118118
struct PySpan {
119119
span: tracing::span::EnteredSpan,
@@ -142,43 +142,43 @@ pub fn register_python_bindings(module: &Bound<'_, PyModule>) -> PyResult<()> {
142142
let f = wrap_pyfunction!(forward_to_tracing, module)?;
143143
f.setattr(
144144
"__module__",
145-
"monarch._rust_bindings.hyperactor_extension.telemetry",
145+
"monarch._src.actor._extension.hyperactor_extension.telemetry",
146146
)?;
147147
module.add_function(f)?;
148148

149149
// Register the span-related functions
150150
let enter_span_fn = wrap_pyfunction!(enter_span, module)?;
151151
enter_span_fn.setattr(
152152
"__module__",
153-
"monarch._rust_bindings.hyperactor_extension.telemetry",
153+
"monarch._src.actor._extension.hyperactor_extension.telemetry",
154154
)?;
155155
module.add_function(enter_span_fn)?;
156156

157157
let exit_span_fn = wrap_pyfunction!(exit_span, module)?;
158158
exit_span_fn.setattr(
159159
"__module__",
160-
"monarch._rust_bindings.hyperactor_extension.telemetry",
160+
"monarch._src.actor._extension.hyperactor_extension.telemetry",
161161
)?;
162162
module.add_function(exit_span_fn)?;
163163

164164
let get_current_span_id_fn = wrap_pyfunction!(get_current_span_id, module)?;
165165
get_current_span_id_fn.setattr(
166166
"__module__",
167-
"monarch._rust_bindings.hyperactor_extension.telemetry",
167+
"monarch._src.actor._extension.hyperactor_extension.telemetry",
168168
)?;
169169
module.add_function(get_current_span_id_fn)?;
170170

171171
let use_real_clock_fn = wrap_pyfunction!(use_real_clock, module)?;
172172
use_real_clock_fn.setattr(
173173
"__module__",
174-
"monarch._rust_bindings.hyperactor_extension.telemetry",
174+
"monarch._src.actor._extension.hyperactor_extension.telemetry",
175175
)?;
176176
module.add_function(use_real_clock_fn)?;
177177

178178
let use_sim_clock_fn = wrap_pyfunction!(use_sim_clock, module)?;
179179
use_sim_clock_fn.setattr(
180180
"__module__",
181-
"monarch._rust_bindings.hyperactor_extension.telemetry",
181+
"monarch._src.actor._extension.hyperactor_extension.telemetry",
182182
)?;
183183
module.add_function(use_sim_clock_fn)?;
184184

monarch_extension/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ crate-type = ["cdylib"]
1616
[dependencies]
1717
anyhow = "1.0.98"
1818
async-trait = "0.1.86"
19-
bincode = "1.3.3"
2019
clap = { version = "4.5.38", features = ["derive", "env", "string", "unicode", "wrap_help"] }
2120
controller = { version = "0.0.0", path = "../controller", optional = true }
2221
hyperactor = { version = "0.0.0", path = "../hyperactor" }
23-
hyperactor_extension = { version = "0.0.0", path = "../hyperactor_extension" }
2422
hyperactor_mesh = { version = "0.0.0", path = "../hyperactor_mesh" }
2523
hyperactor_multiprocess = { version = "0.0.0", path = "../hyperactor_multiprocess" }
26-
libc = "0.2.139"
2724
monarch_hyperactor = { version = "0.0.0", path = "../monarch_hyperactor" }
2825
monarch_messages = { version = "0.0.0", path = "../monarch_messages", optional = true }
2926
monarch_simulator_lib = { version = "0.0.0", path = "../monarch_simulator", optional = true }
@@ -33,7 +30,6 @@ nccl-sys = { path = "../nccl-sys", optional = true }
3330
ndslice = { version = "0.0.0", path = "../ndslice" }
3431
pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods"] }
3532
pyo3-async-runtimes = { version = "0.24", features = ["attributes", "tokio-runtime"] }
36-
serde = { version = "1.0.185", features = ["derive", "rc"] }
3733
tokio = { version = "1.45.0", features = ["full", "test-util", "tracing"] }
3834
torch-sys = { version = "0.0.0", path = "../torch-sys", optional = true }
3935
torch-sys-cuda = { version = "0.0.0", path = "../torch-sys-cuda", optional = true }

0 commit comments

Comments
 (0)