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 w2l@anywhere inference build #930

Open
wants to merge 1 commit 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
169 changes: 62 additions & 107 deletions recipes/streaming_convnets/inference/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,73 +1,72 @@
cmake_minimum_required(VERSION 3.5.1)
cmake_minimum_required(VERSION 3.10)

project(wav2letter-inference)
# Creates build rules for streaming inference

# Creates build rules for wav2letter inference processing graph.
project(streaming_inference CXX C)

include(GNUInstallDirs)

option(W2L_INFERENCE_BUILD_TESTS
"Build tests for wav2letter-inference" ON)
option(W2L_INFERENCE_BUILD_EXAMPLES
"Build examples for wav2letter-inference" ON)
option(STREAMING_INFERENCE_BUILD_TESTS
"Build tests for streaming_inference" ON)
option(STREAMING_INFERENCE_BUILD_EXAMPLES
"Build examples for streaming_inference" ON)
option(STREAMING_INFERENCE_BUILD_TOOLS
"Build converter tools for streaming_inference" ON)
set(KENLM_MAX_ORDER 6 CACHE
STRING
"Maximum ngram order for KenLM"
)

#--- Find flashlight ---
find_package(flashlight CONFIG REQUIRED)
if (NOT TARGET flashlight::fl-libraries)
message(FATAL_ERROR "flashlight::fl-libraries must be built for streaming inference")
endif ()

# Inference backend names MUST match paths along
# inference/module/nn/backend/{backend_name}
set(AVAILABLE_INFERENCE_BACKENDS fbgemm)
set(W2L_INFERENCE_BACKEND fbgemm CACHE
set(STREAMING_INFERENCE_BACKEND fbgemm CACHE
STRING
"Inference backend library"
)
set_property(
CACHE
W2L_INFERENCE_BACKEND
STREAMING_INFERENCE_BACKEND
PROPERTY STRINGS
${AVAILABLE_INFERENCE_BACKENDS}
)

# C++ 14 is required
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(wav2letter-inference "")
add_library(streaming_inference "")

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)

#--- Find gflags ---
#--- Find GFlags ---
find_package(gflags REQUIRED)
if (gflags_FOUND)
message(STATUS "gflags found")
else()
message(FATAL_ERROR "gflags not found")
endif()

#--- Download and build Google test ---
# Only if we're not building w2l tests elsewhere, else we'll
# define the same custom target twice with ExternalProject_add
include(BuildGoogleTest)

#--- Download/build Cereal ---
set(CEREAL_INSTALL_PATH ${CMAKE_CURRENT_LIST_DIR}/cereal)
include(Buildcereal)
# Move cereal headers
install(DIRECTORY ${CEREAL_SOURCE_DIR}/include/cereal
DESTINATION ${CEREAL_INSTALL_PATH}
COMPONENT cereal
FILES_MATCHING
PATTERN "*.hpp"
PATTERN "*.h"
PATTERN ".git" EXCLUDE
)

install(
FILES
${CEREAL_SOURCE_DIR}/LICENSE
${CEREAL_SOURCE_DIR}/README.md
DESTINATION
${CEREAL_INSTALL_PATH}
)
#--- Find/build Cereal ---
# If cereal is found in a user-defined location, use it rather than
# downloading from source
find_package(cereal)
if (NOT TARGET cereal AND NOT cereal_FOUND)
message(STATUS "cereal NOT found. Will download from source")
set(CEREAL_INSTALL_PATH ${FL_INSTALL_INC_DIR}/cereal)
include(${CMAKE_MODULE_PATH}/BuildCereal.cmake)
# Move cereal headers at install time
install(DIRECTORY ${CEREAL_SOURCE_DIR}/include/cereal
DESTINATION ${CEREAL_INSTALL_PATH}
COMPONENT cereal
FILES_MATCHING
PATTERN "*.hpp"
PATTERN "*.h"
PATTERN ".git" EXCLUDE
)
install(FILES ${CEREAL_SOURCE_DIR}/LICENSE ${CEREAL_SOURCE_DIR}/README.md
DESTINATION ${CEREAL_INSTALL_PATH}
)
endif()

# ------------------ add subdirectories --------------------

Expand All @@ -81,95 +80,51 @@ add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/inference/decoder)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/inference/module)

# Creates executable targets foreach example.
if (W2L_INFERENCE_BUILD_EXAMPLES)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/inference/examples)
if (STREAMING_INFERENCE_BUILD_EXAMPLES)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/examples)
endif()

# ------------------------ Tests ------------------------

add_custom_target(inference_tests)

# Builds a single test source file.
function(build_test SRCFILE)
get_filename_component(target ${SRCFILE} NAME_WE)
set(target inference_${target})
message(STATUS "Tests: add executable ${target}")
add_executable(${target} ${SRCFILE})
add_dependencies(wav2letter-inference ${target})
add_dependencies(${target} gtest) # make sure gtest is built first
add_dependencies(inference_tests ${target})
target_link_libraries(
${target}
PRIVATE
${GTEST_LIBRARIES}
streaming_inference_common
streaming_inference_decoder
streaming_inference_modules
Threads::Threads
)
target_include_directories(
${target}
PRIVATE
${CMAKE_SOURCE_DIR}
${GTEST_INCLUDE_DIR}
# going up one directory to use #include "inference/*/*.h"
${wav2letter-inference_SOURCE_DIR}
${W2L_INFERENCE_TESTS_PATH}
${cereal_INCLUDE_DIRS}
)
add_test(${target} ${target})
endfunction(build_test)

set(W2L_INFERENCE_TESTS_PATH
${wav2letter-inference_SOURCE_DIR}/inference/module/test)

set(W2L_INFERENCE_TESTS_SOURCES
${W2L_INFERENCE_TESTS_PATH}/Conv1dTest.cpp
${W2L_INFERENCE_TESTS_PATH}/IdentityTest.cpp
${W2L_INFERENCE_TESTS_PATH}/LayerNormTest.cpp
${W2L_INFERENCE_TESTS_PATH}/LinearTest.cpp
${W2L_INFERENCE_TESTS_PATH}/LogMelFeatureTest.cpp
${W2L_INFERENCE_TESTS_PATH}/MemoryManagerTest.cpp
${W2L_INFERENCE_TESTS_PATH}/ReluTest.cpp
${W2L_INFERENCE_TESTS_PATH}/ResidualTest.cpp
${W2L_INFERENCE_TESTS_PATH}/TDSBlockTest.cpp
)

if (W2L_INFERENCE_BUILD_TESTS)
find_package(Threads REQUIRED)
# Creates executable targets foreach example.
if (STREAMING_INFERENCE_BUILD_TESTS)
enable_testing()
foreach(SRC_FILE ${W2L_INFERENCE_TESTS_SOURCES})
build_test(${SRC_FILE})
endforeach()
include(TestUtils)
# add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/test) // FIXME
endif()

# ------------------------ Tools ------------------------
if (STREAMING_INFERENCE_BUILD_TOOLS)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/tools)
endif()

# -------------- wav2letter-inference top level target -----------------
# -------------- streaming_inference top level target -----------------

set_target_properties(
wav2letter-inference
streaming_inference
PROPERTIES
LINKER_LANGUAGE CXX
CXX_STANDARD 11
CXX_STANDARD 14
)

target_include_directories(
wav2letter-inference
streaming_inference
PUBLIC
${wav2letter-inference_SOURCE_DIR}
${streaming_inference_SOURCE_DIR}
)

target_link_libraries(
wav2letter-inference
streaming_inference
PUBLIC
streaming_inference_common
streaming_inference_decoder
streaming_inference_modules
)

install(TARGETS wav2letter-inference
EXPORT wav2letter-inference-export
install(TARGETS streaming_inference
EXPORT streaming_inference-export
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include/wav2letter/inference
INCLUDES DESTINATION include/inference
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
cmake_minimum_required(VERSION 3.5.1)

include(ExternalProject)

set(cereal_URL https://github.com/USCiLab/cereal.git)
set(cereal_BUILD ${CMAKE_CURRENT_BINARY_DIR}/cereal)
set(cereal_TAG 319ce5f5ee5b76cb80617156bf7c95474a2938b1) # `develop` branch, commit
set(cereal_TAG 02eace19a99ce3cd564ca4e379753d69af08c2c8) # version 1.3.0

# Download Cereal
ExternalProject_Add(
Expand All @@ -18,17 +16,15 @@ ExternalProject_Add(
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DJUST_INSTALL_CEREAL:BOOL=ON

)
ExternalProject_Get_Property(cereal source_dir)
set(CEREAL_SOURCE_DIR ${source_dir})
ExternalProject_Get_Property(cereal binary_dir)
set(CEREAL_BINARY_DIR ${binary_dir})

# Include dir. dependent on build or install
set(CEREAL_INCLUDE_DIR
set(cereal_INCLUDE_DIRS
$<BUILD_INTERFACE:${CEREAL_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CEREAL_INSTALL_PATH}> # see root CMakeLists.txt
)

set(cereal_INCLUDE_DIRS ${CEREAL_INCLUDE_DIR})
80 changes: 80 additions & 0 deletions recipes/streaming_convnets/inference/cmake/BuildGoogleTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
cmake_minimum_required(VERSION 3.10.0)

include(ExternalProject)

set(gtest_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/googletest/src/googletest/googletest/include)
set(gtest_URL https://github.com/google/googletest.git)
set(gtest_BUILD ${CMAKE_CURRENT_BINARY_DIR}/googletest/)
set(gtest_TAG 703bd9caab50b139428cea1aaff9974ebee5742e) # release 1.10.0

if (NOT TARGET gtest)
# Download googletest
ExternalProject_Add(
gtest
PREFIX googletest
GIT_REPOSITORY ${gtest_URL}
GIT_TAG ${gtest_TAG}
BUILD_IN_SOURCE 1
BUILD_COMMAND ${CMAKE_COMMAND} --build .
INSTALL_COMMAND ""
CMAKE_CACHE_ARGS
-DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
-DCMAKE_BUILD_TYPE:STRING=Release
-DBUILD_GMOCK:BOOL=ON
-DBUILD_GTEST:BOOL=ON
-Dgtest_force_shared_crt:BOOL=OFF
)
endif ()

ExternalProject_Get_Property(gtest source_dir)
set(GTEST_SOURCE_DIR ${source_dir})
ExternalProject_Get_Property(gtest binary_dir)
set(GTEST_BINARY_DIR ${binary_dir})

if (BUILD_SHARED_LIBS)
set(LIB_TYPE SHARED)
else()
set(LIB_TYPE STATIC)
endif()

# Library and include dirs
set(GTEST_LIBRARIES
"${GTEST_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${CMAKE_${LIB_TYPE}_LIBRARY_PREFIX}gtest${CMAKE_${LIB_TYPE}_LIBRARY_SUFFIX}"
)
set(GTEST_LIBRARIES_MAIN
"${GTEST_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${CMAKE_${LIB_TYPE}_LIBRARY_PREFIX}gtest_main${CMAKE_${LIB_TYPE}_LIBRARY_SUFFIX}"
)
set(GMOCK_LIBRARIES
"${GTEST_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${CMAKE_${LIB_TYPE}_LIBRARY_PREFIX}gmock${CMAKE_${LIB_TYPE}_LIBRARY_SUFFIX}"
)
set(GMOCK_LIBRARIES_MAIN
"${GTEST_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/${CMAKE_${LIB_TYPE}_LIBRARY_PREFIX}gmock_main${CMAKE_${LIB_TYPE}_LIBRARY_SUFFIX}"
)

set(GTEST_INCLUDE_DIRS ${GTEST_SOURCE_DIR}/googletest/include)
set(GMOCK_INCLUDE_DIRS ${GTEST_SOURCE_DIR}/googlemock/include)
# Make dirs so this can be used as an interface include directory
file(MAKE_DIRECTORY ${GTEST_INCLUDE_DIRS})
file(MAKE_DIRECTORY ${GMOCK_INCLUDE_DIRS})

add_library(GTest::gtest ${LIB_TYPE} IMPORTED)
add_library(GTest::gtest_main ${LIB_TYPE} IMPORTED)
add_library(GTest::gmock ${LIB_TYPE} IMPORTED)
add_library(GTest::gmock_main ${LIB_TYPE} IMPORTED)

set_target_properties(GTest::gtest PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIRS}
IMPORTED_LOCATION ${GTEST_LIBRARIES}
)
set_target_properties(GTest::gtest_main PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIRS}
IMPORTED_LOCATION ${GTEST_LIBRARIES_MAIN}
)
set_target_properties(GTest::gmock PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GMOCK_INCLUDE_DIRS}
IMPORTED_LOCATION ${GMOCK_LIBRARIES}
)
set_target_properties(GTest::gmock_main PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GMOCK_INCLUDE_DIRS}
IMPORTED_LOCATION ${GMOCK_LIBRARIES_MAIN}
)
51 changes: 51 additions & 0 deletions recipes/streaming_convnets/inference/cmake/BuildKenlm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.10.0)

# Required for KenLM to read ARPA files in compressed format
find_package(LibLZMA REQUIRED)
find_package(BZip2 REQUIRED)
find_package(ZLIB REQUIRED)

include(ExternalProject)

set(kenlm_TEMP_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/extern/kenlm)
set(kenlm_URL https://github.com/kpu/kenlm.git)
set(kenlm_BUILD ${CMAKE_CURRENT_BINARY_DIR}/kenlm)
set(kenlm_TAG 4a277534fd33da323205e6ec256e8fd0ff6ee6fa)

if (NOT TARGET kenlm)
# Download kenlm
ExternalProject_Add(
kenlm
PREFIX kenlm
GIT_REPOSITORY ${kenlm_URL}
GIT_TAG ${kenlm_TAG}
BUILD_IN_SOURCE 1
BUILD_COMMAND ${CMAKE_COMMAND} --build .
CMAKE_CACHE_ARGS
-DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
-DCMAKE_INSTALL_PREFIX:PATH=${kenlm_TEMP_INSTALL_DIR}
)
endif ()

# Install the install executed at build time
install(DIRECTORY ${kenlm_TEMP_INSTALL_DIR}/include DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY ${kenlm_TEMP_INSTALL_DIR}/lib DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY ${kenlm_TEMP_INSTALL_DIR}/bin DESTINATION ${CMAKE_INSTALL_PREFIX})

if (BUILD_SHARED_LIBS)
set(LIB_TYPE SHARED)
else()
set(LIB_TYPE STATIC)
endif()

# Library and include dirs
set(KENLM_LIBRARIES
"${kenlm_TEMP_INSTALL_DIR}/lib/${CMAKE_${LIB_TYPE}_LIBRARY_PREFIX}kenlm${CMAKE_${LIB_TYPE}_LIBRARY_SUFFIX}"
"${kenlm_TEMP_INSTALL_DIR}/lib/${CMAKE_${LIB_TYPE}_LIBRARY_PREFIX}kenlm_util${CMAKE_${LIB_TYPE}_LIBRARY_SUFFIX}"
${LIBLZMA_LIBRARIES}
${BZIP2_LIBRARIES}
${ZLIB_LIBRARIES}
)
set(KENLM_INCLUDE_DIRS "${kenlm_TEMP_INSTALL_DIR}/include")
set(KENLM_INCLUDE_DIRS_LM "${kenlm_TEMP_INSTALL_DIR}/include/kenlm")
Loading