FROM nvidia/cuda:13.0.3-cudnn-devel-ubuntu22.04
LABEL maintainer="Hugging Face"

ARG DEBIAN_FRONTEND=noninteractive

# Use login shell to read variables from `~/.profile` (to pass dynamic created variables between RUN commands)
SHELL ["sh", "-lc"]

# The following `ARG` are mainly used to specify the versions explicitly & directly in this docker file, and not meant
# to be used as arguments for docker build (so far).

ARG PYTORCH='2.14.0'
# Example: `cu102`, `cu113`, etc.
ARG CUDA='cu130'

# This needs to be compatible with the above `PYTORCH`.
ARG TORCHCODEC='0.16.0'

ARG FLASH_ATTN='false'

RUN apt update
RUN apt install -y git libsndfile1-dev tesseract-ocr espeak-ng python3 python3-pip ffmpeg git-lfs
RUN git lfs install
RUN python3 -m pip install --no-cache-dir --upgrade pip

ARG REF=main
RUN git clone https://github.com/huggingface/transformers && cd transformers && git checkout $REF

RUN python3 -m pip install --no-cache-dir -e ./transformers[dev]

# 1. Put several commands in a single `RUN` to avoid image/layer exporting issue. Could be revised in the future.
# 2. For `torchcodec`, use `cpu` as we don't have `libnvcuvid.so` on the host runner. See https://github.com/meta-pytorch/torchcodec/issues/912
#    **Important**: We need to specify `torchcodec` version if the torch version is not the latest stable one.
# 3. `set -e` means "exit immediately if any command fails".
# 4. Warning: torchaudio only publishes wheels for the CUDA version it was released with (e.g. 2.11.0
#    only has a cu130 build). If torch is installed with a different CUDA suffix (e.g. cu132), pip
#    will keep the torchaudio installed in the previous step and torchaudio will raise a RuntimeError on import due to the
#    CUDA version mismatch. When upgrading CUDA, make sure torchaudio has a wheel for the new version.
RUN set -e; \
    # Determine torch version
    if [ ${#PYTORCH} -gt 0 ] && [ "$PYTORCH" != "pre" ]; then \
        VERSION="torch==${PYTORCH}.*"; \
        TORCHCODEC_VERSION="torchcodec==${TORCHCODEC}.*"; \
        # torchaudio is in maintenance mode: 2.11 is the final release, compatible with torch 2.11+
        # (no need to upgrade it with torch; it does not pin torch to a specific version)
        if printf '%s\n' "2.11.0" "${PYTORCH}" | sort -V | head -1 | grep -qx "2.11.0"; then \
            TORCHAUDIO_VERSION="torchaudio==2.11.0"; \
        else \
            TORCHAUDIO_VERSION="torchaudio==${PYTORCH}.*"; \
        fi; \
    else \
        VERSION="torch"; \
        TORCHAUDIO_VERSION="torchaudio"; \
        TORCHCODEC_VERSION="torchcodec"; \
    fi; \
    \
    # Log the version being installed
    echo "Installing torch version: $VERSION"; \
    \
    # Install PyTorch packages
    if [ "$PYTORCH" != "pre" ]; then \
        python3 -m pip install --no-cache-dir -U \
            $VERSION \
            torchvision \
            $TORCHAUDIO_VERSION \
            --extra-index-url https://download.pytorch.org/whl/$CUDA; \
        # We need to specify the version if the torch version is not the latest stable one.
        python3 -m pip install --no-cache-dir -U \
            $TORCHCODEC_VERSION --extra-index-url https://download.pytorch.org/whl/cpu; \
    else \
        python3 -m pip install --no-cache-dir -U --pre \
            torch \
            torchvision \
            torchaudio \
            --extra-index-url https://download.pytorch.org/whl/nightly/$CUDA; \
        python3 -m pip install --no-cache-dir -U --pre \
            torchcodec --extra-index-url https://download.pytorch.org/whl/nightly/cpu; \
    fi

RUN python3 -m pip install --no-cache-dir -U timm

RUN [ "$PYTORCH" != "pre" ] && python3 -m pip install --no-cache-dir --no-build-isolation git+https://github.com/facebookresearch/detectron2.git || echo "Don't install detectron2 with nightly torch"

RUN python3 -m pip install --no-cache-dir pytesseract

RUN python3 -m pip install -U "itsdangerous<2.1.0"

RUN python3 -m pip install --no-cache-dir git+https://github.com/huggingface/accelerate@main#egg=accelerate

RUN python3 -m pip install --no-cache-dir git+https://github.com/huggingface/peft@main#egg=peft

# For bettertransformer
RUN python3 -m pip install --no-cache-dir git+https://github.com/huggingface/optimum@main#egg=optimum

# For ONNX export tests (onnxscript pulls in onnx + onnx_ir; onnxruntime-gpu validates the exported
# graph on GPU for faster export testing)
RUN python3 -m pip install --no-cache-dir onnxscript onnxruntime-gpu

# For video model testing
RUN python3 -m pip install --no-cache-dir av

# Some slow tests require bnb
RUN python3 -m pip install --no-cache-dir bitsandbytes

# Some tests require quanto
RUN python3 -m pip install --no-cache-dir quanto

# Install FA2:
#
# FA2 is built from source because Dao-AILab stopped publishing wheels for torch>=2.9.
# Key changes vs the naive `pip install flash-attn --no-build-isolation`:
#   1. ninja       – parallel compilation (without it the build falls back to sequential make)
#   2. wheel       – force-reinstall the pip-managed wheel (>=0.43) instead of the Ubuntu 22.04
#                    system wheel (0.37.1) that still imports pkg_resources, which was removed in
#                    setuptools>=83 (pulled in by torch>=2.13). Correctness fix, not a speed opt.
#   3. C++20       – torch>=2.14 requires C++20; FA2 v2.8.3.post1 still ships -std=c++17 in its
#                    setup.py, so we patch it with sed before building.
#   4. SKIP_CK_BUILD=True  – skip ROCm composable_kernel submodule; on NVIDIA it is useless
#                    but setup.py unconditionally sets SKIP_CK_BUILD=False, causing ~1100
#                    extra .cu files to be compiled.
#   5. csrc/cutlass – init only the CUTLASS submodule (headers required by flash_attn kernels).
#   6. FLASH_ATTN_CUDA_ARCHS='80' – compile only for Ampere (SM 8.0/8.6, covers A10G & A100);
#                    skips Hopper (90), Blackwell (100/120) and cuts compile time ~4× (biggest
#                    speedup along with ninja).
#   7. Dynamic MAX_JOBS / NVCC_THREADS – derived from available RAM so the build never OOMs
#                    on the CI runner (formula: floor(RAM_GB/2.8), capped at nproc, then
#                    split as NVCC_THREADS=4 / MAX_JOBS=product÷4).
#   8. --allow-unsupported-compiler – CUDA 13.0 dropped GCC 11 from its supported-compiler
#                    matrix; Ubuntu 22.04 ships GCC 11 by default, so NVCC hard-errors without
#                    this flag even though GCC 11 compiles FA2 fine in practice.
#
# FA3 and FA4 are skipped — FA3/FA4 require Hopper/Blackwell; our runners use Ampere (A10G).
RUN if [ "$FLASH_ATTN" != "false" ]; then \
        set -e; \
        \
        python3 -m pip install --no-cache-dir ninja; \
        python3 -m pip install --force-reinstall --no-cache-dir wheel; \
        \
        cd /tmp; \
        git clone --depth=1 --branch v2.8.3.post1 \
            https://github.com/Dao-AILab/flash-attention.git flash-attention; \
        cd flash-attention; \
        \
        sed -i 's/-std=c++17/-std=c++20/g' setup.py; \
        sed -i 's/SKIP_CK_BUILD = os.getenv.*if USE_TRITON_ROCM else False/SKIP_CK_BUILD = True/' setup.py; \
        git submodule update --init --depth=1 csrc/cutlass; \
        \
        RAM_GB=$(free -g | awk '/^Mem:/{print $2}'); \
        NCPU=$(nproc); \
        MAX_PRODUCT=$(python3 -c "print(min(${NCPU}, int(${RAM_GB}/2.8)))"); \
        NVCC_THREADS=4; \
        MAX_JOBS=$(python3 -c "print(max(1, ${MAX_PRODUCT} // ${NVCC_THREADS}))"); \
        echo "Building FA2: MAX_JOBS=${MAX_JOBS} NVCC_THREADS=${NVCC_THREADS} RAM=${RAM_GB}GB CPUs=${NCPU}"; \
        \
        FLASH_ATTN_CUDA_ARCHS='80' \
        MAX_JOBS=${MAX_JOBS} \
        NVCC_THREADS=${NVCC_THREADS} \
        FLASH_ATTENTION_FORCE_BUILD=TRUE \
        NVCC_APPEND_FLAGS='--allow-unsupported-compiler' \
            python3 setup.py bdist_wheel --dist-dir=dist; \
        python3 -m pip install --no-cache-dir dist/*.whl; \
        cd /tmp && rm -rf flash-attention; \
    else \
        echo "Skipping FA2 install (FLASH_ATTN=${FLASH_ATTN})"; \
    fi

# TODO (ydshieh): check this again
# `quanto` will install `ninja` which leads to many `CUDA error: an illegal memory access ...` in some model tests
# (`deformable_detr`, `rwkv`, `mra`)
RUN python3 -m pip uninstall -y ninja

# For `nougat` tokenizer
RUN python3 -m pip install --no-cache-dir python-Levenshtein

# For `FastSpeech2ConformerTokenizer` tokenizer
RUN python3 -m pip install --no-cache-dir g2p-en

# For serving tests (audio pipelines)
RUN python3 -m pip install --no-cache-dir librosa python-multipart

# For Some bitsandbytes tests
RUN python3 -m pip install --no-cache-dir einops

# For `VibeVoice` (added in PR #40546)
RUN python3 -m pip install --no-cache-dir diffusers

# `kernels` may give different outputs (within 1e-5 range) even with the same model (weights) and the same inputs
RUN python3 -m pip uninstall -y kernels

# When installing in editable mode, `transformers` is not recognized as a package.
# this line must be added in order for python to be aware of transformers.
RUN cd transformers && python3 setup.py develop

# Smoke test: fail the image build immediately if a later `pip install` replaced or broke the pinned
# CUDA torch stack (e.g. a dep that drags in a different torch/CUDA/NCCL — which surfaces as
# `undefined symbol: ncclCommResume` at `import torch`; see run 28991812987). We import torch (which
# eagerly loads `libtorch_cuda.so`) AND assert the version still matches the `${PYTORCH}` pin, since
# transformers only requires `torch>=2.4` — a silent swap to a newer torch otherwise passes every
# version check and reaches the GPU test jobs. No GPU is present at build time, so we only exercise
# the import + CUDA runtime load, not device availability.
RUN set -e; \
    python3 -c "import torch; print('torch version:', torch.__version__); torch.cuda.is_available()"; \
    if [ "$PYTORCH" != "pre" ]; then \
        python3 -c "import torch, sys; v = torch.__version__.split('+')[0]; sys.exit(0 if v.startswith('${PYTORCH}') else 'ERROR: torch is ' + torch.__version__ + ', expected ${PYTORCH}.* - the pinned CUDA build was clobbered')"; \
    fi
