二、ONNX Runtime增加新的operator/kernel

来源文档
onnx增加新的operator/kernel

可以通过以下3种方式在ONNXRuntime中编写和注册新operator/kernel

1. 使用实验性的c API,暂不推荐,因为API还不稳定,可能会有大变动,该方法不需要编译ORT项目源码。 Using the experimental custom op API in the C API (onnxruntime_c_api.h)

Note: These APIs are experimental and will change in the next release. They’re released now for feedback and experimentation.

  • Create an OrtCustomOpDomain with the domain name used by the custom ops
  • Create an OrtCustomOp structure for each op and add them to the OrtCustomOpDomain with OrtCustomOpDomain_Add
  • Call OrtAddCustomOpDomain to add the custom domain of ops to the session options
    See this for an example called MyCustomOp that uses the C++ helper API (onnxruntime_cxx_api.h).
    参考例子:https://github.com/onnx/tutorials/tree/master/PyTorchCustomOperator

2. 使用RegisterCustomRegistry API,该方法不需要编译ORT项目源码

  • 通过在include的路径下的OpKernel and OpSchema APIs实现自己的kernel and schema。

  • 新建CustomRegistry object 并使用 registry注册你自己的 kernel and schema。

  • 通过使用RegisterCustomRegistry API在ONNXRuntime注册定制的registry。

  • Implement your kernel and schema (if required) using the OpKernel and OpSchema APIs (headers are in the include folder).

  • Create a CustomRegistry object and register your kernel and schema with this registry.

  • Register the custom registry with ONNXRuntime using RegisterCustomRegistry API.

See
this for an example.

3. 给ONNXRuntime贡献Op,及直接修改onnxruntime内部的源码。该方法需要编译ORT项目源码

这主要是针对正在向ONNX提议的Op。这样,您如果马上需要部署该Op无需等待ONNX团队的批准。
This is mostly meant for ops that are in the process of being proposed to ONNX. This way you don’t have to wait for an approval from the ONNX team
if the op is required in production today.
See this for an example.

发布了45 篇原创文章 · 获赞 21 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/xxradon/article/details/104100114