FROM nvidia/cuda:13.2.1-cudnn-devel-ubuntu24.04
ENV DEBIAN_FRONTEND=noninteractive

# "Unminify"
RUN rm -f /etc/dpkg/dpkg.cfg.d/excludes && \
    rm -f /etc/apt/apt.conf.d/docker-clean && \
    rm -f /etc/apt/apt.conf.d/01-nodoc && \
    rm -f /usr/bin/man && \
    dpkg-divert --quiet --remove /usr/bin/man

# Install packages
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt update && apt-get dist-upgrade --autoremove -y && apt-get install -y \
    python3-pip \
    python3-dev \
    python3-venv \
    build-essential \
    man \
    man-db \
    wget \
    bzip2 \
    curl \
    git \
    cmake

# Set up nvfp4 python virtual environment
WORKDIR /opt/nvfp4
RUN python3 -m venv /opt/nvfp4/py-venv
ENV PATH="/opt/nvfp4/py-venv/bin:${PATH}"
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked pip3 install torch torchvision torchaudio --upgrade --index-url \
    https://download.pytorch.org/whl/cu132/


# Fetch convert_to_quant, and install it into our python venv.
WORKDIR /opt/nvfp4
RUN git clone --depth=1 https://github.com/silveroxides/convert_to_quant && cd convert_to_quant && rm -rf .git && pip3 install -r requirements.txt && pip3 install .

# Clone and compile llama.cpp (save some space by removing unneeded .git data)
RUN git clone --depth=1 https://github.com/ggml-org/llama.cpp && rm -rf llama.cpp/.git
WORKDIR /opt/nvfp4/llama.cpp

# Build llama.cpp, the ldconfig is important
RUN echo "/usr/local/cuda-13.2/compat" > /etc/ld.so.conf.d/988-cuda-compat.conf && ldconfig
RUN cmake -B build -S . \
     -DCMAKE_BUILD_TYPE=Release \
     -DGGML_CUDA=ON \
     -DCMAKE_CUDA_ARCHITECTURES=120 \
     -DLLAMA_BUILD_TESTS=OFF \
     -DLLAMA_BUILD_EXAMPLES=ON \
    && cmake --build build -j$(nproc) \
    && cmake --build build --target install \
    && rm -rf build

# Copy and install zefie's nvfp4 quant scripts
RUN mkdir nvfp4_gguf_quant
WORKDIR /opt/nvfp4/nvfp4_gguf_quant
COPY --exclude=__pycache__ nvfp4gguf ./nvfp4gguf
COPY --chmod=0644 gguf_to_nvfp4_gguf.py hf_to_nvfp4_gguf.py hf_to_nvfp4_safetensors.py requirements.txt safetensors_to_nvfp4_gguf.py hf_info.py gguf_info.py .

# Install requirements for zefie's nvfp4 quant scripts
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked pip3 install -r requirements.txt
COPY --chmod=0644 docker/README.txt /workspace/README.txt
COPY --chmod=0755 docker/hf_info docker/gguf_info docker/hf_to_nvfp4_gguf docker/hf_to_nvfp4_safetensors docker/gguf_to_nvfp4_gguf docker/safetensors_to_nvfp4_gguf /usr/local/bin/

# Install manpages for zefie's nvfp4 quant scripts, ctq, and heretic
RUN mkdir -p /usr/local/share/man/man1
COPY --chmod=0644 man/man1/* /usr/local/share/man/man1/
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked pip3 install heretic-llm

# Finalize and settle at /workspace
ENV DEBIAN_FRONTEND=
WORKDIR /workspace
ENTRYPOINT []
