Skip to content

Commit

Permalink
cuda_check builds and works
Browse files Browse the repository at this point in the history
  • Loading branch information
tuas-travis-ci committed Mar 13, 2024
1 parent 1e4bfd4 commit 6f25d5f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 18 deletions.
133 changes: 133 additions & 0 deletions docker/Dockerfile.arm
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
FROM arm64/ubuntu:22.04

ARG USERNAME=tuas USER_UID=1000 USER_GID=1000 DEBIAN_FRONTEND=noninteractive

# Needed to spawn up a GUI in headless mode for matplotplus to work
ENV QT_QPA_PLATFORM="vnc"

# Create a non-root user
RUN groupadd -f --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
# exit 0 ensures that it won't prematurely stop if for some reason the user already exists

# https://gist.github.com/SSARCandy/fc960d8905330ac695e71e3f3807ce3d
# OpenCV dependencies from above
RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \
--mount=target=/var/cache/apt,type=cache,sharing=locked \
rm -f /etc/apt/apt.conf.d/docker-clean \
&& apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y build-essential \
software-properties-common \
sudo \
gdb \
git \
wget \
ccache \
vim \
curl \
unzip \
protobuf-compiler \
libopencv-dev \
# Need these to install Python 3.11 from pyenv
python3-pip \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
libncursesw5-dev \
xz-utils \
tk-dev \
# libxm12-dev \
libxmlsec1-dev \
libffi-dev \
liblzma-dev \
# needed for matplotplus
gnuplot \
# imagemagick with c++ dependency
graphicsmagick-libmagick-dev-compat

RUN apt-add-repository universe
RUN apt-get install -y cpplint

# Update Python to 3.11 so that libtorch can be properly built from source
ENV PYENV_ROOT="/.pyenv"
RUN curl https://pyenv.run | bash
ENV PATH="${PATH}:${PYENV_ROOT}/bin"
RUN eval "$(pyenv init -)"
RUN pyenv install 3.11
RUN pyenv global 3.11
RUN pip3 install typing-extensions PyYAML

RUN echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME

# Download latest CMake from their repositories
RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \
--mount=target=/var/cache/apt,type=cache,sharing=locked \
rm -f /etc/apt/apt.conf.d/docker-clean \
&& apt-get update \
&& wget https://github.com/Kitware/CMake/releases/download/v3.27.7/cmake-3.27.7-linux-x86_64.sh \
-q -O /tmp/cmake-install.sh \
&& chmod u+x /tmp/cmake-install.sh \
&& mkdir /opt/cmake-3.24.1 \
&& /tmp/cmake-install.sh --skip-license --prefix=/opt/cmake-3.24.1 \
&& rm /tmp/cmake-install.sh \
&& ln -s /opt/cmake-3.24.1/bin/* /usr/local/bin

# install prebuilt MAVSDK to system
# https://github.com/mavlink/MAVSDK/releases
ARG MAVSDK_VERSION=1.4.17
ARG MAVSDK_DEB=libmavsdk-dev_${MAVSDK_VERSION}_debian11_arm64.deb
RUN wget https://github.com/mavlink/MAVSDK/releases/download/v${MAVSDK_VERSION}/${MAVSDK_DEB} \
&& dpkg -i ${MAVSDK_DEB} \
&& rm ${MAVSDK_DEB}

# the official docs say also these
# https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html
# cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
# python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

# pull pre-built libtorch
ARG LIBTORCH_VERSION=2.1.0
ARG LIBTORCH_INSTALL_DIR=/libtorch-tmp
WORKDIR ${LIBTORCH_INSTALL_DIR}
RUN git clone -b main --recurse-submodule https://github.com/pytorch/pytorch.git
RUN mkdir pytorch-build
RUN cd pytorch-build
RUN cmake -D_GLIBCXX_USE_CXX11_ABI=1 -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_BUILD_TYPE:STRING=Release -DPYTHON_EXECUTABLE:PATH=`which python3` -DCMAKE_PREFIX_PATH=../pytorch-install ${LIBTORCH_INSTALL_DIR}/pytorch
RUN cmake --build . --target install

# RUN wget "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip" \
# && unzip "libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}+cpu.zip"
# needed to build torchvision below and to build targets that use libtorch inside the container
ENV CMAKE_PREFIX_PATH="${LIBTORCH_INSTALL_DIR}/libtorch"

# pull and build torchvision
# refer to this page for version compatibilty with pytorch (libtorch) https://github.com/pytorch/pytorch/wiki/PyTorch-Versions
ARG TORCHVISION_VERSION=0.16.0
ARG TORCHVISION_INSTALL_DIR=/torchvision-tmp
WORKDIR ${TORCHVISION_INSTALL_DIR}
RUN wget "https://github.com/pytorch/vision/archive/refs/tags/v${TORCHVISION_VERSION}.zip" \
&& unzip "v${TORCHVISION_VERSION}.zip" \
&& mkdir -p "${TORCHVISION_INSTALL_DIR}/vision-${TORCHVISION_VERSION}/build" \
&& cd "${TORCHVISION_INSTALL_DIR}/vision-${TORCHVISION_VERSION}/build" \
&& cmake -DWITH_CUDA=off -D_GLIBCXX_USE_CXX11_ABI=1 -DCMAKE_BUILD_TYPE=Release .. \
&& make -j2 \
&& make install

WORKDIR /obcpp
COPY . .

RUN rm -rf /obcpp/build
WORKDIR /obcpp/build
ENV CMAKE_PREFIX_PATH="/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Torch;/usr/local/share/cmake/TorchVision"
RUN GITHUB_ACTIONS=true cmake -DCMAKE_PREFIX_PATH="/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Torch;/usr/local/share/cmake/TorchVision" -DCMAKE_MODULE_PATH="/usr/local/share/cmake/TorchVision" -DCMAKE_BUILD_TYPE="Debug" ..

RUN make cuda_check VERBOSE=1

# login as non-root user
# USER $USERNAME

CMD [ "/obcpp/build/bin/obcpp" ]
45 changes: 28 additions & 17 deletions docker/Dockerfile.nvidia
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \
# needed for matplotplus
gnuplot \
# imagemagick with c++ dependency
#graphicsmagick-libmagick-dev-compat \
libmagick++-dev \
#graphicsmagick-libmagick-dev-compat libgraphicsmagick++1-dev libmagick++-6-headers \
libmagick++-dev \
# needed for pytorch
libopenblas-dev

Expand Down Expand Up @@ -119,38 +119,49 @@ ARG JETPACK_VERSION=512
# RUN wget "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip" \
# && unzip "libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}+cpu.zip"
# needed to build torchvision below and to build targets that use libtorch inside the container
ENV CMAKE_PREFIX_PATH="${LIBTORCH_INSTALL_DIR}/libtorch"
#ENV CMAKE_PREFIX_PATH="${LIBTORCH_INSTALL_DIR}/libtorch"

# pull and build torchvision
# refer to this page for version compatibilty with pytorch (libtorch) https://github.com/pytorch/pytorch/wiki/PyTorch-Versions
ARG TORCHVISION_VERSION=0.16.0
ARG TORCHVISION_INSTALL_DIR=/torchvision-tmp
#ARG TORCHVISION_INSTALL_DIR=/torchvision-tmp
WORKDIR ${TORCHVISION_INSTALL_DIR}
RUN wget "https://github.com/pytorch/vision/archive/refs/tags/v${TORCHVISION_VERSION}.zip" \
&& unzip "v${TORCHVISION_VERSION}.zip" \
#&& python setup.py install
&& mkdir -p "${TORCHVISION_INSTALL_DIR}/vision-${TORCHVISION_VERSION}/build" \
&& cd "${TORCHVISION_INSTALL_DIR}/vision-${TORCHVISION_VERSION}/build" \
&& cmake -DWITH_CUDA=off -D_GLIBCXX_USE_CXX11_ABI=1 -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Torch" .. \
&& cd vision-0.16.0 \
&& mkdir build \
&& cd build \
&& cmake -DWITH_CUDA=1 -DCUDA_HAS_FP16=1 -DCUDA_NO_HALF_OPERATORS=1 -DCUDA_NO_HALF_CONVERSIONS=1 -DCUDA_NO_HALF2_OPERATORS=1 -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Torch" .. \
&& make -j2 \
&& make install

WORKDIR /obcpp
COPY . .
#&& WITH_CUDA=1 python3 setup.py install

#&& mkdir -p "${TORCHVISION_INSTALL_DIR}/vision-${TORCHVISION_VERSION}/build" \
#&& cd "${TORCHVISION_INSTALL_DIR}/vision-${TORCHVISION_VERSION}/build" \
#&& cmake -DWITH_CUDA=1 -DCUDA_HAS_FP16=1 -D__CUDA_NO_HALF_OPERATORS__=1 -D__CUDA_NO_HALF_CONVERSIONS__=1 -D__CUDA_NO_HALF2_OPERATORS__=1 -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Torch" .. \
#&& make -j2 \
#&& make install

# Install g++10 and replace the older version. For some reason some c++ 20 features aren't working with g++9 even though
# we have CMake configured to use c++ 20 https://stackoverflow.com/questions/69037873/why-am-i-missing-c20-headers-and-how-do-i-fix-this
RUN apt-get update && apt-get install -y g++-10
RUN apt-get update && apt-get install -y g++-10 gcc-10
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
RUN update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10
RUN update-alternatives --set gcc /usr/bin/gcc-10
RUN update-alternatives --set g++ /usr/bin/g++-10

WORKDIR /obcpp
COPY . .

RUN rm -rf /obcpp/build
WORKDIR /obcpp/build
RUN GITHUB_ACTIONS=true cmake -DCMAKE_PREFIX_PATH="/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Torch" ..
RUN make VERBOSE=1
ENV CMAKE_PREFIX_PATH="/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Torch;/usr/local/share/cmake/TorchVision"
RUN GITHUB_ACTIONS=true cmake -DCMAKE_PREFIX_PATH="/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Torch;/usr/local/share/cmake/TorchVision" -DCMAKE_MODULE_PATH="/usr/local/share/cmake/TorchVision" -DCMAKE_BUILD_TYPE="Debug" ..

# login as non-root user
USER $USERNAME
RUN make load_torchvision_model VERBOSE=1

#RUN pip3 install cpplint
#ENV PATH="${PATH}:~/.local/bin/"
# login as non-root user
# USER $USERNAME

CMD [ "/obcpp/build/bin/obcpp" ]
6 changes: 6 additions & 0 deletions docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ all:

build-jetson-image:
DOCKER_BUILDKIT=1 docker build --tag tritonuas/obcpp:nvidia --file Dockerfile.nvidia ..

build-arm-image:
DOCKER_BUILDKIT=1 docker build --tag tritonuas/obcpp:arm --file Dockerfile.arm ..

run-jetson-cuda-check:
docker run -it --rm --runtime nvidia -i tritonuas/obcpp:nvidia /obcpp/build/bin/cuda_check
2 changes: 2 additions & 0 deletions tests/integration/cuda_check.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <torch/torch.h>

int main() {
std::cout << "Have CUDA? " << torch::cuda::is_available() << std::endl;
}
7 changes: 6 additions & 1 deletion tests/integration/load_torchvision_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ int main (int argc, char *argv[]) {
return 1;
}

if (torch::cuda::is_available()) {
module.to(torch::kCUDA);
std::cout << "successfully moved model to GPU (CUDA)" << std::endl;
}

std::cout << "loaded model without crashing" << std::endl;
return 0;
}
}

0 comments on commit 6f25d5f

Please sign in to comment.