[Rust Frontend] Add Python bridge for Rust tool parsers (#44624)
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
@@ -35,6 +35,9 @@ ROOT_DIR = Path(__file__).parent
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PRECOMPILED_RUST_FRONTEND_PATH = ROOT_DIR / "vllm" / "vllm-rs"
|
||||
# setuptools-rust installs PyO3 artifacts as `<module>.<ext-suffix>`, where the
|
||||
# suffix ends with `.so` on Linux and macOS alike (e.g. `_rust_foo.abi3.so`).
|
||||
PRECOMPILED_RUST_EXTENSION_MEMBER_REGEX = re.compile(r"vllm/_rust_[^/]*\.so$")
|
||||
|
||||
# cannot import envs directly because it depends on vllm,
|
||||
# which is not installed yet
|
||||
@@ -56,6 +59,25 @@ def should_require_rust_frontend() -> bool:
|
||||
return value.lower() not in ("", "0", "false", "no")
|
||||
|
||||
|
||||
def get_precompiled_rust_extension_paths() -> list[Path]:
|
||||
return sorted((ROOT_DIR / "vllm").glob("_rust_*.so"))
|
||||
|
||||
|
||||
def get_missing_precompiled_rust_extension_modules() -> list[str]:
|
||||
present = {
|
||||
path.name.split(".", 1)[0] for path in get_precompiled_rust_extension_paths()
|
||||
}
|
||||
return [
|
||||
module_name
|
||||
for module_name in rust_build.rust_py_extension_module_names()
|
||||
if module_name not in present
|
||||
]
|
||||
|
||||
|
||||
def has_precompiled_rust_extensions() -> bool:
|
||||
return not get_missing_precompiled_rust_extension_modules()
|
||||
|
||||
|
||||
if sys.platform.startswith("darwin") and VLLM_TARGET_DEVICE != "cpu":
|
||||
logger.warning("VLLM_TARGET_DEVICE automatically set to `cpu` due to macOS")
|
||||
VLLM_TARGET_DEVICE = "cpu"
|
||||
@@ -423,19 +445,31 @@ class precompiled_build_ext(build_ext):
|
||||
|
||||
|
||||
class precompiled_build_rust(build_rust):
|
||||
"""Skips local Rust builds when the precompiled wheel already ships vllm-rs."""
|
||||
"""Skips local Rust builds when all precompiled Rust artifacts are present."""
|
||||
|
||||
def run(self) -> None:
|
||||
if PRECOMPILED_RUST_FRONTEND_PATH.exists():
|
||||
missing = []
|
||||
if not PRECOMPILED_RUST_FRONTEND_PATH.exists():
|
||||
missing.append(str(PRECOMPILED_RUST_FRONTEND_PATH))
|
||||
missing_rust_extensions = get_missing_precompiled_rust_extension_modules()
|
||||
if missing_rust_extensions:
|
||||
missing.extend(
|
||||
str(ROOT_DIR / "vllm" / f"{module_name}*.so")
|
||||
for module_name in missing_rust_extensions
|
||||
)
|
||||
|
||||
if not missing:
|
||||
logger.info(
|
||||
"Skipping local Rust build: using precompiled %s",
|
||||
"Skipping local Rust build: using precompiled %s and %s",
|
||||
PRECOMPILED_RUST_FRONTEND_PATH,
|
||||
get_precompiled_rust_extension_paths(),
|
||||
)
|
||||
return
|
||||
|
||||
logger.warning(
|
||||
"Precompiled wheel did not provide %s; falling back to local Rust build.",
|
||||
PRECOMPILED_RUST_FRONTEND_PATH,
|
||||
"Precompiled wheel did not provide all Rust artifacts (%s); "
|
||||
"falling back to local Rust build.",
|
||||
", ".join(missing),
|
||||
)
|
||||
super().run()
|
||||
|
||||
@@ -758,6 +792,14 @@ class precompiled_wheel_utils:
|
||||
if member.filename in exact_members:
|
||||
file_members.append(member)
|
||||
continue
|
||||
if (
|
||||
extract_rust_frontend
|
||||
and PRECOMPILED_RUST_EXTENSION_MEMBER_REGEX.match(
|
||||
member.filename
|
||||
)
|
||||
):
|
||||
file_members.append(member)
|
||||
continue
|
||||
|
||||
if not extract_extensions:
|
||||
continue
|
||||
@@ -1111,6 +1153,12 @@ package_data = {
|
||||
}
|
||||
|
||||
|
||||
def add_vllm_package_data(filename: str) -> None:
|
||||
vllm_files = package_data.setdefault("vllm", [])
|
||||
if filename not in vllm_files:
|
||||
vllm_files.append(filename)
|
||||
|
||||
|
||||
# If using precompiled artifacts, extract and patch package_data in advance.
|
||||
if USE_PRECOMPILED_RUST_FRONTEND:
|
||||
wheel_url, download_filename = precompiled_wheel_utils.determine_wheel_url()
|
||||
@@ -1126,9 +1174,9 @@ if USE_PRECOMPILED_RUST_FRONTEND:
|
||||
# If the rust frontend binary is already present in the source tree (e.g.,
|
||||
# pre-built in a separate Docker build stage), ship it as-is.
|
||||
if PRECOMPILED_RUST_FRONTEND_PATH.exists():
|
||||
vllm_files = package_data.setdefault("vllm", [])
|
||||
if "vllm-rs" not in vllm_files:
|
||||
vllm_files.append("vllm-rs")
|
||||
add_vllm_package_data("vllm-rs")
|
||||
for rust_extension_path in get_precompiled_rust_extension_paths():
|
||||
add_vllm_package_data(rust_extension_path.name)
|
||||
|
||||
if _no_device():
|
||||
ext_modules = []
|
||||
@@ -1141,13 +1189,15 @@ else:
|
||||
if USE_PRECOMPILED_EXTENSIONS
|
||||
else cmake_build_ext,
|
||||
}
|
||||
if USE_PRECOMPILED_RUST_FRONTEND or PRECOMPILED_RUST_FRONTEND_PATH.exists():
|
||||
if (
|
||||
USE_PRECOMPILED_RUST_FRONTEND
|
||||
or PRECOMPILED_RUST_FRONTEND_PATH.exists()
|
||||
or has_precompiled_rust_extensions()
|
||||
):
|
||||
cmdclass["build_rust"] = precompiled_build_rust
|
||||
|
||||
# Rust frontend binary, built via setuptools-rust and installed into the
|
||||
# package directory alongside the Python modules.
|
||||
# TODO: we may use `RustBin` to directly install it into `bin` directory, but this
|
||||
# requires extra work on using precompiled binaries.
|
||||
# Rust artifacts, built via setuptools-rust and installed into the package
|
||||
# directory alongside the Python modules.
|
||||
rust_extensions = rust_build.rust_extensions(
|
||||
optional=not should_require_rust_frontend()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user