从源代码构建

此项目支持 Python,可以轻松地从源代码构建,或者进行一个简单的 cmake 构建而不依赖 Python。

Python 包

该包包含所有自定义算子和一些用于操作 ONNX 模型的 Python 脚本。

  • 在 Windows 上安装包含 C++ 开发工具的 Visual Studio,或者在 Linux 上安装 gcc(>8.0),在 macOS 上安装 xcode,并在类 Unix 平台上安装 cmake。(提示:在 Windows 平台上,如果使用了 Visual Studio 中捆绑的 cmake,请指定 set 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 包。

Web-Assembly

由于 WASM 缺乏良好的动态链接机制,ONNXRuntime-Extensions 将被构建为静态库并与 ONNXRuntime 链接。以下是在构建 onnxruntime 时,用于在 ONNXRuntime 包中包含 ONNXRuntime-Extensions 的两个附加参数:--use_extensions 和 –extensions_overridden_path

C++ 共享库

对于任何其他情况,请运行 build.batbash ./build.sh 构建库。默认情况下,DLL 或库将在目录 out/<OS>/<FLAVOR> 中生成。还有一个单元测试可帮助验证构建。

VC Runtime 静态链接 如果您想使用 VC Runtime 静态链接构建二进制文件,请在运行 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.cn/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日)