# Copyright 2023-2024 PKU-Alignment Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
#
# Dockerfile for Safe-RLHF
#
# Create docker image with command:
#
#   $ docker build --tag safe-rlhf:latest .
#

ARG cuda_docker_tag="11.8.0-cudnn8-devel-ubuntu22.04"
FROM nvidia/cuda:"${cuda_docker_tag}"

ENV DEBIAN_FRONTEND=noninteractive
SHELL ["/bin/bash", "-c"]

# Install packages
RUN apt-get update && \
    apt-get install -y sudo ca-certificates openssl \
        git ssh build-essential gcc g++ make \
        python3-dev python3-venv && \
    rm -rf /var/lib/apt/lists/*

ENV LANG C.UTF-8
ENV CC=gcc CXX=g++

# Add a new user
RUN useradd -m -s /bin/bash safe-rlhf && \
    echo "safe-rlhf ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
USER safe-rlhf
RUN echo "export PS1='[\[\e[1;33m\]\u\[\e[0m\]:\[\e[1;35m\]\w\[\e[0m\]]\$ '" >> ~/.bashrc

# Setup virtual environment
RUN /usr/bin/python3 -m venv --upgrade-deps ~/venv && rm -rf ~/.pip/cache
RUN PIP_EXTRA_INDEX_URL="https://download.pytorch.org/whl/cu$(echo "${CUDA_VERSION}" | cut -d'.' -f-2  | tr -d '.')" && \
    echo "export PIP_EXTRA_INDEX_URL='${PIP_EXTRA_INDEX_URL}'" >> ~/venv/bin/activate && \
    echo "source /home/safe-rlhf/venv/bin/activate" >> ~/.bashrc

# Install dependencies
WORKDIR /home/safe-rlhf/safe-rlhf
COPY --chown=safe-rlhf requirements.txt requirements.txt
RUN source ~/venv/bin/activate && \
    python -m pip install -r requirements.txt && \
    rm -rf ~/.pip/cache ~/.cache/pip

COPY --chown=safe-rlhf . .

# Install safe-rlhf
RUN source ~/venv/bin/activate && \
    make install-editable && \
    rm -rf .eggs *.egg-info ~/.pip/cache ~/.cache/pip

ENTRYPOINT [ "/bin/bash", "--login" ]
