Update dockerfile and add docker build action (#3283)

* Use github docker registry

* Fix DDP train for GPU in exclusive mode

* Improve docker image - compile gsplat, decrease image size

* Drop unrelated change

* Add build docker image action

* Rename build docker image action

* nit

* Remove commented line from Dockerfile

* Fix dockerfile when explicit source is specified

* Fix failing dynamo build for torch.compile

* Lock dockerfile and tcnn versions

* Add `torch.cuda.is_available()` condition

* Drop set_cuda_device

* Fix build docker image github action

* Docker build save disk space

* Set MAX_JOBS to limit resource usage for docker build

* Try bumping `MAX_JOBS` 2 => 4

* Install fixed gsplat version from nerfstudio's pyproject.toml

* Update docs

* Finish docker build action

* Fix ignores push when PR

---------

Co-authored-by: Gina Wu <ginawu98@gmail.com>
Co-authored-by: Brent Yi <yibrenth@gmail.com>
This commit is contained in:
authored and GitHub committed 2024-09-05 10:12:21 +02:00
1 parent 96b7fe2230
commit f86dbe6d45
3 files changed
+169 -170

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
name: Build Docker Image
on:
workflow_dispatch:
workflow_call:
pull_request:
push:
branches:
- main
- master
tags:
- 'v*'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-publish-docker-image:
runs-on: ubuntu-latest
name: build-and-publish-docker-image
permissions:
packages: write
contents: read
attestations: write
id-token: write
steps:
- uses: actions/checkout@v4
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Free root space
uses: almahmoud/free-root-space@main
with:
remove-gcc: false
remove-cplusplus: false
- name: Build and push Docker image
id: push
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
with:
context: .
file: ./Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: ${{ github.event_name != 'pull_request' }}
+103 -155
View File
@@ -1,185 +1,133 @@
ARG CUDA_VERSION=11.8.0 # syntax=docker/dockerfile:1
ARG OS_VERSION=22.04 ARG UBUNTU_VERSION=22.04
# Define base image. ARG NVIDIA_CUDA_VERSION=11.8.0
FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${OS_VERSION} # CUDA architectures, required by Colmap and tiny-cuda-nn. Use >= 8.0 for faster TCNN.
ARG CUDA_VERSION ARG CUDA_ARCHITECTURES="90;89;86;80;75;70;61"
ARG OS_VERSION ARG NERFSTUDIO_VERSION=""
# Define username, user uid and gid # Pull source either provided or from git.
ARG USERNAME=user FROM scratch as source_copy
ARG USER_UID=1000 ONBUILD COPY . /tmp/nerfstudio
ARG USER_GID=$USER_UID FROM alpine/git as source_no_copy
ARG NERFSTUDIO_VERSION
ONBUILD RUN git clone --branch ${NERFSTUDIO_VERSION} --recursive https://github.com/nerfstudio-project/nerfstudio.git /tmp/nerfstudio
ARG NERFSTUDIO_VERSION
FROM source_${NERFSTUDIO_VERSION:+no_}copy as source
# metainformation FROM nvidia/cuda:${NVIDIA_CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION} as builder
LABEL org.opencontainers.image.version = "0.1.18" ARG CUDA_ARCHITECTURES
LABEL org.opencontainers.image.source = "https://github.com/nerfstudio-project/nerfstudio" ARG NVIDIA_CUDA_VERSION
LABEL org.opencontainers.image.licenses = "Apache License 2.0" ARG UBUNTU_VERSION
LABEL org.opencontainers.image.base.name="docker.io/library/nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${OS_VERSION}"
# Variables used at build time.
## CUDA architectures, required by Colmap and tiny-cuda-nn.
## NOTE: All commonly used GPU architectures are included and supported here. To speedup the image build process remove all architectures but the one of your explicit GPU. Find details here: https://developer.nvidia.com/cuda-gpus (8.6 translates to 86 in the line below) or in the docs.
ARG CUDA_ARCHITECTURES=90;89;86;80;75;70;61;52;37
# Set environment variables.
## Set non-interactive to prevent asking for user inputs blocking image creation.
ENV DEBIAN_FRONTEND=noninteractive ENV DEBIAN_FRONTEND=noninteractive
## Set timezone as it is required by some packages. ENV QT_XCB_GL_INTEGRATION=xcb_egl
ENV TZ=Europe/Berlin
## CUDA Home, required to find CUDA in some packages.
ENV CUDA_HOME="/usr/local/cuda"
# Install required apt packages and clear cache afterwards.
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends --no-install-suggests \
build-essential \
cmake \
curl \
ffmpeg \
git \ git \
libatlas-base-dev \ cmake \
ninja-build \
build-essential \
libboost-program-options-dev \
libboost-filesystem-dev \ libboost-filesystem-dev \
libboost-graph-dev \ libboost-graph-dev \
libboost-program-options-dev \
libboost-system-dev \ libboost-system-dev \
libboost-test-dev \
libhdf5-dev \
libcgal-dev \
libeigen3-dev \ libeigen3-dev \
libflann-dev \ libflann-dev \
libfreeimage-dev \ libfreeimage-dev \
libgflags-dev \
libglew-dev \
libgoogle-glog-dev \
libmetis-dev \ libmetis-dev \
libprotobuf-dev \ libgoogle-glog-dev \
libqt5opengl5-dev \ libgtest-dev \
libsqlite3-dev \ libsqlite3-dev \
libsuitesparse-dev \ libglew-dev \
nano \
protobuf-compiler \
python-is-python3 \
python3.10-dev \
python3-pip \
qtbase5-dev \ qtbase5-dev \
sudo \ libqt5opengl5-dev \
vim-tiny \ libcgal-dev \
wget && \ libceres-dev \
rm -rf /var/lib/apt/lists/* python3.10-dev \
python3-pip
# Build and install COLMAP.
# Install GLOG (required by ceres). RUN git clone https://github.com/colmap/colmap.git && \
RUN git clone --branch v0.6.0 https://github.com/google/glog.git --single-branch && \
cd glog && \
mkdir build && \
cd build && \
cmake .. && \
make -j `nproc` && \
make install && \
cd ../.. && \
rm -rf glog
# Add glog path to LD_LIBRARY_PATH.
ENV LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/lib"
# Install Ceres-solver (required by colmap).
RUN git clone --branch 2.1.0 https://ceres-solver.googlesource.com/ceres-solver.git --single-branch && \
cd ceres-solver && \
git checkout $(git describe --tags) && \
mkdir build && \
cd build && \
cmake .. -DBUILD_TESTING=OFF -DBUILD_EXAMPLES=OFF && \
make -j `nproc` && \
make install && \
cd ../.. && \
rm -rf ceres-solver
# Install colmap.
RUN git clone --branch 3.8 https://github.com/colmap/colmap.git --single-branch && \
cd colmap && \ cd colmap && \
git checkout "3.9.1" && \
mkdir build && \ mkdir build && \
cd build && \ cd build && \
cmake .. -DCUDA_ENABLED=ON \ mkdir -p /build && \
-DCMAKE_CUDA_ARCHITECTURES=${CUDA_ARCHITECTURES} && \ cmake .. -GNinja "-DCMAKE_CUDA_ARCHITECTURES=${CUDA_ARCHITECTURES}" \
make -j `nproc` && \ -DCMAKE_INSTALL_PREFIX=/build/colmap && \
make install && \ ninja install -j1 && \
cd ../.. && \ cd ~
rm -rf colmap
# Create non root user, add it to custom group and setup environment. # Upgrade pip and install dependencies.
RUN groupadd --gid $USER_GID $USERNAME \ # pip install torch==2.2.2 torchvision==0.17.2 --index-url https://download.pytorch.org/whl/cu118 && \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -d /home/${USERNAME} --shell /usr/bin/bash RUN pip install --no-cache-dir --upgrade pip 'setuptools<70.0.0' && \
# OPTIONAL pip install --no-cache-dir torch==2.1.2+cu118 torchvision==0.16.2+cu118 'numpy<2.0.0' --extra-index-url https://download.pytorch.org/whl/cu118 && \
# If sudo privilages are not required comment below line git clone --branch master --recursive https://github.com/cvg/Hierarchical-Localization.git /opt/hloc && \
# Create simple password for user and add it to sudo group cd /opt/hloc && git checkout v1.4 && python3.10 -m pip install --no-cache-dir . && cd ~ && \
# Update group so that it is not required to type password for commands: apt update/upgrade/install/remove TCNN_CUDA_ARCHITECTURES="${CUDA_ARCHITECTURES}" pip install --no-cache-dir "git+https://github.com/NVlabs/tiny-cuda-nn.git@b3473c81396fe927293bdfd5a6be32df8769927c#subdirectory=bindings/torch" && \
RUN echo "${USERNAME}:password" | chpasswd \ pip install --no-cache-dir pycolmap==0.6.1 pyceres==2.1 omegaconf==2.3.0
&& usermod -aG sudo ${USERNAME} \
&& echo "%sudo ALL=NOPASSWD:/usr/bin/apt-get update, /usr/bin/apt-get upgrade, /usr/bin/apt-get install, /usr/bin/apt-get remove" >> /etc/sudoers
# Create workspace folder and change ownership to new user # Install gsplat and nerfstudio.
RUN mkdir /workspace && chown ${USER_UID}:${USER_GID} /workspace # NOTE: both are installed jointly in order to prevent docker cache with latest
# gsplat version (we do not expliticly specify the commit hash).
#
# We set MAX_JOBS to reduce resource usage for GH actions:
# - https://github.com/nerfstudio-project/gsplat/blob/db444b904976d6e01e79b736dd89a1070b0ee1d0/setup.py#L13-L23
COPY --from=source /tmp/nerfstudio/ /tmp/nerfstudio
RUN export TORCH_CUDA_ARCH_LIST="$(echo "$CUDA_ARCHITECTURES" | tr ';' '\n' | awk '$0 > 70 {print substr($0,1,1)"."substr($0,2)}' | tr '\n' ' ' | sed 's/ $//')" && \
export MAX_JOBS=4 && \
GSPLAT_VERSION="$(sed -n 's/.*gsplat==\s*\([^," '"'"']*\).*/\1/p' /tmp/nerfstudio/pyproject.toml)" && \
pip install --no-cache-dir git+https://github.com/nerfstudio-project/gsplat.git@v${GSPLAT_VERSION} && \
pip install --no-cache-dir /tmp/nerfstudio 'numpy<2.0.0' && \
rm -rf /tmp/nerfstudio
# Switch to new user and workdir. # Fix permissions
USER ${USER_UID} RUN chmod -R go=u /usr/local/lib/python3.10 && \
WORKDIR /home/${USERNAME} chmod -R go=u /build
# Add local user binary folder to PATH variable. #
ENV PATH="${PATH}:/home/${USERNAME}/.local/bin" # Docker runtime stage.
#
FROM nvidia/cuda:${NVIDIA_CUDA_VERSION}-runtime-ubuntu${UBUNTU_VERSION} as runtime
ARG CUDA_ARCHITECTURES
ARG NVIDIA_CUDA_VERSION
ARG UBUNTU_VERSION
# Upgrade pip and install packages. LABEL org.opencontainers.image.source = "https://github.com/nerfstudio-project/nerfstudio"
RUN python3.10 -m pip install --no-cache-dir --upgrade pip setuptools==69.5.1 pathtools promise pybind11 omegaconf LABEL org.opencontainers.image.licenses = "Apache License 2.0"
LABEL org.opencontainers.image.base.name="docker.io/library/nvidia/cuda:${NVIDIA_CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION}"
LABEL org.opencontainers.image.documentation = "https://docs.nerf.studio/"
# Install pytorch and submodules # Minimal dependencies to run COLMAP binary compiled in the builder stage.
# echo "${CUDA_VERSION}" | sed 's/.$//' | tr -d '.' -- CUDA_VERSION -> delete last digit -> delete all '.' # Note: this reduces the size of the final image considerably, since all the
RUN CUDA_VER=$(echo "${CUDA_VERSION}" | sed 's/.$//' | tr -d '.') && python3.10 -m pip install --no-cache-dir \ # build dependencies are not needed.
torch==2.1.2+cu${CUDA_VER} \ RUN apt-get update && \
torchvision==0.16.2+cu${CUDA_VER} \ apt-get install -y --no-install-recommends --no-install-suggests \
--extra-index-url https://download.pytorch.org/whl/cu${CUDA_VER} libboost-filesystem1.74.0 \
libboost-program-options1.74.0 \
libc6 \
libceres2 \
libfreeimage3 \
libgcc-s1 \
libgl1 \
libglew2.2 \
libgoogle-glog0v5 \
libqt5core5a \
libqt5gui5 \
libqt5widgets5 \
python3.10 \
python3.10-dev \
build-essential \
python-is-python3 \
ffmpeg
# Install tiny-cuda-nn (we need to set the target architectures as environment variable first). # Copy packages from builder stage.
ENV TCNN_CUDA_ARCHITECTURES=${CUDA_ARCHITECTURES} COPY --from=builder /build/colmap/ /usr/local/
RUN python3.10 -m pip install --no-cache-dir git+https://github.com/NVlabs/tiny-cuda-nn.git#subdirectory=bindings/torch COPY --from=builder /usr/local/lib/python3.10/dist-packages/ /usr/local/lib/python3.10/dist-packages/
COPY --from=builder /usr/local/bin/ns* /usr/local/bin/
# Install pycolmap, required by hloc. # Install nerfstudio cli auto completion
RUN git clone --branch v0.4.0 --recursive https://github.com/colmap/pycolmap.git && \ RUN /bin/bash -c 'ns-install-cli --mode install'
cd pycolmap && \
python3.10 -m pip install --no-cache-dir . && \
cd ..
# Install hloc 1.4 as alternative feature detector and matcher option for nerfstudio.
RUN git clone --branch master --recursive https://github.com/cvg/Hierarchical-Localization.git && \
cd Hierarchical-Localization && \
git checkout v1.4 && \
python3.10 -m pip install --no-cache-dir -e . && \
cd ..
# Install pyceres from source
RUN git clone --branch v1.0 --recursive https://github.com/cvg/pyceres.git && \
cd pyceres && \
python3.10 -m pip install --no-cache-dir -e . && \
cd ..
# Install pixel perfect sfm.
RUN git clone --recursive https://github.com/cvg/pixel-perfect-sfm.git && \
cd pixel-perfect-sfm && \
git reset --hard 40f7c1339328b2a0c7cf71f76623fb848e0c0357 && \
git clean -df && \
python3.10 -m pip install --no-cache-dir -e . && \
cd ..
# Copy nerfstudio folder and give ownership to user.
COPY --chown=${USER_UID}:${USER_GID} . /home/${USERNAME}/nerfstudio
# Install nerfstudio dependencies.
RUN cd nerfstudio && \
python3.10 -m pip install --no-cache-dir -e . && \
cd ..
# Switch to workspace folder and install nerfstudio cli auto completion
WORKDIR /workspace
RUN ns-install-cli --mode install
# Bash as default entrypoint. # Bash as default entrypoint.
CMD /bin/bash -l CMD /bin/bash -l
# Force changing password on first container run
# Change line above: CMD /bin/bash -l -> CMD /bin/bash -l -c passwd && /usr/bin/bash -l
+4 -15
View File
@@ -222,10 +222,10 @@ Instead of installing and compiling prerequisites, setting up the environment an
### Prerequisites ### Prerequisites
Docker ([get docker](https://docs.docker.com/get-docker/)) and nvidia GPU drivers ([get nvidia drivers](https://www.nvidia.de/Download/index.aspx?lang=de)), capable of working with CUDA 11.8, must be installed. Docker ([get docker](https://docs.docker.com/get-docker/)) and nvidia GPU drivers ([get nvidia drivers](https://www.nvidia.de/Download/index.aspx?lang=de)), capable of working with CUDA 11.8, must be installed.
The docker image can then either be pulled from [here](https://hub.docker.com/r/dromni/nerfstudio/tags) (replace <version> with the actual version, e.g. 0.1.18) The docker image can then either be pulled from [here](https://github.com/nerfstudio-project/nerfstudio/pkgs/container/nerfstudio) (`latest` can be replaced with a fixed version, e.g., `1.1.3`)
```bash ```bash
docker pull dromni/nerfstudio:<version> docker pull ghcr.io/nerfstudio-project/nerfstudio:latest
``` ```
or be built from the repository using or be built from the repository using
@@ -240,22 +240,11 @@ For example, here's how to build with support for GeForce 30xx series GPUs:
```bash ```bash
docker build \ docker build \
--build-arg CUDA_VERSION=11.8.0 \
--build-arg CUDA_ARCHITECTURES=86 \ --build-arg CUDA_ARCHITECTURES=86 \
--build-arg OS_VERSION=22.04 \
--tag nerfstudio-86 \ --tag nerfstudio-86 \
--file Dockerfile . --file Dockerfile .
``` ```
The user inside the container is called 'user' and is mapped to the local user with ID 1000 (usually the first non-root user on Linux systems).
If you suspect that your user might have a different id, override `USER_ID` during the build as follows:
```bash
docker build \
--build-arg USER_ID=$(id -u) \
--file Dockerfile .
```
### Using an interactive container ### Using an interactive container
The docker container can be launched with an interactive terminal where nerfstudio commands can be entered as usual. Some parameters are required and some are strongly recommended for usage as following: The docker container can be launched with an interactive terminal where nerfstudio commands can be entered as usual. Some parameters are required and some are strongly recommended for usage as following:
@@ -269,7 +258,7 @@ docker run --gpus all \ # Give the conta
--rm \ # Remove container after it is closed (recommended). --rm \ # Remove container after it is closed (recommended).
-it \ # Start container in interactive mode. -it \ # Start container in interactive mode.
--shm-size=12gb \ # Increase memory assigned to container to avoid memory limitations, default is 64 MB (recommended). --shm-size=12gb \ # Increase memory assigned to container to avoid memory limitations, default is 64 MB (recommended).
dromni/nerfstudio:<tag> # Docker image name if you pulled from docker hub. ghcr.io/nerfstudio-project/nerfstudio:<tag> # Docker image name if you pulled from GitHub.
<--- OR ---> <--- OR --->
nerfstudio # Docker image tag if you built the image from the Dockerfile by yourself using the command from above. nerfstudio # Docker image tag if you built the image from the Dockerfile by yourself using the command from above.
``` ```
@@ -280,7 +269,7 @@ Besides, the container can also directly be used by adding the nerfstudio comman
```bash ```bash
docker run --gpus all -u $(id -u) -v /folder/of/your/data:/workspace/ -v /home/<YOUR_USER>/.cache/:/home/user/.cache/ -p 7007:7007 --rm -it --shm-size=12gb # Parameters. docker run --gpus all -u $(id -u) -v /folder/of/your/data:/workspace/ -v /home/<YOUR_USER>/.cache/:/home/user/.cache/ -p 7007:7007 --rm -it --shm-size=12gb # Parameters.
dromni/nerfstudio:<tag> \ # Docker image name ghcr.io/nerfstudio-project/nerfstudio:<tag> \ # Docker image name if you pulled from GitHub.
ns-process-data video --data /workspace/video.mp4 # Smaple command of nerfstudio. ns-process-data video --data /workspace/video.mp4 # Smaple command of nerfstudio.
``` ```