From 72fc78480075c7b61a488113acdfdfe23f9aa7b9 Mon Sep 17 00:00:00 2001 From: Colin Taylor Date: Fri, 17 Oct 2025 17:37:10 -0700 Subject: [PATCH 1/2] build monarch_extension as extension-module Differential Revision: D84956118 --- controller/Cargo.toml | 2 +- monarch_extension/Cargo.toml | 2 +- monarch_hyperactor/Cargo.toml | 2 +- monarch_messages/Cargo.toml | 2 +- monarch_rdma/extension/Cargo.toml | 2 +- monarch_tensor_worker/Cargo.toml | 2 +- monarch_types/Cargo.toml | 2 +- setup.py | 9 ++++++++- torch-sys-cuda/Cargo.toml | 2 +- torch-sys/Cargo.toml | 2 +- 10 files changed, 17 insertions(+), 10 deletions(-) diff --git a/controller/Cargo.toml b/controller/Cargo.toml index c8f696963..53eeddf95 100644 --- a/controller/Cargo.toml +++ b/controller/Cargo.toml @@ -27,7 +27,7 @@ hyperactor_multiprocess = { version = "0.0.0", path = "../hyperactor_multiproces monarch_messages = { version = "0.0.0", path = "../monarch_messages" } nccl-sys = { path = "../nccl-sys" } ndslice = { version = "0.0.0", path = "../ndslice" } -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } serde = { version = "1.0.219", features = ["derive", "rc"] } serde_json = { version = "1.0.140", features = ["alloc", "float_roundtrip", "raw_value", "unbounded_depth"] } tokio = { version = "1.47.1", features = ["full", "test-util", "tracing"] } diff --git a/monarch_extension/Cargo.toml b/monarch_extension/Cargo.toml index 7f7740d93..f22221829 100644 --- a/monarch_extension/Cargo.toml +++ b/monarch_extension/Cargo.toml @@ -34,7 +34,7 @@ monarch_tensor_worker = { version = "0.0.0", path = "../monarch_tensor_worker", monarch_types = { version = "0.0.0", path = "../monarch_types" } nccl-sys = { path = "../nccl-sys", optional = true } ndslice = { version = "0.0.0", path = "../ndslice" } -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } rdmaxcel-sys = { path = "../rdmaxcel-sys", optional = true } serde = { version = "1.0.219", features = ["derive", "rc"] } tokio = { version = "1.47.1", features = ["full", "test-util", "tracing"] } diff --git a/monarch_hyperactor/Cargo.toml b/monarch_hyperactor/Cargo.toml index 4dd77e107..ecc97902b 100644 --- a/monarch_hyperactor/Cargo.toml +++ b/monarch_hyperactor/Cargo.toml @@ -40,7 +40,7 @@ ndslice = { version = "0.0.0", path = "../ndslice" } nix = { version = "0.30.1", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "signal", "term", "time", "user", "zerocopy"] } once_cell = "1.21" opentelemetry = "0.29" -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } pyo3-async-runtimes = { version = "0.24", features = ["attributes", "tokio-runtime"] } serde = { version = "1.0.219", features = ["derive", "rc"] } serde_bytes = "0.11" diff --git a/monarch_messages/Cargo.toml b/monarch_messages/Cargo.toml index 9a92bb95c..71092188c 100644 --- a/monarch_messages/Cargo.toml +++ b/monarch_messages/Cargo.toml @@ -14,7 +14,7 @@ enum-as-inner = "0.6.0" hyperactor = { version = "0.0.0", path = "../hyperactor" } monarch_types = { version = "0.0.0", path = "../monarch_types" } ndslice = { version = "0.0.0", path = "../ndslice" } -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } serde = { version = "1.0.219", features = ["derive", "rc"] } serde_bytes = "0.11" thiserror = "2.0.12" diff --git a/monarch_rdma/extension/Cargo.toml b/monarch_rdma/extension/Cargo.toml index 33e2f3b9a..a7a80d99c 100644 --- a/monarch_rdma/extension/Cargo.toml +++ b/monarch_rdma/extension/Cargo.toml @@ -18,7 +18,7 @@ hyperactor = { version = "0.0.0", path = "../../hyperactor" } hyperactor_mesh = { version = "0.0.0", path = "../../hyperactor_mesh" } monarch_hyperactor = { version = "0.0.0", path = "../../monarch_hyperactor" } monarch_rdma = { version = "0.0.0", path = ".." } -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } serde = { version = "1.0.219", features = ["derive", "rc"] } serde_json = { version = "1.0.140", features = ["alloc", "float_roundtrip", "raw_value", "unbounded_depth"] } tracing = { version = "0.1.41", features = ["attributes", "valuable"] } diff --git a/monarch_tensor_worker/Cargo.toml b/monarch_tensor_worker/Cargo.toml index c622407f8..51895181a 100644 --- a/monarch_tensor_worker/Cargo.toml +++ b/monarch_tensor_worker/Cargo.toml @@ -26,7 +26,7 @@ monarch_types = { version = "0.0.0", path = "../monarch_types" } ndslice = { version = "0.0.0", path = "../ndslice" } nix = { version = "0.30.1", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "signal", "term", "time", "user", "zerocopy"] } parking_lot = { version = "0.12.1", features = ["send_guard"] } -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } serde = { version = "1.0.219", features = ["derive", "rc"] } serde_json = { version = "1.0.140", features = ["alloc", "float_roundtrip", "raw_value", "unbounded_depth"] } sorted-vec = "0.8.3" diff --git a/monarch_types/Cargo.toml b/monarch_types/Cargo.toml index 01be830bb..862c7ea09 100644 --- a/monarch_types/Cargo.toml +++ b/monarch_types/Cargo.toml @@ -10,7 +10,7 @@ license = "BSD-3-Clause" [dependencies] derive_more = { version = "1.0.0", features = ["full"] } hyperactor = { version = "0.0.0", path = "../hyperactor" } -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } serde = { version = "1.0.219", features = ["derive", "rc"] } serde_bytes = "0.11" diff --git a/setup.py b/setup.py index b944710af..ae25d75e2 100644 --- a/setup.py +++ b/setup.py @@ -119,6 +119,13 @@ def run(self): with open("README.md", encoding="utf8") as f: readme = f.read() +python_lib_dir = sysconfig.get_config_var("LIBDIR") # path to libpython3.10.so.1.0 + +rust_binding_args = ["--"] +if not USE_TENSOR_ENGINE: + rust_binding_args.append("--no-default-features") +rust_binding_args += [f"-C", f"link-args=-Wl,-rpath,{python_lib_dir}"] + rust_extensions = [ RustBin( target="process_allocator", @@ -131,7 +138,7 @@ def run(self): path="monarch_extension/Cargo.toml", debug=False, features=["tensor_engine"] if USE_TENSOR_ENGINE else [], - args=[] if USE_TENSOR_ENGINE else ["--no-default-features"], + args=rust_binding_args, ), ] diff --git a/torch-sys-cuda/Cargo.toml b/torch-sys-cuda/Cargo.toml index 75c010230..7e8612361 100644 --- a/torch-sys-cuda/Cargo.toml +++ b/torch-sys-cuda/Cargo.toml @@ -15,7 +15,7 @@ cxx = "1.0.119" derive_more = { version = "1.0.0", features = ["full"] } fxhash = "0.2.1" nccl-sys = { path = "../nccl-sys" } -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } serde = { version = "1.0.219", features = ["derive", "rc"] } thiserror = "2.0.12" torch-sys = { version = "0.0.0", path = "../torch-sys" } diff --git a/torch-sys/Cargo.toml b/torch-sys/Cargo.toml index 1b3f27265..dd06b2ce4 100644 --- a/torch-sys/Cargo.toml +++ b/torch-sys/Cargo.toml @@ -19,7 +19,7 @@ hyperactor = { version = "0.0.0", path = "../hyperactor" } monarch_types = { version = "0.0.0", path = "../monarch_types" } nccl-sys = { path = "../nccl-sys", optional = true } paste = "1.0.14" -pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] } +pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] } regex = "1.11.1" serde = { version = "1.0.219", features = ["derive", "rc"] } thiserror = "2.0.12" From 1a621ead50bbdbf86ee8243908ef54d7b7b6b83a Mon Sep 17 00:00:00 2001 From: Shayne Fletcher Date: Sat, 18 Oct 2025 10:19:55 -0700 Subject: [PATCH 2/2] build monarch_extension as extension-module (2) (#1611) Summary: Pull Request resolved: https://github.com/meta-pytorch/monarch/pull/1611 the goal is to inject an rpath into the built `_rust_bindings.so`. we put `-C link-args=-Wl,-rpath,` into `RUST_FLAGS`. Differential Revision: D84986921 --- setup.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index ae25d75e2..f34e7af52 100644 --- a/setup.py +++ b/setup.py @@ -120,11 +120,8 @@ def run(self): readme = f.read() python_lib_dir = sysconfig.get_config_var("LIBDIR") # path to libpython3.10.so.1.0 - -rust_binding_args = ["--"] -if not USE_TENSOR_ENGINE: - rust_binding_args.append("--no-default-features") -rust_binding_args += [f"-C", f"link-args=-Wl,-rpath,{python_lib_dir}"] +rpath_flag = f"-C link-args=-Wl,-rpath,{python_lib_dir}" +os.environ["RUSTFLAGS"] = (os.environ.get("RUSTFLAGS", "") + " " + rpath_flag).strip() rust_extensions = [ RustBin( @@ -138,7 +135,7 @@ def run(self): path="monarch_extension/Cargo.toml", debug=False, features=["tensor_engine"] if USE_TENSOR_ENGINE else [], - args=rust_binding_args, + args=[] if USE_TENSOR_ENGINE else ["--no-default-features"], ), ]