SentencePiece android ndk编译

LLaMa等LLM语言模型一般使用SentencePiece tokenizer,在端侧部署需要编译和使用其c++版本。

在安卓平台使用NDK编译

CMakeLists.txt需要进行一些修改:

src/CMakeLists.txt如下位置加上log依赖,否则提示android log相关符号不存在。

SentencePiece android ndk编译_第1张图片

此外,入口处的CMakeLists.txt加上

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")

可以使得编译后的so大小显著降低,从16.8MB降低到1.9MB。

build.sh参考

#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# please set env SDK_ROOT, NDK_ROOT, ANDROID_ABI, MINSDKVERSION

# export SDK_ROOT=/root/codes/cmdline-tools/sdk/
# export NDK_ROOT=/root/codes/cmdline-tools/sdk//ndk/24.0.8215888/

mkdir -p ${SCRIPT_DIR}/build
cd ${SCRIPT_DIR}/build

if [ -z ${ANDROID_ABI} ] ; then
    ANDROID_ABI=arm64-v8a
fi
if [ -z ${MINSDKVERSION} ] ; then
    MINSDKVERSION=21
fi

echo "SDK_ROOT:" $SDK_ROOT
echo "NDK_ROOT:" $NDK_ROOT
echo "ANDROID_ABI:" $ANDROID_ABI
echo "MINSDKVERSION:" $MINSDKVERSION

${SDK_ROOT}/cmake/3.18.1/bin/cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_TOOLCHAIN_FILE=${NDK_ROOT}/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=${ANDROID_ABI} \
    -DANDROID_NDK=${NDK_ROOT} \
    -DANDROID_PLATFORM=android-${MINSDKVERSION} \
    -DCMAKE_ANDROID_ARCH_ABI=${ANDROID_ABI} \
    -DCMAKE_ANDROID_NDK=${NDK_ROOT} \
    -DCMAKE_MAKE_PROGRAM=${SDK_ROOT}/cmake/3.18.1/bin/ninja \
    -DCMAKE_SYSTEM_NAME=Android \
    -DCMAKE_SYSTEM_VERSION=${MINSDKVERSION} \
    -DANDROID_STL=c++_static \
    -GNinja \
    ..
if [ $? -ne 0 ]; then
    echo "ERROR: cmake $TARGET_NAME failed"
    exit 1
fi

${SDK_ROOT}/cmake/3.18.1/bin/ninja
if [ $? -ne 0 ]; then
    echo "ERROR: build $TARGET_NAME failed"
    exit 1
fi

C++API使用方法

https://github.com/google/sentencepiece/blob/master/doc/api.md

你可能感兴趣的:(推理引擎,android,sentencepiece,llama,安卓,NDK)