microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
wenbingl-patch-2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

include/op_def_struct.h

281lines · modeblame

c599b00dWenbing Li3 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4//.A very thin wrapper of ONNXRuntime Custom Operator Callback ABI, which
5// is only used in the custom-op kernels. For the general ORT C++ invocation, like end-to-end
6// testing, the ONNXRuntime public C++ APIs should be used since there is no binary compatible requirement.
7
8#pragma once
01e3a636Wenbing Li2 years ago9#include <cstdint>
c599b00dWenbing Li3 years ago10#include <cstddef>
11#include <array>
12#include <memory>
13#include <string>
14#include <vector>
15#include <utility>
16#include <type_traits>
914509d5Wenbing Li2 years ago17#include <optional>
22340011cao lei2 years ago18#include <functional>
c599b00dWenbing Li3 years ago19
5e44a7c3Scott McKay3 years ago20#include "exceptions.h"
a0c26255Wenbing Li2 years ago21#include "onnxruntime_extensions.h"
64646279Wenbing Li2 years ago22#include "custom_op/custom_op_lite.h"
914509d5Wenbing Li2 years ago23
24#define MIN_ORT_VERSION_SUPPORTED 11
8f36cf32Tang, Cheng3 years ago25
914509d5Wenbing Li2 years ago26namespace Ort {
27namespace Custom {
28
f9290e8bWenbing Li2 years ago29template <typename T>
30inline OrtStatusPtr ToApiStatus(const T& status) {
97ee9eb5Wenbing Li2 years ago31return (OrtStatus*)status;
f9290e8bWenbing Li2 years ago32}
33
34template <>
35inline OrtStatusPtr ToApiStatus(const OrtStatusPtr& status) {
36return status;
37}
38
39template <typename RType, typename... Args>
914509d5Wenbing Li2 years ago40struct FunctionKernel {
f9290e8bWenbing Li2 years ago41using ComputeFn = std::function<RType(Args...)>;
914509d5Wenbing Li2 years ago42
f9290e8bWenbing Li2 years ago43RType Compute(Args... args) const {
914509d5Wenbing Li2 years ago44return compute_fn_(args...);
45}
46
47ComputeFn compute_fn_;
48};
49
50// primary template handles types that have no nested ::type member:
51template <class, class = void>
52struct IsFunctionKernel {
53typedef std::false_type type;
54};
55
56// specialization recognizes types that do have a nested ::type member:
57template <class T>
a0c26255Wenbing Li2 years ago58struct IsFunctionKernel<T, std::void_t<typename T::ComputeFn>> {
914509d5Wenbing Li2 years ago59typedef std::true_type type;
60};
61
62// Helper type
63template <typename T>
64struct ComputeArgsList;
65
66// Specialization for member function
f9290e8bWenbing Li2 years ago67template <typename RType, typename C, typename... Args>
68struct ComputeArgsList<RType (C::*)(Args...) const> {
69using FunctionType = RType (*)(Args...);
70using MemberFunctionType = RType (C::*)(Args...) const;
71using ResultType = RType;
914509d5Wenbing Li2 years ago72};
73
3b889fc4Tang, Cheng2 years ago74template<typename, typename T>
75struct HasOnModelAttach {
76static_assert(
77std::integral_constant<T, false>::value,
78"Second template parameter needs to be of function type.");
79};
80
81// specialization that does the checking
82
83template<typename C, typename Ret, typename... Args>
84struct HasOnModelAttach<C, Ret(Args...)> {
85private:
86template<typename T>
87static constexpr auto check(T*)
88-> typename
89std::is_same<
90decltype( std::declval<T>().OnModelAttach( std::declval<Args>()... ) ),
91Ret
92>::type; // attempt to call it and see if the return type is correct
93
94template<typename>
95static constexpr std::false_type check(...);
96
97typedef decltype(check<C>(0)) type;
98
99public:
100static constexpr bool value = type::value;
101};
102
22340011cao lei2 years ago103template <typename T, typename = void>
104struct CustomOp_defined_getInputMemoryType : std::false_type {};
105
106template <typename T>
107struct CustomOp_defined_getInputMemoryType<T, std::void_t<decltype(&T::GetInputMemoryType)>> : std::true_type {};
108
914509d5Wenbing Li2 years ago109template <typename CustomOpKernel>
110struct OrtLiteCustomStructV2 : public OrtLiteCustomOp {
111using ComputeFunction = decltype(&CustomOpKernel::Compute);
112using RegularComputeType = typename ComputeArgsList<ComputeFunction>::FunctionType;
f9290e8bWenbing Li2 years ago113using RType = typename ComputeArgsList<ComputeFunction>::ResultType;
914509d5Wenbing Li2 years ago114
115template <typename... Args>
f9290e8bWenbing Li2 years ago116using MemberComputeType = RType (CustomOpKernel::*)(Args...) const;
914509d5Wenbing Li2 years ago117
118struct KernelEx : public CustomOpKernel {
119struct {
120std::string ep_{};
121std::unique_ptr<OrtW::CustomOpApi> api_;
122} extra_;
123};
124
125template <typename T>
126static OrtStatusPtr InitKernel(KernelEx& kernel,
a0c26255Wenbing Li2 years ago127const OrtApi& api, const OrtKernelInfo& info, RegularComputeType fn, T t) {
3b889fc4Tang, Cheng2 years ago128if constexpr (HasOnModelAttach<KernelEx, OrtStatusPtr(const OrtApi&, const OrtKernelInfo&)>::value){
129auto status = kernel.OnModelAttach(api, info);
130return ToApiStatus(status);
131}
132else {
133auto status = kernel.OnModelAttach(OrtAttributeReader(api, info));
134return ToApiStatus(status);
135}
914509d5Wenbing Li2 years ago136}
137
138static OrtStatusPtr InitKernel(
a0c26255Wenbing Li2 years ago139KernelEx& kernel,
140const OrtApi& api, const OrtKernelInfo& info, RegularComputeType fn, std::true_type) {
914509d5Wenbing Li2 years ago141kernel.compute_fn_ = fn;
142return nullptr;
143}
144
145template <typename... Args>
146void ParseArgs(MemberComputeType<Args...> fn) {
147OrtLiteCustomOp::ParseArgs<Args...>(OrtLiteCustomOp::input_types_, OrtLiteCustomOp::output_types_);
148}
149
150// TODO: consider to disable these legacy functions for mobile build to save binary size
151template <typename... Args>
152void DefineCallbackFunctionsLegacy(MemberComputeType<Args...> fn, RegularComputeType regular_fn) {
153OrtCustomOp::CreateKernel = [](const OrtCustomOp* this_, const OrtApi* ort_api, const OrtKernelInfo* info) {
154auto self = static_cast<const OrtLiteCustomStructV2<CustomOpKernel>*>(this_);
155auto kernel = std::make_unique<KernelEx>();
156typedef typename IsFunctionKernel<CustomOpKernel>::type type_flag;
f9290e8bWenbing Li2 years ago157auto status = InitKernel(*kernel, *ort_api, *info, self->regular_fn_, type_flag());
914509d5Wenbing Li2 years ago158OrtW::ThrowOnError(*ort_api, status);
159
160kernel->extra_.ep_ = self->execution_provider_;
161kernel->extra_.api_ = std::make_unique<OrtW::CustomOpApi>(*ort_api);
162return reinterpret_cast<void*>(kernel.release());
163};
164
165OrtCustomOp::KernelCompute = [](void* op_kernel, OrtKernelContext* context) {
166auto kernel = reinterpret_cast<KernelEx*>(op_kernel);
167std::vector<TensorPtr> tensors;
168auto t = CreateTuple<0, 0, Args...>(kernel->extra_.api_.get(),
169context,
170tensors,
171kernel->extra_.api_->KernelContext_GetInputCount(context),
172kernel->extra_.api_->KernelContext_GetOutputCount(context),
173kernel->extra_.ep_);
174std::apply([kernel](Args const&... t_args) {
f9290e8bWenbing Li2 years ago175auto status = kernel->Compute(t_args...); OrtW::API::ThrowOnError(ToApiStatus(status)); }, t);
914509d5Wenbing Li2 years ago176};
177
178OrtCustomOp::KernelDestroy = [](void* op_kernel) {
179std::unique_ptr<KernelEx>(reinterpret_cast<KernelEx*>(op_kernel)).reset();
180};
181}
182
cd7d6459Wenbing Li2 years ago183#if ORT_API_VERSION >= 16
914509d5Wenbing Li2 years ago184template <typename... Args>
185void DefineCallbackFunctions(MemberComputeType<Args...> fn, RegularComputeType regular_fn) {
186OrtCustomOp::CreateKernel = nullptr;
187OrtCustomOp::KernelCompute = nullptr;
188
22340011cao lei2 years ago189if constexpr (CustomOp_defined_getInputMemoryType<CustomOpKernel>::value) {
190OrtCustomOp::GetInputMemoryType = [](const OrtCustomOp* /*this_*/, size_t index) -> OrtMemType {
191return CustomOpKernel::GetInputMemoryType(index);
192};
193}
194
914509d5Wenbing Li2 years ago195OrtCustomOp::CreateKernelV2 = [](const OrtCustomOp* this_,
196const OrtApi* api, const OrtKernelInfo* info, void** op_kernel) -> OrtStatusPtr {
197if (api == nullptr) {
198assert(false && "Got a null pointer for ORT api on calling CreateKernelV2");
199// should never happened, what we can do?
200return nullptr;
201}
202
203if (this_ == nullptr || info == nullptr || op_kernel == nullptr) {
204return api->CreateStatus(ORT_INVALID_ARGUMENT, "OrtCustomOp::CreateKernelV2: received a null pointer");
205}
206
207auto self = static_cast<const OrtLiteCustomStructV2<CustomOpKernel>*>(this_);
208auto kernel = std::make_unique<KernelEx>();
209if (kernel == nullptr) {
210return api->CreateStatus(ORT_FAIL, "OrtCustomOp::CreateKernelV2: failed to new a kernel, OOM?");
211}
212
213typedef typename IsFunctionKernel<CustomOpKernel>::type flag_type;
f9290e8bWenbing Li2 years ago214auto status = InitKernel(*kernel, *api, *info, self->regular_fn_, flag_type());
914509d5Wenbing Li2 years ago215if (status == nullptr) {
216kernel->extra_.ep_ = self->execution_provider_;
217kernel->extra_.api_ = std::make_unique<OrtW::CustomOpApi>(*api);
218*op_kernel = reinterpret_cast<void*>(kernel.release());
219}
220
221return status;
222};
223
224OrtCustomOp::KernelComputeV2 = [](void* op_kernel, OrtKernelContext* context) -> OrtStatusPtr {
a0c26255Wenbing Li2 years ago225auto kernel = reinterpret_cast<KernelEx*>(op_kernel);
914509d5Wenbing Li2 years ago226std::vector<TensorPtr> tensors;
227auto t = CreateTuple<0, 0, Args...>(kernel->extra_.api_.get(),
228context,
229tensors,
230kernel->extra_.api_->KernelContext_GetInputCount(context),
231kernel->extra_.api_->KernelContext_GetOutputCount(context),
232kernel->extra_.ep_);
f9290e8bWenbing Li2 years ago233return std::apply([kernel](Args const&... t_args) {
234auto status = kernel->Compute(t_args...);
235return ToApiStatus(status); }, t);
914509d5Wenbing Li2 years ago236};
237
238OrtCustomOp::KernelDestroy = [](void* op_kernel) {
239std::unique_ptr<KernelEx>(reinterpret_cast<KernelEx*>(op_kernel)).reset();
240};
241}
cd7d6459Wenbing Li2 years ago242#endif // ORT_API_VERSION >= 16
914509d5Wenbing Li2 years ago243
244OrtLiteCustomStructV2(const char* op_name,
245const char* execution_provider,
246RegularComputeType fn_compute = nullptr)
247: OrtLiteCustomOp(op_name, execution_provider), regular_fn_(fn_compute) {
248ParseArgs(&CustomOpKernel::Compute);
249
cd7d6459Wenbing Li2 years ago250#if ORT_API_VERSION >= 16
f9290e8bWenbing Li2 years ago251if (OrtCustomOp::version >= 16) {
914509d5Wenbing Li2 years ago252DefineCallbackFunctions(&CustomOpKernel::Compute, fn_compute);
253} else
cd7d6459Wenbing Li2 years ago254#endif // ORT_API_VERSION >= 16
914509d5Wenbing Li2 years ago255{
256DefineCallbackFunctionsLegacy(&CustomOpKernel::Compute, fn_compute);
257}
258}
259
260RegularComputeType regular_fn_{};
261};
262
f9290e8bWenbing Li2 years ago263template <typename RType, typename... Args>
914509d5Wenbing Li2 years ago264OrtLiteCustomOp* CreateLiteCustomOpV2(const char* op_name,
265const char* execution_provider,
f9290e8bWenbing Li2 years ago266RType (*custom_compute_fn)(Args...)) {
267using LiteOp = OrtLiteCustomStructV2<FunctionKernel<RType, Args...>>;
914509d5Wenbing Li2 years ago268return std::make_unique<LiteOp>(op_name, execution_provider, custom_compute_fn).release();
269}
270
271template <typename OpKernel>
272OrtLiteCustomOp* CreateLiteCustomOpV2(const char* op_name,
273const char* execution_provider) {
274using LiteOp = OrtLiteCustomStructV2<OpKernel>;
275return std::make_unique<LiteOp>(op_name, execution_provider).release();
276}
277
278} // namespace Custom
279} // namespace Ort
280
8f36cf32Tang, Cheng3 years ago281namespace ortc = Ort::Custom;