microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
rel-0.4

Branches

Tags

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

Clone

HTTPS

Download ZIP

includes/ocos.h

113lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include <vector>
7#include <functional>
8
9#define ORT_API_MANUAL_INIT
10#include "onnxruntime_cxx_api.h"
11#undef ORT_API_MANUAL_INIT
12
13
14// A helper API to support test kernels.
15// Must be invoked before RegisterCustomOps.
16extern "C" bool ORT_API_CALL AddExternalCustomOp(const OrtCustomOp* c_op);
17
18const char c_OpDomain[] = "ai.onnx.contrib";
19
20struct BaseKernel {
21 BaseKernel(OrtApi api) : api_(api), info_(nullptr), ort_(api_) {}
22 BaseKernel(OrtApi api, const OrtKernelInfo* info) : api_(api), info_(info), ort_(api_) {}
23
24 bool HasAttribute(const char* name) const;
25
26 template <class T>
27 bool TryToGetAttribute(const char* name, T& value);
28
29 template <class T>
30 T TryToGetAttributeWithDefault(const char* name, T default_value) {
31 T& result = default_value;
32 TryToGetAttribute(name, result);
33 return result;
34 }
35
36 void SetOutput(OrtKernelContext* ctx, size_t output_idx, const std::vector<int64_t>& dim, const std::vector<int64_t>& data);
37
38 protected:
39 OrtErrorCode GetErrorCodeAndRelease(OrtStatusPtr status);
40 OrtApi api_; // keep a copy of the struct, whose ref is used in the ort_
41 Ort::CustomOpApi ort_;
42 const OrtKernelInfo* info_;
43};
44
45struct OrtTensorDimensions : std::vector<int64_t> {
46 OrtTensorDimensions() = default;
47 OrtTensorDimensions(Ort::CustomOpApi& ort, const OrtValue* value) {
48 OrtTensorTypeAndShapeInfo* info = ort.GetTensorTypeAndShape(value);
49 std::vector<int64_t>::operator=(ort.GetTensorShape(info));
50 ort.ReleaseTensorTypeAndShapeInfo(info);
51 }
52
53 int64_t Size() const {
54 int64_t s = 1.;
55 for (auto it = begin(); it != end(); ++it)
56 s *= *it;
57 return s;
58 }
59};
60
61
62template <typename... Args>
63class CuopContainer {
64 public:
65 CuopContainer() : ocos_list_({[]() { return new Args; }()...}) {
66 ocos_list_.push_back(nullptr);
67 }
68
69 ~CuopContainer() {
70 // skip the last null pointer.
71 for (auto i = 0; i < ocos_list_.size() - 1; i++) {
72 delete ocos_list_[i];
73 }
74
75 ocos_list_.clear();
76 }
77
78 const OrtCustomOp** GetList() {
79 return &const_cast<const OrtCustomOp*&>(ocos_list_.front());
80 }
81
82 private:
83 std::vector<OrtCustomOp*> ocos_list_;
84};
85
86struct CustomOpClassBegin{
87};
88
89typedef std::function<const OrtCustomOp**()> FxLoadCustomOpFactory;
90
91template <typename _Begin_place_holder, typename... Args>
92const OrtCustomOp** LoadCustomOpClasses() {
93 static CuopContainer<Args...> ctr; // Let C++ runtime take cares of the MP initializing.
94 return ctr.GetList();
95}
96
97#if defined(PYTHON_OP_SUPPORT)
98const OrtCustomOp* FetchPyCustomOps(size_t& count);
99OrtStatusPtr RegisterPythonDomainAndOps(OrtSessionOptions*, const OrtApi*);
100bool EnablePyCustomOps(bool enable = true);
101#endif
102
103#ifdef ENABLE_MATH
104extern FxLoadCustomOpFactory LoadCustomOpClasses_Math;
105#endif // ENABLE_MATH
106
107#ifdef ENABLE_TOKENIZER
108extern FxLoadCustomOpFactory LoadCustomOpClasses_Tokenizer;
109#endif // ENABLE_TOKENIZER
110
111#ifdef ENABLE_TF_STRING
112extern FxLoadCustomOpFactory LoadCustomOpClasses_Text;
113#endif // ENABLE_TF_STRING
114