Skip to content

Commit f3ae02d

Browse files
committed
[monarch] add actor_extension
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 Differential Revision: [D77847588](https://our.internmc.facebook.com/intern/diff/D77847588/) ghstack-source-id: 294539856 Pull Request resolved: #440
1 parent ff2d075 commit f3ae02d

Some content is hidden

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

82 files changed

+329
-261
lines changed

actor_extension/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods"] }
17+
pyo3-async-runtimes = { git = "https://github.com/PyO3/pyo3-async-runtimes", rev = "c5a3746f110b4d246556b0f6c29f5f555919eee3", features = ["attributes", "tokio-runtime"] }

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.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.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.actor._extension.code_sync"
7979
)]
8080
pub struct RsyncMeshClient {
8181
actor_mesh: SharedCell<RootActorMesh<'static, rsync::RsyncActor>>,

actor_extension/src/lib.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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.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.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+
117+
println!("hello I am imported :)");
118+
Ok(())
119+
}
File renamed without changes.

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.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.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.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.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.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.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.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.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.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.actor._extension.hyperactor_extension.telemetry",
182182
)?;
183183
module.add_function(use_sim_clock_fn)?;
184184

monarch_extension/src/lib.rs

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#[cfg(feature = "tensor_engine")]
1212
mod client;
13-
pub mod code_sync;
1413
#[cfg(feature = "tensor_engine")]
1514
mod controller;
1615
#[cfg(feature = "tensor_engine")]
@@ -24,8 +23,6 @@ mod simulator_client;
2423
#[cfg(feature = "tensor_engine")]
2524
mod tensor_worker;
2625

27-
mod blocking;
28-
mod panic;
2926
use pyo3::prelude::*;
3027

3128
#[pyfunction]
@@ -64,20 +61,6 @@ fn get_or_add_new_module<'py>(
6461
#[pymodule]
6562
#[pyo3(name = "_rust_bindings")]
6663
pub fn mod_init(module: &Bound<'_, PyModule>) -> PyResult<()> {
67-
monarch_hyperactor::runtime::initialize(module.py())?;
68-
let runtime = monarch_hyperactor::runtime::get_tokio_runtime();
69-
::hyperactor::initialize(runtime.handle().clone());
70-
71-
monarch_hyperactor::shape::register_python_bindings(&get_or_add_new_module(
72-
module,
73-
"monarch_hyperactor.shape",
74-
)?)?;
75-
76-
monarch_hyperactor::selection::register_python_bindings(&get_or_add_new_module(
77-
module,
78-
"monarch_hyperactor.selection",
79-
)?)?;
80-
8164
#[cfg(feature = "tensor_engine")]
8265
{
8366
client::register_python_bindings(&get_or_add_new_module(
@@ -125,69 +108,6 @@ pub fn mod_init(module: &Bound<'_, PyModule>) -> PyResult<()> {
125108
module,
126109
"monarch_extension.simulation_tools",
127110
)?)?;
128-
monarch_hyperactor::bootstrap::register_python_bindings(&get_or_add_new_module(
129-
module,
130-
"monarch_hyperactor.bootstrap",
131-
)?)?;
132-
133-
monarch_hyperactor::proc::register_python_bindings(&get_or_add_new_module(
134-
module,
135-
"monarch_hyperactor.proc",
136-
)?)?;
137-
138-
monarch_hyperactor::actor::register_python_bindings(&get_or_add_new_module(
139-
module,
140-
"monarch_hyperactor.actor",
141-
)?)?;
142-
143-
monarch_hyperactor::mailbox::register_python_bindings(&get_or_add_new_module(
144-
module,
145-
"monarch_hyperactor.mailbox",
146-
)?)?;
147-
148-
monarch_hyperactor::alloc::register_python_bindings(&get_or_add_new_module(
149-
module,
150-
"monarch_hyperactor.alloc",
151-
)?)?;
152-
monarch_hyperactor::channel::register_python_bindings(&get_or_add_new_module(
153-
module,
154-
"monarch_hyperactor.channel",
155-
)?)?;
156-
monarch_hyperactor::actor_mesh::register_python_bindings(&get_or_add_new_module(
157-
module,
158-
"monarch_hyperactor.actor_mesh",
159-
)?)?;
160-
monarch_hyperactor::proc_mesh::register_python_bindings(&get_or_add_new_module(
161-
module,
162-
"monarch_hyperactor.proc_mesh",
163-
)?)?;
164-
165-
monarch_hyperactor::runtime::register_python_bindings(&get_or_add_new_module(
166-
module,
167-
"monarch_hyperactor.runtime",
168-
)?)?;
169-
hyperactor_extension::alloc::register_python_bindings(&get_or_add_new_module(
170-
module,
171-
"hyperactor_extension.alloc",
172-
)?)?;
173-
hyperactor_extension::telemetry::register_python_bindings(&get_or_add_new_module(
174-
module,
175-
"hyperactor_extension.telemetry",
176-
)?)?;
177-
code_sync::register_python_bindings(&get_or_add_new_module(
178-
module,
179-
"monarch_extension.code_sync",
180-
)?)?;
181-
182-
crate::panic::register_python_bindings(&get_or_add_new_module(
183-
module,
184-
"monarch_extension.panic",
185-
)?)?;
186-
187-
crate::blocking::register_python_bindings(&get_or_add_new_module(
188-
module,
189-
"monarch_extension.blocking",
190-
)?)?;
191111

192112
// Add feature detection function
193113
module.add_function(wrap_pyfunction!(has_tensor_engine, module)?)?;

0 commit comments

Comments
 (0)