microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
includes/ocos.h
126lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING |
| 7 | #include <string> |
| 8 | #include <algorithm> |
| 9 | #include <functional> |
| 10 | #include <iterator> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "onnxruntime_customop.hpp" |
| 14 | |
| 15 | // A helper API to support test kernels. |
| 16 | // Must be invoked before RegisterCustomOps. |
| 17 | extern "C" bool ORT_API_CALL AddExternalCustomOp(const OrtCustomOp* c_op); |
| 18 | |
| 19 | constexpr const char* c_OpDomain = "ai.onnx.contrib"; |
| 20 | constexpr const char* c_ComMsExtOpDomain = "com.microsoft.extensions"; |
| 21 | |
| 22 | struct BaseKernel { |
| 23 | BaseKernel(const OrtApi& api, const OrtKernelInfo& info) noexcept : api_(api), info_(info), ort_(api_) { |
| 24 | } |
| 25 | |
| 26 | template <class T> |
| 27 | bool TryToGetAttribute(const char* name, T& value) const noexcept; |
| 28 | |
| 29 | template <class T> |
| 30 | T TryToGetAttributeWithDefault(const char* name, const T& default_value) const noexcept { |
| 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, |
| 37 | const std::vector<int64_t>& data); |
| 38 | |
| 39 | protected: |
| 40 | OrtErrorCode GetErrorCodeAndRelease(OrtStatusPtr status) const noexcept; |
| 41 | const OrtApi& api_; |
| 42 | OrtW::CustomOpApi ort_; |
| 43 | const OrtKernelInfo& info_; |
| 44 | }; |
| 45 | |
| 46 | struct OrtTensorDimensions : std::vector<int64_t> { |
| 47 | OrtTensorDimensions() = default; |
| 48 | OrtTensorDimensions(const OrtW::CustomOpApi& ort, const OrtValue* value) { |
| 49 | OrtTensorTypeAndShapeInfo* info = ort.GetTensorTypeAndShape(value); |
| 50 | std::vector<int64_t>::operator=(ort.GetTensorShape(info)); |
| 51 | ort.ReleaseTensorTypeAndShapeInfo(info); |
| 52 | } |
| 53 | |
| 54 | int64_t Size() const { |
| 55 | int64_t s = 1; |
| 56 | for (auto it = begin(); it != end(); ++it) |
| 57 | s *= *it; |
| 58 | return s; |
| 59 | } |
| 60 | |
| 61 | bool IsScalar() const { |
| 62 | return empty(); |
| 63 | } |
| 64 | |
| 65 | bool IsVector() const { |
| 66 | return size() == 1; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | template <typename... Args> |
| 71 | class CuopContainer { |
| 72 | public: |
| 73 | CuopContainer() : op_instances_({[]() { return std::make_shared<Args>(); }()...}) { |
| 74 | ocos_list_.reserve(op_instances_.size()); |
| 75 | std::transform(op_instances_.begin(), op_instances_.end(), std::back_inserter(ocos_list_), |
| 76 | [](const std::shared_ptr<OrtCustomOp>& custom_op) { return custom_op.get(); }); |
| 77 | } |
| 78 | |
| 79 | const std::vector<const OrtCustomOp*>& GetCustomOps() const { |
| 80 | return ocos_list_; |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | std::vector<const OrtCustomOp*> ocos_list_; |
| 85 | std::vector<std::shared_ptr<OrtCustomOp>> op_instances_; // use shared_ptr to capture type specific deleter |
| 86 | }; |
| 87 | |
| 88 | struct CustomOpClassBegin { |
| 89 | }; |
| 90 | |
| 91 | using FxLoadCustomOpFactory = std::function<const std::vector<const OrtCustomOp*>&()>; |
| 92 | |
| 93 | template <typename _Begin_place_holder, typename... Args> |
| 94 | const std::vector<const OrtCustomOp*>& LoadCustomOpClasses() { |
| 95 | static CuopContainer<Args...> ctr; // Let C++ runtime take cares of the MP initializing. |
| 96 | return ctr.GetCustomOps(); |
| 97 | } |
| 98 | |
| 99 | #if defined(PYTHON_OP_SUPPORT) |
| 100 | const OrtCustomOp* FetchPyCustomOps(size_t& count); |
| 101 | OrtStatusPtr RegisterPythonDomainAndOps(OrtSessionOptions*, const OrtApi*); |
| 102 | #endif |
| 103 | |
| 104 | #ifdef ENABLE_MATH |
| 105 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Math; |
| 106 | #endif // ENABLE_MATH |
| 107 | |
| 108 | #ifdef ENABLE_TOKENIZER |
| 109 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Tokenizer; |
| 110 | #endif // ENABLE_TOKENIZER |
| 111 | |
| 112 | #ifdef ENABLE_TF_STRING |
| 113 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Text; |
| 114 | #endif // ENABLE_TF_STRING |
| 115 | |
| 116 | #ifdef ENABLE_CV2 |
| 117 | extern FxLoadCustomOpFactory LoadCustomOpClasses_CV2; |
| 118 | #endif // ENABLE_OPENCV |
| 119 | |
| 120 | #ifdef ENABLE_VISION |
| 121 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Vision; |
| 122 | #endif |
| 123 | |
| 124 | #ifdef ENABLE_DR_LIBS |
| 125 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Audio; |
| 126 | #endif |
| 127 | |