ONNX Runtime 源码构建
ort-build
从源码构建 ONNX Runtime。当被要求为 ONNX Runtime 进行构建、编译或生成 CMake 文件时使用此技能。
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 2.2w
- Forks
- 4.4k
01 前言 Front Matter
- name
- ort-build
- description
- Build ONNX Runtime from source. Use this skill when asked to build, compile, or generate CMake files for ONNX Runtime.
02 资源文件 22
- 资产文件assets/acceptance-observer.md3.8 KB
- 资产文件assets/autopilot-triage-schema.json2.7 KB
- 资产文件assets/confidence-gate-rubric.md4.4 KB
- 资产文件assets/final-report-template.md1.6 KB
- 资产文件assets/fix-prompt.md4.1 KB
- 资产文件assets/ground-truth-table.md3.8 KB
- 资产文件assets/ideate-prompt.md3.1 KB
- 资产文件assets/implement-bug.md943 B
- 资产文件assets/implement-docs.md1.1 KB
- 资产文件assets/implement-feature.md1.1 KB
- 资产文件assets/implement-refactor.md1.4 KB
- 资产文件assets/model-routing.md15.8 KB
- 资产文件assets/plan-panel-prompt.md5.6 KB
- 资产文件assets/plan-schema.json6.2 KB
- 资产文件assets/solution-pipeline-prompt.md10.9 KB
- 资产文件assets/strategic-alignment-prompt.md4.6 KB
- 资产文件assets/task-implement-prompt.md3.4 KB
- 资产文件assets/triage-digest-template.md1.9 KB
- 资产文件assets/triage-prompt.md1.8 KB
- 资产文件assets/verdict-schema.json3.4 KB
- 资产文件assets/wave-gate-rubric.md7.7 KB
- 参考文档references/strategic-alignment-gate.md7.8 KB
03 SKILL.md 正文
Building ONNX Runtime
The build scripts build.sh (Linux/macOS) and build.bat (Windows) delegate to tools/ci_build/build.py.
Build phases
Three phases, controlled by flags:
--update— generate CMake build files--build— compile (add--parallelto speed this up)--test— run tests
For native builds, if none are specified (and --skip_tests is not passed), all three run by default. For cross-compiled builds, the default is --update + --build only.
When to use --update
You need --update when:
- First build in a new build directory
- New source files are added (some CMake targets use glob patterns, others use explicit file lists — re-run to pick up new files either way)
- CMake configuration changes (new flags, updated CMakeLists.txt)
You do not need --update when only modifying existing .cc/.h files — just use --build. Skipping it saves time.
Examples
# Full build (update + build + test)
./build.sh --config Release --parallel
.\build.bat --config Release --parallel # Windows
# Just regenerate CMake files
./build.sh --config Release --update
# Just compile (skip CMake regeneration and tests)
./build.sh --config Release --build --parallel
# Just run tests (after a prior build)
./build.sh --config Release --test
# Build with CUDA execution provider
./build.sh --config Release --parallel --use_cuda --cuda_home /usr/local/cuda --cudnn_home /usr/local/cuda
# Configure and build the WebGPU execution provider as a shared library (Windows)
.\build.bat --config RelWithDebInfo --build_dir .\build\WGPU --use_webgpu --build_shared_lib --update --build --parallel
# Incrementally rebuild the same WebGPU configuration after changing existing source files
.\build.bat --config RelWithDebInfo --build_dir .\build\WGPU --use_webgpu --build_shared_lib --build --parallel
# Build Python wheel
./build.sh --config Release --parallel --build_wheel
# Build a specific CMake target (much faster than a full build)
./build.sh --config Release --build --parallel --target onnxruntime_common
# Load flags from an option file (one flag per line)
./build.sh "@./custom_options.opt" --build --parallel
Key flags
| Flag | Description |
|---|---|
--config |
Debug, MinSizeRel, Release, or RelWithDebInfo |
--parallel |
Enable parallel compilation (recommended) |
--skip_tests |
Skip running tests after build |
--build_wheel |
Build the Python wheel package |
--use_cuda |
Enable CUDA EP. Requires --cuda_home/--cudnn_home or CUDA_HOME/CUDNN_HOME env vars. On Windows, only cuda_home/CUDA_HOME is validated. |
--target T |
Build a specific CMake target (requires --build; e.g., onnxruntime_common, onnxruntime_test_all) |
--use_webgpu |
Enable WebGPU EP. To run its tests locally on Linux without a GPU, see the webgpu-local-testing skill. |
--cmake_extra_defines onnxruntime_QUICK_BUILD=ON |
Faster CUDA build: instantiates a reduced kernel set. Side effect: Flash is compiled for head_dim 128 only, so most attention shapes fall back to MEA (changes which attention kernel is compiled/dispatched). Don't use it to characterize Flash-vs-arch behavior. |
--build_dir |
Build output directory |
Build output path
Default: build/<Platform>/<Config>/ where Platform is Linux, MacOS, or Windows.
With Visual Studio multi-config generators, the config name appears twice (e.g., build/Windows/Release/Release/).
It may be customized with --build_dir.
For example, --build_dir .\build\WGPU --config RelWithDebInfo creates the CMake build tree at
build/WGPU/RelWithDebInfo/; Visual Studio places final binaries in its RelWithDebInfo/ subdirectory.
The --build_shared_lib flag in the WebGPU example is optional and is only needed when building the ONNX Runtime DLL.
Agent tips
- Activate a Python virtual environment before building. See "Python > Virtual environment" in
AGENTS.md. - Build flags can silently reroute which kernel/code path executes. A build option can
change which kernel is compiled, and therefore which code path actually runs — so a CI
failure can live in a different code path than your local build exercises. Before
hypothesizing a hardware- or algorithm-specific cause (e.g. "this GPU arch miscomputes"),
first identify which kernel actually ran for the failing configuration (see the
ort-testskill → "Verify which path/kernel actually executed"). Concrete instance:onnxruntime_QUICK_BUILD=ONcompiles FlashAttention for head_dim 128 only, so most attention shapes silently dispatch to Memory-Efficient Attention instead of Flash — details in thecuda-attention-kernel-patternsskill. - Prefer
python tools/ci_build/build.pydirectly overbuild.bat/build.shwhen redirecting output. The.batwrapper runs incmd.exe, which breaks PowerShell redirection. - Redirect output to a file (e.g.,
> build_log.txt 2>&1). Build output is large and will overflow terminal buffers. - Run builds in the background — a full build can take tens of minutes to over an hour. Poll the log for
"Build complete"or errors. - Use
--parallelby default unless the user says otherwise. - Ask the user what they want to build (config, execution providers, wheel, etc.) if not clear from their prompt.
04 同仓库更多 Skills
工作空间估计共享头文件
workspace-estimation-shared-header
在编辑边界安全的共享头文件时使用,该头文件须同时被树内代码和共享提供者/插件桥接代码包含(例如 include/onnxruntime/core/framework/workspace_requirement.h,或任何未来支持 OrtKernelWorkspaceEstimateFunc / DeclareWorkspaceRequirements 的头文件,参见 issue #29775 的 Phase-A 工作空间估计路线图),或者当工作空间估计数学辅助函数需要从未来的插件 EP DLL 中调用时。涵盖 Node 前向声明的 ODR 陷阱,以及在实现 PR #29811(MatMulNBits 试点)时发现的数学辅助函数与图解析包装器之间的重用边界。
ONNX Runtime WebGPU 本地测试
webgpu-local-testing
在没有真实 GPU 的 Linux 上构建并运行 ONNX Runtime WebGPU 提供者测试,使用软件 Vulkan 适配器(Mesa lavapipe)。当需要在非 Mac 环境下运行 WebGPU EP 内核时使用——Linux 的 webgpu CI 环节仅做构建,因此软件 Vulkan 是本地实际运行 WebGPU 正确性测试的方式。范围——lavapipe 仅能验证主机侧的 enforce/shape 缺陷以及不含 MatMul 的内核;任何包含 MatMul 的图(包括扩展的 Attention 节点测试)都会使 lavapipe 崩溃,此类测试仅在 macOS-arm64 Metal 上运行,Metal 是这些测试的权威来源。涵盖安装(在 Azure Linux 上用 dnf)、--use_webgpu 构建标志、onnxruntime_provider_test 目标、VK_ICD_FILENAMES,以及 lavapipe 的 MatMul 崩溃陷阱。
Python setattr 安全审查
python-kwargs-setattr-security
在审查或修复 ONNX Runtime 中使用用户可控的 kwargs 通过 setattr() 配置 C++ 扩展对象(SessionOptions、RunOptions 等)的 Python 代码时使用。用于应用白名单模式,防止通过反射属性访问进行任意文件写入和其他攻击。
ONNX Runtime Transformers GPU 测试
ort-transformers-gpu-pytest
针对 GPU wheel 运行 ONNX Runtime transformers Python 测试(onnxruntime/test/python/transformers),并确认真实的 cuDNN/flash SDPA 调度。当 transformers pytest 因 ModuleNotFoundError onnxruntime.capi 失败、torch 自带的 CUDA/cuDNN 库覆盖了 ORT 构建的库(导致错误版本调度或加载错误),或必须证明测试确实执行了 cuDNN SDPA 解码层而非静默跳过或回退到 MATH 时使用。
ONNX Runtime 测试执行与调试
ort-test
运行 ONNX Runtime 测试。当被要求运行测试、调试测试失败,或在 ONNX Runtime 中查找并执行特定测试用例时使用此技能。
ONNX Runtime 发布说明撰写
ort-release-notes
使用预设配置为完整 ORT 或限定范围的组件发布起草 ONNX Runtime 发布说明。适用于生成带 PR 链接的亮点摘要、根据 compile_contributors.py 的输出整理人工贡献者致谢名单,以及应用预设的路径过滤。