Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPU support #45

Merged
merged 22 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ set(GPU_RUNTIME "CUDA" CACHE STRING "HIP or CUDA")
set(OPENCV_DIR "OPENCV_DIR-NOTFOUND" CACHE PATH "Path to the OPENCV installation directory")

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()

enable_language(${GPU_RUNTIME})
set(CMAKE_${GPU_RUNTIME}_STANDARD 17)
set(${GPU_RUNTIME}_STANDARD 17)

if(GPU_RUNTIME STREQUAL "CUDA")
set(CMAKE_CUDA_ARCHITECTURES 70 75)
find_package(CUDAToolkit REQUIRED)
else()
find_package(CUDAToolkit)
if (CUDAToolkit-NOTFOUND)
message(WARNING "CUDA toolkit not found, building with CPU support only")
set(GPU_RUNTIME "CPU")
endif()
elseif(GPU_RUNTIME STREQUAL "HIP")
set(USE_HIP ON CACHE BOOL "Use HIP for GPU acceleration")

if(NOT DEFINED HIP_PATH)
Expand All @@ -40,6 +40,13 @@ else()
list(APPEND CMAKE_PREFIX_PATH "${ROCM_ROOT}")
endif()

set(CMAKE_CXX_STANDARD 17)
if((GPU_RUNTIME STREQUAL "CUDA") OR (GPU_RUNTIME STREQUAL "HIP"))
enable_language(${GPU_RUNTIME})
set(CMAKE_${GPU_RUNTIME}_STANDARD 17)
set(${GPU_RUNTIME}_STANDARD 17)
endif()

if (NOT WIN32 AND NOT APPLE)
set(STDPPFS_LIBRARY stdc++fs)
endif()
Expand All @@ -52,38 +59,48 @@ if (NOT WIN32 AND NOT APPLE)
endif()
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_calib3d)

add_library(gsplat vendor/gsplat/forward.cu vendor/gsplat/backward.cu vendor/gsplat/bindings.cu vendor/gsplat/helpers.cuh)
if(GPU_RUNTIME STREQUAL "CUDA")
set(GPU_LIBRARIES "cuda")
target_link_libraries(gsplat PUBLIC cuda)
else(GPU_RUNTIME STREQUAL "HIP")
set(GPU_INCLUDE_DIRS "${ROCM_ROOT}/include")
target_compile_definitions(gsplat PRIVATE USE_HIP __HIP_PLATFORM_AMD__)
set(GSPLAT_LIBS gsplat_cpu)
if((GPU_RUNTIME STREQUAL "CUDA") OR (GPU_RUNTIME STREQUAL "HIP"))
add_library(gsplat vendor/gsplat/forward.cu vendor/gsplat/backward.cu vendor/gsplat/bindings.cu vendor/gsplat/helpers.cuh)
list(APPEND GSPLAT_LIBS gsplat)
if(GPU_RUNTIME STREQUAL "CUDA")
set(GPU_LIBRARIES "cuda")
target_link_libraries(gsplat PUBLIC cuda)
set_target_properties(gsplat PROPERTIES CUDA_ARCHITECTURES "70;75")
else(GPU_RUNTIME STREQUAL "HIP")
set(GPU_INCLUDE_DIRS "${ROCM_ROOT}/include")
target_compile_definitions(gsplat PRIVATE USE_HIP __HIP_PLATFORM_AMD__)
endif()
target_include_directories(gsplat PRIVATE
${PROJECT_SOURCE_DIR}/vendor/glm
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
${TORCH_INCLUDE_DIRS}
)
set_target_properties(gsplat PROPERTIES LINKER_LANGUAGE CXX)
endif()

target_include_directories(gsplat PRIVATE
${PROJECT_SOURCE_DIR}/vendor/glm
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
${TORCH_INCLUDE_DIRS}
)
set_target_properties(gsplat PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(gsplat PROPERTIES CUDA_ARCHITECTURES "70;75")
add_library(gsplat_cpu vendor/gsplat-cpu/gsplat_cpu.cpp)
target_include_directories(gsplat_cpu PRIVATE ${TORCH_INCLUDE_DIRS})

add_executable(opensplat opensplat.cpp point_io.cpp nerfstudio.cpp model.cpp kdtree_tensor.cpp spherical_harmonics.cpp cv_utils.cpp utils.cpp project_gaussians.cpp rasterize_gaussians.cpp ssim.cpp optim_scheduler.cpp colmap.cpp input_data.cpp tensor_math.cpp)
set_property(TARGET opensplat PROPERTY CXX_STANDARD 17)
target_include_directories(opensplat PRIVATE ${PROJECT_SOURCE_DIR}/vendor/glm ${GPU_INCLUDE_DIRS})
target_link_libraries(opensplat PUBLIC ${STDPPFS_LIBRARY} ${GPU_LIBRARIES} gsplat ${TORCH_LIBRARIES} ${OpenCV_LIBS})
target_link_libraries(opensplat PUBLIC ${STDPPFS_LIBRARY} ${GPU_LIBRARIES} ${GSPLAT_LIBS} ${TORCH_LIBRARIES} ${OpenCV_LIBS})
if(GPU_RUNTIME STREQUAL "HIP")
target_compile_definitions(opensplat PRIVATE USE_HIP __HIP_PLATFORM_AMD__)
elseif(GPU_RUNTIME STREQUAL "CUDA")
target_compile_definitions(opensplat PRIVATE USE_CUDA)
endif()

if(OPENSPLAT_BUILD_SIMPLE_TRAINER)
add_executable(simple_trainer simple_trainer.cpp project_gaussians.cpp rasterize_gaussians.cpp)
add_executable(simple_trainer simple_trainer.cpp project_gaussians.cpp rasterize_gaussians.cpp cv_utils.cpp)
target_include_directories(simple_trainer PRIVATE ${PROJECT_SOURCE_DIR}/vendor/glm ${GPU_INCLUDE_DIRS})
target_link_libraries(simple_trainer PUBLIC ${GPU_LIBRARIES} gsplat ${TORCH_LIBRARIES} ${OpenCV_LIBS})
target_link_libraries(simple_trainer PUBLIC ${GPU_LIBRARIES} ${GSPLAT_LIBS} ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET simple_trainer PROPERTY CXX_STANDARD 17)
if(GPU_RUNTIME STREQUAL "HIP")
target_compile_definitions(simple_trainer PRIVATE USE_HIP __HIP_PLATFORM_AMD__)
elseif(GPU_RUNTIME STREQUAL "CUDA")
target_compile_definitions(simple_trainer PRIVATE USE_CUDA)
endif()
endif()

Expand Down
57 changes: 43 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@ A free and open source implementation of 3D gaussian splatting written in C++, f

OpenSplat takes camera poses + sparse points in [COLMAP](https://colmap.github.io/) or [nerfstudio](https://docs.nerf.studio/quickstart/custom_dataset.html) project format and computes a [scene file](https://drive.google.com/file/d/1w-CBxyWNXF3omA8B_IeOsRmSJel3iwyr/view?usp=sharing) (.ply) that can be later imported for viewing, editing and rendering in other [software](https://github.com/MrNeRF/awesome-3D-gaussian-splatting?tab=readme-ov-file#open-source-implementations).

Graphics card recommended, but not required! OpenSplat runs the fastest on NVIDIA and AMD GPUs, but can also run entirely on CPU power (~100x slower).

Commercial use allowed and encouraged under the terms of the [AGPLv3](https://www.tldrlegal.com/license/gnu-affero-general-public-license-v3-agpl-3-0). ✅

## Build (CUDA)
## Build

Requirements:

* **CUDA**: Make sure you have the CUDA compiler (`nvcc`) in your PATH and that `nvidia-smi` is working. https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
* **libtorch**: Visit https://pytorch.org/get-started/locally/ and select your OS, for package select "LibTorch". Make sure to match your version of CUDA if you want to leverage GPU support in libtorch.
* **OpenCV**: `sudo apt install libopencv-dev` should do it.
* **libtorch**: See instructions below

### CPU

**libtorch**: Visit https://pytorch.org/get-started/locally/ and select your OS, for package select "LibTorch". For compute platform you can select "CPU".

Then:

```bash
git clone https://github.com/pierotofy/OpenSplat OpenSplat
cd OpenSplat
mkdir build && cd build
cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch/ .. && make -j$(nproc)
```

### CUDA

Additional requirement:

* **CUDA**: Make sure you have the CUDA compiler (`nvcc`) in your PATH and that `nvidia-smi` is working. https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html

**libtorch**: Visit https://pytorch.org/get-started/locally/ and select your OS, for package select "LibTorch". Make sure to match your version of CUDA if you want to leverage GPU support in libtorch.

Then:

Expand All @@ -27,12 +49,13 @@ Requirements:

The software has been tested on Ubuntu 20.04 and Windows. With some changes it could run on macOS (help us by opening a PR?).

## Build (ROCm via HIP)
Requirements:
### ROCm via HIP

Additional requirement:

* **ROCm**: Make sure you have the ROCm installed at /opt/rocm. https://rocm.docs.amd.com/projects/install-on-linux/en/latest/tutorial/quick-start.html
* **libtorch**: Visit https://pytorch.org/get-started/locally/ and select your OS, for package select "LibTorch". Make sure to match your version of ROCm (5.7) if you want to leverage AMD GPU support in libtorch.
* **OpenCV**: `sudo apt install libopencv-dev` should do it.

**libtorch**: Visit https://pytorch.org/get-started/locally/ and select your OS, for package select "LibTorch". Make sure to match your version of ROCm (5.7) if you want to leverage AMD GPU support in libtorch.

Then:

Expand All @@ -44,13 +67,18 @@ Then:
cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch/ -DGPU_RUNTIME="HIP" -DHIP_ROOT_DIR=/opt/rocm -DOPENSPLAT_BUILD_SIMPLE_TRAINER=ON ..
make
```

In addition, you can leverage Jinja to build the project
```
cmake -GNinja -DCMAKE_PREFIX_PATH=/path/to/libtorch/ -DGPU_RUNTIME="HIP" -DHIP_ROOT_DIR=/opt/rocm -DOPENSPLAT_BUILD_SIMPLE_TRAINER=ON ..
jinja
```

## Docker Build (CUDA)
```bash
cmake -GNinja -DCMAKE_PREFIX_PATH=/path/to/libtorch/ -DGPU_RUNTIME="HIP" -DHIP_ROOT_DIR=/opt/rocm -DOPENSPLAT_BUILD_SIMPLE_TRAINER=ON ..
jinja
```

## Docker Build

### CUDA

Navigate to the root directory of OpenSplat repo that has Dockerfile and run the following command to build the Docker image:

```bash
Expand All @@ -70,7 +98,8 @@ docker build \
--build-arg CMAKE_BUILD_TYPE=Release .
```

## Docker Build (ROCm via HIP)
### ROCm via HIP

Navigate to the root directory of OpenSplat repo that has Dockerfile and run the following command to build the Docker image:
```bash
docker build \
Expand Down Expand Up @@ -138,11 +167,11 @@ cd /code/build
We recently released OpenSplat, so there's lots of work to do.

* Support for running on AMD cards (more testing needed)
* Support for running on CPU-only
* Improve speed / reduce memory usage
* Distributed computation using multiple machines
* Real-time training viewer output
* Compressed scene outputs
* Automatic filtering
* Your ideas?

https://github.com/pierotofy/OpenSplat/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement
Expand Down
12 changes: 12 additions & 0 deletions gsplat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef GSPLAT_H
#define GSPLAT_H

#include "vendor/gsplat/config.h"

#if defined(USE_HIP) || defined(USE_CUDA)
#include "vendor/gsplat/bindings.h"
#endif

#include "vendor/gsplat-cpu/bindings.h"

#endif
Loading