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

"ctranslate2 SO Library Compilation Failure on Android, Seeking Solutions and Reference Steps" #1848

Open
samhuang1991 opened this issue Jan 19, 2025 · 1 comment

Comments

@samhuang1991
Copy link

How do I compile a ctranslate2 SO library to run on Android? I have already compiled a model using opennmt-py and quantized the model with ctranslate2. Now I want to import the quantized model into Android. The difficulty I'm facing is that I don't know why the compilation of the app on Android fails after compiling the SO on my PC. Can you provide a reference?

@ebraraktas
Copy link
Contributor

I have compiled ctranslate for android. I don't know about your build setup, but here is what I did for mine:

  • I compiled the library with cmake, the script below creates cmake build directory under build/${TRIPLE}:
#!/bin/bash
set -eo pipefail

if [ -z "$ANDROID_NDK_HOME" ]; then
	echo "Please set $ANDROID_NDK_HOME"
	exit 1
fi

DEFAULT_TRIPLES="aarch64-linux-android armv7-linux-androideabi"
if [ "$1" = "-h" ]; then
	echo You can call this script with target triples:
	echo "$0 aarch64-linux-android "
	echo If missing, default values are: "$DEFAULT_TRIPLES"
	exit 0
fi
TRIPLES=${*:-$DEFAULT_TARGETS}

for TRIPLE in $TRIPLES; do
	if [[ "$TRIPLE" = aarch64-linux-android ]]; then
		ABI="arm64-v8a"
	elif [[ "$TRIPLE" = armv7-linux-androideabi ]]; then
		ABI="armeabi-v7a"
	else
		echo "Unknown target triple: $TRIPLE. Skipping..."
		continue
	fi
	cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
		-DWITH_MKL=OFF \
		-DCMAKE_BUILD_TYPE=Release \
		-DOPENMP_RUNTIME=COMP \
		-DWITH_RUY=ON \
		-DENABLE_CPU_DISPATCH=OFF \
		-DBUILD_CLI=OFF \
		-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_HOME}"/build/cmake/android.toolchain.cmake \
		-DANDROID_ABI="$ABI" \
		-DANDROID_PLATFORM=android-31 \
		-DBUILD_SHARED_LIBS=ON \
		-B build/"${TRIPLE}"
done

Then run this to build (replace triple accordingly):

cmake --build build/aarch64-linux-android --config Release -- -j $(nproc)
  • I built libctranslate2.so to access from rust code so I wrote simple unsafe shim header and its implementation, but for android project with C++, you may not need one. Therefore, you need to integrate built SO file with your app depending on your codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants