# build stage
FROM registry.fedoraproject.org/fedora:43 AS builder

# deps + rocm toolchain (native fedora packages)
RUN dnf -y --nodocs --setopt=install_weak_deps=False \
  install \
  make gcc cmake lld clang clang-devel compiler-rt libcurl-devel \
  rocminfo radeontop 'rocm-*' 'rocblas-*' hipblas 'hipblas-*' \
  git vim rsync sudo tar xz patch \
  && dnf clean all && rm -rf /var/cache/dnf/*

# llama.cpp
WORKDIR /opt/llama.cpp
ARG REPO=https://github.com/ggerganov/llama.cpp.git
ARG BRANCH=master
RUN git clone -b ${BRANCH} --single-branch --recursive ${REPO} .

COPY llama-grammar.patch /tmp/llama-grammar.patch

# build + install
RUN git clean -xdf \
  && git submodule update --recursive \
  && patch -p1 < /tmp/llama-grammar.patch \
  && HIPCXX="$(hipconfig -l)/clang" HIP_PATH="$(hipconfig -R)" \
  cmake -S . -B build \
  -DGGML_HIP=ON \
  -DAMDGPU_TARGETS=gfx1151 \
  -DCMAKE_BUILD_TYPE=Release \
  -DGGML_RPC=ON \
  -DLLAMA_HIP_UMA=ON \
  && cmake --build build --config Release -- -j$(nproc) \
  && cmake --install build --config Release

# libs
RUN find /opt/llama.cpp/build -type f -name 'lib*.so*' -exec cp {} /usr/lib64/ \; \
  && ldconfig

# runtime stage
FROM registry.fedoraproject.org/fedora-minimal:43

# runtime deps (native fedora rocm + utilities)
RUN microdnf -y --nodocs --setopt=install_weak_deps=0 \
  install \
  bash ca-certificates libatomic libstdc++ libgcc libgomp sudo \
  rocminfo radeontop 'rocm-*' 'rocblas-*' hipblas 'hipblas-*' \
  procps-ng \
  && microdnf clean all && rm -rf /var/cache/dnf/*

# copy
COPY --from=builder /usr/local/ /usr/local/
COPY --from=builder /usr/include/rocwmma /usr/include/rocwmma
COPY --from=builder /opt/llama.cpp/build/bin/rpc-* /usr/local/bin/

# ld
RUN echo "/usr/local/lib"  > /etc/ld.so.conf.d/local.conf \
  && echo "/usr/local/lib64" >> /etc/ld.so.conf.d/local.conf \
  && ldconfig \
  && cp -n /usr/local/lib/libllama*.so* /usr/lib64/ 2>/dev/null || true \
  && ldconfig

# helper
COPY gguf-vram-estimator.py /usr/local/bin/gguf-vram-estimator.py
RUN chmod +x /usr/local/bin/gguf-vram-estimator.py

# shell
CMD ["/bin/bash"]
