From ef8df4bf539d9383b56ae415c0605a01c6ac0437 Mon Sep 17 00:00:00 2001 From: "Murillo Rojas, Luis" Date: Fri, 17 Jan 2025 15:17:06 -0600 Subject: [PATCH 1/2] Solve PyTorch Ops compilation error in Windows --- cpp/open3d/ml/impl/continuous_conv/ContinuousConv.h | 6 ++++-- .../ml/impl/continuous_conv/ContinuousConvBackpropFilter.h | 6 ++++-- .../ml/impl/continuous_conv/ContinuousConvTranspose.h | 6 ++++-- .../continuous_conv/ContinuousConvTransposeBackpropFilter.h | 6 ++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cpp/open3d/ml/impl/continuous_conv/ContinuousConv.h b/cpp/open3d/ml/impl/continuous_conv/ContinuousConv.h index 570cdd84730..a0f66ad9bea 100644 --- a/cpp/open3d/ml/impl/continuous_conv/ContinuousConv.h +++ b/cpp/open3d/ml/impl/continuous_conv/ContinuousConv.h @@ -59,6 +59,9 @@ void _CConvComputeFeaturesCPU(TOut* out_features, memset(out_features, 0, sizeof(TOut) * num_out * out_channels); + typedef Eigen::Array Matrix; + typedef Eigen::Array Matrix3C; + tbb::parallel_for( tbb::blocked_range(0, num_out, 32), [&](const tbb::blocked_range& r) { @@ -72,13 +75,12 @@ void _CConvComputeFeaturesCPU(TOut* out_features, in_channels * spatial_filter_size, range_length); B.setZero(); - typedef Eigen::Array Matrix; Matrix infeat(VECSIZE, in_channels); Eigen::Array offsets_(offsets[0], offsets[1], offsets[2]); - Eigen::Array inv_extents; + Matrix3C inv_extents; if (INDIVIDUAL_EXTENT == false) { if (ISOTROPIC_EXTENT) { inv_extents = 1 / extents[0]; diff --git a/cpp/open3d/ml/impl/continuous_conv/ContinuousConvBackpropFilter.h b/cpp/open3d/ml/impl/continuous_conv/ContinuousConvBackpropFilter.h index b75089cd889..42e427424a0 100644 --- a/cpp/open3d/ml/impl/continuous_conv/ContinuousConvBackpropFilter.h +++ b/cpp/open3d/ml/impl/continuous_conv/ContinuousConvBackpropFilter.h @@ -63,6 +63,9 @@ void _CConvBackropFilterCPU(TOut* filter_backprop, memset(filter_backprop, 0, sizeof(TOut) * total_filter_size); std::mutex filter_backprop_mutex; + typedef Eigen::Array Matrix; + typedef Eigen::Array Matrix3C; + tbb::parallel_for( tbb::blocked_range(0, num_out, 32), [&](const tbb::blocked_range& r) { @@ -74,13 +77,12 @@ void _CConvBackropFilterCPU(TOut* filter_backprop, Eigen::Matrix C( out_channels, range_length); - typedef Eigen::Array Matrix; Matrix infeat(VECSIZE, in_channels); Eigen::Array offsets_(offsets[0], offsets[1], offsets[2]); - Eigen::Array inv_extents; + Matrix3C inv_extents; if (INDIVIDUAL_EXTENT == false) { if (ISOTROPIC_EXTENT) { inv_extents = 1 / extents[0]; diff --git a/cpp/open3d/ml/impl/continuous_conv/ContinuousConvTranspose.h b/cpp/open3d/ml/impl/continuous_conv/ContinuousConvTranspose.h index 1a19cdc4034..2e997a747a5 100644 --- a/cpp/open3d/ml/impl/continuous_conv/ContinuousConvTranspose.h +++ b/cpp/open3d/ml/impl/continuous_conv/ContinuousConvTranspose.h @@ -61,6 +61,9 @@ void _CConvTransposeComputeFeaturesCPU( memset(out_features, 0, sizeof(TOut) * num_out * out_channels); + typedef Eigen::Array Matrix; + typedef Eigen::Array Matrix3C; + tbb::parallel_for( tbb::blocked_range(0, num_out, 32), [&](const tbb::blocked_range& r) { @@ -70,13 +73,12 @@ void _CConvTransposeComputeFeaturesCPU( in_channels * spatial_filter_size, range_length); B.setZero(); - typedef Eigen::Array Matrix; Matrix infeat(VECSIZE, in_channels); Eigen::Array offsets_(offsets[0], offsets[1], offsets[2]); - Eigen::Array inv_extents; + Matrix3C inv_extents; if (INDIVIDUAL_EXTENT == false) { if (ISOTROPIC_EXTENT) { inv_extents = 1 / extents[0]; diff --git a/cpp/open3d/ml/impl/continuous_conv/ContinuousConvTransposeBackpropFilter.h b/cpp/open3d/ml/impl/continuous_conv/ContinuousConvTransposeBackpropFilter.h index 296850f24a2..b1cb93c0949 100644 --- a/cpp/open3d/ml/impl/continuous_conv/ContinuousConvTransposeBackpropFilter.h +++ b/cpp/open3d/ml/impl/continuous_conv/ContinuousConvTransposeBackpropFilter.h @@ -64,6 +64,9 @@ void _CConvTransposeBackpropFilterCPU(TOut* filter_backprop, sizeof(TOut) * spatial_filter_size * in_channels * out_channels); std::mutex filter_backprop_mutex; + typedef Eigen::Array Matrix; + typedef Eigen::Array Matrix3C; + tbb::parallel_for( tbb::blocked_range(0, num_out, 32), [&](const tbb::blocked_range& r) { @@ -75,13 +78,12 @@ void _CConvTransposeBackpropFilterCPU(TOut* filter_backprop, Eigen::Matrix C( out_channels, range_length); - typedef Eigen::Array Matrix; Matrix infeat(VECSIZE, in_channels); Eigen::Array offsets_(offsets[0], offsets[1], offsets[2]); - Eigen::Array inv_extents; + Matrix3C inv_extents; if (INDIVIDUAL_EXTENT == false) { if (ISOTROPIC_EXTENT) { inv_extents = 1 / extents[0]; From 57ff4a24ef7cc8c48e51c31383b8b700c26319a8 Mon Sep 17 00:00:00 2001 From: "Murillo Rojas, Luis" Date: Fri, 31 Jan 2025 16:12:16 -0600 Subject: [PATCH 2/2] Fix the python package generation with pytorch ops --- cpp/open3d/ml/pytorch/CMakeLists.txt | 3 ++- cpp/pybind/CMakeLists.txt | 3 ++- cpp/pybind/generate_torch_ops_wrapper.py | 10 ++++++++++ python/open3d/ml/tf/__init__.py | 2 +- python/open3d/ml/torch/__init__.py | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cpp/open3d/ml/pytorch/CMakeLists.txt b/cpp/open3d/ml/pytorch/CMakeLists.txt index 76e40b82791..8969e7d57bd 100644 --- a/cpp/open3d/ml/pytorch/CMakeLists.txt +++ b/cpp/open3d/ml/pytorch/CMakeLists.txt @@ -123,7 +123,8 @@ set(TORCH_OPS_ARCH_DIR "${TORCH_OPS_DIR}/$,cuda,cpu>") set_target_properties(open3d_torch_ops PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${TORCH_OPS_ARCH_DIR}" - ARCHIVE_OUTPUT_DIRECTORY "${TORCH_OPS_ARCH_DIR}") + ARCHIVE_OUTPUT_DIRECTORY "${TORCH_OPS_ARCH_DIR}" + RUNTIME_OUTPUT_DIRECTORY "${TORCH_OPS_ARCH_DIR}") # Do not add "lib" prefix set_target_properties(open3d_torch_ops PROPERTIES PREFIX "") diff --git a/cpp/pybind/CMakeLists.txt b/cpp/pybind/CMakeLists.txt index 6efae9a17fd..faa5a11bfbf 100644 --- a/cpp/pybind/CMakeLists.txt +++ b/cpp/pybind/CMakeLists.txt @@ -169,8 +169,9 @@ endif() # add additional optional compiled modules if (BUILD_PYTORCH_OPS) list( APPEND COMPILED_MODULE_PATH_LIST $ ) + set(DEPENDENCIES_DIR "$") add_custom_command( OUTPUT "${CMAKE_BINARY_DIR}/lib/ml/torch/python/ops.py" "${CMAKE_BINARY_DIR}/lib/ml/torch/python/return_types.py" - COMMAND ${Python3_EXECUTABLE} generate_torch_ops_wrapper.py --input_ops_py_in "${PYTHON_PACKAGE_SRC_DIR}/open3d/ml/torch/python/ops.py.in" --input_return_types_py_in "${PYTHON_PACKAGE_SRC_DIR}/open3d/ml/torch/python/return_types.py.in" --output_dir "${CMAKE_BINARY_DIR}/lib/ml/torch/python/" --lib $ --tensorflow_ops_dir "${CMAKE_CURRENT_SOURCE_DIR}/../open3d/ml/tensorflow" + COMMAND ${Python3_EXECUTABLE} generate_torch_ops_wrapper.py --input_ops_py_in "${PYTHON_PACKAGE_SRC_DIR}/open3d/ml/torch/python/ops.py.in" --input_return_types_py_in "${PYTHON_PACKAGE_SRC_DIR}/open3d/ml/torch/python/return_types.py.in" --output_dir "${CMAKE_BINARY_DIR}/lib/ml/torch/python/" --lib $ --tensorflow_ops_dir "${CMAKE_CURRENT_SOURCE_DIR}/../open3d/ml/tensorflow" --dependencies_dir "${DEPENDENCIES_DIR}" DEPENDS open3d_torch_ops WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMENT "Generating python ops.py and return_types.py" ) diff --git a/cpp/pybind/generate_torch_ops_wrapper.py b/cpp/pybind/generate_torch_ops_wrapper.py index bda52b8fc83..fa3e948af73 100644 --- a/cpp/pybind/generate_torch_ops_wrapper.py +++ b/cpp/pybind/generate_torch_ops_wrapper.py @@ -130,12 +130,22 @@ def main(): type=str, required=True, help="This is cpp/open3d/ml/tensorflow") + parser.add_argument("--dependencies_dir", + type=str, + required=True, + help="PyTorch dependencies directory") args = parser.parse_args() print(args) + if sys.platform == "win32": + open3d_deps = os.add_dll_directory(args.dependencies_dir) + torch.ops.load_library(args.lib) + if sys.platform == "win32": + open3d_deps.close() + generated_function_strs = '' generated_namedtuple_strs = '' for schema in torch._C._jit_get_all_schemas(): diff --git a/python/open3d/ml/tf/__init__.py b/python/open3d/ml/tf/__init__.py index 86ce9dd38fa..f5069efa30f 100644 --- a/python/open3d/ml/tf/__init__.py +++ b/python/open3d/ml/tf/__init__.py @@ -11,7 +11,7 @@ from open3d import _build_config if not _build_config["Tensorflow_VERSION"]: - raise Exception('Open3D was not built with TensorFlow support!') + raise ImportError('Open3D was not built with TensorFlow support!') _o3d_tf_version = _build_config["Tensorflow_VERSION"].split('.') if _tf_version.split('.')[:2] != _o3d_tf_version[:2]: diff --git a/python/open3d/ml/torch/__init__.py b/python/open3d/ml/torch/__init__.py index 09a48b131aa..161420fbcb5 100644 --- a/python/open3d/ml/torch/__init__.py +++ b/python/open3d/ml/torch/__init__.py @@ -12,7 +12,7 @@ from open3d import _build_config if not _build_config["Pytorch_VERSION"]: - raise Exception('Open3D was not built with PyTorch support!') + raise ImportError('Open3D was not built with PyTorch support!') _o3d_torch_version = _verp(_build_config["Pytorch_VERSION"]) # Check match with PyTorch version, any patch level is OK if _verp(_torch.__version__).release[:2] != _o3d_torch_version.release[:2]: