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

Add Rocm execution provider for AMD GPUs #1110

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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/linux-rocm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
name: linux-rocm

on:
push:
branches:
- master
- rocm
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'
paths:
- '.github/workflows/linux-rocm.yaml'
- '.github/scripts/test-online-transducer.sh'
- '.github/scripts/test-online-paraformer.sh'
- '.github/scripts/test-offline-transducer.sh'
- '.github/scripts/test-offline-ctc.sh'
- '.github/scripts/test-online-ctc.sh'
- '.github/scripts/test-offline-tts.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
- 'sherpa-onnx/c-api/*'
- 'c-api-examples/**'
pull_request:
branches:
- master
paths:
- '.github/workflows/linux-rocm.yaml'
- '.github/scripts/test-online-transducer.sh'
- '.github/scripts/test-online-paraformer.sh'
- '.github/scripts/test-offline-transducer.sh'
- '.github/scripts/test-offline-ctc.sh'
- '.github/scripts/test-online-ctc.sh'
- '.github/scripts/test-online-ctc.sh'
- '.github/scripts/test-offline-tts.sh'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
- 'sherpa-onnx/c-api/*'

workflow_dispatch:

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

jobs:
linux_rocm:
runs-on: ${{ matrix.os }}
name: ${{ matrix.build_type }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
# build_type: [Release, Debug]
build_type: [Release]

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

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

- name: Configure CMake
shell: bash
run: |
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
cmake --version

mkdir build
cd build
cmake \
-D CMAKE_BUILD_TYPE=${{ matrix.build_type }} \
-D CMAKE_INSTALL_PREFIX=./install \
-D BUILD_SHARED_LIBS=ON \
-D SHERPA_ONNX_ENABLE_ROCM=ON \
..

- name: Build sherpa-onnx for ubuntu
shell: bash
run: |
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"

cd build
make -j2
make install

ls -lh lib
ls -lh bin

echo "----"
ls -lh install/lib

echo "----"
ls -lh install/bin

- name: Display dependencies of sherpa-onnx for linux
shell: bash
run: |
file build/bin/sherpa-onnx
readelf -d build/bin/sherpa-onnx
echo "----"
ldd build/bin/sherpa-onnx

- name: Test spoken language identification
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline-language-identification

.github/scripts/test-spoken-language-identification.sh

- name: Test online CTC
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx

.github/scripts/test-online-ctc.sh

- name: Test offline TTS
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline-tts

.github/scripts/test-offline-tts.sh

- name: Test online paraformer
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx

.github/scripts/test-online-paraformer.sh


- name: Test offline Whisper
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline

.github/scripts/test-offline-whisper.sh

- name: Test offline CTC
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline

.github/scripts/test-offline-ctc.sh

- name: Test offline transducer
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline

.github/scripts/test-offline-transducer.sh

- name: Test online transducer
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx

.github/scripts/test-online-transducer.sh

- name: Test online transducer (C API)
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=decode-file-c-api

.github/scripts/test-online-transducer.sh

- name: Copy files
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
shell: bash
run: |
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-x64-rocm
mkdir $dst

cp -a build/install/bin $dst/
cp -a build/install/lib $dst/
cp -a build/install/include $dst/

tree $dst

tar cjvf ${dst}.tar.bz2 $dst

- name: Release pre-compiled binaries and libs for linux x64
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-x64-rocm.tar.bz2
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ option(SHERPA_ONNX_ENABLE_PORTAUDIO "Whether to build with portaudio" ON)
option(SHERPA_ONNX_ENABLE_JNI "Whether to build JNI internface" OFF)
option(SHERPA_ONNX_ENABLE_C_API "Whether to build C API" ON)
option(SHERPA_ONNX_ENABLE_WEBSOCKET "Whether to build webscoket server/client" ON)
option(SHERPA_ONNX_ENABLE_GPU "Enable ONNX Runtime GPU support" OFF)
option(SHERPA_ONNX_ENABLE_GPU "Enable ONNX Runtime NVIDIA GPU support" OFF)
option(SHERPA_ONNX_ENABLE_ROCM "Enable ONNX Runtime AMD GPU support" OFF)
option(SHERPA_ONNX_ENABLE_WASM "Whether to enable WASM" OFF)
option(SHERPA_ONNX_ENABLE_WASM_TTS "Whether to enable WASM for TTS" OFF)
option(SHERPA_ONNX_ENABLE_WASM_ASR "Whether to enable WASM for ASR" OFF)
Expand Down Expand Up @@ -82,6 +83,10 @@ if(SHERPA_ONNX_ENABLE_JNI AND NOT BUILD_SHARED_LIBS)
endif()

if(SHERPA_ONNX_ENABLE_GPU)
if(SHERPA_ONNX_ENABLE_ROCM)
message(FATAL_ERROR "Both SHERPA_ONNX_ENABLE_GPU and SHERPA_ONNX_ENABLE_ROCM are ON. Please set at most one of them to ON.")
endif()

message(WARNING "\
Compiling for NVIDIA GPU is enabled. Please make sure cudatoolkit
is installed on your system. Otherwise, you will get errors at runtime.
Expand All @@ -94,6 +99,16 @@ to install CUDA toolkit if you have not installed it.")
endif()
endif()

if(SHERPA_ONNX_ENABLE_ROCM)
message(WARNING "\
Compiling for AMD GPU is enabled. Please make sure ROCm
is installed on your system. Otherwise, you will get errors at runtime.")
if(NOT BUILD_SHARED_LIBS)
message(STATUS "Set BUILD_SHARED_LIBS to ON since SHERPA_ONNX_ENABLE_ROCM is ON")
set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE)
endif()
endif()

# see https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html
# https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake
if(MSVC)
Expand All @@ -117,6 +132,7 @@ message(STATUS "SHERPA_ONNX_ENABLE_JNI ${SHERPA_ONNX_ENABLE_JNI}")
message(STATUS "SHERPA_ONNX_ENABLE_C_API ${SHERPA_ONNX_ENABLE_C_API}")
message(STATUS "SHERPA_ONNX_ENABLE_WEBSOCKET ${SHERPA_ONNX_ENABLE_WEBSOCKET}")
message(STATUS "SHERPA_ONNX_ENABLE_GPU ${SHERPA_ONNX_ENABLE_GPU}")
message(STATUS "SHERPA_ONNX_ENABLE_ROCM ${SHERPA_ONNX_ENABLE_ROCM}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM ${SHERPA_ONNX_ENABLE_WASM}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM_TTS ${SHERPA_ONNX_ENABLE_WASM_TTS}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM_ASR ${SHERPA_ONNX_ENABLE_WASM_ASR}")
Expand Down
101 changes: 101 additions & 0 deletions cmake/onnxruntime-linux-x86_64-rocm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright (c) 2022-2023 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 x86_64)
message(FATAL_ERROR "This file is for x86_64 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}")
endif()

if(NOT SHERPA_ONNX_ENABLE_ROCM)
message(FATAL_ERROR "This file is for AMD GPU only. Given SHERPA_ONNX_ENABLE_ROCM: ${SHERPA_ONNX_ENABLE_ROCM}")
endif()

set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.18.1/onnxruntime-linux-x64-rocm-Release-1.18.1.zip")
set(onnxruntime_URL2 "https://hub.nuaa.cf/csukuangfj/onnxruntime-libs/releases/download/v1.18.1/onnxruntime-linux-x64-rocm-Release-1.18.1.zip")
set(onnxruntime_HASH "SHA256=fe6674d0d4d72d9361667de2ada44a81591c3769c63a87421636317590be659e")

# 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-x64-rocm-Release-1.18.1.zip
${CMAKE_SOURCE_DIR}/onnxruntime-linux-x64-rocm-Release-1.18.1.zip
${CMAKE_BINARY_DIR}/onnxruntime-linux-x64-rocm-Release-1.18.1.zip
/tmp/onnxruntime-linux-x64-rocm-Release-1.18.1.zip
/star-fj/fangjun/download/github/onnxruntime-linux-x64-rocm-Release-1.18.1.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}")

find_library(location_onnxruntime onnxruntime
PATHS
"${onnxruntime_SOURCE_DIR}/lib"
NO_CMAKE_SYSTEM_PATH
)

message(STATUS "location_onnxruntime: ${location_onnxruntime}")

add_library(onnxruntime SHARED IMPORTED)

set_target_properties(onnxruntime PROPERTIES
IMPORTED_LOCATION ${location_onnxruntime}
INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include"
)

find_library(location_onnxruntime_rocm_lib onnxruntime_providers_rocm
PATHS
"${onnxruntime_SOURCE_DIR}/lib"
NO_CMAKE_SYSTEM_PATH
)

add_library(onnxruntime_providers_rocm SHARED IMPORTED)
set_target_properties(onnxruntime_providers_rocm PROPERTIES
IMPORTED_LOCATION ${location_onnxruntime_rocm_lib}
)
message(STATUS "location_onnxruntime_rocm_lib: ${location_onnxruntime_rocm_lib}")

# for libonnxruntime_providers_shared.so
find_library(location_onnxruntime_providers_shared_lib onnxruntime_providers_shared
PATHS
"${onnxruntime_SOURCE_DIR}/lib"
NO_CMAKE_SYSTEM_PATH
)
add_library(onnxruntime_providers_shared SHARED IMPORTED)
set_target_properties(onnxruntime_providers_shared PROPERTIES
IMPORTED_LOCATION ${location_onnxruntime_providers_shared_lib}
)
message(STATUS "location_onnxruntime_providers_shared_lib: ${location_onnxruntime_providers_shared_lib}")

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)
2 changes: 2 additions & 0 deletions cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function(download_onnxruntime)
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
if(SHERPA_ONNX_ENABLE_GPU)
include(onnxruntime-linux-x86_64-gpu)
elseif(SHERPA_ONNX_ENABLE_ROCM)
include(onnxruntime-linux-x86_64-rocm)
elseif(BUILD_SHARED_LIBS)
include(onnxruntime-linux-x86_64)
else()
Expand Down
7 changes: 7 additions & 0 deletions sherpa-onnx/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ if(SHERPA_ONNX_ENABLE_GPU)
)
endif()

if(SHERPA_ONNX_ENABLE_ROCM)
target_link_libraries(sherpa-onnx-core
onnxruntime_providers_rocm
onnxruntime_providers_shared
)
endif()

if(BUILD_SHARED_LIBS)
target_link_libraries(sherpa-onnx-core onnxruntime)
else()
Expand Down
Loading
Loading