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

[Fix] Add isolated option for TorchScript SDK backend #1002

Merged
merged 3 commits into from
Sep 6, 2022
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
32 changes: 18 additions & 14 deletions csrc/mmdeploy/net/torchscript/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

project(mmdeploy_torch_net)

find_package(Torch REQUIRED)
find_package(TorchVision)
option(MMDEPLOY_TORCHSCRIPT_SDK_BACKEND "Build TorchScript SDK backend" OFF)
lzhangzz marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can MMDEPLOY_BUILD_SDK=ON and MMDEPLOY_TARGET_BACKENDS="torchscript" be enough?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you just want the converter to work with torchscript (for optimization passes), this would introduce the chaos of different C++ ABIs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only converter is concerned, we can disable MMDEPLOY_BUILD_SDK

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I need to use converter with optimizer to convert TensorRT models and use SDK with TensorRT backend?


mmdeploy_add_module(${PROJECT_NAME} torch_net.cpp)
if (MMDEPLOY_TORCHSCRIPT_SDK_BACKEND)
find_package(Torch REQUIRED)
find_package(TorchVision QUIET)

target_link_libraries(${PROJECT_NAME} PRIVATE
${TORCH_LIBRARIES})
mmdeploy_add_module(${PROJECT_NAME} torch_net.cpp)

target_link_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${Torch_DIR}/../../../lib>)
target_link_libraries(${PROJECT_NAME} PRIVATE
${TORCH_LIBRARIES})

target_link_libraries(${PROJECT_NAME} PRIVATE
mmdeploy_torchscript_ops_obj)
target_link_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${Torch_DIR}/../../../lib>)

if (TorchVision_FOUND)
target_link_libraries(${PROJECT_NAME} PRIVATE TorchVision::TorchVision)
target_compile_definitions(${PROJECT_NAME} PRIVATE -DMMDEPLOY_USE_TORCHVISION=1)
endif ()
target_link_libraries(${PROJECT_NAME} PRIVATE
mmdeploy_torchscript_ops_obj)

if (TorchVision_FOUND)
target_link_libraries(${PROJECT_NAME} PRIVATE TorchVision::TorchVision)
target_compile_definitions(${PROJECT_NAME} PRIVATE -DMMDEPLOY_USE_TORCHVISION=1)
endif ()

add_library(mmdeploy::torch_net ALIAS ${PROJECT_NAME})
add_library(mmdeploy::torch_net ALIAS ${PROJECT_NAME})
endif ()
8 changes: 8 additions & 0 deletions docs/en/05-supported-backends/torchscript.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ make -j$(nproc)

- You could follow the instructions of tutorial [How to convert model](../02-how-to-run/convert_model.md)

## SDK backend

TorchScript SDK backend may be built by passing `-DMMDEPLOY_TORCHSCRIPT_SDK_BACKEND=ON` to `cmake`.

Notice that `libtorch` is sensitive to C++ ABI versions. On platforms defaulted to C++11 ABI (e.g. Ubuntu 16+) one may
pass `-DCMAKE_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=0"` to `cmake` to use pre-C++11 ABI for building. In this case all
dependencies with ABI sensitive interfaces (e.g. OpenCV) must be built with pre-C++11 ABI.

## FAQs

- Error: `projects/thirdparty/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:96 (message):Your installed Caffe2 version uses cuDNN but I cannot find the cuDNN libraries. Please set the proper cuDNN prefixes and / or install cuDNN.`
Expand Down
8 changes: 8 additions & 0 deletions docs/zh_cn/05-supported-backends/torchscript.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ make -j$(nproc)

- You could follow the instructions of tutorial [How to convert model](../02-how-to-run/convert_model.md)

## SDK backend

TorchScript SDK backend may be built by passing `-DMMDEPLOY_TORCHSCRIPT_SDK_BACKEND=ON` to `cmake`.

Notice that `libtorch` is sensitive to C++ ABI versions. On platforms defaulted to C++11 ABI (e.g. Ubuntu 16+) one may
pass `-DCMAKE_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=0"` to `cmake` to use pre-C++11 ABI for building. In this case all
dependencies with ABI sensitive interfaces (e.g. OpenCV) must be built with pre-C++11 ABI.

## FAQs

- Error: `projects/thirdparty/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:96 (message):Your installed Caffe2 version uses cuDNN but I cannot find the cuDNN libraries. Please set the proper cuDNN prefixes and / or install cuDNN.`
Expand Down