From c86aba36e9bb752774a0f28ff2861676d487c360 Mon Sep 17 00:00:00 2001 From: Creylay Date: Fri, 27 Nov 2026 11:06:45 -0300 Subject: [PATCH 1/6] fix(docker): ship the uv-managed interpreter in the CUDA runtime image The runtime stage copied only /opt/venv, whose python symlinks point at the interpreter uv downloaded under /root/.local/share/uv/python. They were left dangling, so python3 fell back to the system 3.10 without the venv packages. Pin Python 3.12 (the CI default), install it under /opt/python and copy it next to the venv. The apt python3 package is no longer needed. Closes #1028 --- Dockerfile.cuda | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Dockerfile.cuda b/Dockerfile.cuda index 067728548a..f17b095c90 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -29,7 +29,12 @@ COPY --from=ghcr.io/astral-sh/uv:0.11 /uv /bin/uv # cuda extra = cu128 torch wheels + llama-cpp-python compiled with CUDA offload COPY pyproject.toml uv.lock ./ -ENV UV_PROJECT_ENVIRONMENT=/opt/venv +# Pin a uv-managed interpreter and install it under /opt/python so the +# runtime stage can copy it next to the venv (the venv symlinks point at it). +ENV UV_PROJECT_ENVIRONMENT=/opt/venv \ + UV_PYTHON=3.12 \ + UV_PYTHON_PREFERENCE=only-managed \ + UV_PYTHON_INSTALL_DIR=/opt/python ENV CMAKE_ARGS="-DGGML_CUDA=on" RUN uv sync --locked --extra cuda --no-dev --no-install-project --no-cache @@ -37,11 +42,8 @@ RUN uv sync --locked --extra cuda --no-dev --no-install-project --no-cache FROM nvidia/cuda:12.8.0-runtime-ubuntu22.04 WORKDIR /app -RUN apt-get update && apt-get install -y \ - python3 \ - && rm -rf /var/lib/apt/lists/* - -# copy the resolved virtualenv only (not build tools) +# copy the managed interpreter and the resolved virtualenv (not build tools) +COPY --from=builder /opt/python /opt/python COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" From 91dfdba3b188d499f6b60275867372a530322562 Mon Sep 17 00:00:00 2001 From: Creylay Date: Fri, 27 Nov 2026 11:06:46 -0300 Subject: [PATCH 2/6] fix(docker): install libgomp1 in the CUDA runtime image Every llama-cpp-python shared library links against the OpenMP runtime. The builder gets it through build-essential, the runtime base image does not, so import llama_cpp failed with 'libgomp.so.1: cannot open shared object file'. Closes #1042 --- Dockerfile.cuda | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Dockerfile.cuda b/Dockerfile.cuda index f17b095c90..a9a962c589 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -42,6 +42,11 @@ RUN uv sync --locked --extra cuda --no-dev --no-install-project --no-cache FROM nvidia/cuda:12.8.0-runtime-ubuntu22.04 WORKDIR /app +# libgomp1: OpenMP runtime required by every llama-cpp-python shared library +RUN apt-get update && apt-get install -y \ + libgomp1 \ + && rm -rf /var/lib/apt/lists/* + # copy the managed interpreter and the resolved virtualenv (not build tools) COPY --from=builder /opt/python /opt/python COPY --from=builder /opt/venv /opt/venv From 5aa07bca00eafe5aaae9100bd005ee2018cc9cb0 Mon Sep 17 00:00:00 2001 From: Creylay Date: Fri, 27 Nov 2026 11:06:46 -0300 Subject: [PATCH 3/6] fix(docker): build llama.cpp CUDA kernels for several GPU generations Without CMAKE_CUDA_ARCHITECTURES llama.cpp compiles only for the build host's GPU, so the image did not run on any other card. Target T4, A100, A10/RTX 30xx and L4/RTX 40xx, plus PTX for sm_89 so newer cards can JIT. Closes #1044 --- Dockerfile.cuda | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Dockerfile.cuda b/Dockerfile.cuda index a9a962c589..3d6b8ca0fc 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -35,7 +35,10 @@ ENV UV_PROJECT_ENVIRONMENT=/opt/venv \ UV_PYTHON=3.12 \ UV_PYTHON_PREFERENCE=only-managed \ UV_PYTHON_INSTALL_DIR=/opt/python -ENV CMAKE_ARGS="-DGGML_CUDA=on" +# Build llama.cpp kernels for several GPU generations instead of only the +# build host's GPU: T4 (75), A100 (80), A10/RTX 30xx (86), L4/RTX 40xx (89), +# plus PTX for 89 so newer cards can JIT compile. +ENV CMAKE_ARGS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=75-real;80-real;86-real;89-real;89-virtual" RUN uv sync --locked --extra cuda --no-dev --no-install-project --no-cache # -------- Stage 3: runtime (CUDA runtime, no build tools) -------- From 692742bc66f0aebe8fae9af548ab11570a719ed2 Mon Sep 17 00:00:00 2001 From: Creylay Date: Fri, 27 Nov 2026 11:06:47 -0300 Subject: [PATCH 4/6] fix(llama): import torch before llama_cpp so CUDA builds load NCCL When llama_cpp is imported first, torch skips its NCCL preload and import torch fails with 'undefined symbol: ncclCommShrink'. llama_utils imported llama_cpp at module level, so the GGUF models always hit the bad order. Import llama_cpp lazily in llama_utils, add a test that checks the order in a fresh interpreter, and preload libnccl in the CUDA image so it stays correct regardless of future import changes. Closes #1043 --- .../back/models/hugging_face/llama_utils.py | 28 ++++++++++----- Dockerfile.cuda | 7 ++++ tests/back/models/test_llama_import_order.py | 35 +++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 tests/back/models/test_llama_import_order.py diff --git a/DashAI/back/models/hugging_face/llama_utils.py b/DashAI/back/models/hugging_face/llama_utils.py index fe7e73a353..98aa0b6383 100644 --- a/DashAI/back/models/hugging_face/llama_utils.py +++ b/DashAI/back/models/hugging_face/llama_utils.py @@ -12,10 +12,20 @@ logger = logging.getLogger(__name__) -try: - import llama_cpp -except ImportError: - llama_cpp = None + +def _get_llama_cpp(): + """Import ``llama_cpp`` on first use, or return ``None`` if not installed. + + The import is deliberately lazy: in a CUDA build, importing ``llama_cpp`` + before ``torch`` makes torch skip its NCCL preload and ``import torch`` + then fails with "undefined symbol: ncclCommShrink". Importing this module + must therefore never import ``llama_cpp`` (see issue #1043). + """ + try: + import llama_cpp + except ImportError: + return None + return llama_cpp # llama_log_callback signature: void (*)(ggml_log_level level, @@ -445,13 +455,14 @@ def _capturing_log_cb(level, msg, user_data): def _lib_dir() -> Path: - return Path(os.path.dirname(llama_cpp.__file__)) / "lib" + return Path(os.path.dirname(_get_llama_cpp().__file__)) / "lib" @lru_cache(maxsize=1) def _load_llama_lib(): """Load llama.dll and install our own log callback so CUDA init output can be captured in _log_capture instead of printed to the real stderr.""" + llama_cpp = _get_llama_cpp() if llama_cpp is None: return None lib = llama_cpp.llama_cpp.load_shared_library("llama", _lib_dir()) @@ -468,7 +479,7 @@ def _load_llama_lib(): @lru_cache(maxsize=1) def _all_ggml_libs() -> tuple: """Load every shared lib in llama_cpp/lib/ so backend symbols are reachable.""" - if llama_cpp is None: + if _get_llama_cpp() is None: return () if sys.platform.startswith("win"): patterns = ("*.dll",) @@ -582,7 +593,7 @@ def get_llama_gpu_devices_formatted() -> list[str]: """GPU device strings visible to llama_cpp (not torch). Example: ["GPU 0: NVIDIA GeForce RTX 5080 - Compute Capability 12.0"] """ - if llama_cpp is None: + if _get_llama_cpp() is None: return [] try: @@ -620,6 +631,7 @@ def is_gpu_available_for_llama_cpp() -> bool: ``True`` if GPU offloading is available; ``False`` if ``llama-cpp-python`` is not installed or if the check raises an unexpected exception. """ + llama_cpp = _get_llama_cpp() if llama_cpp is None: return False try: @@ -665,5 +677,5 @@ def __is_gpu_available_for_llama_cpp_v02() -> bool: ``True`` if the ``ggml_init_cublas`` attribute is found in the loaded library, ``False`` otherwise. """ - lib = llama_cpp.llama_cpp._load_shared_library("llama") + lib = _get_llama_cpp().llama_cpp._load_shared_library("llama") return hasattr(lib, "ggml_init_cublas") diff --git a/Dockerfile.cuda b/Dockerfile.cuda index 3d6b8ca0fc..f7bb9145c6 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -55,6 +55,13 @@ COPY --from=builder /opt/python /opt/python COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" +# libtorch_cuda.so has no DT_NEEDED entry for libnccl and relies on torch +# preloading it, which is skipped when llama_cpp is imported first +# ("undefined symbol: ncclCommShrink"). Preload it explicitly. +RUN ln -s "$(ls /opt/venv/lib/python3.*/site-packages/nvidia/nccl/lib/libnccl.so.2)" \ + /usr/local/lib/libnccl.so.2 +ENV LD_PRELOAD=/usr/local/lib/libnccl.so.2 + # application code COPY . . # built frontend from stage 1 diff --git a/tests/back/models/test_llama_import_order.py b/tests/back/models/test_llama_import_order.py new file mode 100644 index 0000000000..9ead2261ee --- /dev/null +++ b/tests/back/models/test_llama_import_order.py @@ -0,0 +1,35 @@ +"""Import-order guard for llama_cpp vs torch (issue #1043). + +In CUDA builds, importing ``llama_cpp`` before ``torch`` makes torch skip its +NCCL preload, and ``import torch`` then fails with +"undefined symbol: ncclCommShrink". These checks run in a fresh interpreter +because the current test process has already imported both libraries. +""" + +import subprocess +import sys +import textwrap + + +def _run(code: str) -> None: + result = subprocess.run( + [sys.executable, "-c", textwrap.dedent(code)], + capture_output=True, + text=True, + timeout=300, + ) + assert result.returncode == 0, result.stderr + + +def test_gguf_base_imports_torch_before_llama_cpp(): + _run( + """ + import sys + import DashAI.back.models.hugging_face.gguf_text_generation_base # noqa: F401 + modules = list(sys.modules) + if "llama_cpp" in modules: + assert modules.index("torch") < modules.index("llama_cpp"), ( + "llama_cpp was imported before torch" + ) + """ + ) From 2bfc10a19326a8e2bada35954be140a69b1f963d Mon Sep 17 00:00:00 2001 From: Creylay Date: Fri, 27 Nov 2026 14:22:50 -0300 Subject: [PATCH 5/6] fix(jobs): skip the loaded-model cleanup when the first worker starts The Huey consumer starts alongside the app and pre-warms its worker, which cleared the loaded_model table. On a fresh database this ran before the app's migrations created it, logging "no such table: loaded_model". The app already clears the table right after migrating, so only clear it when replacing a dead worker. --- .../dependencies/job_queues/huey_job_queue.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/DashAI/back/dependencies/job_queues/huey_job_queue.py b/DashAI/back/dependencies/job_queues/huey_job_queue.py index 2d45fb347f..76e57d84d9 100644 --- a/DashAI/back/dependencies/job_queues/huey_job_queue.py +++ b/DashAI/back/dependencies/job_queues/huey_job_queue.py @@ -462,17 +462,12 @@ def _ensure_worker(self) -> None: if self._worker_proc is not None and self._worker_proc.is_alive(): return - # A worker killed by a cancel or a crash leaves its rows behind - try: - from kink import di - - from DashAI.back.dependencies.model_cache_status import ( - clear_loaded_models, - ) - - clear_loaded_models(di["session_factory"]) - except Exception: - log.exception("Could not clear stale loaded model rows") + # A worker killed by a cancel or a crash leaves its rows behind. The + # first worker has nothing to clear: the app clears the table right + # after running migrations, and since this consumer starts alongside + # the app, on a fresh database the table may not exist yet. + if self._worker_proc is not None: + self._clear_model_rows() ctx = multiprocessing.get_context("spawn") self._worker_in_q = ctx.Queue() From dd005391eb9f2616dd6fb0b437d4aad0a3dc4832 Mon Sep 17 00:00:00 2001 From: Cristian Tamblay Date: Fri, 4 Dec 2026 12:19:27 -0300 Subject: [PATCH 6/6] fix(docker): build llama.cpp CPU kernels for a portable x86-64 baseline GGML_NATIVE defaults to ON, so llama.cpp compiles its CPU kernels with -march=native and the image only runs on CPUs with the build host's instruction set. An image built on an AVX-512 host dies with SIGILL (exit 132) in load_tensors on a CPU without AVX-512. CUDA initialises first, so the crash looks like a GPU problem. This is the CPU side of the same trap #1044 fixes for CUDA architectures. Turn GGML_NATIVE off and name the baseline: AVX2, FMA and F16C, present on every Haswell-or-later host, without AVX-512. Verified on GCP: an image built on a Cascade Lake host (AVX-512) went from SIGILL to all layers offloaded on a Broadwell host with a T4, at the same tokens per second. With these flags, configuring the vendored llama.cpp compiles ggml-cpu with -mavx2 -mfma -mf16c instead of -march=native. --- Dockerfile.cuda | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Dockerfile.cuda b/Dockerfile.cuda index f7bb9145c6..138323499e 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -38,7 +38,13 @@ ENV UV_PROJECT_ENVIRONMENT=/opt/venv \ # Build llama.cpp kernels for several GPU generations instead of only the # build host's GPU: T4 (75), A100 (80), A10/RTX 30xx (86), L4/RTX 40xx (89), # plus PTX for 89 so newer cards can JIT compile. -ENV CMAKE_ARGS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=75-real;80-real;86-real;89-real;89-virtual" +# GGML_NATIVE is the same trap on the CPU side: it is ON by default and compiles +# the CPU kernels with -march=native, so an image built on a host with AVX-512 +# dies with SIGILL on one without it. CUDA initialises first, so the crash looks +# like a GPU problem. Turn it off and name the baseline explicitly: AVX2/FMA/F16C +# are present on every Haswell-or-later host, AVX-512 is not. +ENV CMAKE_ARGS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=75-real;80-real;86-real;89-real;89-virtual \ +-DGGML_NATIVE=OFF -DGGML_AVX=ON -DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON -DGGML_AVX512=OFF" RUN uv sync --locked --extra cuda --no-dev --no-install-project --no-cache # -------- Stage 3: runtime (CUDA runtime, no build tools) --------