Skip to content

feat: 开放 webgpu 推理配置 - #4

Merged
MistEO merged 3 commits into
MaaXYZ:devfrom
SherkeyXD:dev
Sep 18, 2026
Merged

MistEO merged 3 commits into
MaaXYZ:devfrom
SherkeyXD:dev

Conversation

@SherkeyXD

@SherkeyXD SherkeyXD commented Sep 17, 2026 •

Copy link
Copy Markdown

Sourcery 摘要

在运行时和受支持的模型中启用可配置的 ONNX Runtime WebGPU 推理。

新功能:

  • 通过 ONNX Runtime 添加 WebGPU 作为受支持的推理设备,包括运行时配置和设备选择。

增强功能:

  • 添加 WebGPU 后端验证、自动后端初始化、提供程序检测、设备选择和张量支持。
  • 允许适用的 OCR 模型声明对 ONNX Runtime WebGPU 的支持。

构建:

  • 在生成的和已配置的核心功能定义中启用 WebGPU。
Original summary in English

Sourcery 摘要

在运行时配置、后端初始化、张量处理以及受支持的 OCR 模型中,实现可配置的 ONNX Runtime WebGPU 推理。

新功能:

  • 添加可配置的 ONNX Runtime WebGPU 推理,支持设备选择和可选的设备 ID。
  • 支持的 OCR 模型可以声明其与 ONNX Runtime WebGPU 的兼容性。

错误修复:

  • 修正已安装目标的 include 路径,并为下游使用者提供可安装的 CMake 包配置。

增强功能:

  • 添加 WebGPU 提供程序检测、运行时回退行为、执行提供程序验证、自动后端选择以及 WebGPU 张量支持。

构建:

  • 在生成的配置头文件、源配置头文件和导出的构建目标中启用 WebGPU 功能定义。
Original summary in English

Sourcery 总结

支持在运行时配置、后端初始化、张量处理以及受支持的 OCR 模型中配置 ONNX Runtime WebGPU 推理。

新功能:

  • 通过 RuntimeOption 添加可配置的 ONNX Runtime WebGPU 推理,并支持可选的设备选择。
  • 支持的 OCR 模型可声明与 ONNX Runtime WebGPU 的兼容性。

Bug 修复:

  • 为下游使用者提供正确的已安装头文件包含路径,以及可重定位的 CMake 软件包配置。

增强功能:

  • 添加 WebGPU 设备和后端支持、Provider 检测、运行时回退至 CPU、自动后端选择、执行 Provider 验证,以及主机内存张量处理。

构建:

  • 根据链接的 ONNX Runtime 功能启用 WebGPU 功能定义。
Original summary in English

Summary by Sourcery

Enable configurable ONNX Runtime WebGPU inference across runtime configuration, backend initialization, tensor handling, and supported OCR models.

New Features:

  • Add configurable ONNX Runtime WebGPU inference with optional device selection through RuntimeOption.
  • Enable applicable OCR models to declare ONNX Runtime WebGPU compatibility.

Bug Fixes:

  • Provide correct installed include paths and relocatable CMake package configuration for downstream consumers.

Enhancements:

  • Add WebGPU device and backend support, provider detection, runtime fallback to CPU, automatic backend selection, execution-provider validation, and host-memory tensor handling.

Build:

  • Enable WebGPU feature definitions based on the linked ONNX Runtime capabilities.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

您好——我发现了 1 个问题

面向 AI Agent 的提示
请处理本次代码审查中的评论:

## 具体评论

### 评论 1
<location path="fastdeploy/runtime/backends/ort/ort_backend.cc" line_range="571" />
<code_context>
       AdaptivePool2dOp* adaptive_pool2d =
           new AdaptivePool2dOp{"CoreMLExecutionProvider"};
       custom_operators_.push_back(adaptive_pool2d);
+    } else if (option_.device == Device::WEBGPU) {
+      AdaptivePool2dOp* adaptive_pool2d =
+          new AdaptivePool2dOp{"WebGpuExecutionProvider"};
+      custom_operators_.push_back(adaptive_pool2d);
     } else {
       AdaptivePool2dOp* adaptive_pool2d =
</code_context>
<issue_to_address>
**问题 (bug_risk):** WebGPU 自适应池化自定义算子注册时使用了提供程序名称 `WebGpuExecutionProvider`,而代码检测并追加的是 `WebGPUExecutionProvider`/`WebGPU`。ONNX Runtime 会根据完全匹配的执行提供程序名称来匹配自定义内核,因此包含此算子的模型不会使用已注册的 WebGPU 内核,并会失败或报告该算子不受支持。

**触发条件:** 启用 WebGPU 的模型包含自定义 AdaptivePool2d 算子时。

**建议修复:** 使用 ONNX Runtime WebGPU 执行提供程序所使用的确切提供程序名称注册该算子,并与传递给 `AppendExecutionProvider` 的提供程序名称保持一致。

```suggestion
          new AdaptivePool2dOp{"WebGPU"};
```
</issue_to_address>

Sourcery 对开源项目免费——如果您喜欢我们的审查,请考虑分享给他人 ✨
Original comment in English

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="fastdeploy/runtime/backends/ort/ort_backend.cc" line_range="571" />
<code_context>
       AdaptivePool2dOp* adaptive_pool2d =
           new AdaptivePool2dOp{"CoreMLExecutionProvider"};
       custom_operators_.push_back(adaptive_pool2d);
+    } else if (option_.device == Device::WEBGPU) {
+      AdaptivePool2dOp* adaptive_pool2d =
+          new AdaptivePool2dOp{"WebGpuExecutionProvider"};
+      custom_operators_.push_back(adaptive_pool2d);
     } else {
       AdaptivePool2dOp* adaptive_pool2d =
</code_context>
<issue_to_address>
**issue (bug_risk):** The WebGPU adaptive-pooling custom operator is registered with the provider name `WebGpuExecutionProvider`, while the code detects and appends `WebGPUExecutionProvider`/`WebGPU`. ONNX Runtime matches custom kernels by the exact execution-provider name, so models containing this operator do not use the registered WebGPU kernel and fail or report the operator as unsupported.

**Triggers:** When a WebGPU-enabled model contains the custom AdaptivePool2d operator.

**Suggested fix:** Register the operator with the exact provider name used by the ONNX Runtime WebGPU execution provider, consistently with the provider name passed to `AppendExecutionProvider`.

```suggestion
          new AdaptivePool2dOp{"WebGPU"};
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread fastdeploy/runtime/backends/ort/ort_backend.cc
onnxruntime 只在该 EP 参与构建时才把 <webgpu_provider_factory.h> 平铺安装出来
(cmake/onnxruntime.cmake 按 ONNXRUNTIME_PROVIDER_NAMES 逐个 provider glob),
且这个头文件只有注释、没有声明,WebGPU 走的是 generic
SessionOptionsAppendExecutionProvider,因此用它做 __has_include 探测即可:

- WITH_WEBGPU && __has_include(<webgpu_provider_factory.h>) => ENABLE_WEBGPU
- BuildOption 的 WebGPU 分支加 #ifdef ENABLE_WEBGPU 守卫;未编入时告警并回退
  CPUExecutionProvider,沿用 DML/CoreML 的软回退契约。此时 option_.device 已被
  置为 CPU,后续 InitCustomOperators 会正确注册 CPU 版自定义算子
- 补齐 WebGPU 在错误信息/注释中的出现,并为 std::unordered_map 补显式 include
- install(EXPORT ... FILE fastdeploy_ppocrTargets.cmake) 搭配
  configure_package_config_file 生成可编辑的 fastdeploy_ppocrConfig.cmake
- find_dependency(OpenCV COMPONENTS core imgproc):导出的 target 链接
  opencv_core/opencv_imgproc,下游若不先 find_package(OpenCV) 会在链接期报
  ld: library 'opencv_core' not found
- 静态包的 interface 带 $<LINK_ONLY:onnxruntime::onnxruntime>,故仅静态时
  find_dependency(onnxruntime),并随包安装 cmake/Findonnxruntime.cmake,避免
  依赖 onnxruntime 的 CMake 包恰好能被该名字找到
- $<INSTALL_INTERFACE:include>:CMAKE_INSTALL_INCLUDE 在本项目从未定义,原先
  导出的 target 没有任何 include 目录
- 导出 fastdeploy_ppocr_WITH_WEBGPU 能力标记,取值与编译期探测同源
@SherkeyXD

Copy link
Copy Markdown
Author

Ready
记得用 squash

@MistEO
MistEO merged commit e11935e into MaaXYZ:dev Sep 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants