ARG PYTHON_VERSION=3.8.13

FROM 763104351884.dkr.ecr.us-west-2.amazonaws.com/pytorch-inference:1.12.1-cpu-py38-ubuntu20.04-sagemaker

# PYTORCH_SKIP_CUDNN_COMPATIBILITY_CHECK - see https://github.com/autogluon/autogluon/issues/2534
ENV PYTORCH_SKIP_CUDNN_COMPATIBILITY_CHECK=1

# Specify accept-bind-to-port LABEL for inference pipelines to use SAGEMAKER_BIND_TO_PORT
# https://docs.aws.amazon.com/sagemaker/latest/dg/inference-pipeline-real-time.html
LABEL com.amazonaws.sagemaker.capabilities.accept-bind-to-port=true
# Specify multi-models LABEL to indicate container is capable of loading and serving multiple models concurrently
# https://docs.aws.amazon.com/sagemaker/latest/dg/build-multi-model-build-container.html
LABEL com.amazonaws.sagemaker.capabilities.multi-models=true

LABEL maintainer="Amazon AI"
LABEL dlc_major_version="1"

# PYTORCH_SKIP_CUDNN_COMPATIBILITY_CHECK - see https://github.com/autogluon/autogluon/issues/2534
ENV MXNET_CUDNN_LIB_CHECKING=0 \
    MXNET_CUDNN_AUTOTUNE_DEFAULT=0 \
    PYTORCH_SKIP_CUDNN_COMPATIBILITY_CHECK=1

RUN apt-get update \
 && apt-get -y upgrade \
 && apt-get autoremove -y \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

# Install MMS

ARG MMS_VERSION=1.1.8
ARG MXNET_VERSION=1.9.1

RUN pip install --no-cache-dir -U --trusted-host pypi.org --trusted-host files.pythonhosted.org pip \
 && pip install --no-cache-dir -U multi-model-server==${MMS_VERSION} sagemaker-mxnet-inference \
 && pip install --no-cache-dir -U mxnet==${MXNET_VERSION}

RUN mkdir -p /home/model-server/tmp \
 && chown -R model-server /home/model-server

COPY config.properties /home/model-server

COPY deep_learning_container.py /usr/local/bin/deep_learning_container.py
RUN chmod +x /usr/local/bin/deep_learning_container.py

COPY mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py
RUN chmod +x /usr/local/bin/dockerd-entrypoint.py

# Install AutoGluon

ARG AUTOGLUON_VERSION=0.6.2

# Updating certifi - https://pyup.io/p/pypi/certifi/2ff/
# Downgrade protobuf 3.20.1 -> 3.19.6: https://pyup.io/v/51167/f17
# Downgrade scipy 1.9.3 -> 1.8.* to resolve conflict w/ sktime
RUN pip install --no-cache-dir -U --trusted-host pypi.org --trusted-host files.pythonhosted.org pip \
 && pip install --no-cache-dir -U setuptools wheel \
 && pip install --no-cache-dir -U certifi \
 && pip uninstall -y dataclasses \
 && pip install --no-cache-dir -U numpy numba \
 && pip install --no-cache-dir -U autogluon==${AUTOGLUON_VERSION} \
 && mim install -q mmcv-full \
 && pip install --no-cache-dir -U mmdet \
 && pip install -U protobuf==3.19.* scipy==1.8.*

# Removing GluonTS nursery/tsbench package - it is not used in training/inference and have security vulnerabilities
RUN rm -rf /usr/local/lib/python3.8/dist-packages/gluonts/nursery/tsbench

# Remove these to address security issues; these are not used if only python package is used
RUN rm -rf /usr/local/lib/python3.8/dist-packages/ray/jars \
 && rm -rf /opt/conda/lib/python3.8/site-packages/ray/jars

RUN HOME_DIR=/root \
 && curl -o ${HOME_DIR}/oss_compliance.zip https://aws-dlinfra-utilities.s3.amazonaws.com/oss_compliance.zip \
 && unzip ${HOME_DIR}/oss_compliance.zip -d ${HOME_DIR}/ \
 && cp ${HOME_DIR}/oss_compliance/test/testOSSCompliance /usr/local/bin/testOSSCompliance \
 && chmod +x /usr/local/bin/testOSSCompliance \
 && chmod +x ${HOME_DIR}/oss_compliance/generate_oss_compliance.sh \
 && ${HOME_DIR}/oss_compliance/generate_oss_compliance.sh ${HOME_DIR} python \
 && rm -rf ${HOME_DIR}/oss_compliance*

RUN curl -o /licenses-autogluon.txt https://autogluon.s3.us-west-2.amazonaws.com/licenses/THIRD-PARTY-LICENSES.txt

EXPOSE 8080 8081
ENTRYPOINT ["python", "/usr/local/bin/dockerd-entrypoint.py"]
CMD ["multi-model-server", "--start", "--mms-config", "/home/model-server/config.properties"]