-
Notifications
You must be signed in to change notification settings - Fork 989
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support static linking of ctranslate2 (#148)
* support static linking of ctranslate2 * update * remove submodule rust-cxx-cmake-bridge * support alwayslink with whole-archive * update * move export_libs * update docker config * update ctranslate2 * remove * update * update build.rs * parse external libs * cleanup * add cargo fmt
- Loading branch information
Showing
12 changed files
with
428 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
/Cargo.lock | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.7) | ||
|
||
project(ctranslate2_bindings) | ||
|
||
add_subdirectory(CTranslate2) | ||
|
||
add_library(dummy | ||
src/dummy.cc | ||
) | ||
|
||
target_link_libraries(dummy | ||
PRIVATE ctranslate2 | ||
) | ||
|
||
include(cmake/export_libs.cmake) | ||
export_all_target_libs(dummy) |
Submodule CTranslate2
updated
25 files
+24 −0 | CHANGELOG.md | |
+1 −0 | CMakeLists.txt | |
+1 −1 | README.md | |
+1 −0 | docs/guides/transformers.md | |
+9 −1 | include/ctranslate2/layers/attention.h | |
+4 −4 | include/ctranslate2/layers/transformer.h | |
+1 −0 | include/ctranslate2/logging.h | |
+1 −3 | include/ctranslate2/models/model_factory.h | |
+1 −0 | include/ctranslate2/primitives.h | |
+15 −0 | python/cpp/generator.cc | |
+116 −1 | python/ctranslate2/converters/transformers.py | |
+2 −0 | python/ctranslate2/specs/attention_spec.py | |
+9 −1 | python/ctranslate2/specs/transformer_spec.py | |
+1 −1 | python/ctranslate2/version.py | |
+1 −1 | python/tests/requirements.txt | |
+39 −0 | python/tests/test_transformers.py | |
+3 −1 | src/cpu/primitives.cc | |
+6 −1 | src/cuda/primitives.cu | |
+45 −9 | src/layers/attention.cc | |
+6 −3 | src/layers/transformer.cc | |
+12 −12 | src/logging.cc | |
+31 −0 | src/models/model_factory.cc | |
+1 −10 | src/models/transformer.cc | |
+0 −3 | src/models/whisper.cc | |
+3 −0 | src/utils.cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#! /bin/bash | ||
|
||
set -e | ||
set -x | ||
|
||
UNAME="$(uname -s)" | ||
case "${UNAME}" in | ||
Linux*) MACHINE=linux;; | ||
Darwin*) MACHINE=macos;; | ||
*) exit 1;; | ||
esac | ||
|
||
rm -rf build | ||
mkdir build && cd build | ||
|
||
if [[ "$MACHINE" == "macos" ]]; then | ||
CMAKE_EXTRA_OPTIONS='-DCMAKE_OSX_ARCHITECTURES=arm64 -DWITH_ACCELERATE=ON -DWITH_MKL=OFF -DOPENMP_RUNTIME=NONE -DWITH_RUY=ON' | ||
elif [[ "$MACHINE" == "linux" ]]; then | ||
CMAKE_EXTRA_OPTIONS='-DWITH_CUDA=ON -DWITH_CUDNN=ON -DWITH_MKL=ON -DWITH_DNNL=ON -DOPENMP_RUNTIME=COMP -DCUDA_NVCC_FLAGS=-Xfatbin=-compress-all -DCUDA_ARCH_LIST=Common -DCXXFLAGS=-msse4.1' | ||
fi | ||
|
||
|
||
cmake -DBULID_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release -DBUILD_CLI=OFF -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON $CMAKE_EXTRA_OPTIONS .. | ||
|
||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
################################################################################ | ||
|
||
# WARNING: to list the system libraries(ie IMPORTED) you MUST set: | ||
# set_target_properties(your_lib PROPERTIES IMPORTED_GLOBAL TRUE) | ||
# just after the find_package call | ||
# cf https://gitlab.kitware.com/cmake/cmake/-/issues/17256 | ||
# | ||
# https://stackoverflow.com/questions/32756195/recursive-list-of-link-libraries-in-cmake | ||
# https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i | ||
function(_get_link_libraries OUTPUT_LIST TARGET) | ||
list(APPEND VISITED_TARGETS ${TARGET}) | ||
|
||
# DO NOT switch on IMPORTED or not | ||
# An INTERFACE library CAN have LINK_LIBRARIES! | ||
# get_target_property(IMPORTED ${TARGET} IMPORTED) | ||
set(LIBS "") | ||
get_target_property(LIBS_1 ${TARGET} INTERFACE_LINK_LIBRARIES) | ||
get_target_property(LIBS_2 ${TARGET} LINK_LIBRARIES) | ||
list(APPEND LIBS ${LIBS_1} ${LIBS_2}) | ||
|
||
set(LIB_FILES "") | ||
|
||
foreach(LIB ${LIBS}) | ||
if (TARGET ${LIB}) | ||
list(FIND VISITED_TARGETS ${LIB} VISITED) | ||
if (${VISITED} EQUAL -1) | ||
# OLD: get_target_property(LIB_FILE ${LIB} LOCATION) | ||
# NEW: | ||
_get_link_libraries(LINK_LIB_FILES ${LIB}) | ||
set(LIB_FILE ${LIB}) | ||
list(APPEND LIB_FILES ${LINK_LIB_FILES}) | ||
list(APPEND LIB_FILES ${LIB_FILE}) | ||
endif() | ||
elseif(EXISTS ${LIB}) | ||
set(LIB_FILE ${LIB}) | ||
list(APPEND LIB_FILES ${LIB_FILE}) | ||
endif() | ||
endforeach() | ||
|
||
set(VISITED_TARGETS ${VISITED_TARGETS} PARENT_SCOPE) | ||
set(${OUTPUT_LIST} ${LIB_FILES} PARENT_SCOPE) | ||
endfunction() | ||
|
||
################################################################################ | ||
|
||
function(export_all_target_libs TARGET) | ||
# NOTE: get_target_property(CIRCUIT_LIB_LINK_LIBRARIES a_target LINK_LIBRARIES) is NOT transitive | ||
# This function will return eg: "$<TARGET_FILE:rust_cxx>;$<TARGET_FILE:circuit_lib>;" | ||
# b/c generator expression are evaluated LATER | ||
# cf https://stackoverflow.com/questions/59226127/cmake-generator-expression-how-to-get-target-file-property-on-list-of-targets | ||
set(ALL_LINK_LIBRARIES "") | ||
_get_link_libraries(ALL_LINK_LIBRARIES ${TARGET}) | ||
|
||
message(STATUS "ALL_LINK_LIBRARIES : ${ALL_LINK_LIBRARIES}") | ||
|
||
set(ALL_LIBS "") | ||
set(ALL_EXTERNAL_LIBS "") | ||
# TODO move that back into get_link_libraries | ||
# NOTE: we MUST do it in 2 steps: | ||
# - collect all the LINK_LIBRARIES recursively | ||
# - loop on those and get their TARGET_FILE (if not INTERFACE_LIBRARY) | ||
# That is b/c in get_link_libraries a INTERFACE_LIBRARY CAN have link_libraries | ||
# but we CAN NOT evaluate generator expressions at this time. | ||
foreach(LIB ${ALL_LINK_LIBRARIES}) | ||
# MUST skip INTERFACE else: | ||
# CMake Error at src/CMakeLists.txt:136 (add_custom_command): | ||
# Error evaluating generator expression: | ||
# $<TARGET_FILE:rust_cxx> | ||
# Target "rust_cxx" is not an executable or library. | ||
# SHARED_LIBRARY,INTERFACE_LIBRARY,STATIC_LIBRARY | ||
# | ||
if (TARGET ${LIB}) | ||
get_target_property(LIB_TYPE ${LIB} TYPE) | ||
message(STATUS "LIB_TYPE : ${LIB} = ${LIB_TYPE}") | ||
|
||
if(NOT ${LIB_TYPE} STREQUAL "INTERFACE_LIBRARY") | ||
set(LIB_FILE $<TARGET_FILE:${LIB}>) | ||
list(APPEND ALL_LIBS ${LIB_FILE}) | ||
endif() | ||
elseif(EXISTS ${LIB}) | ||
set(LIB_FILE ${LIB}) | ||
message(STATUS "LIB_TYPE : ${LIB} = EXTERNAL") | ||
list(APPEND ALL_LIBS ${LIB_FILE}) | ||
endif() | ||
endforeach() # LIB ${ALL_LIBS} | ||
|
||
message(STATUS "ALL_LIBS : ${ALL_LIBS}") | ||
|
||
# add_custom_command(ie echoing only to stdout) works but more difficult to get from build.rs | ||
# b/c when there is "ninja: no work to do" it will NOT echo on the console | ||
add_custom_command( | ||
TARGET ${TARGET} | ||
POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E echo ${ALL_LIBS} > ${CMAKE_CURRENT_BINARY_DIR}/cmake_generated_libs | ||
# OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cmake_generated_libs | ||
VERBATIM | ||
) | ||
endfunction(export_all_target_libs) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "rust-cxx-cmake-bridge" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
Oops, something went wrong.