FROM nvidia/cuda:12.8.1-devel-ubuntu22.04@sha256:6617a625f4090c76c545a0e7d63f2e441718ef9af7f4efe7dd1242a29e289fd7

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates curl git build-essential clang-14 llvm-14 lld-14 \
    python3 python3-venv util-linux && rm -rf /var/lib/apt/lists/*
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:/usr/lib/llvm-14/bin:/usr/local/cuda/bin:${PATH}"
RUN pip install --no-cache-dir numpy==2.2.6 tiktoken==0.12.0 pillow==11.3.0 && \
    pip install --no-cache-dir torch==2.7.1 --index-url https://download.pytorch.org/whl/cpu

ARG OFA_COMMIT=9dd7dd0d3c1fc0245b43897eacd8d62aa096c4c1
ARG DOOM_COMMIT=e257a09527062bfb986b6e5e0780839b6cbd5b71
RUN git init /opt/open-flapping-airplanes && \
    cd /opt/open-flapping-airplanes && \
    git remote add origin https://github.com/Bobliuuu/open-flapping-airplanes.git && \
    git fetch --depth 1 origin "${OFA_COMMIT}" && git checkout --detach FETCH_HEAD
WORKDIR /opt/open-flapping-airplanes/doom-gpu
RUN git init doomgeneric && cd doomgeneric && \
    git remote add origin https://github.com/ozkl/doomgeneric.git && \
    git fetch --depth 1 origin "${DOOM_COMMIT}" && git checkout --detach FETCH_HEAD
RUN curl --fail --location --retry 3 \
    https://raw.githubusercontent.com/Akbar30Bill/DOOM_wads/master/doom1.wad -o doom1.wad && \
    echo 'f0cefca49926d00903cf57551d901abe  doom1.wad' | md5sum -c -
RUN ./build_doom.sh > /opt/doom-build.log 2>&1 || { tail -100 /opt/doom-build.log; exit 1; }
WORKDIR /opt/open-flapping-airplanes/llm/ofa_fast
ENV DOOM_PTX=/opt/open-flapping-airplanes/doom-gpu/doom.ptx
COPY patches/decode-ticket-limit.patch /opt/reproduction/decode-ticket-limit.patch
RUN git apply --check /opt/reproduction/decode-ticket-limit.patch && \
    git apply /opt/reproduction/decode-ticket-limit.patch
RUN ./build_merged.sh ofa_fast_S -DMERGED_DOOM > /opt/ofa-build.log 2>&1 || \
    { cat /opt/ofa-build.log; exit 1; }
COPY prepare.py reproduce.py /opt/reproduction/
ENV OFA_DATA=/data OFA_RESULTS=/results
CMD ["python", "/opt/reproduction/reproduce.py"]
