diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000000..1550338737 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,11 @@ +# zmq-sys compiles libzmq from zeromq-src, which never defines +# HAVE_POSIX_MEMALIGN, so libzmq malloc()s its command queues (16-byte aligned) +# although command_t is declared aligned(64). A compiler that targets AVX2, such +# as GCC on Ubuntu 26.04's amd64v3 builds, then copies command_t with aligned +# 32-byte stores, which segfault on the first message. These are the values +# libzmq's own build detects on Linux. The per-target variables add to any +# CXXFLAGS in the environment instead of being replaced by it. At the repository +# root so that cargo runs from here (setup.py, `--manifest-path rust/...`) see it. +[env] +CXXFLAGS_x86_64_unknown_linux_gnu = "-DHAVE_POSIX_MEMALIGN=1 -DZMQ_CACHELINE_SIZE=64" +CXXFLAGS_aarch64_unknown_linux_gnu = "-DHAVE_POSIX_MEMALIGN=1 -DZMQ_CACHELINE_SIZE=64" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9f694aecb4..1412962a08 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -141,7 +141,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - name: Install the pinned Rust toolchain (rust-toolchain.toml) + run: rustup toolchain install - uses: Swatinem/rust-cache@v2 with: workspaces: | @@ -154,13 +155,13 @@ jobs: run: sudo apt-get update && sudo apt-get install -y libzmq3-dev - name: Cargo tests (transport + arena semantics) working-directory: rust - # Off with extension-module (see rust/Cargo.toml): the test binary is an - # executable, so it links libpython rather than resolving CPython - # symbols at import time. setup-python's interpreter is --enable-shared, - # so point the loader at its libdir for the run. + # The bindings without extension-module (see rust/Cargo.toml): the test + # binary is an executable, so it links libpython rather than resolving + # CPython symbols at import time. setup-python's interpreter is + # --enable-shared, so point the loader at its libdir for the run. env: LD_LIBRARY_PATH: ${{ env.pythonLocation }}/lib - run: cargo test --release --no-default-features + run: cargo test --release --no-default-features --features python - run: cargo test --release --manifest-path rust/server/Cargo.toml - name: Build the Rust frontend (mstar-server) working-directory: rust/server @@ -172,8 +173,6 @@ jobs: maturin build --release pip install target/wheels/*.whl - name: Interop tests (transport + arena + frontend) - env: - LD_LIBRARY_PATH: ${{ env.pythonLocation }}/lib run: | # pyyaml: the runtime tests reach mstar.model.base, which imports it; # prometheus-client: the frontend's /metrics test renders a real registry; diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b7c9a21634..afd55c527f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,19 +12,155 @@ name: Publish to PyPI # one release run at a time: publish with the engine's pending publisher, # add the next alias's pending publisher, re-run the failed job, repeat. # Uploads skip files that already exist, so re-runs are safe. +# +# mstar-ai ships an sdist, a pure-Python wheel, and Linux wheels per CPython +# version and architecture that also carry the mstar_rust extension (setup.py +# compiles rust/ only under cibuildwheel). pip prefers the Linux wheel where +# one matches and falls back to the pure one elsewhere, which then runs on the +# Python transport and graph runtime. The Linux wheels publish in their own +# job, so a failure in `wheels` holds back only them. on: release: types: [published] + # Build and check the Linux wheels without publishing: by hand, or on a PR + # that touches what goes into them. Only a release runs the pypi jobs. + workflow_dispatch: + pull_request: + paths: + - "rust/**" + - rust-toolchain.toml + - .cargo/config.toml + - setup.py + - pyproject.toml + - MANIFEST.in + - .github/workflows/release.yml + +# Read-only by default: PR runs build PR code. The publish jobs add id-token. +permissions: + contents: read + +# A newer PR or manual run on the same ref cancels one in progress. Release +# runs have their own group, so neither of those can cancel one; a release run +# is never cancelled in progress, though a newer one still replaces a pending one. +concurrency: + group: release-${{ github.event_name }}-${{ github.ref }} + cancel-in-progress: ${{ github.event_name != 'release' }} jobs: + wheels: + strategy: + matrix: + os: [ubuntu-latest, ubuntu-24.04-arm] + runs-on: ${{ matrix.os }} + env: + # Version-specific rather than abi3, matching the extension that + # `maturin develop` builds and the tests exercise. PR and manual runs + # build one CPython per architecture; the check expects one wheel each. + CIBW_BUILD: ${{ github.event_name == 'release' && 'cp312-manylinux_* cp313-manylinux_* cp314-manylinux_*' || 'cp312-manylinux_*' }} + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - name: Install the pinned Rust toolchain (rust-toolchain.toml) + run: rustup toolchain install + - uses: taiki-e/install-action@v2 + with: + tool: cargo-about@0.9.2 + # The licenses of the crates and vendored C/C++ compiled into the wheel, + # shipped in its dist-info/licenses (setuptools' default LICEN[CS]E* + # glob). Fails on any license rust/about.toml does not accept. + - name: Collect third-party licenses + run: | + cargo about generate --locked -c rust/about.toml -m rust/Cargo.toml \ + rust/about.hbs -o LICENSES-THIRD-PARTY-mstar_rust.txt + cargo about generate --locked -c rust/about.toml -m rust/server/Cargo.toml \ + rust/about.hbs -o LICENSES-THIRD-PARTY-mstar-server.txt + - uses: pypa/cibuildwheel@v4.2.1 + env: + CIBW_BEFORE_ALL_LINUX: >- + curl -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain none && + cd {project} && $HOME/.cargo/bin/rustup toolchain install + CIBW_ENVIRONMENT_LINUX: PATH="$HOME/.cargo/bin:$PATH" + with: + output-dir: wheelhouse + - uses: actions/setup-python@v5 + with: + python-version: | + 3.12 + 3.13 + 3.14 + # Outside the build container, with no dependencies installed: the + # extension imports at the version mstar expects, the frontend binary + # runs from the scripts directory find_server_binary looks in without + # any libpython (setup-python puts its libdir on LD_LIBRARY_PATH, which + # would hide a link to one), and both license notices are in the + # metadata. + - name: Check each wheel in a clean venv + shell: python + run: | + import glob, os, re, subprocess, tempfile + expected = re.search(r'^EXPECTED_MSTAR_RUST_VERSION = "(.+)"$', + open("mstar/communication/communicator.py").read(), re.M)[1] + check = """ + import importlib.metadata as md, mstar_rust, os, pathlib, subprocess, sys, sysconfig + assert mstar_rust.__version__ == sys.argv[1], mstar_rust.__version__ + shipped = {f.name for f in md.files('mstar-ai')} + for licenses in ('LICENSES-THIRD-PARTY-mstar_rust.txt', 'LICENSES-THIRD-PARTY-mstar-server.txt'): + assert licenses in shipped, licenses + server = pathlib.Path(sysconfig.get_path('scripts')) / 'mstar-server' + needed = subprocess.run(['readelf', '-d', server], capture_output=True, text=True, check=True).stdout + assert 'libpython' not in needed, needed + env = {k: v for k, v in os.environ.items() if k != 'LD_LIBRARY_PATH'} + subprocess.run([server, '--help'], env=env, check=True) + """ + wheels = sorted(glob.glob("wheelhouse/*.whl")) + assert len(wheels) == len(os.environ["CIBW_BUILD"].split()), wheels + for whl in wheels: + py = "python3." + re.search(r"-cp3(\d+)-", whl).group(1) + venv = tempfile.mkdtemp() + subprocess.run([py, "-m", "venv", venv], check=True) + subprocess.run([f"{venv}/bin/pip", "install", "-q", "--no-deps", whl], check=True) + subprocess.run([f"{venv}/bin/python", "-c", check, expected], check=True) + print("ok", whl) + # These wheels turn the Rust transport and graph runtime on by default + # (MSTAR_RUST_ZMQ / MSTAR_RUST_GRAPH AUTO) on Python versions and an + # architecture rust-transport never runs, so each one also runs test/rust + # with its dependencies installed (CPU torch). From a copy of test/, so + # `mstar` imports from the wheel rather than the checkout. + - name: Run test/rust against each wheel + shell: python + run: | + import glob, re, shutil, subprocess, tempfile + for whl in sorted(glob.glob("wheelhouse/*.whl")): + py = "python3." + re.search(r"-cp3(\d+)-", whl).group(1) + venv, tests = tempfile.mkdtemp(), tempfile.mkdtemp() + subprocess.run([py, "-m", "venv", venv], check=True) + # triton: the arena tests reach mstar.utils.sampling; the rest + # are what ci.yml's rust-transport job adds for test/rust. + subprocess.run([f"{venv}/bin/pip", "install", "-q", + "--extra-index-url", "https://download.pytorch.org/whl/cpu", + whl, "pytest==9.2.0", "prometheus-client", "httpx", "triton"], + check=True) + for d in ("rust", "modular"): # test/rust imports helpers from test/modular + shutil.copytree(f"test/{d}", f"{tests}/test/{d}") + subprocess.run([f"{venv}/bin/pytest", "-q", "-ra", "test/rust"], cwd=tests, check=True) + - uses: actions/upload-artifact@v4 + with: + name: wheels-${{ matrix.os }} + path: wheelhouse/*.whl + pypi: + if: github.event_name == 'release' runs-on: ubuntu-latest environment: pypi permissions: + contents: read id-token: write steps: - uses: actions/checkout@v4 + with: + persist-credentials: false - uses: actions/setup-python@v5 with: python-version: "3.12" @@ -58,3 +194,23 @@ jobs: with: packages-dir: dist-mstar-serve skip-existing: true + + # After pypi, which checked the tag, so these upload only for a release whose + # sdist and pure wheel went up. + pypi-wheels: + needs: [wheels, pypi] + if: github.event_name == 'release' + runs-on: ubuntu-latest + environment: pypi + permissions: + id-token: write + steps: + - uses: actions/download-artifact@v4 + with: + pattern: wheels-* + path: dist + merge-multiple: true + - name: Publish the mstar-ai Linux wheels + uses: pypa/gh-action-pypi-publish@release/v1 + with: + skip-existing: true diff --git a/.gitignore b/.gitignore index 799d448a89..8ee56276c7 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,6 @@ CLAUDE.md # Optional local CI reports .ci-results/ + +# generated by the release workflow (rust/about.toml) +LICENSES-THIRD-PARTY-*.txt diff --git a/AGENTS.md b/AGENTS.md index db455312a4..1f935c3cc8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -191,7 +191,7 @@ python -m pytest test/modular test/distributed test/chatterbox test/kokoro \ test/omnivoice test/sampling_test mstar/model/*/tests benchmark/worker_phases # the CPU Core CI job (CI adds -p xdist -n 4), no GPU or weights pytest test/integration/ # needs GPU + weights -cd rust && cargo test --release --no-default-features && pytest ../test/rust/ +cd rust && cargo test --release --no-default-features --features python && pytest ../test/rust/ ``` The CPU Core job runs every test module under the directories in `CPU_TEST_PATHS` ([scripts/ci/select_cpu_tests.py](scripts/ci/select_cpu_tests.py)), including `test/modular/`, `test/distributed/` and `mstar/model/*/tests/`; there is no list of test files to add to. A new module there runs in CI as soon as it exists, so it must pass or skip on a CPU-only machine: guard CUDA, weights and optional packages with a `skipif` or `pytest.importorskip("")` (import names: `flashinfer`, `misaki`, `qwen_tts`, `mstar_rust`), and give a test that is broken on `main` an in-file `pytest.mark.skip` whose reason says why and names the fixing PR or issue when one exists. A test module outside those directories goes in `NOT_CPU_CORE` there with where it runs instead; `test/ci/test_cpu_selection.py` fails on one in neither, and on two selected modules outside a package with the same basename. A guard on a reference package named like an mstar model (`kokoro`, `chatterbox`, `omnivoice`) imports a submodule only the reference has: once pytest puts `mstar/model` on `sys.path` for `mstar/model/*/tests`, the bare name finds mstar's own package (see [CONTRIBUTING.md](CONTRIBUTING.md)). diff --git a/MANIFEST.in b/MANIFEST.in index 254f8aa7cf..e463efa762 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,14 @@ # Carry the default deployment configs into the sdist so a wheel built from it # can copy them into mstar/default_configs/ (see setup.py). graft configs +# rust/ and the pinned toolchain, so the Rust wheels can be built from the +# sdist too (setup.py compiles them under CIBUILDWHEEL=1). Such a build must +# first run release.yml's two `cargo about generate` commands, or its wheels +# carry no third-party notices. +graft rust +prune rust/target +prune rust/server/target +include rust-toolchain.toml +# the cargo [env] that builds the vendored libzmq correctly (see the file) +include .cargo/config.toml global-exclude __pycache__ *.py[cod] diff --git a/PACKAGING.md b/PACKAGING.md index 0387ff1ad2..5f4121a0f6 100644 --- a/PACKAGING.md +++ b/PACKAGING.md @@ -31,6 +31,53 @@ sdist so a wheel built from the sdist copies them too. Nothing is duplicated in git. The CLI resolves via `importlib.resources.files("mstar.default_configs")` for a pip install and falls back to the repo `configs/` for checkouts. +## Rust extension and frontend in the Linux wheels + +Besides the sdist and a pure-Python `py3-none-any` wheel, each release builds +a manylinux wheel per CPython version (3.12, 3.13, 3.14) and architecture +(x86_64, aarch64) with cibuildwheel. Those wheels also carry the `mstar_rust` +extension (`rust/`) and the `mstar-server` HTTP frontend binary +(`rust/server/`, installed into the environment's `bin/`). `setup.py` compiles +both with setuptools-rust only when `CIBUILDWHEEL=1`; every other build, +including `pip install -e .`, is pure Python, and a checkout builds the Rust +parts with maturin and cargo (docs/installation.rst). pip picks the matching +manylinux wheel and falls back to the pure one elsewhere, which runs on the +Python transport and graph runtime. + +- **Compiler.** `rust-toolchain.toml` at the repo root pins the Rust version + for CI, the release wheels and local builds. Bump it there. +- **Third-party licenses.** Before building, the workflow runs + [cargo-about](https://github.com/EmbarkStudios/cargo-about) with + `rust/about.toml` over `rust/` and over `rust/server` and writes + `LICENSES-THIRD-PARTY-mstar_rust.txt` and + `LICENSES-THIRD-PARTY-mstar-server.txt`, which ship in each Rust wheel's + `dist-info/licenses/`. A crate under a license not in `accepted` + fails the `wheels` job. The C/C++ that crates vendor is declared there by + hand (libzmq via zeromq-src, Oniguruma via onig_sys, esaxx via esaxx-rs); + check those entries when bumping those crates. +- **libzmq.** libzmq 4.3.4, statically linked into both binaries, is + LGPL-3.0-or-later with its own static-linking exception (the end of its + `COPYING.LESSER`, which the notices carry in full). Its source is the + unmodified upstream release that the zeromq-src 0.2.6+4.3.4 crate vendors: + . The build only adds + defines (`.cargo/config.toml`). +- **Two copies of `mstar_rust`.** `maturin develop` (or `pip install ./rust`) + installs `rust/` as a separate `mstar-rust` distribution, a `mstar_rust/` + package directory. In an environment that also has a Rust wheel's top-level + `mstar_rust.*.so`, the package directory wins (Python's path finder takes a + package directory over a same-named module), so the maturin build is what + loads; the startup line `control mesh transport: rust from + ` names it. The two own different files, so uninstalling one never + deletes the other's: `pip uninstall mstar-rust` goes back to the wheel's + copy. +- **Checks.** Each wheel is installed without dependencies into a clean venv + outside the build container and must import `mstar_rust` at the version + `EXPECTED_MSTAR_RUST_VERSION` names, list both license notices, and have an + `mstar-server` that links no libpython and runs `--help` without + `LD_LIBRARY_PATH`. Then each is installed with its dependencies (CPU torch) + and runs `test/rust` from a copy of `test/`, so `mstar` imports from the + wheel. A failure in either keeps the Rust wheels off PyPI. + ## Cutting a release Version lives in `pyproject.toml` (`[project] version`), and the alias @@ -40,9 +87,10 @@ release: 1. Bump the version in all three pyproject files and land it on `main`. 2. Publish a GitHub Release with a matching tag (e.g. `v0.2.1`). The workflow refuses a tag that does not match the version. -3. The `Publish to PyPI` workflow builds the engine and the alias packages and - uploads them over Trusted Publishing (OIDC, no token stored in the repo). - The `pypi` environment asks one of its reviewers to approve the run first. +3. The `Publish to PyPI` workflow builds the engine and the alias packages + and uploads them over Trusted Publishing (OIDC, no token stored in the + repo); the Linux Rust wheels above go up in a job of their own. The `pypi` + environment asks one of its reviewers to approve each publishing job first. One-time PyPI setup: each project needs a pending Trusted Publisher (owner `mstar-project`, repo `mstar`, workflow `release.yml`, environment `pypi`). @@ -67,3 +115,10 @@ twine upload --repository testpypi dist/* # optional TestPyPI dry r The alias packages are published by the release workflow together with the engine, so they need no separate upload. + +`python -m build` makes only the pure-Python wheel. The Linux Rust wheels need +cibuildwheel (Docker) with the workflow's `CIBW_*` settings. The workflow +builds and checks them without publishing, for CPython 3.12 only, on any pull +request that touches `rust/`, the build files or the workflow, and from the +Actions tab on any branch (`Run workflow`); only a published Release builds +all three versions and runs the publishing jobs. diff --git a/README.md b/README.md index 911cf78a11..6fb16fd59e 100644 --- a/README.md +++ b/README.md @@ -166,8 +166,10 @@ The ZeroMQ control mesh can run over a Rust transport (vendored in [`rust/`](rus pyzmq — wire-compatible, selectable per process with `MSTAR_RUST_ZMQ` (default `AUTO`). Note what AUTO-by-default means: on any machine where the extension is built, the whole mesh switches to the Rust transport with no configuration change — each process logs its -choice at startup (`control mesh transport: ...`), and `MSTAR_RUST_ZMQ=0` pins pyzmq. Build it with -[maturin](https://www.maturin.rs): `uv pip install maturin && maturin develop --release -m rust/Cargo.toml`. +choice at startup (`control mesh transport: ...`), and `MSTAR_RUST_ZMQ=0` pins pyzmq. Build it +with [maturin](https://www.maturin.rs): `uv pip install maturin && maturin develop --release -m rust/Cargo.toml`. +The Linux wheels of `mstar-ai` on PyPI carry it, with libzmq 4.3.4 statically linked under +LGPL-3.0-or-later and its static-linking exception ([PACKAGING.md](PACKAGING.md) has where its source is). See the [installation docs](https://m-star.org/mstar/installation.html) and [environment variables](https://m-star.org/mstar/environment_variables.html). diff --git a/docker/Dockerfile.xpu b/docker/Dockerfile.xpu index 2751adbbfa..9e5725ed5d 100644 --- a/docker/Dockerfile.xpu +++ b/docker/Dockerfile.xpu @@ -68,7 +68,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ RUN curl -LsSf https://astral.sh/uv/install.sh | sh ENV PATH="/root/.local/bin:/root/.cargo/bin:$PATH" RUN curl --proto '=https' --retry 3 --retry-delay 2 --tlsv1.2 -sSf https://sh.rustup.rs \ -| sh -s -- -y --no-modify-path --profile minimal && rustc --version && cargo --version +| sh -s -- -y --no-modify-path --profile minimal --default-toolchain none ENV VIRTUAL_ENV="/opt/venv" ENV UV_PYTHON_INSTALL_DIR=/opt/uv/python RUN uv venv --python ${PYTHON_VERSION} ${VIRTUAL_ENV} @@ -79,6 +79,11 @@ WORKDIR /mstar-workspace ARG MSTAR_REPO=https://github.com/mstar-project/mstar.git ARG MSTAR_REF=main RUN git clone --depth 1 --branch "${MSTAR_REF}" "${MSTAR_REPO}" ./mstar +# The toolchain the checkout pins (rust-toolchain.toml), also as the default so +# cargo works outside the clone. +RUN cd ./mstar && rustup toolchain install \ + && rustup default "$(sed -n 's/^channel = "\(.*\)"$/\1/p' rust-toolchain.toml)" \ + && cd / && rustc --version && cargo --version # uv selects the XPU index for PyTorch and TorchCodec in the single install below. # torchaudio's latest XPU wheel is 2.11; the import check below checks this pairing. diff --git a/docs/environment_variables.rst b/docs/environment_variables.rst index efa558cecf..e380ff0de5 100644 --- a/docs/environment_variables.rst +++ b/docs/environment_variables.rst @@ -332,8 +332,9 @@ Read by the ``mstar-server`` binary and its bridge * - ``MSTAR_SERVER_BIN`` - unset - Path to the ``mstar-server`` binary. Fallback order: - ``--rust-frontend-bin``, this variable, ``$PATH``, then the in-repo - ``rust/server/target/release`` build. + ``--rust-frontend-bin``, this variable, the running environment's + scripts directory (where the Linux wheels install it), ``$PATH``, then + the in-repo ``rust/server/target/release`` build. * - ``MSTAR_API_KEY`` - unset - Same rule as the Python server (``rust/server/src/auth.rs``, read diff --git a/docs/installation.rst b/docs/installation.rst index 7c0902b392..5a9a3bccae 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -490,8 +490,15 @@ runs on the pure-Python paths as before. Migrated so far (see wherever that holds, so on a machine with the extension built, installing it is what moves a worker onto the Rust runtime. -Build the extension into your environment with `maturin -`_ (needs a Rust toolchain; ``rustup`` works): +The Linux wheels of ``mstar-ai`` on PyPI (x86_64 and aarch64, CPython 3.12 to +3.14) carry the extension, so ``pip install mstar-ai`` there gets it; other +platforms get the pure-Python wheel. The extension statically links libzmq +4.3.4, under LGPL-3.0-or-later with its static-linking exception; its source is +the upstream `v4.3.4 release `_. +Elsewhere, or to build from a checkout, build the extension into your +environment with `maturin `_ (needs `rustup +`_, which installs the compiler version pinned in +``rust-toolchain.toml``): .. code-block:: bash @@ -499,7 +506,11 @@ Build the extension into your environment with `maturin maturin develop --release -m rust/Cargo.toml Build with ``--release`` — an unoptimized debug build (maturin's default) -costs real latency on the hot receive path. Verify with: +costs real latency on the hot receive path. If ``mstar-ai`` came from a Linux +wheel that carries the extension, this build (a separate ``mstar-rust`` +distribution) takes precedence over the wheel's copy in the same environment; +``pip uninstall mstar-rust`` goes back to the wheel's, and neither uninstall +removes the other's files. Verify with: .. code-block:: bash @@ -509,25 +520,28 @@ costs real latency on the hot receive path. Verify with: ``test/rust`` is skipped wholesale when the extension is not importable, so a green run there without the build above proves nothing. -The Rust-side tests need one extra flag. The crate's default features turn on -``pyo3/extension-module``, which tells pyo3 *not* to link libpython — right +The Rust-side tests need different features. The crate's default features turn +on ``pyo3/extension-module``, which tells pyo3 *not* to link libpython — right for the cdylib, which resolves CPython symbols from the interpreter that imports it, wrong for ``cargo test``, which is an executable and has to link -it itself. Without this the link fails on ``undefined symbol: -PyEval_RestoreThread``: +it itself. Without ``--no-default-features`` the link fails on ``undefined +symbol: PyEval_RestoreThread``; ``--features python`` keeps the bindings and +the graph runtime, whose tests need them: .. code-block:: bash export LD_LIBRARY_PATH="$(python -c 'import sysconfig print(sysconfig.get_config_var("LIBDIR"))')" - cargo test --release --no-default-features --manifest-path rust/Cargo.toml + cargo test --release --no-default-features --features python --manifest-path rust/Cargo.toml Optional: the Rust HTTP frontend -------------------------------- ``mstar-server`` (``rust/server/``) serves the HTTP surface — routes, OpenAI translation, SSE streaming, uploads — from Rust, with the Python process -keeping preprocessing and the conductor protocol. Build and opt in: +keeping preprocessing and the conductor protocol. A Linux wheel that carries +the extension also installs the binary next to ``mstar-serve``; otherwise build +it. Then opt in: .. code-block:: bash @@ -536,8 +550,8 @@ keeping preprocessing and the conductor protocol. Build and opt in: # or, with an explicit config: mstar-serve --config --rust-frontend The binary is resolved via ``--rust-frontend-bin`` / ``MSTAR_SERVER_BIN`` / -``$PATH`` / the in-repo build. Its knobs are listed in -:doc:`environment_variables`. +the environment's scripts directory / ``$PATH`` / the in-repo build. Its knobs +are listed in :doc:`environment_variables`. The OpenAI routes take the model's adapter from the same registry as the Python frontend (``ADAPTER_REGISTRY`` in ``mstar/api_server/openai/adapters.py``): diff --git a/mstar/api_server/entrypoint.py b/mstar/api_server/entrypoint.py index c9fde9ca9b..dfbfff9d3e 100644 --- a/mstar/api_server/entrypoint.py +++ b/mstar/api_server/entrypoint.py @@ -2482,7 +2482,8 @@ def main(argv: list[str] | None = None): parser.add_argument( "--rust-frontend-bin", type=str, default=None, help="Path to the mstar-server binary (default: MSTAR_SERVER_BIN, " - "$PATH, then rust/server/target/release)", + "the environment's scripts dir, $PATH, then " + "rust/server/target/release)", ) args = parser.parse_args(argv) if args.enable_metrics and MISSING_DEPENDENCY is not None: diff --git a/mstar/api_server/rust_frontend.py b/mstar/api_server/rust_frontend.py index 01e8fe7722..bee545c14a 100644 --- a/mstar/api_server/rust_frontend.py +++ b/mstar/api_server/rust_frontend.py @@ -51,6 +51,7 @@ import os import shutil import subprocess +import sysconfig from pathlib import Path from mstar.api_server.auth import API_KEY_ENV @@ -87,10 +88,14 @@ def openai_adapter_class(model_name: str) -> str: def find_server_binary(explicit: str | None = None) -> str: """Resolve the ``mstar-server`` binary: explicit arg, ``MSTAR_SERVER_BIN``, - ``$PATH``, then the in-repo release build.""" + the running environment's scripts directory (where a Linux wheel installs + it, also when the venv is not activated), ``$PATH``, then the in-repo + release build. The scripts directory goes before ``$PATH`` so another + environment's older binary never serves this one's bridge.""" for candidate in ( explicit, os.getenv("MSTAR_SERVER_BIN"), + str(Path(sysconfig.get_path("scripts")) / "mstar-server"), shutil.which("mstar-server"), str(Path(__file__).resolve().parents[2] / "rust" / "server" / "target" / "release" / "mstar-server"), diff --git a/mstar/cli/main.py b/mstar/cli/main.py index 237bcc8a9b..4dca1bc1c9 100644 --- a/mstar/cli/main.py +++ b/mstar/cli/main.py @@ -334,7 +334,8 @@ def build_parser() -> argparse.ArgumentParser: serve.add_argument( "--rust-frontend-bin", default=None, help="path to the mstar-server binary (default: MSTAR_SERVER_BIN, " - "$PATH, then rust/server/target/release)", + "the environment's scripts dir, $PATH, then " + "rust/server/target/release)", ) serve.set_defaults(func=_serve) return parser diff --git a/mstar/communication/communicator.py b/mstar/communication/communicator.py index 34b938ccb6..a21e6ef52c 100644 --- a/mstar/communication/communicator.py +++ b/mstar/communication/communicator.py @@ -285,16 +285,20 @@ def make_communicator(*args, nonblocking_send: bool = False, **kwargs) -> BaseCo # A support bundle must be able to tell what a mesh was running, # and an old wheel left in an env must not silently take over # the whole mesh under AUTO after an upgrade. + # The path says which copy loaded: a maturin-built mstar-rust + # package shadows the module an mstar-ai Linux wheel ships. version = getattr(mstar_rust, "__version__", "") logger.info( - "control mesh transport: rust %s (MSTAR_RUST_ZMQ=%s)", - version, choice) + "control mesh transport: rust %s from %s (MSTAR_RUST_ZMQ=%s)", + version, mstar_rust.__file__, choice) if version != EXPECTED_MSTAR_RUST_VERSION: logger.warning( - "mstar_rust version %s does not match this tree's " - "expected %s — a stale wheel may be shadowing the " - "vendored rust/ build (rebuild with `maturin develop " - "--release`)", version, EXPECTED_MSTAR_RUST_VERSION) + "mstar_rust version %s at %s does not match the expected " + "%s: in a checkout, rebuild it with `maturin develop " + "--release -m rust/Cargo.toml`; in a pip install, upgrade " + "mstar-ai and `pip uninstall mstar-rust` if a leftover " + "build is shadowing the wheel's copy", + version, mstar_rust.__file__, EXPECTED_MSTAR_RUST_VERSION) if not nonblocking_send or hasattr(mstar_rust.ZmqCommunicator, "try_send"): return RustZMQCommunicator(*args, **kwargs) # built before try_send: every send the outbox makes would fail. diff --git a/pyproject.toml b/pyproject.toml index 41ebdbed19..797a08cd2f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools>=78.1.1", "wheel"] +requires = ["setuptools>=78.1.1", "setuptools-rust>=1.11", "wheel"] build-backend = "setuptools.build_meta" [project] @@ -19,6 +19,8 @@ keywords = ["inference", "multimodal", "llm", "serving", "engine"] classifiers = [ "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", "Operating System :: POSIX :: Linux", "Topic :: Scientific/Engineering :: Artificial Intelligence", "Environment :: GPU :: NVIDIA CUDA", diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000000..91e9e496e4 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,6 @@ +# The Rust compiler for rust/ and rust/server: CI, the release wheels and +# local builds all resolve it from here. +[toolchain] +channel = "1.99.1" +profile = "minimal" +components = ["rustfmt", "clippy"] diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 36c73beb32..9c4b147397 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -33,7 +33,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d067ad48b8650848b989a59a86c6c36a995d02d2bf778d45c3c5d57bc2718f02" dependencies = [ "smallvec", - "target-lexicon", + "target-lexicon 0.12.16", ] [[package]] @@ -279,11 +279,10 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.23.5" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7778bffd85cf38175ac1f545509665d0b9b92a198ca7941f131f85f7a4f9a872" +checksum = "7ba0117f4212101ee6544044dae45abe1083d30ce7b29c4b5cbdfa2354e07383" dependencies = [ - "cfg-if", "indoc", "libc", "memoffset", @@ -297,19 +296,18 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.23.5" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94f6cbe86ef3bf18998d9df6e0f3fc1050a8c5efa409bf712e661a4366e010fb" +checksum = "4fc6ddaf24947d12a9aa31ac65431fb1b851b8f4365426e182901eabfb87df5f" dependencies = [ - "once_cell", - "target-lexicon", + "target-lexicon 0.13.5", ] [[package]] name = "pyo3-ffi" -version = "0.23.5" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9f1b4c431c0bb1c8fb0a338709859eed0d030ff6daa34368d3b152a63dfdd8d" +checksum = "025474d3928738efb38ac36d4744a74a400c901c7596199e20e45d98eb194105" dependencies = [ "libc", "pyo3-build-config", @@ -317,9 +315,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.23.5" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc2201328f63c4710f68abdf653c89d8dbc2858b88c5d88b0ff38a75288a9da" +checksum = "2e64eb489f22fe1c95911b77c44cc41e7c19f3082fc81cce90f657cdc42ffded" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -329,9 +327,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.23.5" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fca6726ad0f3da9c9de093d6f116a93c1a38e417ed73bf138472cf4064f72028" +checksum = "100246c0ecf400b475341b8455a9213344569af29a3c841d29270e53102e0fcf" dependencies = [ "heck", "proc-macro2", @@ -505,6 +503,12 @@ version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" +[[package]] +name = "target-lexicon" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca" + [[package]] name = "thiserror" version = "2.0.18" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 8bb9774a34..210afaccd3 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -3,6 +3,7 @@ name = "mstar-rust" version = "0.1.1" edition = "2021" license = "Apache-2.0" +publish = false [lib] name = "mstar_rust" @@ -21,7 +22,7 @@ rmp-serde = "1.3" rustc-hash = "2" thiserror = "2" zmq = "0.10" -pyo3 = { version = "0.23" } +pyo3 = { version = "0.26", optional = true } rmpv = "1.3.1" # Direct msgpack writing. rmpv builds a Value tree first, which allocates a # String for every map KEY -- ~13 per tensor descriptor -- and then copies the @@ -30,12 +31,15 @@ rmpv = "1.3.1" rmp = "0.8" [features] -# On for the cdylib: a Python extension resolves the CPython symbols at import -# time and must NOT link libpython. Off for `cargo test`, whose binary is an -# executable and therefore has to link it -- hence -# `cargo test --no-default-features`. +# `python`: the PyO3 bindings and the modules built on them (graph runtime, +# tensor bookkeeping). `extension-module` adds the cdylib's rule that a Python +# extension resolves the CPython symbols at import time and must NOT link +# libpython. `cargo test` builds an executable, which has to link it -- hence +# `cargo test --no-default-features --features python`. rust/server takes +# neither, so it links no Python at all. default = ["extension-module"] -extension-module = ["pyo3/extension-module"] +python = ["dep:pyo3"] +extension-module = ["python", "pyo3/extension-module"] [profile.release] opt-level = 3 diff --git a/rust/about.hbs b/rust/about.hbs new file mode 100644 index 0000000000..f129eed704 --- /dev/null +++ b/rust/about.hbs @@ -0,0 +1,15 @@ +Third-party software compiled into one Rust binary in this wheel: +LICENSES-THIRD-PARTY-mstar_rust.txt covers the mstar_rust extension and +LICENSES-THIRD-PARTY-mstar-server.txt the mstar-server frontend. mstar itself +is under LICENSE. +{{#each licenses}} + +================================================================================ +{{{name}}} +Used by: +{{#each used_by}} + - {{crate.name}} {{crate.version}} +{{/each}} +-------------------------------------------------------------------------------- +{{{text}}} +{{/each}} diff --git a/rust/about.toml b/rust/about.toml new file mode 100644 index 0000000000..00299d155a --- /dev/null +++ b/rust/about.toml @@ -0,0 +1,77 @@ +# cargo-about config for the LICENSES-THIRD-PARTY-*.txt notices of the Rust +# crates (and the C/C++ they vendor) compiled into the Linux wheels. The release +# workflow runs it once per binary: on rust/ for the mstar_rust extension and on +# rust/server for mstar-server, which share most crates but not pyo3's. +# A crate under any license not listed here fails the wheel build. +accepted = [ + "Apache-2.0", + "Apache-2.0 WITH LLVM-exception", + "LGPL-3.0-or-later WITH AdditionRef-libzmq-static-linking-exception", + "BSD-2-Clause", + "BSD-3-Clause", + "BSL-1.0", + "CDLA-Permissive-2.0", + "ISC", + "MIT", + "Unicode-3.0", + "Unlicense", + "Zlib", +] +targets = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"] +ignore-build-dependencies = false +ignore-dev-dependencies = true +# mstar-rust and mstar-server are this project, covered by LICENSE; this skips +# them because their Cargo.toml says `publish = false`. +private = { ignore = true } +workarounds = ["ring"] + +# The crate fields below cover only the Rust wrappers; these add the vendored +# C/C++ sources that are compiled into the binaries. onig_sys and esaxx-rs are +# only in rust/server's tree, so the rust/ run warns that they are absent. + +# libzmq 4.3.4: LGPL-3.0-or-later plus the static-linking exception appended to +# its COPYING.LESSER ("SPECIAL EXCEPTION GRANTED BY COPYRIGHT HOLDERS"), and +# the BSD-3-Clause SHA-1 it compiles. The exception is libzmq's own +# Classpath-style text, which matches no SPDX exception id +# (LGPL-3.0-linking-exception is different text), hence the AdditionRef. The +# notice carries the whole COPYING.LESSER, exception included. +[zeromq-src.clarify] +license = "(MIT OR Apache-2.0) AND (LGPL-3.0-or-later WITH AdditionRef-libzmq-static-linking-exception) AND BSD-3-Clause" +[[zeromq-src.clarify.files]] +path = "LICENSE-MIT" +license = "MIT" +checksum = "23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3" +[[zeromq-src.clarify.files]] +path = "vendor/COPYING.LESSER" +license = "LGPL-3.0-or-later WITH AdditionRef-libzmq-static-linking-exception" +checksum = "83f32abe61ee58ffb1b007412c08415168c052501dbf56d7a47aaaac52b03ef6" +[[zeromq-src.clarify.files]] +path = "vendor/external/sha1/sha1.c" +license = "BSD-3-Clause" +checksum = "dfe129c5f0d705988c2d07cde42ce44772182e342773c76a0c386beaeeb0097e" +end = "SUCH DAMAGE." + +# Oniguruma (BSD-2-Clause), the regex engine the tokenizers crate binds. +[onig_sys.clarify] +license = "MIT AND BSD-2-Clause" +[[onig_sys.clarify.files]] +path = "LICENSE.md" +license = "MIT" +checksum = "71f321038b088358004bee991635ac09e4c703bec467d3c30c06992c0595f189" +[[onig_sys.clarify.files]] +path = "oniguruma/COPYING" +license = "BSD-2-Clause" +checksum = "70ba5469ea0bab6e18a32d7009068f996503168d27be57747e08da34337ff26f" + +# esaxx (MIT), the C++ suffix-array code esaxx-rs compiles. +[esaxx-rs.clarify] +license = "Apache-2.0 AND MIT" +[[esaxx-rs.clarify.files]] +path = "LICENSE" +license = "Apache-2.0" +checksum = "c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4" +[[esaxx-rs.clarify.files]] +path = "src/esa.hxx" +license = "MIT" +checksum = "cdb4a78fc2d2738d8dd4af2e3b08531d858833822e2a2e968c353d3f40bdfe2d" +end = "OTHER DEALINGS IN THE SOFTWARE." diff --git a/rust/server/Cargo.lock b/rust/server/Cargo.lock index cd5d54db06..62f7aa6bde 100644 --- a/rust/server/Cargo.lock +++ b/rust/server/Cargo.lock @@ -677,15 +677,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "indoc" -version = "2.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706" -dependencies = [ - "rustversion", -] - [[package]] name = "itertools" version = "0.11.0" @@ -802,15 +793,6 @@ dependencies = [ "libc", ] -[[package]] -name = "memoffset" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" -dependencies = [ - "autocfg", -] - [[package]] name = "mime" version = "0.3.17" @@ -862,7 +844,6 @@ version = "0.1.1" dependencies = [ "libc", "memmap2", - "pyo3", "rmp", "rmp-serde", "rmpv", @@ -982,12 +963,6 @@ version = "0.3.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" -[[package]] -name = "portable-atomic" -version = "1.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" - [[package]] name = "potential_utf" version = "0.1.5" @@ -1015,69 +990,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "pyo3" -version = "0.23.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7778bffd85cf38175ac1f545509665d0b9b92a198ca7941f131f85f7a4f9a872" -dependencies = [ - "cfg-if", - "indoc", - "libc", - "memoffset", - "once_cell", - "portable-atomic", - "pyo3-build-config", - "pyo3-ffi", - "pyo3-macros", - "unindent", -] - -[[package]] -name = "pyo3-build-config" -version = "0.23.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94f6cbe86ef3bf18998d9df6e0f3fc1050a8c5efa409bf712e661a4366e010fb" -dependencies = [ - "once_cell", - "target-lexicon", -] - -[[package]] -name = "pyo3-ffi" -version = "0.23.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9f1b4c431c0bb1c8fb0a338709859eed0d030ff6daa34368d3b152a63dfdd8d" -dependencies = [ - "libc", - "pyo3-build-config", -] - -[[package]] -name = "pyo3-macros" -version = "0.23.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc2201328f63c4710f68abdf653c89d8dbc2858b88c5d88b0ff38a75288a9da" -dependencies = [ - "proc-macro2", - "pyo3-macros-backend", - "quote", - "syn", -] - -[[package]] -name = "pyo3-macros-backend" -version = "0.23.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fca6726ad0f3da9c9de093d6f116a93c1a38e417ed73bf138472cf4064f72028" -dependencies = [ - "heck", - "proc-macro2", - "pyo3-build-config", - "quote", - "syn", -] - [[package]] name = "quote" version = "1.0.46" @@ -1725,12 +1637,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" -[[package]] -name = "unindent" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3" - [[package]] name = "untrusted" version = "0.9.0" diff --git a/rust/server/Cargo.toml b/rust/server/Cargo.toml index 78fa41caa8..6bb9b563da 100644 --- a/rust/server/Cargo.toml +++ b/rust/server/Cargo.toml @@ -3,14 +3,16 @@ name = "mstar-server" version = "0.1.0" edition = "2021" license = "Apache-2.0" +publish = false [[bin]] name = "mstar-server" path = "src/main.rs" [dependencies] -# The vendored transport/arena library crate (rust/) — rlib target. -# Executables must link libpython; extension-module suppresses that linkage. +# The vendored transport/arena library crate (rust/) — rlib target. No default +# features: the server uses only the transport, so it builds without `python` +# and links no libpython. mstar-rust = { path = "..", package = "mstar-rust", default-features = false } axum = { version = "0.7", features = ["multipart"] } tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "signal", "sync"] } diff --git a/rust/server/src/main.rs b/rust/server/src/main.rs index 6c70ac3323..b9e0c5b60b 100644 --- a/rust/server/src/main.rs +++ b/rust/server/src/main.rs @@ -68,9 +68,13 @@ use utf8::Utf8Stream; #[tokio::main] async fn main() { - let model_name = std::env::args() - .nth(1) - .expect("usage: mstar-server [port] [conductor_socket_dir] [upload_dir]"); + const USAGE: &str = + "usage: mstar-server [port] [conductor_socket_dir] [upload_dir]"; + let model_name = std::env::args().nth(1).expect(USAGE); + if model_name == "-h" || model_name == "--help" { + println!("{USAGE}"); + return; + } let port: u16 = std::env::args() .nth(2) .and_then(|p| p.parse().ok()) diff --git a/rust/src/bindings.rs b/rust/src/bindings.rs new file mode 100644 index 0000000000..20c9ff090f --- /dev/null +++ b/rust/src/bindings.rs @@ -0,0 +1,327 @@ +//! The PyO3 surface the Python wrappers drive: the `mstar_rust` module. + +use std::os::raw::{c_int, c_void}; +use std::time::Duration; + +use pyo3::exceptions::{PyRuntimeError, PyValueError}; +use pyo3::ffi; +use pyo3::prelude::*; +use pyo3::types::PyBytes; + +use crate::communicator::{RawZmqCommunicator, RecvEvent}; +use crate::shm::{SegmentedShmArena, ShmArena}; + +/// Opaque byte frames over ZMQ PUSH/PULL: ipc or tcp endpoints, lazily-cached +/// peers, and wakeup-fd polling (an eventfd wakes `recv_or_wake` instantly). +/// Encoding is the caller's (pickle today; msgpack by swapping the codec). +#[pyclass(name = "ZmqCommunicator")] +pub struct PyZmqCommunicator { + /// Behind an Arc so the graph runtime can hold a share of the SAME + /// transport and send from Rust, rather than hopping back into Python for + /// every frame. No extra lock: RawZmqCommunicator::send takes &self and + /// guards its own sockets. + inner: std::sync::Arc, +} + +impl PyZmqCommunicator { + pub fn share(&self) -> std::sync::Arc { + self.inner.clone() + } +} + +#[pymethods] +impl PyZmqCommunicator { + /// Bind the PULL inbox at `ipc://{dir}/{my_id}.ipc`. + #[new] + fn new(my_id: &str, dir: &str) -> PyResult { + Ok(Self { + inner: std::sync::Arc::new( + RawZmqCommunicator::bind(my_id, dir) + .map_err(|e| PyRuntimeError::new_err(e.to_string()))?, + ), + }) + } + + /// Bind at an explicit zmq endpoint (e.g. `tcp://0.0.0.0:5701`). + #[staticmethod] + fn bind_endpoint(my_id: &str, endpoint: &str) -> PyResult { + Ok(Self { + inner: std::sync::Arc::new( + RawZmqCommunicator::bind_endpoint(my_id, endpoint) + .map_err(|e| PyRuntimeError::new_err(e.to_string()))?, + ), + }) + } + + fn last_endpoint(&self) -> PyResult { + self.inner + .last_endpoint() + .map_err(|e| PyRuntimeError::new_err(e.to_string())) + } + + fn register_peer(&self, peer_id: &str, endpoint: &str) { + self.inner.register_peer(peer_id, endpoint); + } + + /// Poll `fd` (an eventfd) alongside the inbox; when readable, + /// `recv_or_wake` returns ("wake", None) immediately. The registrant + /// reads/clears the fd (level-triggered). + fn register_wakeup_fd(&self, fd: i32) { + self.inner.register_wakeup_fd(fd); + } + + /// Released-GIL send: a PUSH send blocks when the peer is at its + /// high-water mark, and blocking with the GIL held would freeze every + /// Python thread in the process (pyzmq releases it here too). + fn send(&self, py: Python<'_>, peer_id: &str, data: &[u8]) -> PyResult<()> { + py.detach(|| self.inner.send(peer_id, data)) + .map_err(|e| PyRuntimeError::new_err(e.to_string())) + } + + /// `send` that never blocks: false when the peer is at its high-water + /// mark, with nothing sent. + fn try_send(&self, peer_id: &str, data: &[u8]) -> PyResult { + self.inner.try_send(peer_id, data) + .map_err(|e| PyRuntimeError::new_err(e.to_string())) + } + + fn try_recv<'py>(&self, py: Python<'py>) -> Option> { + self.inner.try_recv().map(|b| PyBytes::new(py, &b)) + } + + /// Drain all currently-queued frames in one call — one GIL round-trip + /// per batch instead of one `try_recv` per message. + fn drain<'py>(&self, py: Python<'py>) -> Vec> { + let frames = py.detach(|| self.inner.drain()); + frames.iter().map(|b| PyBytes::new(py, b)).collect() + } + + fn recv_timeout<'py>(&self, py: Python<'py>, timeout_ms: u64) -> Option> { + py.detach(|| self.inner.recv_timeout(Duration::from_millis(timeout_ms))) + .map(|b| PyBytes::new(py, &b)) + } + + /// ("msg", bytes) | ("wake", None) | ("timeout", None). Raises if a + /// registered wakeup fd is closed/invalid (the registrant's bug — loud, + /// instead of degrading blocking waits into an instant-timeout spin). + fn recv_or_wake<'py>( + &self, + py: Python<'py>, + timeout_ms: u64, + ) -> PyResult<(&'static str, Option>)> { + let ev = py.detach(|| self.inner.recv_or_wake(Duration::from_millis(timeout_ms))); + Ok(match ev { + RecvEvent::Message(b) => ("msg", Some(PyBytes::new(py, &b))), + RecvEvent::Wake => ("wake", None), + RecvEvent::WakeFdError => { + return Err(PyRuntimeError::new_err( + "a registered wakeup fd is closed/invalid (POLLERR/\ + POLLNVAL): fix the EventWakeup lifetime")); + } + RecvEvent::Timeout => ("timeout", None), + }) + } +} + +/// Shared-memory tensor arena for cross-process transport. Producer: +/// `create(name, size)` -> `reserve(nbytes)` -> `torch.frombuffer( +/// memoryview(arena)[off:off+n], dtype=..).copy_(cpu_tensor)`; send the +/// offset descriptor; `free(off)` on reclaim. Consumer: `open(name)` and +/// `torch.frombuffer(memoryview(arena)[off:off+n], ..)` (then H2D). +#[pyclass(name = "ShmArena")] +struct PyShmArena { + arena: ShmArena, +} + +#[pymethods] +impl PyShmArena { + #[staticmethod] + fn create(name: &str, size: usize) -> PyResult { + Ok(Self { + arena: ShmArena::create(name, size) + .map_err(|e| PyRuntimeError::new_err(e.to_string()))?, + }) + } + + #[staticmethod] + fn open(name: &str) -> PyResult { + Ok(Self { + arena: ShmArena::open(name).map_err(|e| PyRuntimeError::new_err(e.to_string()))?, + }) + } + + fn reserve(&self, nbytes: usize) -> PyResult { + self.arena + .reserve(nbytes) + .map_err(|e| PyRuntimeError::new_err(e.to_string())) + } + + fn free(&self, offset: usize) -> bool { + self.arena.free(offset) + } + + #[getter] + fn size(&self) -> usize { + self.arena.size() + } + + #[getter] + fn bytes_free(&self) -> usize { + self.arena.bytes_free() + } + + /// `(base_ptr, len)` for the pinning hook — `cudaHostRegister` the + /// mapping once (e.g. `torch.cuda.cudart().cudaHostRegister(ptr, len, 0)`). + fn ptr_len(&self) -> (usize, usize) { + (self.arena.as_mut_ptr() as usize, self.arena.size()) + } + + fn close(&mut self) { + self.arena.close(); + } + + /// Whole arena as a writable memoryview -> zero-copy `torch.frombuffer`. + unsafe fn __getbuffer__( + slf: Bound<'_, Self>, + view: *mut ffi::Py_buffer, + flags: c_int, + ) -> PyResult<()> { + if view.is_null() { + return Err(PyValueError::new_err("null buffer view")); + } + let borrow = slf.borrow(); + let ptr = borrow.arena.as_mut_ptr() as *mut c_void; + let len = borrow.arena.size() as ffi::Py_ssize_t; + let ret = ffi::PyBuffer_FillInfo(view, slf.as_ptr(), ptr, len, 0, flags); + if ret != 0 { + Err(PyErr::fetch(slf.py())) + } else { + Ok(()) + } + } + + unsafe fn __releasebuffer__(&self, _view: *mut ffi::Py_buffer) {} +} + +/// One segment of a `SegmentedShmArena`, exposed with the buffer protocol so +/// staging stays zero-copy per segment. The mapping never moves, so a +/// memoryview (and a CUDA host-registration of the segment) stays valid for +/// the segment's lifetime. +#[pyclass(name = "ShmSegment")] +struct PyShmSegment { + seg: std::sync::Arc, +} + +#[pymethods] +impl PyShmSegment { + #[getter] + fn size(&self) -> usize { + self.seg.size() + } + + /// `(base_ptr, len)` for the pinning hook (see `ShmArena::ptr_len`). + fn ptr_len(&self) -> (usize, usize) { + (self.seg.as_mut_ptr() as usize, self.seg.size()) + } + + unsafe fn __getbuffer__( + slf: Bound<'_, Self>, + view: *mut ffi::Py_buffer, + flags: c_int, + ) -> PyResult<()> { + if view.is_null() { + return Err(PyValueError::new_err("null buffer view")); + } + let borrow = slf.borrow(); + let ptr = borrow.seg.as_mut_ptr() as *mut c_void; + let len = borrow.seg.size() as ffi::Py_ssize_t; + let ret = ffi::PyBuffer_FillInfo(view, slf.as_ptr(), ptr, len, 0, flags); + if ret != 0 { + Err(PyErr::fetch(slf.py())) + } else { + Ok(()) + } + } + + unsafe fn __releasebuffer__(&self, _view: *mut ffi::Py_buffer) {} +} + +/// Grow-by-segments producer arena with uuid-grouped reclaim. +/// `reserve(n) -> (segment_idx, offset)`; descriptors carry +/// `segment_name(idx)` so consumers keep opening plain `ShmArena`s by name. +/// Segments are created once and never move — registration-friendly. +#[pyclass(name = "SegmentedShmArena")] +struct PySegmentedShmArena { + arena: SegmentedShmArena, +} + +#[pymethods] +impl PySegmentedShmArena { + #[staticmethod] + fn create(base: &str, segment_size: usize, max_segments: usize) -> PyResult { + Ok(Self { + arena: SegmentedShmArena::create(base, segment_size, max_segments) + .map_err(|e| PyRuntimeError::new_err(e.to_string()))?, + }) + } + + /// -> (segment_idx, offset); grows by one segment when full (dedicated + /// segment for oversized allocations), errors at the max_segments cap. + /// GIL released: the growth path creates + maps a new shm segment + /// (file create, preallocate, mmap: on the order of 0.1 s per 256 MiB), + /// which must not stall other Python threads (the serve loop, stream + /// relays). + fn reserve(&self, py: Python<'_>, nbytes: usize) -> PyResult<(usize, usize)> { + py.detach(|| self.arena.reserve(nbytes)) + .map_err(|e| PyRuntimeError::new_err(e.to_string())) + } + + /// GIL released: growth (in `reserve`) holds the segments mutex + /// across an mmap for milliseconds; blocking on that mutex with the + /// GIL held would freeze every Python thread for the duration. + fn free(&self, py: Python<'_>, segment: usize, offset: usize) -> bool { + py.detach(|| self.arena.free(segment, offset)) + } + + #[getter] + fn num_segments(&self, py: Python<'_>) -> usize { + py.detach(|| self.arena.num_segments()) + } + + /// `(total_bytes, free_bytes, largest_free_block)` across all segments. + /// `largest_free_block` collapsing while `free_bytes` stays high is the + /// fragmentation signature (allocations fail / segments grow despite + /// healthy total free space). + fn stats(&self, py: Python<'_>) -> (usize, usize, usize) { + py.detach(|| self.arena.stats()) + } + + fn segment_name(&self, i: usize) -> String { + self.arena.segment_name(i) + } + + /// Shared buffer-protocol view of segment `i`. + fn segment(&self, i: usize) -> PyResult { + self.arena + .segment(i) + .map(|seg| PyShmSegment { seg }) + .ok_or_else(|| PyValueError::new_err(format!("no segment {i}"))) + } +} + +#[pymodule] +fn mstar_rust(m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add("__version__", env!("CARGO_PKG_VERSION"))?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + Ok(()) +} diff --git a/rust/src/graph/runtime.rs b/rust/src/graph/runtime.rs index 31d04d2311..dcc212e192 100644 --- a/rust/src/graph/runtime.rs +++ b/rust/src/graph/runtime.rs @@ -307,7 +307,7 @@ pub struct GraphRuntime { return Ok(()); } let comm = comm.clone(); - py.allow_threads(move || -> Result<(), crate::communicator::CommError> { + py.detach(move || -> Result<(), crate::communicator::CommError> { for (peer, bytes) in &rest { comm.send(peer, bytes)?; } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 310460cbd6..2f704573e0 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,339 +1,22 @@ //! mstar's Rust components, one crate: each module is an independent -//! capability behind its own opt-in flag, and this file is the PyO3 +//! capability behind its own opt-in flag, and `bindings.rs` is the PyO3 //! surface the Python wrappers drive. Currently: `communicator.rs` (the //! ZMQ PUSH/PULL control mesh, as a transport + codec split) and `shm.rs` //! (the shared-memory tensor arena). The crate is not limited to these — //! new components land as new modules with their own bindings. Also -//! usable as a plain Rust library (rlib) by Rust-side consumers. +//! usable as a plain Rust library (rlib) by Rust-side consumers; without the +//! `python` feature (rust/server) it builds only the transport and arena and +//! links no Python, since the bindings, `graph` and `tensors` need pyo3. //! Build: `maturin develop --release` in rust/. +#[cfg(feature = "python")] +mod bindings; pub mod communicator; +#[cfg(feature = "python")] pub mod graph; pub mod shm; +#[cfg(feature = "python")] pub mod tensors; -use std::os::raw::{c_int, c_void}; -use std::time::Duration; - -use pyo3::exceptions::{PyRuntimeError, PyValueError}; -use pyo3::ffi; -use pyo3::prelude::*; -use pyo3::types::PyBytes; - -use communicator::{RawZmqCommunicator, RecvEvent}; -use shm::{SegmentedShmArena, ShmArena}; - -/// Opaque byte frames over ZMQ PUSH/PULL: ipc or tcp endpoints, lazily-cached -/// peers, and wakeup-fd polling (an eventfd wakes `recv_or_wake` instantly). -/// Encoding is the caller's (pickle today; msgpack by swapping the codec). -#[pyclass(name = "ZmqCommunicator")] -pub struct PyZmqCommunicator { - /// Behind an Arc so the graph runtime can hold a share of the SAME - /// transport and send from Rust, rather than hopping back into Python for - /// every frame. No extra lock: RawZmqCommunicator::send takes &self and - /// guards its own sockets. - inner: std::sync::Arc, -} - -impl PyZmqCommunicator { - pub fn share(&self) -> std::sync::Arc { - self.inner.clone() - } -} - -#[pymethods] -impl PyZmqCommunicator { - /// Bind the PULL inbox at `ipc://{dir}/{my_id}.ipc`. - #[new] - fn new(my_id: &str, dir: &str) -> PyResult { - Ok(Self { - inner: std::sync::Arc::new( - RawZmqCommunicator::bind(my_id, dir) - .map_err(|e| PyRuntimeError::new_err(e.to_string()))?, - ), - }) - } - - /// Bind at an explicit zmq endpoint (e.g. `tcp://0.0.0.0:5701`). - #[staticmethod] - fn bind_endpoint(my_id: &str, endpoint: &str) -> PyResult { - Ok(Self { - inner: std::sync::Arc::new( - RawZmqCommunicator::bind_endpoint(my_id, endpoint) - .map_err(|e| PyRuntimeError::new_err(e.to_string()))?, - ), - }) - } - - fn last_endpoint(&self) -> PyResult { - self.inner - .last_endpoint() - .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } - - fn register_peer(&self, peer_id: &str, endpoint: &str) { - self.inner.register_peer(peer_id, endpoint); - } - - /// Poll `fd` (an eventfd) alongside the inbox; when readable, - /// `recv_or_wake` returns ("wake", None) immediately. The registrant - /// reads/clears the fd (level-triggered). - fn register_wakeup_fd(&self, fd: i32) { - self.inner.register_wakeup_fd(fd); - } - - /// Released-GIL send: a PUSH send blocks when the peer is at its - /// high-water mark, and blocking with the GIL held would freeze every - /// Python thread in the process (pyzmq releases it here too). - fn send(&self, py: Python<'_>, peer_id: &str, data: &[u8]) -> PyResult<()> { - py.allow_threads(|| self.inner.send(peer_id, data)) - .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } - - /// `send` that never blocks: false when the peer is at its high-water - /// mark, with nothing sent. - fn try_send(&self, peer_id: &str, data: &[u8]) -> PyResult { - self.inner.try_send(peer_id, data) - .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } - - fn try_recv<'py>(&self, py: Python<'py>) -> Option> { - self.inner.try_recv().map(|b| PyBytes::new(py, &b)) - } - - /// Drain all currently-queued frames in one call — one GIL round-trip - /// per batch instead of one `try_recv` per message. - fn drain<'py>(&self, py: Python<'py>) -> Vec> { - let frames = py.allow_threads(|| self.inner.drain()); - frames.iter().map(|b| PyBytes::new(py, b)).collect() - } - - fn recv_timeout<'py>(&self, py: Python<'py>, timeout_ms: u64) -> Option> { - py.allow_threads(|| self.inner.recv_timeout(Duration::from_millis(timeout_ms))) - .map(|b| PyBytes::new(py, &b)) - } - - /// ("msg", bytes) | ("wake", None) | ("timeout", None). Raises if a - /// registered wakeup fd is closed/invalid (the registrant's bug — loud, - /// instead of degrading blocking waits into an instant-timeout spin). - fn recv_or_wake<'py>( - &self, - py: Python<'py>, - timeout_ms: u64, - ) -> PyResult<(&'static str, Option>)> { - let ev = py.allow_threads(|| self.inner.recv_or_wake(Duration::from_millis(timeout_ms))); - Ok(match ev { - RecvEvent::Message(b) => ("msg", Some(PyBytes::new(py, &b))), - RecvEvent::Wake => ("wake", None), - RecvEvent::WakeFdError => { - return Err(PyRuntimeError::new_err( - "a registered wakeup fd is closed/invalid (POLLERR/\ - POLLNVAL): fix the EventWakeup lifetime")); - } - RecvEvent::Timeout => ("timeout", None), - }) - } -} - -/// Shared-memory tensor arena for cross-process transport. Producer: -/// `create(name, size)` -> `reserve(nbytes)` -> `torch.frombuffer( -/// memoryview(arena)[off:off+n], dtype=..).copy_(cpu_tensor)`; send the -/// offset descriptor; `free(off)` on reclaim. Consumer: `open(name)` and -/// `torch.frombuffer(memoryview(arena)[off:off+n], ..)` (then H2D). -#[pyclass(name = "ShmArena")] -struct PyShmArena { - arena: ShmArena, -} - -#[pymethods] -impl PyShmArena { - #[staticmethod] - fn create(name: &str, size: usize) -> PyResult { - Ok(Self { - arena: ShmArena::create(name, size) - .map_err(|e| PyRuntimeError::new_err(e.to_string()))?, - }) - } - - #[staticmethod] - fn open(name: &str) -> PyResult { - Ok(Self { - arena: ShmArena::open(name).map_err(|e| PyRuntimeError::new_err(e.to_string()))?, - }) - } - - fn reserve(&self, nbytes: usize) -> PyResult { - self.arena - .reserve(nbytes) - .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } - - fn free(&self, offset: usize) -> bool { - self.arena.free(offset) - } - - #[getter] - fn size(&self) -> usize { - self.arena.size() - } - - #[getter] - fn bytes_free(&self) -> usize { - self.arena.bytes_free() - } - - /// `(base_ptr, len)` for the pinning hook — `cudaHostRegister` the - /// mapping once (e.g. `torch.cuda.cudart().cudaHostRegister(ptr, len, 0)`). - fn ptr_len(&self) -> (usize, usize) { - (self.arena.as_mut_ptr() as usize, self.arena.size()) - } - - fn close(&mut self) { - self.arena.close(); - } - - /// Whole arena as a writable memoryview -> zero-copy `torch.frombuffer`. - unsafe fn __getbuffer__( - slf: Bound<'_, Self>, - view: *mut ffi::Py_buffer, - flags: c_int, - ) -> PyResult<()> { - if view.is_null() { - return Err(PyValueError::new_err("null buffer view")); - } - let borrow = slf.borrow(); - let ptr = borrow.arena.as_mut_ptr() as *mut c_void; - let len = borrow.arena.size() as ffi::Py_ssize_t; - let ret = ffi::PyBuffer_FillInfo(view, slf.as_ptr(), ptr, len, 0, flags); - if ret != 0 { - Err(PyErr::fetch(slf.py())) - } else { - Ok(()) - } - } - - unsafe fn __releasebuffer__(&self, _view: *mut ffi::Py_buffer) {} -} - -/// One segment of a `SegmentedShmArena`, exposed with the buffer protocol so -/// staging stays zero-copy per segment. The mapping never moves, so a -/// memoryview (and a CUDA host-registration of the segment) stays valid for -/// the segment's lifetime. -#[pyclass(name = "ShmSegment")] -struct PyShmSegment { - seg: std::sync::Arc, -} - -#[pymethods] -impl PyShmSegment { - #[getter] - fn size(&self) -> usize { - self.seg.size() - } - - /// `(base_ptr, len)` for the pinning hook (see `ShmArena::ptr_len`). - fn ptr_len(&self) -> (usize, usize) { - (self.seg.as_mut_ptr() as usize, self.seg.size()) - } - - unsafe fn __getbuffer__( - slf: Bound<'_, Self>, - view: *mut ffi::Py_buffer, - flags: c_int, - ) -> PyResult<()> { - if view.is_null() { - return Err(PyValueError::new_err("null buffer view")); - } - let borrow = slf.borrow(); - let ptr = borrow.seg.as_mut_ptr() as *mut c_void; - let len = borrow.seg.size() as ffi::Py_ssize_t; - let ret = ffi::PyBuffer_FillInfo(view, slf.as_ptr(), ptr, len, 0, flags); - if ret != 0 { - Err(PyErr::fetch(slf.py())) - } else { - Ok(()) - } - } - - unsafe fn __releasebuffer__(&self, _view: *mut ffi::Py_buffer) {} -} - -/// Grow-by-segments producer arena with uuid-grouped reclaim. -/// `reserve(n) -> (segment_idx, offset)`; descriptors carry -/// `segment_name(idx)` so consumers keep opening plain `ShmArena`s by name. -/// Segments are created once and never move — registration-friendly. -#[pyclass(name = "SegmentedShmArena")] -struct PySegmentedShmArena { - arena: SegmentedShmArena, -} - -#[pymethods] -impl PySegmentedShmArena { - #[staticmethod] - fn create(base: &str, segment_size: usize, max_segments: usize) -> PyResult { - Ok(Self { - arena: SegmentedShmArena::create(base, segment_size, max_segments) - .map_err(|e| PyRuntimeError::new_err(e.to_string()))?, - }) - } - - /// -> (segment_idx, offset); grows by one segment when full (dedicated - /// segment for oversized allocations), errors at the max_segments cap. - /// GIL released: the growth path creates + maps a new shm segment - /// (file create, preallocate, mmap: on the order of 0.1 s per 256 MiB), - /// which must not stall other Python threads (the serve loop, stream - /// relays). - fn reserve(&self, py: Python<'_>, nbytes: usize) -> PyResult<(usize, usize)> { - py.allow_threads(|| self.arena.reserve(nbytes)) - .map_err(|e| PyRuntimeError::new_err(e.to_string())) - } - - /// GIL released: growth (in `reserve`) holds the segments mutex - /// across an mmap for milliseconds; blocking on that mutex with the - /// GIL held would freeze every Python thread for the duration. - fn free(&self, py: Python<'_>, segment: usize, offset: usize) -> bool { - py.allow_threads(|| self.arena.free(segment, offset)) - } - - #[getter] - fn num_segments(&self, py: Python<'_>) -> usize { - py.allow_threads(|| self.arena.num_segments()) - } - - /// `(total_bytes, free_bytes, largest_free_block)` across all segments. - /// `largest_free_block` collapsing while `free_bytes` stays high is the - /// fragmentation signature (allocations fail / segments grow despite - /// healthy total free space). - fn stats(&self, py: Python<'_>) -> (usize, usize, usize) { - py.allow_threads(|| self.arena.stats()) - } - - fn segment_name(&self, i: usize) -> String { - self.arena.segment_name(i) - } - - /// Shared buffer-protocol view of segment `i`. - fn segment(&self, i: usize) -> PyResult { - self.arena - .segment(i) - .map(|seg| PyShmSegment { seg }) - .ok_or_else(|| PyValueError::new_err(format!("no segment {i}"))) - } -} - -#[pymodule] -fn mstar_rust(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add("__version__", env!("CARGO_PKG_VERSION"))?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - Ok(()) -} +#[cfg(feature = "python")] +pub use bindings::PyZmqCommunicator; diff --git a/rust/src/tensors.rs b/rust/src/tensors.rs index 4e370fa920..8c8c33808c 100644 --- a/rust/src/tensors.rs +++ b/rust/src/tensors.rs @@ -1,7 +1,7 @@ //! Port of `TensorBookkeeping`: per-uuid reference state and descriptors. //! //! Never sees a tensor. `TensorStore` stays Python because freeing a record -//! inside a `py.allow_threads` section would drop a `Py` without the +//! inside a `py.detach` section would drop a `Py` without the //! GIL; everything here is integers and interned strings, so it can be held //! across one. //! diff --git a/scripts/ci/Dockerfile b/scripts/ci/Dockerfile index 960e704e15..18c4ca3f62 100644 --- a/scripts/ci/Dockerfile +++ b/scripts/ci/Dockerfile @@ -13,6 +13,10 @@ RUN apt-get update \ && rm -rf /var/lib/apt/lists/* ENV CARGO_HOME=/opt/cargo RUSTUP_HOME=/opt/rustup ENV PATH="/opt/cargo/bin:${PATH}" +# The repo's pinned toolchain, installed into the image so a container does not +# download it on first use (mac.sh passes the repo as the `repo` context). +COPY --from=repo rust-toolchain.toml /opt/rust-toolchain/ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o /tmp/rustup.sh \ - && sh /tmp/rustup.sh -y --profile minimal --default-toolchain stable \ - && rm /tmp/rustup.sh + && sh /tmp/rustup.sh -y --profile minimal --default-toolchain none \ + && rm /tmp/rustup.sh \ + && cd /opt/rust-toolchain && rustup toolchain install diff --git a/scripts/ci/check.sh b/scripts/ci/check.sh index 838a43ebd5..202c7633b4 100755 --- a/scripts/ci/check.sh +++ b/scripts/ci/check.sh @@ -52,7 +52,7 @@ runpy.run_module("pytest", run_name="__main__") # location from the active interpreter, including outside setup-python. python_lib=$(python -c 'import sysconfig; print(sysconfig.get_config_var("LIBDIR"))') export LD_LIBRARY_PATH="$python_lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" - (cd rust && cargo test --release --no-default-features) + (cd rust && cargo test --release --no-default-features --features python) (cd rust/server && cargo build --release) # A fresh wheel directory prevents installing an earlier cached build. diff --git a/scripts/ci/mac.sh b/scripts/ci/mac.sh index 7a39df07db..80ea9143ea 100755 --- a/scripts/ci/mac.sh +++ b/scripts/ci/mac.sh @@ -97,7 +97,7 @@ for job in "${jobs[@]}"; do echo "=== $job ($platform) ===" started=$SECONDS docker build --platform "$platform" --target "$target" \ - --tag "$image" --file scripts/ci/Dockerfile scripts/ci \ + --tag "$image" --file scripts/ci/Dockerfile --build-context repo=. scripts/ci \ > "$job_reports/image-build.log" 2>&1 & active_pid=$! if ! wait "$active_pid"; then diff --git a/setup.py b/setup.py index 2293082ecc..8422e638e2 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,15 @@ """Build shim: copy the repo-root configs/ into mstar/default_configs/ so the -default deployment YAMLs ship inside the wheel under the mstar namespace. - -Project metadata lives in pyproject.toml; this file exists only to populate -the packaged configs at build time. configs/ stays the single source of truth -(unchanged for checkouts and in-flight model PRs); nothing is duplicated in -git. The sdist carries configs/ via MANIFEST.in so a wheel built from the -sdist also picks them up. +default deployment YAMLs ship inside the wheel under the mstar namespace, and +compile the ``mstar_rust`` extension and the ``mstar-server`` binary into the +published Linux wheels. + +Project metadata lives in pyproject.toml; this file exists only for those two +build steps. configs/ stays the single source of truth (unchanged for checkouts +and in-flight model PRs); nothing is duplicated in git. The sdist carries +configs/ via MANIFEST.in so a wheel built from the sdist also picks them up. """ +import os import shutil from pathlib import Path @@ -34,4 +36,24 @@ def run(self): shutil.copy2(y, dst / y.name) -setup(cmdclass={"build_py": build_py_with_configs}) +# Only a CIBUILDWHEEL=1 build compiles rust/ (the release workflow's +# cibuildwheel builds, or a build from the sdist): the mstar_rust extension and +# the mstar-server binary (--rust-frontend), which installs into the +# environment's scripts directory. Every other build is the pure-Python +# package, and a checkout builds both with cargo/maturin (docs/installation.rst). +# setuptools' editable finder cannot import a top-level extension built in +# place, and compiling it would slow every `pip install -e .`. +rust_extensions = [] +if os.environ.get("CIBUILDWHEEL") == "1": + from setuptools_rust import Binding, RustBin, RustExtension, Strip + + # --locked: the checked-in Cargo.lock versions, not whatever resolves on + # the day; the license notices are collected from the same lockfiles. + # Stripped: the symbols are unused at runtime and only bloat six wheels. + wheel_build = {"cargo_manifest_args": ["--locked"], "strip": Strip.All} + rust_extensions += [ + RustExtension("mstar_rust", path="rust/Cargo.toml", binding=Binding.PyO3, **wheel_build), + RustBin("mstar-server", path="rust/server/Cargo.toml", **wheel_build), + ] + +setup(cmdclass={"build_py": build_py_with_configs}, rust_extensions=rust_extensions) diff --git a/test/modular/test_outbox_communicator.py b/test/modular/test_outbox_communicator.py index af3f87938e..0d8e405f0b 100644 --- a/test/modular/test_outbox_communicator.py +++ b/test/modular/test_outbox_communicator.py @@ -232,7 +232,8 @@ def send(self, peer_id, data): if has_try_send: _Zmq.try_send = lambda self, peer_id, data: True version = communicator.EXPECTED_MSTAR_RUST_VERSION if has_try_send else "0.1.0" - monkeypatch.setitem(sys.modules, "mstar_rust", SimpleNamespace(ZmqCommunicator=_Zmq, __version__=version)) + monkeypatch.setitem(sys.modules, "mstar_rust", SimpleNamespace( + ZmqCommunicator=_Zmq, __version__=version, __file__="mstar_rust.so")) monkeypatch.setattr(rust_communicator, "RustZMQCommunicator", lambda *args, **kwargs: "rust") monkeypatch.setattr(communicator, "ZMQCommunicator", lambda *args, **kwargs: "pyzmq") monkeypatch.setenv("MSTAR_RUST_ZMQ", choice) diff --git a/test/rust/test_rust_frontend.py b/test/rust/test_rust_frontend.py index 347b67fdb6..15f4bea354 100644 --- a/test/rust/test_rust_frontend.py +++ b/test/rust/test_rust_frontend.py @@ -7,6 +7,7 @@ import contextlib import json import socket +import sysconfig import tempfile import threading import time @@ -1058,3 +1059,16 @@ def test_images_n_cap_follows_the_env(): out = _post_json(port, "/v1/images/generations", {"model": "cosmos3", "prompt": "x", "n": 2}) assert len(out["data"]) == 2 + + +def test_the_running_environment_s_binary_wins_over_one_on_path(tmp_path, monkeypatch): + # An unactivated venv's python must not launch an older mstar-server that + # happens to be on $PATH against its own bridge code. + scripts, elsewhere = tmp_path / "venv-bin", tmp_path / "on-path" + for d in (scripts, elsewhere): + d.mkdir() + (d / "mstar-server").touch(mode=0o755) + monkeypatch.delenv("MSTAR_SERVER_BIN", raising=False) + monkeypatch.setenv("PATH", str(elsewhere)) + monkeypatch.setattr(sysconfig, "get_path", lambda name: str(scripts)) + assert find_server_binary() == str(scripts / "mstar-server")