Skip to content

Commit e2bf3dc

Browse files
authored
Fix building for onnxruntime >= 1.11.0 (k2-fsa#2981)
1 parent 57a8d88 commit e2bf3dc

5 files changed

Lines changed: 357 additions & 4 deletions

File tree

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
name: test-onnxruntime-version
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- test-onnxruntime
8+
tags:
9+
- 'v[0-9]+.[0-9]+.[0-9]+*'
10+
11+
workflow_dispatch:
12+
13+
concurrency:
14+
group: test-onnxrntime-version-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
jobs:
18+
macos:
19+
runs-on: ${{ matrix.os }}
20+
name: onnxruntime ${{ matrix.version }}
21+
strategy:
22+
fail-fast: false
23+
matrix:
24+
os: [macos-latest]
25+
version: ["1.11.0", "1.11.1", "1.12.0", "1.12.1", "1.13.1", "1.14.0", "1.14.1", "1.15.0", "1.15.1", "1.16.1", "1.16.2", "1.17.0", "1.17.1", "1.17.3", "1.18.0", "1.18.1", "1.19.0", "1.19.2", "1.20.0", "1.20.1", "1.20.2", "1.21.0", "1.21.1", "1.22.0", "1.22.1", "1.22.2", "1.23.0", "1.23.1", "1.23.2"]
26+
27+
steps:
28+
- uses: actions/checkout@v4
29+
with:
30+
fetch-depth: 0
31+
32+
- name: Update version
33+
shell: bash
34+
run: |
35+
./new-release.sh
36+
git diff .
37+
38+
- name: ccache
39+
uses: hendrikmuhs/ccache-action@v1.2
40+
with:
41+
key: ${{ matrix.os }}-onnxruntime-${{ matrix.version }}
42+
43+
- name: Download onnxruntime ${{ matrix.version }}
44+
shell: bash
45+
run: |
46+
version=${{ matrix.version }}
47+
curl -SL -O https://github.com/microsoft/onnxruntime/releases/download/v${version}/onnxruntime-osx-universal2-${version}.tgz
48+
tar xvf onnxruntime-osx-universal2-${version}.tgz
49+
ls -lh onnxruntime-osx-universal2-${version}
50+
51+
ls -lh onnxruntime-osx-universal2-${version}
52+
echo "---"
53+
ls -lh onnxruntime-osx-universal2-${version}/include
54+
echo "---"
55+
ls -lh onnxruntime-osx-universal2-${version}/lib
56+
57+
- name: Configure CMake
58+
shell: bash
59+
run: |
60+
version=${{ matrix.version }}
61+
onnxruntime_dir=$PWD/onnxruntime-osx-universal2-${version}
62+
export SHERPA_ONNXRUNTIME_LIB_DIR=$onnxruntime_dir/lib/
63+
export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$onnxruntime_dir/include/
64+
65+
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
66+
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
67+
cmake --version
68+
69+
mkdir build
70+
cd build
71+
72+
cmake \
73+
-D BUILD_SHARED_LIBS=ON \
74+
-D CMAKE_OSX_ARCHITECTURES='arm64;x86_64' \
75+
-D CMAKE_INSTALL_PREFIX=./install \
76+
..
77+
78+
- name: Build sherpa-onnx for macos
79+
shell: bash
80+
run: |
81+
version=${{ matrix.version }}
82+
onnxruntime_dir=$PWD/onnxruntime-osx-universal2-${version}
83+
export SHERPA_ONNXRUNTIME_LIB_DIR=$onnxruntime_dir/lib/
84+
export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$onnxruntime_dir/include/
85+
86+
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
87+
88+
cd build
89+
make -j2
90+
make install
91+
92+
ls -lh lib
93+
ls -lh bin
94+
95+
file ./bin/sherpa-onnx
96+
97+
rm -fv ./install/include/cargs.h
98+
rm -fv ./install/lib/cargs.h
99+
rm -fv ./install/lib/libcargs.dylib
100+
rm -fv ./install/lib/libcargs.a
101+
rm -rfv ./install/lib/pkgconfig
102+
103+
- name: Display dependencies of sherpa-onnx for macos
104+
shell: bash
105+
run: |
106+
file bin/sherpa-onnx
107+
otool -L build/bin/sherpa-onnx
108+
otool -l build/bin/sherpa-onnx
109+
110+
- name: Copy files
111+
shell: bash
112+
run: |
113+
SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
114+
115+
dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-onnxruntime-${{ matrix.version }}-osx-universal2-shared
116+
mkdir $dst
117+
118+
cp -a build/install/bin $dst/
119+
mkdir $dst/lib
120+
cp -a build/install/lib/*.dylib* $dst/lib/
121+
cp -a build/install/include $dst/
122+
123+
brew install tree
124+
tree $dst
125+
126+
tar cjvf ${dst}.tar.bz2 $dst
127+
128+
- name: Release pre-compiled binaries and libs for macOS
129+
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
130+
uses: svenstaro/upload-release-action@v2
131+
with:
132+
file_glob: true
133+
overwrite: true
134+
file: sherpa-onnx-*osx-universal2*.tar.bz2
135+
136+
- name: Test offline CTC
137+
shell: bash
138+
run: |
139+
export PATH=$PWD/build/bin:$PATH
140+
export EXE=sherpa-onnx-offline
141+
142+
.github/scripts/test-offline-ctc.sh
143+
144+
- name: Test offline speech denoiser
145+
shell: bash
146+
run: |
147+
du -h -d1 .
148+
export PATH=$PWD/build/bin:$PATH
149+
export EXE=sherpa-onnx-offline-denoiser
150+
151+
.github/scripts/test-offline-speech-denoiser.sh
152+
153+
- name: Test offline TTS
154+
shell: bash
155+
run: |
156+
export PATH=$PWD/build/bin:$PATH
157+
export EXE=sherpa-onnx-offline-tts
158+
159+
.github/scripts/test-offline-tts.sh
160+
161+
- name: Test offline Moonshine
162+
shell: bash
163+
run: |
164+
export PATH=$PWD/build/bin:$PATH
165+
export EXE=sherpa-onnx-offline
166+
167+
.github/scripts/test-offline-moonshine.sh
168+
169+
- name: Test C++ API
170+
shell: bash
171+
run: |
172+
du -h -d1 .
173+
export PATH=$PWD/build/bin:$PATH
174+
export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api
175+
export CXX_WHISPER_EXE=whisper-cxx-api
176+
export CXX_SENSE_VOICE_EXE=sense-voice-cxx-api
177+
178+
.github/scripts/test-cxx-api.sh
179+
du -h -d1 .
180+
181+
- name: Test offline speaker diarization
182+
shell: bash
183+
run: |
184+
du -h -d1 .
185+
export PATH=$PWD/build/bin:$PATH
186+
export EXE=sherpa-onnx-offline-speaker-diarization
187+
188+
.github/scripts/test-speaker-diarization.sh
189+
190+
- name: Test offline transducer
191+
shell: bash
192+
run: |
193+
export PATH=$PWD/build/bin:$PATH
194+
export EXE=sherpa-onnx-offline
195+
196+
.github/scripts/test-offline-transducer.sh
197+
198+
- name: Test online punctuation
199+
shell: bash
200+
run: |
201+
export PATH=$PWD/build/bin:$PATH
202+
export EXE=sherpa-onnx-online-punctuation
203+
204+
.github/scripts/test-online-punctuation.sh
205+
206+
- name: Test online CTC
207+
shell: bash
208+
run: |
209+
export PATH=$PWD/build/bin:$PATH
210+
export EXE=sherpa-onnx
211+
212+
.github/scripts/test-online-ctc.sh
213+
214+
- name: Test offline punctuation
215+
shell: bash
216+
run: |
217+
export PATH=$PWD/build/bin:$PATH
218+
export EXE=sherpa-onnx-offline-punctuation
219+
220+
.github/scripts/test-offline-punctuation.sh
221+
222+
- name: Test C API
223+
shell: bash
224+
run: |
225+
export PATH=$PWD/build/bin:$PATH
226+
export SLID_EXE=spoken-language-identification-c-api
227+
export SID_EXE=speaker-identification-c-api
228+
export AT_EXE=audio-tagging-c-api
229+
export PUNCT_EXE=add-punctuation-c-api
230+
231+
.github/scripts/test-c-api.sh
232+
233+
- name: Test Audio tagging
234+
shell: bash
235+
run: |
236+
export PATH=$PWD/build/bin:$PATH
237+
export EXE=sherpa-onnx-offline-audio-tagging
238+
239+
.github/scripts/test-audio-tagging.sh
240+
241+
- name: Test spoken language identification (C++ API)
242+
shell: bash
243+
run: |
244+
export PATH=$PWD/build/bin:$PATH
245+
export EXE=sherpa-onnx-offline-language-identification
246+
247+
.github/scripts/test-spoken-language-identification.sh
248+
249+
- name: Test transducer kws
250+
shell: bash
251+
run: |
252+
export PATH=$PWD/build/bin:$PATH
253+
export EXE=sherpa-onnx-keyword-spotter
254+
255+
.github/scripts/test-kws.sh
256+
257+
- name: Test online paraformer
258+
shell: bash
259+
run: |
260+
export PATH=$PWD/build/bin:$PATH
261+
export EXE=sherpa-onnx
262+
263+
.github/scripts/test-online-paraformer.sh
264+
265+
- name: Test offline Whisper
266+
if: matrix.build_type != 'Debug'
267+
shell: bash
268+
run: |
269+
export PATH=$PWD/build/bin:$PATH
270+
export EXE=sherpa-onnx-offline
271+
272+
.github/scripts/test-offline-whisper.sh
273+
274+
- name: Test online transducer
275+
shell: bash
276+
run: |
277+
export PATH=$PWD/build/bin:$PATH
278+
export EXE=sherpa-onnx
279+
280+
.github/scripts/test-online-transducer.sh
281+
282+
- name: Test online transducer (C API)
283+
shell: bash
284+
run: |
285+
export PATH=$PWD/build/bin:$PATH
286+
export EXE=decode-file-c-api
287+
288+
.github/scripts/test-online-transducer.sh
289+
290+

cmake/onnxruntime.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,14 @@ if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
213213
if(DEFINED ANDROID_ABI)
214214
file(GLOB onnxruntime_lib_files "$ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.so")
215215
else()
216-
file(GLOB onnxruntime_lib_files "$ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime*")
216+
file(GLOB _onnxruntime_all "$ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime*")
217+
set(onnxruntime_lib_files "")
218+
219+
foreach(f ${_onnxruntime_all})
220+
if (NOT IS_DIRECTORY "${f}")
221+
list(APPEND onnxruntime_lib_files "${f}")
222+
endif()
223+
endforeach()
217224
endif()
218225
endif()
219226

sherpa-onnx/csrc/offline-funasr-nano-model.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static inline size_t NumelFromShape(const std::vector<int64_t> &shape) {
4646
return n;
4747
}
4848

49+
#if ORT_API_VERSION >= 14
4950
static inline void AssertTensorIsCpu(const Ort::Value &v, const char *what) {
5051
if (!v.IsTensor()) return;
5152
auto mi = v.GetTensorMemoryInfo();
@@ -55,6 +56,49 @@ static inline void AssertTensorIsCpu(const Ort::Value &v, const char *what) {
5556
SHERPA_ONNX_EXIT(-1);
5657
}
5758
}
59+
#else
60+
static inline void AssertTensorIsCpu(const Ort::Value &v, const char *what) {
61+
if (!v.IsTensor()) return;
62+
63+
const OrtValue* v_ptr = reinterpret_cast<const OrtValue*>(&v);
64+
const OrtMemoryInfo* memory_info = nullptr;
65+
66+
// 1. Get memory info
67+
OrtStatus* status = Ort::GetApi().GetTensorMemoryInfo(v_ptr, &memory_info);
68+
if (status) {
69+
const char* msg = Ort::GetApi().GetErrorMessage(status);
70+
Ort::GetApi().ReleaseStatus(status);
71+
SHERPA_ONNX_LOGE("%s: failed to get tensor memory info: %s", what, msg);
72+
SHERPA_ONNX_EXIT(-1);
73+
}
74+
75+
// 2. Get memory type (OrtMemType)
76+
OrtMemType mem_type;
77+
status = Ort::GetApi().MemoryInfoGetMemType(memory_info, &mem_type);
78+
if (status) {
79+
const char* msg = Ort::GetApi().GetErrorMessage(status);
80+
Ort::GetApi().ReleaseStatus(status);
81+
SHERPA_ONNX_LOGE("%s: failed to get mem type: %s", what, msg);
82+
SHERPA_ONNX_EXIT(-1);
83+
}
84+
85+
// 3. Check CPU
86+
if (mem_type != OrtMemTypeCPU) {
87+
int device_id = 0;
88+
status = Ort::GetApi().MemoryInfoGetId(memory_info, &device_id);
89+
if (status) {
90+
const char* msg = Ort::GetApi().GetErrorMessage(status);
91+
Ort::GetApi().ReleaseStatus(status);
92+
SHERPA_ONNX_LOGE("%s: failed to get device id: %s", what, msg);
93+
SHERPA_ONNX_EXIT(-1);
94+
}
95+
96+
SHERPA_ONNX_LOGE("%s: expected CPU tensor but got mem_type=%d device_id=%d",
97+
what, static_cast<int>(mem_type), device_id);
98+
SHERPA_ONNX_EXIT(-1);
99+
}
100+
}
101+
#endif
58102

59103
static inline std::string ToLower(std::string s) {
60104
std::transform(s.begin(), s.end(), s.begin(),

sherpa-onnx/csrc/onnx-utils.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,18 @@ Ort::Value View(Ort::Value *v) {
191191
auto type_and_shape = v->GetTensorTypeAndShapeInfo();
192192
std::vector<int64_t> shape = type_and_shape.GetShape();
193193

194+
#if ORT_API_VERSION >= 14
194195
auto memory_info = v->GetTensorMemoryInfo();
196+
#else
197+
const OrtMemoryInfo *memory_info = nullptr;
198+
OrtStatus *status = Ort::GetApi().GetTensorMemoryInfo(*v, &memory_info);
199+
if (status != nullptr) {
200+
const char *msg = Ort::GetApi().GetErrorMessage(status);
201+
Ort::GetApi().ReleaseStatus(status);
202+
SHERPA_ONNX_LOGE("Failed to get tensor memory info with error: '%s'", msg);
203+
SHERPA_ONNX_EXIT(-1);
204+
}
205+
#endif
195206

196207
switch (type_and_shape.GetElementType()) {
197208
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:

0 commit comments

Comments
 (0)