Skip to content
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ if(${MODULE_ONNX})
include(cmake_resources/Onnx.cmake)
endif()

if (${MODULE_TORCH})
include(cmake_resources/Torch.cmake)
endif ()

string(TOLOWER ${CMAKE_SYSTEM_NAME} SYSTEM_NAME)
string(TOLOWER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
set(ARCH_DESCRIPTION
Expand Down
3 changes: 3 additions & 0 deletions cmake_resources/Modules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ add_module_option(MODULE_CUDA ON)
# ****** Tensorflow integration ******
add_module_option(MODULE_TENSORFLOW OFF)

# ****** Torch integration ******
add_module_option(MODULE_TORCH ON)

# ****** ONNX integration ******
add_module_option(MODULE_ONNX ON)

Expand Down
80 changes: 80 additions & 0 deletions cmake_resources/Torch.cmake
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)
Comment thread
larissakl marked this conversation as resolved.
Outdated

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}")
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ endif()
if(${MODULE_TEST})
add_subdirectory(Test)
endif()

if (${MODULE_TORCH})
add_subdirectory(Torch)
endif ()
15 changes: 13 additions & 2 deletions src/Core/Assertions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,19 @@ template<class S, class T>
void assertionFailedVerbose(const S& x, const T& y, const char* op,
const char* expr, const char* function,
const char* filename, unsigned int line) {
FailedAssertion("assertion", expr, function, filename, line).stream()
<< x << " " << op << " " << y;
// FailedAssertion("assertion", expr, function, filename, line).stream()
// << x << " " << op << " " << y;

if constexpr (std::is_same_v<T, std::nullptr_t>) {
FailedAssertion("assertion", expr, function, filename, line).stream()
<< x << " " << op << " " << "null";
}
else {
FailedAssertion("assertion", expr, function, filename, line).stream()
<< x << " " << op << " " << y;
}

std::abort();
Comment thread
larissakl marked this conversation as resolved.
Outdated
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Nn/LabelScorer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ endif()
if(${MODULE_ONNX})
target_link_libraries(RasrNn PUBLIC RasrOnnx)
endif()

if (${MODULE_TORCH})
target_link_libraries(RasrNn PUBLIC RasrTorch)
endif ()
12 changes: 12 additions & 0 deletions src/Nn/LabelScorer/DataView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ DataView::DataView(pybind11::array_t<f32> const& array, size_t size, size_t offs
}
#endif

#ifdef MODULE_TORCH
DataView::DataView(Torch::Tensor&& tensor) {
auto tensorPtr = tensor.ptr();

dataPtr_ = std::shared_ptr<f32 const[]>(
tensor.data(),
[tensorPtr](f32 const[]) mutable {});

size_ = tensorPtr->numel();
}
#endif

} // namespace Nn
11 changes: 11 additions & 0 deletions src/Nn/LabelScorer/DataView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 ensure macro, so the RASR one has to be temporarily disabled when including numpy headers to avoid conflicts.


namespace Nn {

/*
Expand All @@ -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();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Tools/Flf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ endif()
if(${MODULE_TENSORFLOW})
target_link_libraries(flf-tool PRIVATE RasrTensorflow)
endif()

if (${MODULE_TORCH})
target_link_libraries(flf-tool PRIVATE RasrTorch)
endif ()
6 changes: 6 additions & 0 deletions src/Tools/Flf/FlfTool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#ifdef MODULE_TENSORFLOW
#include <Tensorflow/Module.hh>
#endif
#ifdef MODULE_TORCH
#include <Torch/Module.hh>
#endif

class FlfTool : public Core::Application {
private:
Expand Down Expand Up @@ -114,6 +117,9 @@ class FlfTool : public Core::Application {
#endif
#ifdef MODULE_TENSORFLOW
INIT_MODULE(Tensorflow);
#endif
#ifdef MODULE_TORCH
INIT_MODULE(Torch);
#endif
setTitle("flf-lattice-tool");
setDefaultLoadConfigurationFile(false);
Expand Down
4 changes: 4 additions & 0 deletions src/Tools/SpeechRecognizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ if(${MODULE_TENSORFLOW})
list(APPEND libraries RasrTensorflow)
endif()

if (${MODULE_TORCH})
list(APPEND libraries RasrTorch)
endif ()

target_link_libraries(speech-recognizer PRIVATE ${libraries})

if(${MODULE_SEARCH_WFST})
Expand Down
6 changes: 6 additions & 0 deletions src/Tools/SpeechRecognizer/SpeechRecognizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#ifdef MODULE_TENSORFLOW
#include <Tensorflow/Module.hh>
#endif
#ifdef MODULE_TORCH
#include <Torch/Module.hh>
#endif

class SpeechRecognizer : public Core::Application {
public:
Expand All @@ -60,6 +63,9 @@ class SpeechRecognizer : public Core::Application {
#ifdef MODULE_TENSORFLOW
INIT_MODULE(Tensorflow);
#endif
#ifdef MODULE_TORCH
INIT_MODULE(Torch);
#endif

setTitle("speech-recognizer");
}
Expand Down
23 changes: 23 additions & 0 deletions src/Torch/CMakeLists.txt
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)
Loading
Loading