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

Support RISC-V #609

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 205 additions & 0 deletions .github/workflows/riscv64-linux.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
name: riscv64-linux

on:
push:
branches:
- master
paths:
- '.github/workflows/riscv64-linux.yaml'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
- 'toolchains/riscv64-linux-gnu.toolchain.cmake'
- 'build-riscv64-linux-gnu.sh'
tags:
- '*'
pull_request:
branches:
- master
paths:
- '.github/workflows/riscv64-linux.yaml'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
- 'toolchains/riscv64-linux-gnu.toolchain.cmake'
- 'build-riscv64-linux-gnu.sh'

workflow_dispatch:

concurrency:
group: riscv64-linux-${{ github.ref }}
cancel-in-progress: true

jobs:
riscv64_linux:
runs-on: ${{ matrix.os }}
name: ${{ matrix.os }} ${{ matrix.lib_type }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
lib_type: [static, shared]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: ccache
uses: hendrikmuhs/[email protected]
with:
key: ${{ matrix.os }}-riscv64-${{ matrix.lib_type }}

- name: cache-qemu
id: cache-qemu
uses: actions/cache@v4
with:
path: qemu-install
key: qemu-riscv-install-20240225

- name: install-qemu-build-deps
if: steps.cache-qemu.outputs.cache-hit != 'true'
run: |
sudo apt-get update
sudo apt-get install autoconf automake autotools-dev ninja-build

- name: checkout-qemu
if: steps.cache-qemu.outputs.cache-hit != 'true'
uses: actions/checkout@v3
with:
repository: qemu/qemu
path: qemu

- name: qemu
if: steps.cache-qemu.outputs.cache-hit != 'true'
run: |
cd qemu
./configure --prefix=$GITHUB_WORKSPACE/qemu-install --target-list=riscv64-linux-user --disable-system
make -j2
make install
ls -lh $GITHUB_WORKSPACE/qemu-install
ls -lh $GITHUB_WORKSPACE/qemu-install/bin

- name: cache-toolchain
id: cache-toolchain
uses: actions/cache@v4
with:
path: toolchain
key: riscv64-glibc-ubuntu-20.04-gcc-nightly-2023.10.17-nightly

- name: Download toolchain
if: steps.cache-toolchain.outputs.cache-hit != 'true'
shell: bash
run: |
mkdir $GITHUB_WORKSPACE/toolchain
wget -q https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2023.10.17/riscv64-glibc-ubuntu-20.04-gcc-nightly-2023.10.17-nightly.tar.gz
tar xvf ./riscv64-glibc-ubuntu-20.04-gcc-nightly-2023.10.17-nightly.tar.gz --strip-components 1 -C $GITHUB_WORKSPACE/toolchain

- 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-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

./build-riscv64-linux-gnu.sh

ls -lh build-riscv64-linux-gnu/bin
ls -lh build-riscv64-linux-gnu/lib

file build-riscv64-linux-gnu/bin/sherpa-onnx

- 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

ls -lh ./build-riscv64-linux-gnu/bin

echo "----------sherpa-onnx----------"
qemu-riscv64 ./build-riscv64-linux-gnu/bin/sherpa-onnx --help
readelf -d ./build-riscv64-linux-gnu/bin/sherpa-onnx

echo "----------sherpa-onnx-offline----------"
qemu-riscv64 ./build-riscv64-linux-gnu/bin/sherpa-onnx-offline --help
readelf -d ./build-riscv64-linux-gnu/bin/sherpa-onnx-offline

echo "----------sherpa-onnx-offline-tts----------"
qemu-riscv64 ./build-riscv64-linux-gnu/bin/sherpa-onnx-offline-tts --help
readelf -d ./build-riscv64-linux-gnu/bin/sherpa-onnx-offline-tts

- 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-${{ matrix.lib_type }}
mkdir $dst

ls -lh build-riscv64-linux-gnu/install/lib

cp -a build-riscv64-linux-gnu/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/install/lib $dst/
rm -fv $dst/lib/libasound.so
rm -fv $dst/lib/libonnxruntime.so
rm -fv $dst/lib/libsherpa-onnx-fst.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-shared
path: sherpa-onnx-*linux-riscv64-shared.tar.bz2

- uses: actions/upload-artifact@v4
if: matrix.lib_type == 'static'
with:
name: sherpa-onnx-linux-riscv64-static
path: sherpa-onnx-*linux-riscv64-static.tar.bz2

- name: Release pre-compiled binaries and libs for riscv64 linux ${{ matrix.lib_type }}
if: github.repository_owner == 'csukuangfj' || 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
71 changes: 71 additions & 0 deletions build-riscv64-linux-gnu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash

if ! command -v riscv64-unknown-linux-gnu-g++ &> /dev/null; then
echo "Please install the toolchain first."
echo
echo "You can use the following command to install the toolchain:"
echo
echo " mkdir -p $HOME/software"
echo " cd $HOME/software"
echo " mkdir riscv64-glibc-ubuntu-18.04-nightly-2022.11.12-nightly"
echo " cd riscv64-glibc-ubuntu-18.04-nightly-2022.11.12-nightly"
echo " wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2022.11.12/riscv64-glibc-ubuntu-18.04-nightly-2022.11.12-nightly.tar.gz"
echo " tar xvf riscv64-glibc-ubuntu-18.04-nightly-2022.11.12-nightly.tar.gz --strip-components=1"
echo " export PATH=$HOME/software/riscv64-glibc-ubuntu-18.04-nightly-2022.11.12-nightly/bin"
echo
exit 1
fi

if [ x$dir = x"" ]; then
dir=build-riscv64-linux-gnu
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 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=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 static link
BUILD_SHARED_LIBS=OFF
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=OFF \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DCMAKE_TOOLCHAIN_FILE=../toolchains/riscv64-linux-gnu.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/
64 changes: 64 additions & 0 deletions cmake/onnxruntime-linux-riscv64-static.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) 2022-2024 Xiaomi Corporation
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(BUILD_SHARED_LIBS)
message(FATAL_ERROR "This file is for building static libraries. BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}")
endif()

set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.18.0/onnxruntime-linux-riscv64-static_lib-1.18.0.zip")
set(onnxruntime_URL2 "https://hub.nuaa.cf/csukuangfj/onnxruntime-libs/releases/download/v1.18.0/onnxruntime-linux-riscv64-static_lib-1.18.0.zip")
set(onnxruntime_HASH "SHA256=6791d695d17118db6815364c975a9d7ea9a8909754516ed1b089fe015c20912e")

# 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-linux-riscv64-static_lib-1.18.0.zip
${CMAKE_SOURCE_DIR}/onnxruntime-linux-riscv64-static_lib-1.18.0.zip
${CMAKE_BINARY_DIR}/onnxruntime-linux-riscv64-static_lib-1.18.0.zip
/tmp/onnxruntime-linux-riscv64-static_lib-1.18.0.zip
/star-fj/fangjun/download/github/onnxruntime-linux-riscv64-static_lib-1.18.0.zip
)

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}")

# for static libraries, we use onnxruntime_lib_files directly below
include_directories(${onnxruntime_SOURCE_DIR}/include)

file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/lib*.a")

set(onnxruntime_lib_files ${onnxruntime_lib_files} PARENT_SCOPE)

message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
install(FILES ${onnxruntime_lib_files} DESTINATION lib)
Loading
Loading