工作空间估计共享头文件
workspace-estimation-shared-header
在编辑边界安全的共享头文件时使用,该头文件须同时被树内代码和共享提供者/插件桥接代码包含(例如 include/onnxruntime/core/framework/workspace_requirement.h,或任何未来支持 OrtKernelWorkspaceEstimateFunc / DeclareWorkspaceRequirements 的头文件,参见 issue #29775 的 Phase-A 工作空间估计路线图),或者当工作空间估计数学辅助函数需要从未来的插件 EP DLL 中调用时。涵盖 Node 前向声明的 ODR 陷阱,以及在实现 PR #29811(MatMulNBits 试点)时发现的数学辅助函数与图解析包装器之间的重用边界。
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 2.2w
- Forks
- 4.4k
01 前言 Front Matter
- name
- workspace-estimation-shared-header
- description
- Use when editing a boundary-safe shared header meant to be included by both in-tree and shared-provider/plugin-bridge code (e.g. include/onnxruntime/core/framework/workspace_requirement.h, or any future header supporting OrtKernelWorkspaceEstimateFunc / DeclareWorkspaceRequirements per issue #29775's Phase-A workspace-estimation roadmap), or when a workspace-estimation math helper needs to be callable from a future plugin EP DLL. Covers a Node forward-declaration ODR trap and the math-helper vs. graph-parsing-wrapper reuse boundary discovered while implementing PR #29811 (MatMulNBits pilot).
03 SKILL.md 正文
Workspace-Estimation Shared Header: DLL-Boundary Pitfalls
Lessons from implementing the two-level (EstimateWorkspace / DeclareWorkspaceRequirements)
workspace-size estimation pilot for MatMulNBits (issue #29775 Phase-A, PR #29811). The
WorkspaceRequirement struct in include/onnxruntime/core/framework/workspace_requirement.h is
designed to be included by both in-tree kernel code and future plugin-EP adapter code — this is exactly
the kind of dual-included, DLL-boundary-crossing header where these gotchas apply.
1. Never forward-declare Node in a header included from both worlds
Symptom: a compile failure or, worse, a silent type mismatch that depends on include order —
because onnxruntime::Node is not one type across the whole codebase. In-tree code sees class Node
from core/graph/graph.h. Shared-provider/plugin-bridge code (anything reachable from a plugin DLL)
sees a different Node type from a provider-bridge header (e.g. struct Node final). These are two
distinct types that happen to share a name — different "ODR worlds."
The trap: writing class Node; (or any forward-declaration of Node) in a header that might be
#included from both worlds. Whichever world's real definition gets included later in the same
translation unit can clash with your forward-declaration's class-key (class vs struct), or — worse
— the header can compile fine in isolation and only fail (or silently pick the wrong type) once combined
with a specific set of other includes.
The fix: omit the forward-declaration entirely. Don't try to avoid the #include of the real Node
header for compile-time savings in a shared, dual-included header — let each translation unit's own
real includes provide whatever Node type it needs. If you only need a pointer/reference and think a
forward-declare is a safe optimization, it is not safe here specifically because the two worlds disagree
on the underlying type.
How to confirm you're clear: the header must build cleanly when included from an in-tree-only translation unit AND (once such a target exists) from a plugin-DLL translation unit, without relying on which one happens to be included first.
2. Keep the shared/reusable half of a workspace-estimation function free of graph types
When a kernel's workspace-size computation needs to be callable from both in-tree and (eventually) plugin code, split it into two pieces:
- Pure-math core — takes only plain shape/arch integers (e.g.
ComputeFpAIntBGemmWorkspaceSize(int m, int n, int k, int sm, int multiProcessorCount)). NoNode&, noNodeArg, noTensorShapeparsing, no ORT graph types at all. This is the part a future plugin implementation can call verbatim — plugin kernels don't haveNode&access, only the C-ABI shape representation (OrtNode,Node_GetInputShape). - Graph-parsing wrapper — extracts the plain integers from
const Node&/NodeArg/TensorShape(or from the plugin's C-ABI shape accessors). This half is inherently different per build configuration and is NOT reusable across the DLL boundary; it must be reimplemented against whichever shape representation the caller has.
Why this matters concretely: if you accidentally let the pure-math core accept or touch an in-tree graph type (even just to read one field), you've made it impossible to reuse from the plugin path without either (a) linking in-tree graph headers into the plugin DLL (defeats the purpose of a plugin boundary) or (b) duplicating the whole function. Keep the split clean from the start.
How to confirm you're clear: grep the pure-math function's signature and body for any ORT graph type
(Node, NodeArg, TensorShape, GraphViewer, etc.) — it should only ever see plain scalars
(int/int64_t/size_t) and, at most, opaque device-property values already extracted by the caller.
If you find a graph type anywhere in that function, the split has leaked and needs to be pulled apart
before a plugin implementation can reuse it.
Related, not yet built: docs/annotated_partitioning/future_directions_constrained_env.md (Phase A /
plugin-ABI sections) describes the intended plugin-side C ABI surface for DeclareWorkspaceRequirements,
but as of PR #29811 no plugin-side implementation exists yet — see that doc's "Cost of a Real
Plugin-Side Override (Deferred)" note for what's still missing beyond just following this split.
04 同仓库更多 Skills
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 的输出整理人工贡献者致谢名单,以及应用预设的路径过滤。
ONNX Runtime 代码检查与格式化
ort-lint
对 ONNX Runtime 代码执行代码检查(lint)和格式化。当被要求对 ONNX Runtime 中的 C++ 或 Python 文件进行 lint、格式化或代码风格检查时使用此技能。