microsoft/onnxruntime-extensions

Public

mirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
leca/pagedAttention

Branches

Tags

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

Clone

HTTPS

Download ZIP

include/op_def_struct.h

281lines · modecode

1// 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
9#include <cstdint>
10#include <cstddef>
11#include <array>
12#include <memory>
13#include <string>
14#include <vector>
15#include <utility>
16#include <type_traits>
17#include <optional>
18#include <functional>
19
20#include "exceptions.h"
21#include "onnxruntime_extensions.h"
22#include "custom_op/custom_op_lite.h"
23
24#define MIN_ORT_VERSION_SUPPORTED 11
25
26namespace Ort {
27namespace Custom {
28
29template <typename T>
30inline OrtStatusPtr ToApiStatus(const T& status) {
31 return status.CreateOrtStatus();
32}
33
34template <>
35inline OrtStatusPtr ToApiStatus(const OrtStatusPtr& status) {
36 return status;
37}
38
39template <typename RType, typename... Args>
40struct FunctionKernel {
41 using ComputeFn = std::function<RType(Args...)>;
42
43 RType Compute(Args... args) const {
44 return compute_fn_(args...);
45 }
46
47 ComputeFn compute_fn_;
48};
49
50// primary template handles types that have no nested ::type member:
51template <class, class = void>
52struct IsFunctionKernel {
53 typedef std::false_type type;
54};
55
56// specialization recognizes types that do have a nested ::type member:
57template <class T>
58struct IsFunctionKernel<T, std::void_t<typename T::ComputeFn>> {
59 typedef std::true_type type;
60};
61
62// Helper type
63template <typename T>
64struct ComputeArgsList;
65
66// Specialization for member function
67template <typename RType, typename C, typename... Args>
68struct ComputeArgsList<RType (C::*)(Args...) const> {
69 using FunctionType = RType (*)(Args...);
70 using MemberFunctionType = RType (C::*)(Args...) const;
71 using ResultType = RType;
72};
73
74template<typename, typename T>
75struct HasOnModelAttach {
76 static_assert(
77 std::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:
86 template<typename T>
87 static constexpr auto check(T*)
88 -> typename
89 std::is_same<
90 decltype( std::declval<T>().OnModelAttach( std::declval<Args>()... ) ),
91 Ret
92 >::type; // attempt to call it and see if the return type is correct
93
94 template<typename>
95 static constexpr std::false_type check(...);
96
97 typedef decltype(check<C>(0)) type;
98
99public:
100 static constexpr bool value = type::value;
101};
102
103template <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
109template <typename CustomOpKernel>
110struct OrtLiteCustomStructV2 : public OrtLiteCustomOp {
111 using ComputeFunction = decltype(&CustomOpKernel::Compute);
112 using RegularComputeType = typename ComputeArgsList<ComputeFunction>::FunctionType;
113 using RType = typename ComputeArgsList<ComputeFunction>::ResultType;
114
115 template <typename... Args>
116 using MemberComputeType = RType (CustomOpKernel::*)(Args...) const;
117
118 struct KernelEx : public CustomOpKernel {
119 struct {
120 std::string ep_{};
121 std::unique_ptr<OrtW::CustomOpApi> api_;
122 } extra_;
123 };
124
125 template <typename T>
126 static OrtStatusPtr InitKernel(KernelEx& kernel,
127 const OrtApi& api, const OrtKernelInfo& info, RegularComputeType fn, T t) {
128 if constexpr (HasOnModelAttach<KernelEx, OrtStatusPtr(const OrtApi&, const OrtKernelInfo&)>::value){
129 auto status = kernel.OnModelAttach(api, info);
130 return ToApiStatus(status);
131 }
132 else {
133 auto status = kernel.OnModelAttach(OrtAttributeReader(api, info));
134 return ToApiStatus(status);
135 }
136 }
137
138 static OrtStatusPtr InitKernel(
139 KernelEx& kernel,
140 const OrtApi& api, const OrtKernelInfo& info, RegularComputeType fn, std::true_type) {
141 kernel.compute_fn_ = fn;
142 return nullptr;
143 }
144
145 template <typename... Args>
146 void ParseArgs(MemberComputeType<Args...> fn) {
147 OrtLiteCustomOp::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
151 template <typename... Args>
152 void DefineCallbackFunctionsLegacy(MemberComputeType<Args...> fn, RegularComputeType regular_fn) {
153 OrtCustomOp::CreateKernel = [](const OrtCustomOp* this_, const OrtApi* ort_api, const OrtKernelInfo* info) {
154 auto self = static_cast<const OrtLiteCustomStructV2<CustomOpKernel>*>(this_);
155 auto kernel = std::make_unique<KernelEx>();
156 typedef typename IsFunctionKernel<CustomOpKernel>::type type_flag;
157 auto status = InitKernel(*kernel, *ort_api, *info, self->regular_fn_, type_flag());
158 OrtW::ThrowOnError(*ort_api, status);
159
160 kernel->extra_.ep_ = self->execution_provider_;
161 kernel->extra_.api_ = std::make_unique<OrtW::CustomOpApi>(*ort_api);
162 return reinterpret_cast<void*>(kernel.release());
163 };
164
165 OrtCustomOp::KernelCompute = [](void* op_kernel, OrtKernelContext* context) {
166 auto kernel = reinterpret_cast<KernelEx*>(op_kernel);
167 std::vector<TensorPtr> tensors;
168 auto t = CreateTuple<0, 0, Args...>(kernel->extra_.api_.get(),
169 context,
170 tensors,
171 kernel->extra_.api_->KernelContext_GetInputCount(context),
172 kernel->extra_.api_->KernelContext_GetOutputCount(context),
173 kernel->extra_.ep_);
174 std::apply([kernel](Args const&... t_args) {
175 auto status = kernel->Compute(t_args...); OrtW::API::ThrowOnError(ToApiStatus(status)); }, t);
176 };
177
178 OrtCustomOp::KernelDestroy = [](void* op_kernel) {
179 std::unique_ptr<KernelEx>(reinterpret_cast<KernelEx*>(op_kernel)).reset();
180 };
181 }
182
183#if ORT_API_VERSION >= 16
184 template <typename... Args>
185 void DefineCallbackFunctions(MemberComputeType<Args...> fn, RegularComputeType regular_fn) {
186 OrtCustomOp::CreateKernel = nullptr;
187 OrtCustomOp::KernelCompute = nullptr;
188
189 if constexpr (CustomOp_defined_getInputMemoryType<CustomOpKernel>::value) {
190 OrtCustomOp::GetInputMemoryType = [](const OrtCustomOp* /*this_*/, size_t index) -> OrtMemType {
191 return CustomOpKernel::GetInputMemoryType(index);
192 };
193 }
194
195 OrtCustomOp::CreateKernelV2 = [](const OrtCustomOp* this_,
196 const OrtApi* api, const OrtKernelInfo* info, void** op_kernel) -> OrtStatusPtr {
197 if (api == nullptr) {
198 assert(false && "Got a null pointer for ORT api on calling CreateKernelV2");
199 // should never happened, what we can do?
200 return nullptr;
201 }
202
203 if (this_ == nullptr || info == nullptr || op_kernel == nullptr) {
204 return api->CreateStatus(ORT_INVALID_ARGUMENT, "OrtCustomOp::CreateKernelV2: received a null pointer");
205 }
206
207 auto self = static_cast<const OrtLiteCustomStructV2<CustomOpKernel>*>(this_);
208 auto kernel = std::make_unique<KernelEx>();
209 if (kernel == nullptr) {
210 return api->CreateStatus(ORT_FAIL, "OrtCustomOp::CreateKernelV2: failed to new a kernel, OOM?");
211 }
212
213 typedef typename IsFunctionKernel<CustomOpKernel>::type flag_type;
214 auto status = InitKernel(*kernel, *api, *info, self->regular_fn_, flag_type());
215 if (status == nullptr) {
216 kernel->extra_.ep_ = self->execution_provider_;
217 kernel->extra_.api_ = std::make_unique<OrtW::CustomOpApi>(*api);
218 *op_kernel = reinterpret_cast<void*>(kernel.release());
219 }
220
221 return status;
222 };
223
224 OrtCustomOp::KernelComputeV2 = [](void* op_kernel, OrtKernelContext* context) -> OrtStatusPtr {
225 auto kernel = reinterpret_cast<KernelEx*>(op_kernel);
226 std::vector<TensorPtr> tensors;
227 auto t = CreateTuple<0, 0, Args...>(kernel->extra_.api_.get(),
228 context,
229 tensors,
230 kernel->extra_.api_->KernelContext_GetInputCount(context),
231 kernel->extra_.api_->KernelContext_GetOutputCount(context),
232 kernel->extra_.ep_);
233 return std::apply([kernel](Args const&... t_args) {
234 auto status = kernel->Compute(t_args...);
235 return ToApiStatus(status); }, t);
236 };
237
238 OrtCustomOp::KernelDestroy = [](void* op_kernel) {
239 std::unique_ptr<KernelEx>(reinterpret_cast<KernelEx*>(op_kernel)).reset();
240 };
241 }
242#endif // ORT_API_VERSION >= 16
243
244 OrtLiteCustomStructV2(const char* op_name,
245 const char* execution_provider,
246 RegularComputeType fn_compute = nullptr)
247 : OrtLiteCustomOp(op_name, execution_provider), regular_fn_(fn_compute) {
248 ParseArgs(&CustomOpKernel::Compute);
249
250#if ORT_API_VERSION >= 16
251 if (OrtCustomOp::version >= 16) {
252 DefineCallbackFunctions(&CustomOpKernel::Compute, fn_compute);
253 } else
254#endif // ORT_API_VERSION >= 16
255 {
256 DefineCallbackFunctionsLegacy(&CustomOpKernel::Compute, fn_compute);
257 }
258 }
259
260 RegularComputeType regular_fn_{};
261};
262
263template <typename RType, typename... Args>
264OrtLiteCustomOp* CreateLiteCustomOpV2(const char* op_name,
265 const char* execution_provider,
266 RType (*custom_compute_fn)(Args...)) {
267 using LiteOp = OrtLiteCustomStructV2<FunctionKernel<RType, Args...>>;
268 return std::make_unique<LiteOp>(op_name, execution_provider, custom_compute_fn).release();
269}
270
271template <typename OpKernel>
272OrtLiteCustomOp* CreateLiteCustomOpV2(const char* op_name,
273 const char* execution_provider) {
274 using LiteOp = OrtLiteCustomStructV2<OpKernel>;
275 return std::make_unique<LiteOp>(op_name, execution_provider).release();
276}
277
278} // namespace Custom
279} // namespace Ort
280
281namespace ortc = Ort::Custom;
282