mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 21:11:03 +08:00
Add spacemit ort ep for spacemit riscv cpus (#2837)
This pull request significantly extends the project's hardware compatibility by integrating a dedicated SpacemiT Execution Provider for ONNX Runtime. The changes enable efficient model inference on SpacemiT RISC-V CPUs, leveraging their RVV1.0 capabilities. This involves updates to the build system, new CMake modules for toolchain and ONNX Runtime package handling, and modifications to the core provider and session management logic to recognize and configure the SpacemiT EP.
This commit is contained in:
1 parent
3fd5a5d551
commit
586cd19e22
11 files changed
+547
-1
No files matched your search
+299
@@ -0,0 +1,299 @@
|
||||
name: riscv64-spacemit-linux
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
paths:
|
||||
- '.github/workflows/riscv64-spacemit-linux.yaml'
|
||||
- 'cmake/**'
|
||||
- 'sherpa-onnx/csrc/*'
|
||||
- 'sherpa-onnx/c-api/*'
|
||||
- 'toolchains/riscv64-linux-gnu-spacemit.toolchain.cmake'
|
||||
- 'build-riscv64-linux-gnu-spacemit.sh'
|
||||
tags:
|
||||
- 'v[0-9]+.[0-9]+.[0-9]+*'
|
||||
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: riscv64-spacemit-linux-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
riscv64_spacemit_linux:
|
||||
runs-on: ${{ matrix.os }}
|
||||
name: ${{ matrix.os }} ${{ matrix.lib_type }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
lib_type: [shared] #, static]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Update version
|
||||
shell: bash
|
||||
run: |
|
||||
./new-release.sh
|
||||
git diff .
|
||||
|
||||
- name: ccache
|
||||
uses: hendrikmuhs/ccache-action@v1.2
|
||||
with:
|
||||
key: ${{ matrix.os }}-riscv64-spacemit-${{ matrix.lib_type }}
|
||||
|
||||
- name: cache-qemu
|
||||
id: cache-qemu
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: qemu-install
|
||||
key: qemu-riscv-spacemit-install-20250818
|
||||
|
||||
- name: qemu
|
||||
if: steps.cache-qemu.outputs.cache-hit != 'true'
|
||||
run: |
|
||||
wget -q https://archive.spacemit.com/spacemit-ai/qemu/jdsk-qemu-v10.0.2.tar.gz
|
||||
tar -xf jdsk-qemu-v10.0.2.tar.gz
|
||||
mkdir -p qemu-install/bin
|
||||
|
||||
cp -v ./jdsk-qemu/bin/qemu-riscv64 ./qemu-install/bin
|
||||
|
||||
- name: cache-toolchain
|
||||
id: cache-toolchain
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: toolchain
|
||||
key: https://archive.spacemit.com/toolchain/spacemit-toolchain-linux-glibc-x86_64-v1.1.2.tar.xz
|
||||
|
||||
- name: Download toolchain
|
||||
if: steps.cache-toolchain.outputs.cache-hit != 'true'
|
||||
shell: bash
|
||||
run: |
|
||||
wget -q https://archive.spacemit.com/toolchain/spacemit-toolchain-linux-glibc-x86_64-v1.1.2.tar.xz
|
||||
|
||||
mkdir $GITHUB_WORKSPACE/toolchain
|
||||
|
||||
tar xvf spacemit-toolchain-linux-glibc-x86_64-v1.1.2.tar.xz --strip-components 1 -C $GITHUB_WORKSPACE/toolchain
|
||||
ls -lh $GITHUB_WORKSPACE/toolchain/bin
|
||||
|
||||
- name: Display toolchain info
|
||||
shell: bash
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/toolchain/bin:$PATH
|
||||
riscv64-unknown-linux-gnu-gcc --version
|
||||
|
||||
- name: Display qemu-riscv64 -h
|
||||
shell: bash
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/qemu-install/bin:$PATH
|
||||
export QEMU_LD_PREFIX=$GITHUB_WORKSPACE/toolchain/sysroot
|
||||
qemu-riscv64 -h
|
||||
|
||||
- name: build riscv64-spacemit-linux
|
||||
shell: bash
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/toolchain/bin:$PATH
|
||||
|
||||
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
|
||||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
|
||||
|
||||
cmake --version
|
||||
|
||||
lib_type=${{ matrix.lib_type }}
|
||||
|
||||
if [[ $lib_type == "shared" ]]; then
|
||||
export BUILD_SHARED_LIBS=ON
|
||||
else
|
||||
export BUILD_SHARED_LIBS=OFF
|
||||
fi
|
||||
|
||||
export RISCV_ROOT_PATH=$GITHUB_WORKSPACE/toolchain
|
||||
./build-riscv64-linux-gnu-spacemit.sh
|
||||
|
||||
ls -lh build-riscv64-linux-gnu-spacemit/bin
|
||||
ls -lh build-riscv64-linux-gnu-spacemit/lib
|
||||
|
||||
echo "---install/lib---"
|
||||
ls -lh build-riscv64-linux-gnu-spacemit/install/lib
|
||||
|
||||
echo "---install/bin---"
|
||||
ls -lh build-riscv64-linux-gnu-spacemit/install/bin
|
||||
|
||||
file build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx
|
||||
|
||||
readelf -d build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx
|
||||
|
||||
- name: Copy files
|
||||
shell: bash
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/toolchain/bin:$PATH
|
||||
riscv64-unknown-linux-gnu-strip --version
|
||||
|
||||
SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
|
||||
|
||||
dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-linux-riscv64-spacemit-${{ matrix.lib_type }}
|
||||
mkdir $dst
|
||||
|
||||
cp -v $GITHUB_WORKSPACE/toolchain/sysroot/lib/ld-linux-riscv64-lp64d.so.1 build-riscv64-linux-gnu-spacemit/install/lib/
|
||||
|
||||
ls -lh build-riscv64-linux-gnu-spacemit/install/lib
|
||||
|
||||
cp -a build-riscv64-linux-gnu-spacemit/install/bin $dst/
|
||||
ls -lh $dst/bin/*
|
||||
riscv64-unknown-linux-gnu-strip $dst/bin/*
|
||||
ls -lh $dst
|
||||
|
||||
lib_type=${{ matrix.lib_type }}
|
||||
if [[ $lib_type == "shared" ]]; then
|
||||
cp -a build-riscv64-linux-gnu-spacemit/install/lib $dst/
|
||||
rm -fv $dst/lib/libasound.so
|
||||
fi
|
||||
|
||||
tree $dst
|
||||
|
||||
tar cjvf ${dst}.tar.bz2 $dst
|
||||
|
||||
- uses: actions/upload-artifact@v4
|
||||
if: matrix.lib_type == 'shared'
|
||||
with:
|
||||
name: sherpa-onnx-linux-riscv64-spacemit-shared
|
||||
path: sherpa-onnx-*linux-riscv64-spacemit-shared.tar.bz2
|
||||
|
||||
# https://huggingface.co/docs/hub/spaces-github-actions
|
||||
- name: Publish to huggingface
|
||||
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
|
||||
env:
|
||||
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
||||
uses: nick-fields/retry@v3
|
||||
with:
|
||||
max_attempts: 20
|
||||
timeout_seconds: 200
|
||||
shell: bash
|
||||
command: |
|
||||
SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
|
||||
git config --global user.email "csukuangfj@gmail.com"
|
||||
git config --global user.name "Fangjun Kuang"
|
||||
|
||||
rm -rf huggingface
|
||||
export GIT_CLONE_PROTECTION_ACTIVE=false
|
||||
|
||||
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface
|
||||
|
||||
cd huggingface
|
||||
dst=riscv64-spacemit/$SHERPA_ONNX_VERSION
|
||||
mkdir -p $dst
|
||||
|
||||
cp -v ../sherpa-onnx-*-shared.tar.bz2 $dst/
|
||||
|
||||
git status
|
||||
git lfs track "*.bz2"
|
||||
|
||||
git add .
|
||||
|
||||
git commit -m "upload sherpa-onnx-${SHERPA_ONNX_VERSION}-linux-riscv64-spacemit-shared.tar.bz2"
|
||||
|
||||
git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs main
|
||||
|
||||
- uses: actions/upload-artifact@v4
|
||||
if: matrix.lib_type == 'static'
|
||||
with:
|
||||
name: sherpa-onnx-linux-riscv64-spacemit-static
|
||||
path: sherpa-onnx-*linux-riscv64-spacemit-static.tar.bz2
|
||||
|
||||
- name: Release pre-compiled binaries and libs for riscv64 linux ${{ matrix.lib_type }}
|
||||
if: github.repository_owner == 'csukuangfj' && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
|
||||
uses: svenstaro/upload-release-action@v2
|
||||
with:
|
||||
file_glob: true
|
||||
overwrite: true
|
||||
file: sherpa-onnx-*linux-riscv64*.tar.bz2
|
||||
repo_name: k2-fsa/sherpa-onnx
|
||||
repo_token: ${{ secrets.UPLOAD_GH_SHERPA_ONNX_TOKEN }}
|
||||
tag: v1.12.11
|
||||
|
||||
- name: Release pre-compiled binaries and libs for riscv64 linux ${{ matrix.lib_type }}
|
||||
if: github.repository_owner == 'k2-fsa' && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
|
||||
uses: svenstaro/upload-release-action@v2
|
||||
with:
|
||||
file_glob: true
|
||||
overwrite: true
|
||||
file: sherpa-onnx-*linux-riscv64*.tar.bz2
|
||||
|
||||
- name: Test sherpa-onnx
|
||||
shell: bash
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/toolchain/bin:$PATH
|
||||
export PATH=$GITHUB_WORKSPACE/qemu-install/bin:$PATH
|
||||
export QEMU_LD_PREFIX=$GITHUB_WORKSPACE/toolchain/sysroot
|
||||
export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/toolchain/sysroot/lib
|
||||
export QEMU_ARGS="-cpu max,vlen=256,elen=64,vext_spec=v1.0"
|
||||
|
||||
ls -lh ./build-riscv64-linux-gnu-spacemit/bin
|
||||
|
||||
echo "----------sherpa-onnx----------"
|
||||
qemu-riscv64 ${QEMU_ARGS} ./build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx --help
|
||||
readelf -d ./build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx
|
||||
|
||||
echo "----------sherpa-onnx-offline----------"
|
||||
qemu-riscv64 ${QEMU_ARGS} ./build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx-offline --help
|
||||
readelf -d ./build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx-offline
|
||||
|
||||
echo "----------sherpa-onnx-offline-tts----------"
|
||||
qemu-riscv64 ${QEMU_ARGS} ./build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx-offline-tts --help
|
||||
readelf -d ./build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx-offline-tts
|
||||
|
||||
- name: Test streaming speech recognition
|
||||
shell: bash
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/toolchain/bin:$PATH
|
||||
export PATH=$GITHUB_WORKSPACE/qemu-install/bin:$PATH
|
||||
export QEMU_LD_PREFIX=$GITHUB_WORKSPACE/toolchain/sysroot
|
||||
export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/toolchain/sysroot/lib
|
||||
export QEMU_ARGS="-cpu max,vlen=256,elen=64,vext_spec=v1.0"
|
||||
echo "Some mistakes in ep graph partition, disable op Gather for spacemit-ep now, will be fixed soon."
|
||||
export SPACEMIT_EP_DISABLE_OP_TYPE_FILTER="Gather"
|
||||
|
||||
wget -q https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23.tar.bz2
|
||||
tar xvf sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23.tar.bz2
|
||||
rm sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23.tar.bz2
|
||||
|
||||
qemu-riscv64 ${QEMU_ARGS} ./build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx \
|
||||
--provider=spacemit \
|
||||
--tokens=./sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23/tokens.txt \
|
||||
--encoder=./sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23/encoder-epoch-99-avg-1.onnx \
|
||||
--decoder=./sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23/decoder-epoch-99-avg-1.onnx \
|
||||
--joiner=./sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23/joiner-epoch-99-avg-1.onnx \
|
||||
./sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23/test_wavs/0.wav
|
||||
|
||||
- name: Test offline tts
|
||||
shell: bash
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/toolchain/bin:$PATH
|
||||
export PATH=$GITHUB_WORKSPACE/qemu-install/bin:$PATH
|
||||
export QEMU_LD_PREFIX=$GITHUB_WORKSPACE/toolchain/sysroot
|
||||
export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/toolchain/sysroot/lib
|
||||
export QEMU_ARGS="-cpu max,vlen=256,elen=64,vext_spec=v1.0"
|
||||
echo "Some mistakes in ep graph partition, disable op Gather;Cast;ConvTranspose for spacemit-ep now, will be fixed soon."
|
||||
export SPACEMIT_EP_DISABLE_OP_TYPE_FILTER="Gather;Cast;ConvTranspose"
|
||||
|
||||
wget -q https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-en_US-lessac-medium.tar.bz2
|
||||
tar xf vits-piper-en_US-lessac-medium.tar.bz2
|
||||
rm vits-piper-en_US-lessac-medium.tar.bz2
|
||||
|
||||
qemu-riscv64 ${QEMU_ARGS} ./build-riscv64-linux-gnu-spacemit/bin/sherpa-onnx-offline-tts \
|
||||
--provider=spacemit \
|
||||
--vits-model=./vits-piper-en_US-lessac-medium/en_US-lessac-medium.onnx \
|
||||
--vits-data-dir=./vits-piper-en_US-lessac-medium/espeak-ng-data \
|
||||
--vits-tokens=./vits-piper-en_US-lessac-medium/tokens.txt \
|
||||
--output-filename=./liliana-piper-en_US-lessac-medium.wav \
|
||||
'liliana, the most beautiful and lovely assistant of our team!'
|
||||
|
||||
- uses: actions/upload-artifact@v4
|
||||
if: matrix.lib_type == 'shared'
|
||||
with:
|
||||
name: wave
|
||||
path: ./*.wav
|
||||
Regular → Executable
+2
@@ -161,3 +161,5 @@ sherpa-onnx-qnn-10-seconds-sense-voice-zh-en-ja-ko-yue-2024-07-17-int8-android-a
|
||||
sherpa-onnx-paraformer-zh-int8-2025-10-07
|
||||
sherpa-onnx-qnn-5-seconds-zipformer-ctc-zh-2025-07-03-int8-android-aarch64
|
||||
sherpa-onnx-qnn-10-seconds-zipformer-ctc-zh-2025-07-03-int8-android-aarch64
|
||||
build-riscv64-linux-gnu-spacemit/
|
||||
spacemit-toolchain*
|
||||
@@ -62,6 +62,7 @@ option(SHERPA_ONNX_BUILD_C_API_EXAMPLES "Whether to enable C API examples" ${SUG
|
||||
option(SHERPA_ONNX_ENABLE_RKNN "Whether to build for RKNN NPU " OFF)
|
||||
option(SHERPA_ONNX_ENABLE_ASCEND_NPU "Whether to build for Ascend NPU " OFF)
|
||||
option(SHERPA_ONNX_ENABLE_QNN "Whether to build for Qualcomm NPU" OFF)
|
||||
option(SHERPA_ONNX_ENABLE_SPACEMIT "Whether to build for SpacemiT CPUs " OFF)
|
||||
|
||||
set(SHERPA_ONNX_LINUX_ARM64_GPU_ONNXRUNTIME_VERSION "1.11.0" CACHE STRING "Used only for Linux ARM64 GPU. Set to 1.11.0 if you use CUDA 10.2 and cudnn8. Set it to 1.16.0 if you use CUDA 11.4 and cudnn8. Set it to 1.18.0 if you use CUDA 12.2 and cudnn8. Set it to 1.18.1 if you use CUDA 12.6 and cudnn9")
|
||||
|
||||
@@ -181,6 +182,7 @@ message(STATUS "SHERPA_ONNX_BUILD_C_API_EXAMPLES: ${SHERPA_ONNX_BUILD_C_API_EXAM
|
||||
message(STATUS "SHERPA_ONNX_ENABLE_RKNN: ${SHERPA_ONNX_ENABLE_RKNN}")
|
||||
message(STATUS "SHERPA_ONNX_ENABLE_ASCEND_NPU: ${SHERPA_ONNX_ENABLE_ASCEND_NPU}")
|
||||
message(STATUS "SHERPA_ONNX_ENABLE_QNN: ${SHERPA_ONNX_ENABLE_QNN}")
|
||||
message(STATUS "SHERPA_ONNX_ENABLE_SPACEMIT: ${SHERPA_ONNX_ENABLE_SPACEMIT}")
|
||||
message(STATUS "SHERPA_ONNX_LINK_D3D: ${SHERPA_ONNX_LINK_D3D}")
|
||||
|
||||
if(BUILD_SHARED_LIBS OR SHERPA_ONNX_ENABLE_JNI)
|
||||
@@ -308,6 +310,10 @@ if(SHERPA_ONNX_ENABLE_QNN)
|
||||
add_definitions(-DSHERPA_ONNX_ENABLE_QNN=1)
|
||||
endif()
|
||||
|
||||
if(SHERPA_ONNX_ENABLE_SPACEMIT)
|
||||
add_definitions(-DSHERPA_ONNX_ENABLE_SPACEMIT=1)
|
||||
endif()
|
||||
|
||||
if(SHERPA_ONNX_ENABLE_ASCEND_NPU)
|
||||
set(ASCEND_TOOLKIT_HOME)
|
||||
if(NOT DEFINED ENV{ASCEND_TOOLKIT_HOME})
|
||||
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
|
||||
SPACEMIT_TOOLCHAIN=spacemit-toolchain-linux-glibc-x86_64-v1.1.2
|
||||
DOWNLOAD_URL="https://archive.spacemit.com/toolchain/${SPACEMIT_TOOLCHAIN}.tar.xz"
|
||||
DOWNLOAD_FILE="./riscv-spacemit-toolchain.tar.gz"
|
||||
|
||||
if [ -n "$RISCV_ROOT_PATH" ] && [ -d "$RISCV_ROOT_PATH" ]; then
|
||||
echo "LOCAL RISCV_ROOT_PATH: $RISCV_ROOT_PATH"
|
||||
else
|
||||
wget -O "$DOWNLOAD_FILE" "$DOWNLOAD_URL" --quiet --show-progress
|
||||
tar -xf "$DOWNLOAD_FILE"
|
||||
export RISCV_ROOT_PATH=$PWD/${SPACEMIT_TOOLCHAIN}
|
||||
echo "DOWNLOAD RISCV_ROOT_PATH: $RISCV_ROOT_PATH"
|
||||
fi
|
||||
|
||||
|
||||
if [ x$dir = x"" ]; then
|
||||
dir=build-riscv64-linux-gnu-spacemit
|
||||
fi
|
||||
mkdir -p $dir
|
||||
cd $dir
|
||||
|
||||
if [ ! -f alsa-lib/src/.libs/libasound.so ]; then
|
||||
echo "Start to cross-compile alsa-lib"
|
||||
if [ ! -d alsa-lib ]; then
|
||||
git clone --depth 1 --branch v1.2.12 https://github.com/alsa-project/alsa-lib
|
||||
fi
|
||||
# If it shows:
|
||||
# ./gitcompile: line 79: libtoolize: command not found
|
||||
# Please use:
|
||||
# sudo apt-get install libtool m4 automake
|
||||
#
|
||||
pushd alsa-lib
|
||||
CC=$RISCV_ROOT_PATH/bin/riscv64-unknown-linux-gnu-gcc ./gitcompile --host=riscv64-unknown-linux-gnu
|
||||
popd
|
||||
echo "Finish cross-compiling alsa-lib"
|
||||
fi
|
||||
|
||||
export CPLUS_INCLUDE_PATH=$PWD/alsa-lib/include:$CPLUS_INCLUDE_PATH
|
||||
export SHERPA_ONNX_ALSA_LIB_DIR=$PWD/alsa-lib/src/.libs
|
||||
|
||||
if [[ x"$BUILD_SHARED_LIBS" == x"" ]]; then
|
||||
# By default, use shared libraries
|
||||
BUILD_SHARED_LIBS=ON
|
||||
fi
|
||||
|
||||
cmake \
|
||||
-DBUILD_PIPER_PHONMIZE_EXE=OFF \
|
||||
-DBUILD_PIPER_PHONMIZE_TESTS=OFF \
|
||||
-DBUILD_ESPEAK_NG_EXE=OFF \
|
||||
-DBUILD_ESPEAK_NG_TESTS=OFF \
|
||||
-DCMAKE_INSTALL_PREFIX=./install \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DBUILD_SHARED_LIBS=$BUILD_SHARED_LIBS \
|
||||
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_JNI=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_C_API=ON \
|
||||
-DSHERPA_ONNX_ENABLE_WEBSOCKET=ON \
|
||||
-DSHERPA_ONNX_ENABLE_SPACEMIT=ON \
|
||||
-DCMAKE_TOOLCHAIN_FILE=../toolchains/riscv64-linux-gnu-spacemit.toolchain.cmake \
|
||||
..
|
||||
|
||||
make VERBOSE=1 -j4
|
||||
make install/strip
|
||||
|
||||
# Enable it if only needed
|
||||
# cp -v $SHERPA_ONNX_ALSA_LIB_DIR/libasound.so* ./install/lib/
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
|
||||
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
|
||||
|
||||
if(NOT CMAKE_SYSTEM_NAME STREQUAL Linux)
|
||||
message(FATAL_ERROR "This file is for Linux only. Given: ${CMAKE_SYSTEM_NAME}")
|
||||
endif()
|
||||
|
||||
if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL riscv64)
|
||||
message(FATAL_ERROR "This file is for riscv64 only. Given: ${CMAKE_SYSTEM_PROCESSOR}")
|
||||
endif()
|
||||
|
||||
if(NOT BUILD_SHARED_LIBS)
|
||||
message(FATAL_ERROR "This file is for building shared libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}, SHERPA_ONNX_ENABLE_SPACEMIT: ${SHERPA_ONNX_ENABLE_SPACEMIT}")
|
||||
endif()
|
||||
|
||||
set(onnxruntime_pkg_name "spacemit-ort.riscv64.2.0.1.tar.gz")
|
||||
set(onnxruntime_URL "https://archive.spacemit.com/spacemit-ai/onnxruntime/${onnxruntime_pkg_name}")
|
||||
set(onnxruntime_HASH "SHA256=8a15035aca34d5fd95f24444d4c7843265c1a81f49d84ec6fe9c6d0fdf5b55cf")
|
||||
|
||||
# If you don't have access to the Internet,
|
||||
# please download onnxruntime to one of the following locations.
|
||||
# You can add more if you want.
|
||||
set(possible_file_locations
|
||||
$ENV{HOME}/Downloads/${onnxruntime_pkg_name}
|
||||
${CMAKE_SOURCE_DIR}/${onnxruntime_pkg_name}
|
||||
${CMAKE_BINARY_DIR}/${onnxruntime_pkg_name}
|
||||
/tmp/${onnxruntime_pkg_name}
|
||||
/star-fj/fangjun/download/github/${onnxruntime_pkg_name}
|
||||
)
|
||||
|
||||
foreach(f IN LISTS possible_file_locations)
|
||||
if(EXISTS ${f})
|
||||
set(onnxruntime_URL "${f}")
|
||||
file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
|
||||
message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")
|
||||
set(onnxruntime_URL2)
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
FetchContent_Declare(onnxruntime
|
||||
URL
|
||||
${onnxruntime_URL}
|
||||
${onnxruntime_URL2}
|
||||
URL_HASH ${onnxruntime_HASH}
|
||||
)
|
||||
|
||||
FetchContent_GetProperties(onnxruntime)
|
||||
if(NOT onnxruntime_POPULATED)
|
||||
message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")
|
||||
FetchContent_Populate(onnxruntime)
|
||||
endif()
|
||||
message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")
|
||||
|
||||
find_library(location_onnxruntime
|
||||
NAMES onnxruntime
|
||||
PATHS "${onnxruntime_SOURCE_DIR}/lib"
|
||||
NO_CMAKE_SYSTEM_PATH
|
||||
)
|
||||
|
||||
message(STATUS "location_onnxruntime: ${location_onnxruntime}")
|
||||
|
||||
find_library(location_spacemit_ep
|
||||
NAMES spacemit_ep
|
||||
PATHS "${onnxruntime_SOURCE_DIR}/lib"
|
||||
NO_CMAKE_SYSTEM_PATH
|
||||
)
|
||||
|
||||
message(STATUS "location_spacemit_ep: ${location_spacemit_ep}")
|
||||
|
||||
add_library(onnxruntime SHARED IMPORTED)
|
||||
add_library(spacemit_ep SHARED IMPORTED)
|
||||
|
||||
set_target_properties(onnxruntime PROPERTIES
|
||||
IMPORTED_LOCATION ${location_onnxruntime}
|
||||
IMPORTED_LOCATION "${onnxruntime_SOURCE_DIR}/lib/libonnxruntime.so"
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include/"
|
||||
)
|
||||
|
||||
set_target_properties(spacemit_ep PROPERTIES
|
||||
IMPORTED_LOCATION ${location_spacemit_ep}
|
||||
IMPORTED_LOCATION "${onnxruntime_SOURCE_DIR}/lib/libspacemit_ep.so"
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include/"
|
||||
)
|
||||
|
||||
file(GLOB onnxruntime_lib_files
|
||||
"${onnxruntime_SOURCE_DIR}/lib/libonnxruntime*")
|
||||
message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
|
||||
install(FILES ${onnxruntime_lib_files} DESTINATION lib)
|
||||
|
||||
file(GLOB spacemit_ep_lib_files
|
||||
"${onnxruntime_SOURCE_DIR}/lib/libspacemit_ep*")
|
||||
message(STATUS "spacemit_ep lib files: ${spacemit_ep_lib_files}")
|
||||
install(FILES ${spacemit_ep_lib_files} DESTINATION lib)
|
||||
Regular → Executable
+3
-1
@@ -7,7 +7,9 @@ function(download_onnxruntime)
|
||||
if(SHERPA_ONNX_ENABLE_WASM)
|
||||
include(onnxruntime-wasm-simd)
|
||||
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL riscv64)
|
||||
if(BUILD_SHARED_LIBS)
|
||||
if(SHERPA_ONNX_ENABLE_SPACEMIT)
|
||||
include(onnxruntime-linux-riscv64-spacemit)
|
||||
elseif(BUILD_SHARED_LIBS)
|
||||
include(onnxruntime-linux-riscv64)
|
||||
else()
|
||||
include(onnxruntime-linux-riscv64-static)
|
||||
|
||||
Regular → Executable
+8
@@ -339,6 +339,14 @@ if(SHERPA_ONNX_ENABLE_QNN)
|
||||
target_include_directories(sherpa-onnx-core PRIVATE ${QNN_SDK_ROOT}/include/QNN)
|
||||
endif()
|
||||
|
||||
if(SHERPA_ONNX_ENABLE_SPACEMIT)
|
||||
if(TARGET spacemit_ep)
|
||||
target_link_libraries(sherpa-onnx-core spacemit_ep)
|
||||
else()
|
||||
target_link_libraries(sherpa-onnx-core ${spacemit_ep_lib_files})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(TARGET onnxruntime)
|
||||
target_link_libraries(sherpa-onnx-core onnxruntime)
|
||||
else()
|
||||
|
||||
@@ -29,6 +29,8 @@ Provider StringToProvider(std::string s) {
|
||||
return Provider::kTRT;
|
||||
} else if (s == "directml") {
|
||||
return Provider::kDirectML;
|
||||
} else if (s == "spacemit") {
|
||||
return Provider::kSpacemiT;
|
||||
} else {
|
||||
SHERPA_ONNX_LOGE("Unsupported string: %s. Fallback to cpu", s.c_str());
|
||||
return Provider::kCPU;
|
||||
|
||||
@@ -21,6 +21,7 @@ enum class Provider {
|
||||
kNNAPI = 4, // NnapiExecutionProvider
|
||||
kTRT = 5, // TensorRTExecutionProvider
|
||||
kDirectML = 6, // DmlExecutionProvider
|
||||
kSpacemiT = 7, // SpacemiTExecutionProvider
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Regular → Executable
+31
@@ -23,6 +23,10 @@
|
||||
#include "dml_provider_factory.h" // NOLINT
|
||||
#endif
|
||||
|
||||
#if defined(SHERPA_ONNX_ENABLE_SPACEMIT)
|
||||
#include "spacemit_ort_env.h" // NOLINT
|
||||
#endif
|
||||
|
||||
namespace sherpa_onnx {
|
||||
|
||||
static void OrtStatusFailure(OrtStatus *status, const char *s) {
|
||||
@@ -239,6 +243,33 @@ Ort::SessionOptions GetSessionOptionsImpl(
|
||||
(int32_t)__ANDROID_API__);
|
||||
#else
|
||||
SHERPA_ONNX_LOGE("NNAPI is for Android only. Fallback to cpu");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case Provider::kSpacemiT: {
|
||||
#if defined(SHERPA_ONNX_ENABLE_SPACEMIT)
|
||||
SHERPA_ONNX_LOGE("Use SpacemiT Execution Provider");
|
||||
// when using SpacemiT Execution Provider, set intra_op_num_threads and
|
||||
// inter_op_num_threads to 1 can improve performance.
|
||||
// all ops run on ep, no need to create multiple threads in onnxruntime.
|
||||
// ep will create SPACEMIT_EP_INTRA_THREAD_NUM threads as intra threads.
|
||||
std::unordered_map<std::string, std::string> provider_options;
|
||||
SHERPA_ONNX_LOGE("Set IntraOpNumThreads to 1");
|
||||
sess_opts.SetIntraOpNumThreads(1);
|
||||
SHERPA_ONNX_LOGE("Set InterOpNumThreads to 1");
|
||||
sess_opts.SetInterOpNumThreads(1);
|
||||
SHERPA_ONNX_LOGE("Set SPACEMIT_EP_INTRA_THREAD_NUM to %d", num_threads);
|
||||
provider_options.insert(
|
||||
std::make_pair("SPACEMIT_EP_INTRA_THREAD_NUM", std::to_string(num_threads)));
|
||||
OrtStatus* sts = Ort::SessionOptionsSpaceMITEnvInit(sess_opts, provider_options);
|
||||
if (sts) {
|
||||
const auto &api = Ort::GetApi();
|
||||
const char *msg = api.GetErrorMessage(sts);
|
||||
SHERPA_ONNX_LOGE("Failed to enable SpacemiT Execution Provider: %s. Fallback to cpu", msg);
|
||||
api.ReleaseStatus(sts);
|
||||
}
|
||||
#else
|
||||
SHERPA_ONNX_LOGE("SpacemiT Execution Provider is for SpacemiT AI-CPUs only. Fallback to cpu!");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
set(CMAKE_SYSTEM_NAME Linux)
|
||||
set(CMAKE_SYSTEM_PROCESSOR riscv64)
|
||||
set(CMAKE_SYSTEM_VERSION 1)
|
||||
|
||||
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "^(riscv)")
|
||||
message(STATUS "HOST SYSTEM ${CMAKE_HOST_SYSTEM_PROCESSOR}")
|
||||
else()
|
||||
set(GNU_MACHINE riscv64-unknown-linux-gnu CACHE STRING "GNU compiler triple")
|
||||
if(DEFINED ENV{RISCV_ROOT_PATH})
|
||||
file(TO_CMAKE_PATH $ENV{RISCV_ROOT_PATH} RISCV_ROOT_PATH)
|
||||
else()
|
||||
message(FATAL_ERROR "RISCV_ROOT_PATH env must be defined")
|
||||
endif()
|
||||
|
||||
set(RISCV_ROOT_PATH ${RISCV_ROOT_PATH} CACHE STRING "root path to riscv toolchain")
|
||||
set(CMAKE_C_COMPILER ${RISCV_ROOT_PATH}/bin/riscv64-unknown-linux-gnu-gcc)
|
||||
set(CMAKE_CXX_COMPILER ${RISCV_ROOT_PATH}/bin/riscv64-unknown-linux-gnu-g++)
|
||||
set(CMAKE_STRIP ${RISCV_ROOT_PATH}/bin/riscv64-unknown-linux-gnu-strip)
|
||||
set(CMAKE_FIND_ROOT_PATH "${RISCV_ROOT_PATH}/sysroot")
|
||||
set(CMAKE_SYSROOT "${RISCV_ROOT_PATH}/sysroot")
|
||||
endif()
|
||||
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
|
||||
set(CMAKE_C_FLAGS "-march=rv64gcv_zfh_zvfh_zba_zicbop_zihintpause -mabi=lp64d -ftree-vectorize ${CMAKE_C_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS "-march=rv64gcv_zfh_zvfh_zba_zicbop_zihintpause -mabi=lp64d -ftree-vectorize ${CXX_FLAGS}")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -latomic -lrt -lpthread")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_SYSROOT}")
|
||||
Reference in new issue
Block a user