# qwen38-flash-next-llamacpp-gb10: llama.cpp v1.0.0 (github.com/ggml-org/llama.cpp, MIT), built for the DGX
# Spark's GB10 (linux-aarch64, sm_121) with CUDA 13. v1.0.0 is the first release with the `qwen4exp`
# architecture (Qwen3.8-Flash-Next: hyper-connections, the PLE n-gram table read lazily from its own shard) and
# the qwen3vl_merger picture reader.
#
# Build stage: NVIDIA's PyTorch 25.x container (Ubuntu 24.04, CUDA 13.0, nvcc with compute_121, cmake 3.31),
# pinned by digest; only its toolchain is used. The source is the v1.0.0 tag's commit, checked by SHA-256.
# No web UI (LLAMA_USE_PREBUILT_UI would download one at build time; the capsule has its own page).
FROM nvcr.io/nvidia/pytorch@sha256:f3b4a33fd60d5e0358287bb44d57e91eda45e8f1681fef1bfd6c969371417130 AS build
ARG LLAMA_COMMIT=a6d1a74dbebce3a16f4f2ceddbf38c21a08d8674
ARG LLAMA_SHA256=5f2e8db8f5ddac1582bc702c0c22b59f1795a7e6de8387c3db22e5840725665b
RUN set -e; cd /tmp; \
    curl -fsSL -o src.tgz "https://github.com/ggml-org/llama.cpp/archive/$LLAMA_COMMIT.tar.gz"; \
    echo "$LLAMA_SHA256  src.tgz" | sha256sum -c -; \
    tar xzf src.tgz; mv "llama.cpp-$LLAMA_COMMIT" /src; rm src.tgz
WORKDIR /src
RUN cmake -B build -DCMAKE_BUILD_TYPE=Release \
      -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=121 -DGGML_NATIVE=OFF -DGGML_CUDA_NCCL=OFF \
      -DBUILD_SHARED_LIBS=OFF -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF \
      -DLLAMA_BUILD_UI=OFF -DLLAMA_USE_PREBUILT_UI=OFF -DLLAMA_OPENSSL=OFF \
 && cmake --build build --config Release -j"$(nproc)" --target llama-server \
 && mkdir -p /out/bin /out/lib \
 && cp build/bin/llama-server /out/bin/ \
 && cp -a /usr/local/cuda/lib64/libcudart.so.13* /usr/local/cuda/lib64/libcublas.so.13* \
          /usr/local/cuda/lib64/libcublasLt.so.13* /out/lib/ \
 && cp LICENSE /out/LICENSE.llama.cpp

# Runtime: ubuntu:24.04 (arm64), pinned by digest, with the server and the three CUDA libraries it links.
FROM ubuntu@sha256:c92376659a7f14cc1f51615d73c9f4b7d8e18d59f370e02b4ed683ab4485c66c
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates curl libgomp1 python3 \
 && rm -rf /var/lib/apt/lists/* /var/cache/apt/*
COPY --from=build /out/bin/llama-server /opt/llama/bin/llama-server
COPY --from=build /out/lib/ /opt/llama/lib/
COPY --from=build /out/LICENSE.llama.cpp /opt/llama/
ENV LD_LIBRARY_PATH=/opt/llama/lib PYTHONUNBUFFERED=1 LANG=C.UTF-8 \
    NVIDIA_VISIBLE_DEVICES=all NVIDIA_DRIVER_CAPABILITIES=compute,utility
# Every library resolves except libcuda.so.1, which the NVIDIA runtime mounts from the driver at start.
RUN ! ldd /opt/llama/bin/llama-server | grep 'not found' | grep -v 'libcuda\.so\.1'
