packaging: llama rebuild stage checks with ldd (libcuda is a runtime library),...

packaging: llama rebuild stage checks with ldd (libcuda is a runtime library), rpc-server script needs no import
Co-Authored-By: 's avatarClaude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FKm7Z56iKjHgNkHc6VhWKz
parent 22dc3785
...@@ -29,8 +29,10 @@ while [ $# -gt 0 ]; do ...@@ -29,8 +29,10 @@ while [ $# -gt 0 ]; do
shift shift
done done
VER=$("$PYTHON" -c "import llama_cpp; print(llama_cpp.__version__)") # Version and lib dir WITHOUT importing the package: a wheel installed
LIBDIR=$("$PYTHON" -c "import llama_cpp, os; print(os.path.join(os.path.dirname(llama_cpp.__file__), 'lib'))") # --no-deps (as the image builds do) cannot be imported before its deps are.
VER=$("$PYTHON" -c "from importlib.metadata import version; print(version('llama-cpp-python'))")
LIBDIR=$("$PYTHON" -c "import importlib.util, os; s=importlib.util.find_spec('llama_cpp'); print(os.path.join(os.path.dirname(s.origin), 'lib'))")
BINDIR=$("$PYTHON" -c "import sys, os; print(os.path.dirname(sys.executable))") BINDIR=$("$PYTHON" -c "import sys, os; print(os.path.dirname(sys.executable))")
DEST="${DEST:-$BINDIR}" DEST="${DEST:-$BINDIR}"
if [ -z "$CUDA$VULKAN" ]; then if [ -z "$CUDA$VULKAN" ]; then
......
...@@ -30,7 +30,7 @@ ENV DEBIAN_FRONTEND=noninteractive FORCE_CMAKE=1 CMAKE_BUILD_PARALLEL_LEVEL=8 MA ...@@ -30,7 +30,7 @@ ENV DEBIAN_FRONTEND=noninteractive FORCE_CMAKE=1 CMAKE_BUILD_PARALLEL_LEVEL=8 MA
CC=gcc CXX=g++ CUDAHOSTCXX=g++ CC=gcc CXX=g++ CUDAHOSTCXX=g++
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential ca-certificates cmake curl git libgomp1 libvulkan-dev \ build-essential ca-certificates cmake curl git libgomp1 libvulkan-dev \
ninja-build pkg-config vulkan-tools glslang-tools glslc \ ninja-build pkg-config vulkan-tools glslang-tools glslc spirv-headers spirv-tools \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
RUN set -eux; \ RUN set -eux; \
curl -fsSL -o /tmp/python.tar.gz \ curl -fsSL -o /tmp/python.tar.gz \
...@@ -58,21 +58,26 @@ RUN set -eux; \ ...@@ -58,21 +58,26 @@ RUN set -eux; \
--force-reinstall /tmp/llama-wheels/llama_cpp_python-*.whl; \ --force-reinstall /tmp/llama-wheels/llama_cpp_python-*.whl; \
install -m 755 /tmp/llama-wheels/rpc-server /opt/coderai/python/bin/rpc-server; \ install -m 755 /tmp/llama-wheels/rpc-server /opt/coderai/python/bin/rpc-server; \
rm -rf /tmp/llama-wheels; \ rm -rf /tmp/llama-wheels; \
# The wheel must load in THIS image (its CUDA runtime is on local-libs) and # The wheel must link against THIS image (its CUDA runtime is on local-libs)
# carry the RPC backend — a wheel that links a library the image lacks is # and carry the RPC backend. libcuda.so is the driver — present only at run
# exactly the failure this stage exists to catch before the flatten. # time, mounted by the NVIDIA container runtime — so it is the one library
# that may legitimately be missing here, and the check is ldd, not import.
LD_LIBRARY_PATH=/opt/coderai/local-libs:/usr/local/nvidia/lib64 \ LD_LIBRARY_PATH=/opt/coderai/local-libs:/usr/local/nvidia/lib64 \
/opt/coderai/python/bin/python3 - <<'PY' /opt/coderai/python/bin/python3 - <<'PY'
import ctypes, glob, os, llama_cpp import glob, os, re, subprocess, importlib.util
d = os.path.join(os.path.dirname(llama_cpp.__file__), "lib") d = os.path.join(os.path.dirname(importlib.util.find_spec("llama_cpp").origin), "lib")
libs = [ctypes.CDLL(p, mode=ctypes.RTLD_GLOBAL) for p in bad = []
(glob.glob(d + "/libggml.so")[:1] + glob.glob(d + "/libggml-base.so")[:1])] for lib in sorted(glob.glob(d + "/lib*.so")):
fn = next(getattr(l, "ggml_backend_reg_by_name") for l in libs if hasattr(l, "ggml_backend_reg_by_name")) out = subprocess.run(["ldd", lib], capture_output=True, text=True).stdout
fn.restype = ctypes.c_void_p; fn.argtypes = [ctypes.c_char_p] for line in out.splitlines():
try: if "not found" in line and not re.search(r"libcuda\.so|libnvidia-ml\.so", line):
getattr(libs[0], "ggml_backend_load_all")() bad.append(f"{os.path.basename(lib)}: {line.strip()}")
except Exception: assert not bad, "unresolved libraries: " + "; ".join(bad)
pass names = " ".join(os.path.basename(x) for x in glob.glob(d + "/lib*"))
assert fn(b"RPC"), "RPC backend missing from the rebuilt llama-cpp-python" syms = subprocess.run(["nm", "-D", "--defined-only"] + glob.glob(d + "/libggml*.so"),
print("llama-cpp-python", llama_cpp.__version__, "with RPC backend: OK") capture_output=True, text=True).stdout
assert "libggml-rpc" in names or "ggml_backend_rpc" in syms, \
"RPC backend missing from the rebuilt llama-cpp-python"
print("llama-cpp-python links cleanly and carries the RPC backend: OK")
PY PY
RUN /opt/coderai/python/bin/rpc-server --help | head -2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment