microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
includes/ocos.h
124lines · 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 | bool HasAttribute(const char* name) const noexcept; |
| 27 | |
| 28 | template <class T> |
| 29 | bool TryToGetAttribute(const char* name, T& value) const noexcept; |
| 30 | |
| 31 | template <class T> |
| 32 | T TryToGetAttributeWithDefault(const char* name, T default_value) const noexcept { |
| 33 | T& result = default_value; |
| 34 | TryToGetAttribute(name, result); |
| 35 | return result; |
| 36 | } |
| 37 | |
| 38 | void SetOutput(OrtKernelContext* ctx, size_t output_idx, const std::vector<int64_t>& dim, |
| 39 | const std::vector<int64_t>& data); |
| 40 | |
| 41 | protected: |
| 42 | OrtErrorCode GetErrorCodeAndRelease(OrtStatusPtr status) const noexcept; |
| 43 | const OrtApi& api_; |
| 44 | OrtW::CustomOpApi ort_; |
| 45 | const OrtKernelInfo& info_; |
| 46 | }; |
| 47 | |
| 48 | struct OrtTensorDimensions : std::vector<int64_t> { |
| 49 | OrtTensorDimensions() = default; |
| 50 | OrtTensorDimensions(const OrtW::CustomOpApi& ort, const OrtValue* value) { |
| 51 | OrtTensorTypeAndShapeInfo* info = ort.GetTensorTypeAndShape(value); |
| 52 | std::vector<int64_t>::operator=(ort.GetTensorShape(info)); |
| 53 | ort.ReleaseTensorTypeAndShapeInfo(info); |
| 54 | } |
| 55 | |
| 56 | int64_t Size() const { |
| 57 | int64_t s = 1; |
| 58 | for (auto it = begin(); it != end(); ++it) |
| 59 | s *= *it; |
| 60 | return s; |
| 61 | } |
| 62 | |
| 63 | bool IsScalar() const { |
| 64 | return empty(); |
| 65 | } |
| 66 | |
| 67 | bool IsVector() const { |
| 68 | return size() == 1; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | template <typename... Args> |
| 73 | class CuopContainer { |
| 74 | public: |
| 75 | CuopContainer() : op_instances_({[]() { return std::make_shared<Args>(); }()...}) { |
| 76 | ocos_list_.reserve(op_instances_.size()); |
| 77 | std::transform(op_instances_.begin(), op_instances_.end(), std::back_inserter(ocos_list_), |
| 78 | [](const std::shared_ptr<OrtCustomOp>& custom_op) { return custom_op.get(); }); |
| 79 | } |
| 80 | |
| 81 | const std::vector<const OrtCustomOp*>& GetCustomOps() const { |
| 82 | return ocos_list_; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | std::vector<const OrtCustomOp*> ocos_list_; |
| 87 | std::vector<std::shared_ptr<OrtCustomOp>> op_instances_; // use shared_ptr to capture type specific deleter |
| 88 | }; |
| 89 | |
| 90 | struct CustomOpClassBegin { |
| 91 | }; |
| 92 | |
| 93 | using FxLoadCustomOpFactory = std::function<const std::vector<const OrtCustomOp*>&()>; |
| 94 | |
| 95 | template <typename _Begin_place_holder, typename... Args> |
| 96 | const std::vector<const OrtCustomOp*>& LoadCustomOpClasses() { |
| 97 | static CuopContainer<Args...> ctr; // Let C++ runtime take cares of the MP initializing. |
| 98 | return ctr.GetCustomOps(); |
| 99 | } |
| 100 | |
| 101 | #if defined(PYTHON_OP_SUPPORT) |
| 102 | const OrtCustomOp* FetchPyCustomOps(size_t& count); |
| 103 | OrtStatusPtr RegisterPythonDomainAndOps(OrtSessionOptions*, const OrtApi*); |
| 104 | #endif |
| 105 | |
| 106 | #ifdef ENABLE_MATH |
| 107 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Math; |
| 108 | #endif // ENABLE_MATH |
| 109 | |
| 110 | #ifdef ENABLE_TOKENIZER |
| 111 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Tokenizer; |
| 112 | #endif // ENABLE_TOKENIZER |
| 113 | |
| 114 | #ifdef ENABLE_TF_STRING |
| 115 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Text; |
| 116 | #endif // ENABLE_TF_STRING |
| 117 | |
| 118 | #ifdef ENABLE_CV2 |
| 119 | extern FxLoadCustomOpFactory LoadCustomOpClasses_CV2; |
| 120 | #endif // ENABLE_OPENCV |
| 121 | |
| 122 | #ifdef ENABLE_VISION |
| 123 | extern FxLoadCustomOpFactory LoadCustomOpClasses_Vision; |
| 124 | #endif |
| 125 | |