Skip to content

Commit fbf4d69

Browse files
JenniferWangfacebook-github-bot
authored andcommitted
make monarch build compatible for both conda and uv
Summary: ## The Problem Basically currently the monarch wheel cannot be installed by `uv`: ``` ImportError: libpython3.10.so.1.0: cannot open shared object file: No such file or directory ``` ## Root Cause The root cause is - monarch wheel is built in Conda environment with rpaths baked into the compiled `.so` files - `uv` installs Python in a different location (`~/.local/share/uv/python/...`), and therefore the baked-in paths don't exist in uv environments ## Proper Fix There are workaround for `uv` installation, e.g. set `LD_LIBRARY_PATH` to point to `uv`'s Python library directory, but I think we should do a proper fix to the monarch build since ChatGPT said "Best practice is not to link extensions against libpython on Unix; the loader resolves Python symbols from the running interpreter. PyO3’s extension-module feature exists to avoid linking to libpython altogether. Manylinux guidance and PyO3 docs echo this." https://github.com/pypa/manylinux/issues/69?utm_source=chatgpt.com This diff is an attempt to use ` extension-module` feature and roughly does two things * Turn on the feature (in BUCK) * Remove the hardcoded `rpath` from `setup.py` ----- ## Appendix Workaround by setting `LD_LIBRARY_PATH` 1. Find your uv Python location: ```bash uv run python -c "import sys; print(sys.executable)" # Example output: /home/user/.local/share/uv/python/cpython-3.12.12-linux-x86_64-gnu/bin/python3.12 ``` 2. Set LD_LIBRARY_PATH: ```bash # For Python 3.12 (adjust version as needed) export LD_LIBRARY_PATH="$HOME/.local/share/uv/python/cpython-3.12.12-linux-x86_64-gnu/lib:$LD_LIBRARY_PATH" ``` Differential Revision: D86537232
1 parent f5a0c71 commit fbf4d69

File tree

3 files changed

+5
-23
lines changed

3 files changed

+5
-23
lines changed

monarch_extension/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ monarch_tensor_worker = { version = "0.0.0", path = "../monarch_tensor_worker",
3434
monarch_types = { version = "0.0.0", path = "../monarch_types" }
3535
nccl-sys = { path = "../nccl-sys", optional = true }
3636
ndslice = { version = "0.0.0", path = "../ndslice" }
37-
pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] }
37+
pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] }
3838
rdmaxcel-sys = { path = "../rdmaxcel-sys", optional = true }
3939
serde = { version = "1.0.219", features = ["derive", "rc"] }
4040
tokio = { version = "1.47.1", features = ["full", "test-util", "tracing"] }

setup.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,10 @@ def run(self):
121121
readme = f.read()
122122

123123
if sys.platform.startswith("linux"):
124-
# Always include the active env's lib (Conda-safe)
124+
# With extension-module, we don't link libpython, but we still need
125+
# RPATH for finding libtorch and other PyTorch libraries
125126
conda_lib = os.path.join(sys.prefix, "lib")
126127

127-
# Only use LIBDIR if it actually contains the current libpython
128-
ldlib = sysconfig.get_config_var("LDLIBRARY") or ""
129-
libdir = sysconfig.get_config_var("LIBDIR") or ""
130-
py_lib = ""
131-
if libdir and ldlib:
132-
cand = os.path.join(libdir, ldlib)
133-
if os.path.exists(cand) and os.path.realpath(libdir) != os.path.realpath(
134-
conda_lib
135-
):
136-
py_lib = libdir
137-
138-
# Prefer sidecar .so next to the extension; then the conda env;
139-
# then (optionally) py_lib
140128
flags = [
141129
"-C",
142130
"link-arg=-Wl,--enable-new-dtags",
@@ -145,14 +133,8 @@ def run(self):
145133
"-C",
146134
"link-arg=-Wl,-rpath,$ORIGIN",
147135
"-C",
148-
"link-arg=-Wl,-rpath,$ORIGIN/..",
149-
"-C",
150-
"link-arg=-Wl,-rpath,$ORIGIN/../../..",
151-
"-C",
152-
"link-arg=-Wl,-rpath," + conda_lib,
136+
"link-arg=-Wl,-rpath," + conda_lib, # For libtorch
153137
]
154-
if py_lib:
155-
flags += ["-C", "link-arg=-Wl,-rpath," + py_lib]
156138

157139
cur = os.environ.get("RUSTFLAGS", "")
158140
os.environ["RUSTFLAGS"] = (cur + " " + " ".join(flags)).strip()

torch-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ hyperactor = { version = "0.0.0", path = "../hyperactor" }
1919
monarch_types = { version = "0.0.0", path = "../monarch_types" }
2020
nccl-sys = { path = "../nccl-sys", optional = true }
2121
paste = "1.0.14"
22-
pyo3 = { version = "0.24", features = ["anyhow", "multiple-pymethods", "py-clone"] }
22+
pyo3 = { version = "0.24", features = ["anyhow", "extension-module", "multiple-pymethods", "py-clone"] }
2323
regex = "1.11.1"
2424
serde = { version = "1.0.219", features = ["derive", "rc"] }
2525
thiserror = "2.0.12"

0 commit comments

Comments
 (0)