-
Notifications
You must be signed in to change notification settings - Fork 17
Add Module Torch #213
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
base: master
Are you sure you want to change the base?
Add Module Torch #213
Changes from 8 commits
fc6eabb
bdbba6c
6ec558a
16c1efe
203d9d2
bb3912c
4f6063f
87400a3
2774b15
6e007be
e8d9701
cbe709a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| find_package(Python3 REQUIRED COMPONENTS Interpreter) | ||
|
|
||
| set(_torch_root "" CACHE PATH "Optional root directory of a LibTorch installation") | ||
| set(_torch_cmake_prefix "" CACHE PATH "Optional Torch CMake prefix directory") | ||
| set(_torch_lib_dir "" CACHE PATH "Optional Torch library directory") | ||
|
|
||
| # Optional manual override | ||
| if (_torch_root) | ||
| list(PREPEND CMAKE_PREFIX_PATH "${_torch_root}") | ||
| endif () | ||
|
|
||
| # Auto-discover Torch from the active Python environment | ||
| if (NOT _torch_cmake_prefix) | ||
| execute_process( | ||
| COMMAND | ||
| "${Python3_EXECUTABLE}" -c | ||
| "import torch; print(torch.utils.cmake_prefix_path)" | ||
| RESULT_VARIABLE _torch_cmake_prefix_res | ||
| OUTPUT_VARIABLE _torch_cmake_prefix | ||
| ERROR_VARIABLE _torch_cmake_prefix_err | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE | ||
| ) | ||
|
|
||
| if (NOT _torch_cmake_prefix_res EQUAL 0) | ||
| message( | ||
| FATAL_ERROR | ||
| "MODULE_TORCH=ON, but Python could not import torch.\n" | ||
| "Python executable: ${Python3_EXECUTABLE}\n" | ||
| "Error:\n${_torch_cmake_prefix_err}" | ||
| ) | ||
| endif () | ||
| endif () | ||
|
|
||
| message(STATUS "Torch CMake prefix: ${_torch_cmake_prefix}") | ||
| list(PREPEND CMAKE_PREFIX_PATH "${_torch_cmake_prefix}") | ||
|
|
||
| find_package(Torch REQUIRED CONFIG) | ||
|
|
||
| if (NOT _torch_lib_dir) | ||
| execute_process( | ||
| COMMAND | ||
| "${Python3_EXECUTABLE}" -c | ||
| "import torch.utils.cpp_extension as e; print(e.TORCH_LIB_PATH)" | ||
| RESULT_VARIABLE _torch_lib_dir_res | ||
| OUTPUT_VARIABLE _torch_lib_dir | ||
| ERROR_VARIABLE _torch_lib_dir_err | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE | ||
| ) | ||
|
|
||
| if (NOT _torch_lib_dir_res EQUAL 0) | ||
| message( | ||
| FATAL_ERROR | ||
| "Could not determine Torch library directory via torch.utils.cpp_extension.\n" | ||
| "Python executable: ${Python3_EXECUTABLE}\n" | ||
| "Error:\n${_torch_lib_dir_err}" | ||
| ) | ||
| endif () | ||
| endif () | ||
|
|
||
| message(STATUS "Torch library directory: ${_torch_lib_dir}") | ||
| message(STATUS "Torch include dirs: ${TORCH_INCLUDE_DIRS}") | ||
| message(STATUS "Torch libraries: ${TORCH_LIBRARIES}") | ||
| message(STATUS "Torch CXX flags: ${TORCH_CXX_FLAGS}") | ||
|
|
||
| add_library(RasrExternalTorch INTERFACE) | ||
|
|
||
| if (TARGET torch) | ||
| target_link_libraries(RasrExternalTorch INTERFACE torch) | ||
| else () | ||
| target_include_directories(RasrExternalTorch INTERFACE ${TORCH_INCLUDE_DIRS}) | ||
| target_link_libraries(RasrExternalTorch INTERFACE ${TORCH_LIBRARIES}) | ||
| endif () | ||
|
|
||
| if (TORCH_CXX_FLAGS) | ||
| separate_arguments(_torch_cxx_flags NATIVE_COMMAND "${TORCH_CXX_FLAGS}") | ||
| target_compile_options(RasrExternalTorch INTERFACE ${_torch_cxx_flags}) | ||
| endif () | ||
|
|
||
| # For running RASR binaries without manually setting LD_LIBRARY_PATH | ||
| target_link_options(RasrExternalTorch INTERFACE "LINKER:-rpath,${_torch_lib_dir}") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,3 +45,7 @@ endif() | |
| if(${MODULE_TEST}) | ||
| add_subdirectory(Test) | ||
| endif() | ||
|
|
||
| if (${MODULE_TORCH}) | ||
| add_subdirectory(Torch) | ||
| endif () | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,13 @@ | |
| #pragma pop_macro("ensure") | ||
| #endif | ||
|
|
||
| #ifdef MODULE_TORCH | ||
| #pragma push_macro("ensure") | ||
| #undef ensure | ||
| #include <Torch/Tensor.hh> | ||
| #pragma pop_macro("ensure") | ||
| #endif | ||
|
Comment on lines
+33
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This macro thing was a TF specific workaround. Did you just copy & paste it or is it also needed for Torch?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was needed for Torch as well. I don't remember the exact problem, but there some collision with the ensure macro.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem used to be that both numpy headers and RASR define an |
||
|
|
||
| namespace Nn { | ||
|
|
||
| /* | ||
|
|
@@ -56,6 +63,10 @@ public: | |
| DataView(pybind11::array_t<f32> const& array, size_t size, size_t offset = 0ul); | ||
| #endif | ||
|
|
||
| #ifdef MODULE_TORCH | ||
| DataView(Torch::Tensor&& tensor); | ||
| #endif | ||
|
|
||
| f32 const* data() const { | ||
| return dataPtr_.get(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| add_library( | ||
| RasrTorch STATIC | ||
| ConformerStateManager.cc | ||
| DummyStateManager.cc | ||
| IOSpecification.cc | ||
| IOSpecParser.cc | ||
| Model.cc | ||
| Module.cc | ||
| Session.cc | ||
| StateManager.cc | ||
| TorchEncoder.cc | ||
| TorchFeatureScorer.cc | ||
| TorchForwardNode.cc | ||
| ) | ||
|
|
||
| target_link_libraries( | ||
| RasrTorch PUBLIC RasrExternalTorch RasrCore RasrFlow RasrMath RasrMm | ||
| RasrNn | ||
| ) | ||
|
|
||
| add_rasr_check_executable(check-torch check.cc) | ||
|
|
||
| target_link_rasr_check_libraries(check-torch PRIVATE RasrTorch) |
Uh oh!
There was an error while loading. Please reload this page.