從原始碼構建

本專案支援 Python,可以輕鬆地從原始碼構建,或者在沒有 Python 依賴的情況下進行簡單的 cmake 構建。

Python 包

該包包含所有自定義運算子和一些用於操作 ONNX 模型的 Python 指令碼。

  • 在 Windows 上安裝帶有 C++ 開發工具的 Visual Studio,或者在 Linux 上安裝 gcc(>8.0) 或在 macOS 上安裝 xcode,並在類 Unix 平臺上安裝 cmake。(提示:在 Windows 平臺上,如果使用了 Visual Studio 中捆綁的 cmake,請指定設定 VSDEVCMD=%ProgramFiles(x86)%\Microsoft Visual Studio<VERSION_YEAR><Edition>\Common7\Tools\VsDevCmd.bat
  • 如果在 Windows 上執行,請確保已啟用長檔名,包括 作業系統 和 git:git config --system core.longpaths true
  • 準備 Python 環境並安裝 requirements.txt 中的 pip 包。
  • pip install . 用於構建和安裝包。
    或者 pip install -e . 以開發模式安裝包,這對於開發者更友好,因為 Python 程式碼更改無需將檔案複製到磁碟上的不同位置即可生效。(提示:setup.cfg 中的 debug=1 將使 C++ 程式碼在 Python 程序中可除錯。)

測試

  • ‘pip install -r requirements-dev.txt’ 用於安裝開發所需的 pip 包。
  • 在專案根目錄中執行 pytest test

有關已驗證構建配置的完整列表,請參閱此處

Java 包

執行 bash ./build.sh -DOCOS_BUILD_JAVA=ON 以在 out//Release 資料夾中構建 jar 包

Android 包

使用 ./tools/android/build_aar.py 構建 Android AAR 包。

iOS 包

使用 ./tools/ios/build_xcframework.py 構建 iOS xcframework 包。

WebAssembly

由於 WASM 中缺乏良好的動態連結機制,ONNXRuntime-Extensions 將被構建為靜態庫並與 ONNXRuntime 連結。以下是構建 onnxruntime 時包含 ONNXRuntime-Extensions 足跡的兩個附加引數 –-use_extensions 和 –extensions_overridden_path

C++ 共享庫

對於任何其他情況,請執行 build.batbash ./build.sh 來構建庫。預設情況下,DLL 或庫將生成在 out/<OS>/<FLAVOR> 目錄中。有一個單元測試可以幫助驗證構建。

VC 執行時靜態連結 如果您想使用 VC 執行時靜態連結構建二進位制檔案,請在執行 build.bat 時新增引數 _-DCMAKE_MSVC_RUNTIME_LIBRARY=”MultiThreaded$<$:Debug>"_

請檢視此連結 https://docs.opensource.microsoft.com/releasing/general-guidance/copyright-headers/ 以獲取原始檔版權頭。

為 Java 包構建包含 onnxruntime-extensions 的 ONNX Runtime

以下步驟僅在 Windows 平臺上進行演示,Linux 和 MacOS 等其他平臺的操作類似。

也支援 Android 構建;請參閱此處以獲取構建 AAR 包的引數。

所需工具

  1. 安裝 Visual Studio 2022(包含 cmake、git、桌面 C++)
  2. 安裝 miniconda 以獲得 Python 支援(用於 onnxruntime 構建)
  3. OpenJDK: https://docs.microsoft.com/en-us/java/openjdk/download (OpenJDK 11.0.15 LTS)
  4. Gradle: https://gradle.org/releases/ (v6.9.2)

命令

在 Windows Terminal 中啟動 Developer PowerShell for VS 2022

	. $home\miniconda3\shell\condabin\conda-hook.ps1
	conda activate base

	$env:JAVA_HOME="C:\Program Files\Microsoft\jdk-11.0.15.10-hotspot"
	# clone ONNXRuntime
	git clone -b rel-1.12.0 https://github.com/microsoft/onnxruntime.git onnxruntime

	# clone onnxruntime-extensions
	git clone https://github.com/microsoft/onnxruntime-extensions.git onnxruntime_extensions

	# build JAR package in this folder
	mkdir ortall.build
	cd ortall.build
	python ..\onnxruntime\tools\ci_build\build.py --config Release --cmake_generator "Visual Studio 17 2022" --build_java --build_dir .  --use_extensions --extensions_overridden_path "..\onnxruntime-extensions"

依賴項

下表列出了 onnxruntime-extensions 各個依賴項的版本。這些是我們的 CI 日常且廣泛驗證的配置。

Python 3.8 3.9 3.10 3.11
Onnxruntime 1.12.1 (2022 年 8 月 4 日) 1.13.1 (2022 年 10 月 24 日) 1.14.1 (2023 年 3 月 2 日) 1.15.0 (2023 年 5 月 24 日)