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

[Backend] Integrate Paddle to ml module #7028

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
120 changes: 120 additions & 0 deletions 3rdparty/cmake/FindPaddle.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Find the Paddle root and use the provided cmake module
# The following variables will be set:
# - Paddle_FOUND
# - Paddle_VERSION
# - Paddle_ROOT
# - Paddle_DEFINITIONS
#
# - PADDLE_FOUND
# - PADDLE_INCLUDE_DIRS
# - PADDLE_LIBRARY_DIRS
# - PADDLE_LIBRARIES
# - PADDLE_CXX_FLAGS
#
# and import the target 'paddle'.

if(NOT Paddle_FOUND)
# Searching for Paddle requires the python executable
if (NOT Python3_EXECUTABLE)
message(FATAL_ERROR "Python 3 not found in top level file")
endif()

if(BUILD_CUDA_MODULE)
find_package(CUDAToolkit REQUIRED)
string(SUBSTRING ${CUDAToolkit_VERSION} 0 4 CUDA_VERSION)
endif()

message(STATUS "Getting Paddle properties ...")

set(Paddle_FETCH_PROPERTIES
"import os"
"import paddle"
"import sysconfig"
"print(paddle.__version__, end=';')"
"print(os.path.dirname(paddle.__file__), end=';')"
"print(sysconfig.get_path('include', scheme='posix_prefix'), end=';')"
)
execute_process(
COMMAND ${Python3_EXECUTABLE} "-c" "${Paddle_FETCH_PROPERTIES}"
OUTPUT_VARIABLE Paddle_PROPERTIES
)


list(GET Paddle_PROPERTIES 0 Paddle_VERSION)
list(GET Paddle_PROPERTIES 1 Paddle_ROOT)
list(GET Paddle_PROPERTIES 2 Python_INCLUDE)

set(Paddle_CXX11_ABI True)

unset(Paddle_FETCH_PROPERTIES)
unset(Paddle_PROPERTIES)

add_library(paddle STATIC IMPORTED)

# handle include directories
set(PADDLE_INCLUDE_DIRS)
list(APPEND PADDLE_INCLUDE_DIRS "${Paddle_ROOT}/include")
list(APPEND PADDLE_INCLUDE_DIRS "${Paddle_ROOT}/include/third_party")
list(APPEND PADDLE_INCLUDE_DIRS "${Python_INCLUDE}")

if(BUILD_CUDA_MODULE)
list(APPEND PADDLE_INCLUDE_DIRS "${CUDAToolkit_INCLUDE_DIRS}")
endif()

# handle library directories
set(PADDLE_LIBRARY_DIRS)
list(APPEND PADDLE_LIBRARY_DIRS "${Paddle_ROOT}/libs")
list(APPEND PADDLE_LIBRARY_DIRS "${Paddle_ROOT}/base")

if(BUILD_CUDA_MODULE)
list(APPEND PADDLE_LIBRARY_DIRS "${CUDAToolkit_LIBRARY_DIR}")
endif()

# handle libraries
set(PADDLE_LIBRARIES)
find_library(PADDLE_LIB NAMES paddle PATHS "${Paddle_ROOT}/base")
list(APPEND PADDLE_LIBRARY_DIRS "${PADDLE_LIB}")

if(BUILD_CUDA_MODULE)
find_library(CUDART_LIB NAMES cudart PATHS "${CUDAToolkit_LIBRARY_DIR}")
list(APPEND PADDLE_LIBRARY_DIRS "${CUDART_LIB}")
endif()

# handle compile flags
set(PADDLE_CXX_FLAGS)
if(BUILD_CUDA_MODULE)
set(PADDLE_CXX_FLAGS "-DPADDLE_WITH_CUDA ${PADDLE_CXX_FLAGS}")
endif()

set_target_properties(paddle PROPERTIES
IMPORTED_LOCATION "${PADDLE_LIB}"
)
set_target_properties(paddle PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${PADDLE_INCLUDE_DIRS}"
)
set_property(TARGET paddle PROPERTY INTERFACE_COMPILE_OPTIONS "${PADDLE_CXX_FLAGS}")

set(PADDLE_FOUND True)
endif()

if(PRINT_ONCE)
message(STATUS "Paddle version: ${Paddle_VERSION}")
message(STATUS " root dir: ${Paddle_ROOT}")
message(STATUS " compile flags: ${PADDLE_CXX_FLAGS}")
if (UNIX AND NOT APPLE)
message(STATUS " use cxx11 abi: ${Paddle_CXX11_ABI}")
endif()
foreach(idir ${PADDLE_INCLUDE_DIRS})
message(STATUS " include dirs: ${idir}")
endforeach(idir)
foreach(ldir ${PADDLE_LIBRARY_DIRS})
message(STATUS " library dirs: ${ldir}")
endforeach(ldir)
foreach(lib ${PADDLE_LIBRARIES})
message(STATUS " libraries: ${lib}")
endforeach(lib)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Paddle DEFAULT_MSG Paddle_VERSION
Paddle_ROOT)
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ option(BUILD_AZURE_KINECT "Build support for Azure Kinect sensor" OFF
# ML library options
option(BUILD_TENSORFLOW_OPS "Build ops for TensorFlow" OFF)
option(BUILD_PYTORCH_OPS "Build ops for PyTorch" OFF)
option(BUILD_PADDLE_OPS "Build ops for Paddle" OFF)
option(BUNDLE_OPEN3D_ML "Includes the Open3D-ML repo in the wheel" OFF)

# Release build options
Expand Down Expand Up @@ -288,6 +289,9 @@ endif()
if(BUILD_SYCL_MODULE AND BUILD_PYTORCH_OPS)
message(FATAL_ERROR "BUILD_SYCL_MODULE=ON requires BUILD_PYTORCH_OPS=OFF")
endif()
if(BUILD_SYCL_MODULE AND BUILD_PADDLE_OPS)
message(FATAL_ERROR "BUILD_SYCL_MODULE=ON requires BUILD_PADDLE_OPS=OFF")
endif()
if(BUILD_SYCL_MODULE AND BUILD_CUDA_MODULE)
message(FATAL_ERROR "BUILD_SYCL_MODULE and BUILD_SYCL_MODULE cannot be on at the same time for now.")
endif()
Expand Down
9 changes: 9 additions & 0 deletions cpp/open3d/ml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ if (BUILD_TENSORFLOW_OPS AND WIN32)
# see https://github.com/tensorflow/custom-op/issues/24
endif()

if (BUILD_PADDLE_OPS AND (WIN32 OR APPLE))
message(FATAL_ERROR "Building Paddle ops on Windows or MacOS is currently not supported.")
endif()

if (BUILD_TENSORFLOW_OPS)
add_subdirectory(tensorflow)
endif()
Expand All @@ -11,4 +15,9 @@ if (BUILD_PYTORCH_OPS)
add_subdirectory(pytorch)
endif()

if (BUILD_PADDLE_OPS)
add_subdirectory(paddle)
endif()


add_subdirectory(contrib)
119 changes: 119 additions & 0 deletions cpp/open3d/ml/paddle/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
if(BUILD_CUDA_MODULE)
message(STATUS "Building Paddle ops with CUDA")
else()
message(STATUS "Building Paddle ops")
endif()

set(PRINT_ONCE ON)
find_package(Paddle REQUIRED)

if (Paddle_VERSION VERSION_LESS 3.0.0)
message(FATAL_ERROR "Please update to Paddle 3.0.0+ to build Paddle Ops.")
endif()

add_library(open3d_paddle_ops SHARED)

target_sources(open3d_paddle_ops PRIVATE
PaddleHelper.cpp
misc/BuildSpatialHashTableOpKernel.cpp
misc/BuildSpatialHashTableOps.cpp
misc/FixedRadiusSearchOps.cpp
misc/FixedRadiusSearchOpKernel.cpp
misc/RadiusSearchOps.cpp
misc/RadiusSearchOpKernel.cpp
misc/InvertNeighborsListOps.cpp
misc/InvertNeighborsListOpKernel.cpp
misc/KnnSearchOps.cpp
misc/KnnSearchOpKernel.cpp
misc/RaggedToDenseOpKernel.cpp
misc/RaggedToDenseOps.cpp
)

if (BUILD_CUDA_MODULE)
target_sources(open3d_paddle_ops PRIVATE
misc/BuildSpatialHashTableOpKernel.cu
misc/FixedRadiusSearchOpKernel.cu
misc/InvertNeighborsListOpKernel.cu
misc/RaggedToDenseOpKernel.cu
)

endif()

open3d_show_and_abort_on_warning(open3d_paddle_ops)
open3d_set_global_properties(open3d_paddle_ops)

# Set output directory according to architecture (cpu/cuda)
get_target_property(PADDLE_OPS_DIR open3d_paddle_ops LIBRARY_OUTPUT_DIRECTORY)
set(PADDLE_OPS_ARCH_DIR
"${PADDLE_OPS_DIR}/$<IF:$<BOOL:${BUILD_CUDA_MODULE}>,cuda,cpu>")
set_target_properties(open3d_paddle_ops PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${PADDLE_OPS_ARCH_DIR}"
ARCHIVE_OUTPUT_DIRECTORY "${PADDLE_OPS_ARCH_DIR}")

# Do not add "lib" prefix
set_target_properties(open3d_paddle_ops PROPERTIES PREFIX "")
set_target_properties(open3d_paddle_ops PROPERTIES DEBUG_POSTFIX "_debug")

target_include_directories(open3d_paddle_ops SYSTEM PRIVATE
${PROJECT_SOURCE_DIR}/cpp
${PADDLE_INCLUDE_DIRS}
)

target_link_libraries(open3d_paddle_ops PRIVATE
paddle
Open3D::Open3D
Open3D::3rdparty_eigen3
Open3D::3rdparty_fmt
Open3D::3rdparty_nanoflann
TBB::tbb
)
if (TARGET Open3D::3rdparty_parallelstl)
target_link_libraries(open3d_paddle_ops PRIVATE
Open3D::3rdparty_parallelstl
)
endif()
if (TARGET Open3D::3rdparty_onedpl)
target_link_libraries(open3d_paddle_ops PRIVATE
Open3D::3rdparty_onedpl
)
endif()

if (BUILD_CUDA_MODULE)
target_link_libraries(open3d_paddle_ops PRIVATE
Open3D::3rdparty_cutlass
${PADDLE_LIBRARIES}
CUDA::cuda_driver
)

if (TARGET Open3D::3rdparty_cub)
target_link_libraries(open3d_paddle_ops PRIVATE
Open3D::3rdparty_cub
)
endif()
endif()

install(TARGETS open3d_paddle_ops EXPORT Open3DPaddleOps
LIBRARY DESTINATION ${Open3D_INSTALL_LIB_DIR}
)
install(EXPORT Open3DPaddleOps NAMESPACE ${PROJECT_NAME}:: DESTINATION ${Open3D_INSTALL_CMAKE_DIR})

if (BUILD_SHARED_LIBS AND UNIX)
file(CONFIGURE OUTPUT open3d_paddle_ops.pc.in
CONTENT [=[
prefix=${pcfiledir}/../..
libdir=${prefix}/lib
includedir=${prefix}/include/

Name: Open3D Paddle Ops
Description: @PROJECT_DESCRIPTION@ This library contains 3D ML Ops for use with Paddle.
URL: @PROJECT_HOMEPAGE_URL@
Version: @PROJECT_VERSION@
Requires: Open3D = @PROJECT_VERSION@
Cflags:
Libs: -lopen3d_paddle_ops]=] @ONLY NEWLINE_STYLE LF)
file(GENERATE OUTPUT open3d_paddle_ops.pc INPUT
"${CMAKE_CURRENT_BINARY_DIR}/open3d_paddle_ops.pc.in"
TARGET open3d_paddle_ops)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/open3d_paddle_ops.pc"
DESTINATION "${Open3D_INSTALL_LIB_DIR}/pkgconfig")
endif()
42 changes: 42 additions & 0 deletions cpp/open3d/ml/paddle/PaddleHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------

#include "PaddleHelper.h"

paddle::Tensor InitializedEmptyTensor(const phi::DataType dtype,
const phi::IntArray& shape,
const phi::Place& place) {
switch (dtype) {
case phi::DataType::INT8:
return InitializedEmptyTensor<int8_t>(shape, place);
break;
case phi::DataType::UINT8:
return InitializedEmptyTensor<uint8_t>(shape, place);
break;
case phi::DataType::INT16:
return InitializedEmptyTensor<int16_t>(shape, place);
break;
case phi::DataType::FLOAT32:
return InitializedEmptyTensor<float>(shape, place);
break;
case phi::DataType::INT32:
return InitializedEmptyTensor<int>(shape, place);
break;
case phi::DataType::FLOAT64:
return InitializedEmptyTensor<double>(shape, place);
break;
case phi::DataType::INT64:
return InitializedEmptyTensor<int64_t>(shape, place);
break;
default:
PD_CHECK(false,
"Only support phi::DataType as `INT8`, `UINT8`, `INT16`, "
"`FLOAT32`, `FLOAT64`, "
"`INT32` and `INT64` but got %s.",
phi::DataTypeToString(dtype));
}
}
Loading