FROM nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04
# UTF-8 encoding is necessary for printing non-ASCII characters to the
# terminal.
ENV LC_ALL C.UTF-8
# Install Python.
# Note that PyTorch does not yet support Python later than 3.9.
RUN apt-get update -y && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        # Installs add-apt-repository.
        software-properties-common \
        && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt-get update -y && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        python3.9 \
        python3.9-dev \
        # Required by Poetry
        python3.9-venv \
        # Required by numpy
        python3.9-distutils \
        # Required by matplotlib
        python3.9-tk \
        && \
    rm -rf /var/lib/apt/lists/*
# Poetry won't get installed correctly unless a `python` binary is present.
# The specific Python installation it points to also needs to have the venv
# module installed. The version of Python used to run the installation script
# should also match.
RUN ln -s "`which python3.9`" /usr/local/bin/python && \
    ln -sf "`which python3.9`" /usr/local/bin/python3
# Install some other packages.
RUN apt-get update -y && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        curl \
        unzip \
        texlive-xetex \
        && \
    rm -rf /var/lib/apt/lists/*
# Install Poetry.
# See https://python-poetry.org/docs/#installing-with-the-official-installer
RUN cd /tmp && \
    curl -sSL https://install.python-poetry.org > install-poetry.py && \
    POETRY_HOME=/usr/local/poetry python3 install-poetry.py && \
    rm install-poetry.py
ENV PATH /usr/local/poetry/bin:${PATH}
# Stores Python packages in the local directory.
# See https://python-poetry.org/docs/configuration/#virtualenvsin-project-boolean
ENV POETRY_VIRTUALENVS_IN_PROJECT true
# Install git.
RUN apt-get update -y && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        git \
        && \
    rm -rf /var/lib/apt/lists/*
# Install SentencePiece.
# See https://github.com/google/sentencepiece#build-and-install-sentencepiece-command-line-tools-from-c-source
RUN apt-get update -y && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        cmake \
        build-essential \
        pkg-config \
        libgoogle-perftools-dev \
        && \
    rm -rf /var/lib/apt/lists/*
RUN version=0.1.97 && \
    cd /tmp && \
    curl -L -o sentencepiece.tar.gz https://github.com/google/sentencepiece/archive/refs/tags/v$version.tar.gz && \
    ls -lh && \
    tar xzf sentencepiece.tar.gz && \
    rm sentencepiece.tar.gz && \
    cd sentencepiece-$version && \
    mkdir build && \
    cd build && \
    cmake .. && \
    make -j `nproc` && \
    make install && \
    ldconfig -v && \
    cd ../.. && \
    rm -r sentencepiece-$version
ENV PYTHONPATH /app/src:${PYTHONPATH}
WORKDIR /app/
