Skip to content

Commit 7b762b1

Browse files
scottcjtdanpovey
authored andcommitted
[build,src] Upgrade TensorFlow RNN to 2.0 (#3771)
* Improve CMake build script * Builds or ignore CUDA objects based on availibility of CUDA tools. * Builds C++ binaries of tfrnnlm if `-DTENSORFLOW_DIR=...` is provided. * Select Apple BLAS by default when building on MacOS. * Enable CTest for tests (run `ctest` or `make test`). Typical build on Linux with CUDA+MKL: ``` export MKLROOT cmake -DMATHLIB=MKL <cmakefile> make ctest ``` * Update model training script to TensorFlow v2 flavor * Enable rescore tool to load from SavedModel format * Update the training script which uses custom loss function Still tried to mimic original behavior without whole pipeline ready. * Fix numerical stability of the fast softmax model
1 parent 7d6cc37 commit 7b762b1

File tree

10 files changed

+493
-618
lines changed

10 files changed

+493
-618
lines changed

CMakeLists.txt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ include(GNUInstallDirs)
66
include(Utils)
77
include(third_party/get_third_party)
88

9+
# Should update cmake to a more recent version which supports FindPython3.
10+
find_package(PythonInterp)
11+
if(NOT PYTHON_EXECUTABLE OR PYTHON_VERSION_MAJOR LESS 3)
12+
message(WARNING "Needs python3 to auto-generate most CMake files, but not found. "
13+
"Will try `python3` directly...")
14+
set(PYTHON_EXECUTABLE "python3")
15+
endif()
16+
917
message(STATUS "Running gen_cmake_skeleton.py")
10-
execute_process(COMMAND python
18+
execute_process(COMMAND ${PYTHON_EXECUTABLE}
1119
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/gen_cmake_skeleton.py"
1220
"${CMAKE_CURRENT_SOURCE_DIR}/src"
1321
"--quiet"
@@ -28,11 +36,21 @@ if(BUILD_SHARED_LIBS)
2836
endif()
2937
endif()
3038

31-
set(MATHLIB "OpenBLAS" CACHE STRING "OpenBLAS|MKL|Accelerate")
39+
if(APPLE)
40+
# Use built-in BLAS on MacOS by default.
41+
set(MATHLIB "Accelerate" CACHE STRING "OpenBLAS|MKL|Accelerate")
42+
else()
43+
set(MATHLIB "OpenBLAS" CACHE STRING "OpenBLAS|MKL|Accelerate")
44+
endif()
3245
option(KALDI_BUILD_EXE "If disabled, will make add_kaldi_executable a no-op" ON)
3346
option(KALDI_BUILD_TEST "If disabled, will make add_kaldi_test_executable a no-op" ON)
3447
option(KALDI_USE_PATCH_NUMBER "Use MAJOR.MINOR.PATCH format, otherwise MAJOR.MINOR" OFF)
3548

49+
if (KALDI_BUILD_TEST)
50+
include(CTest)
51+
enable_testing()
52+
endif()
53+
3654
link_libraries(${CMAKE_DL_LIBS})
3755

3856
find_package(Threads)
@@ -53,6 +71,19 @@ elseif(MATHLIB STREQUAL "MKL")
5371
include_directories($ENV{MKLROOT}/include) # TODO: maybe not use env, idk, find_package doesnt handle includes...
5472
link_libraries(${BLAS_LIBRARIES} ${LAPACK_LIBRARIES})
5573
elseif(MATHLIB STREQUAL "Accelerate")
74+
execute_process(COMMAND sw_vers -productVersion
75+
OUTPUT_VARIABLE MACOS_VERSION)
76+
if(MACOS_VERSION VERSION_LESS "10.12" AND MACOS_VERSION VERSION_GREATER_EQUAL "10.11")
77+
message(WARNING
78+
"**BAD WARNING**: You are using OS X El Capitan. Some versions of this OS"
79+
" have a bug in the BLAS implementation that affects Kaldi."
80+
" After compiling, cd to matrix/ and type 'make test'. The"
81+
" test will fail if the problem exists in your version."
82+
" Eventually this issue will be fixed by system updates from"
83+
" Apple. Unexplained crashes with reports of NaNs will"
84+
" be caused by this bug, but some recipes will (sometimes) work."
85+
)
86+
endif()
5687
set(BLA_VENDOR "Apple")
5788
find_package(BLAS REQUIRED)
5889
find_package(LAPACK REQUIRED)
@@ -160,6 +191,11 @@ add_subdirectory(src/kws)
160191

161192
add_subdirectory(src/itf)
162193

194+
if(TENSORFLOW_DIR)
195+
add_subdirectory(src/tfrnnlm)
196+
add_subdirectory(src/tfrnnlmbin)
197+
endif()
198+
163199
# add all cuda libraries
164200
if(CUDA_FOUND)
165201
add_subdirectory(src/cudafeat)

cmake/Utils.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ macro(add_kaldi_test_executable)
3636
cmake_parse_arguments(kaldi_test_exe "" "NAME" "SOURCES;DEPENDS" ${ARGN})
3737
add_executable(${kaldi_test_exe_NAME} ${kaldi_test_exe_SOURCES})
3838
target_link_libraries(${kaldi_test_exe_NAME} PRIVATE ${kaldi_test_exe_DEPENDS})
39+
add_test(
40+
NAME ${kaldi_test_exe_NAME}
41+
COMMAND ${kaldi_test_exe_NAME}
42+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
3943
# list(APPEND KALDI_TEST_EXECUTABLES ${kaldi_test_exe_NAME})
4044
install(TARGETS ${kaldi_test_exe_NAME} RUNTIME DESTINATION testbin)
4145

cmake/gen_cmake_skeleton.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,13 @@ def gen_code(self):
180180

181181
if len(self.cuda_source_list) > 0:
182182
self.source_list.append("${CUDA_OBJS}")
183-
ret.append("cuda_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)")
184-
ret.append("cuda_compile(CUDA_OBJS")
183+
ret.append("if(CUDA_FOUND)")
184+
ret.append(" cuda_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)")
185+
ret.append(" cuda_compile(CUDA_OBJS")
185186
for f in self.cuda_source_list:
186-
ret.append(" " + f)
187-
ret.append(")\n")
187+
ret.append(" " + f)
188+
ret.append(" )")
189+
ret.append("endif()\n")
188190

189191
ret.append("add_library(" + self.target_name)
190192
for f in self.source_list:
@@ -278,6 +280,8 @@ def write_file(self):
278280

279281
subdirs = get_subdirectories(".")
280282
for d in subdirs:
283+
if d.startswith('tfrnnlm'):
284+
continue
281285
cmakelists = CMakeListsFile(d)
282286
if is_bin_dir(d):
283287
for f in get_files(d):

0 commit comments

Comments
 (0)