microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
pyop/pykernel.h
94lines · modeblame
28b2ab10Wenbing Li5 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. | |
| 3 | | |
| 4 | #pragma once | |
3ef90b46Xavier Dupré5 years ago | 5 | |
28b2ab10Wenbing Li5 years ago | 6 | #include <vector> |
| 7 | #include <map> | |
| 8 | #include "ocos.h" | |
| 9 | | |
| 10 | struct PyCustomOpDef { | |
| 11 | std::string op_type; | |
5320af1eWenbing Li3 years ago | 12 | uint64_t obj_id = 0; |
28b2ab10Wenbing Li5 years ago | 13 | std::vector<int> input_types; |
| 14 | std::vector<int> output_types; | |
a0769463Xavier Dupré5 years ago | 15 | std::vector<std::string> attrs; |
28b2ab10Wenbing Li5 years ago | 16 | |
e36205eeXavier Dupré5 years ago | 17 | static void AddOp(const PyCustomOpDef* cod); |
28b2ab10Wenbing Li5 years ago | 18 | |
5a3c6295Wenbing Li5 years ago | 19 | // no initializer here to avoid gcc whole-archive |
| 20 | static const int undefined; | |
| 21 | static const int dt_float; | |
| 22 | static const int dt_uint8; | |
| 23 | static const int dt_int8; | |
| 24 | static const int dt_uint16; | |
| 25 | static const int dt_int16; | |
| 26 | static const int dt_int32; | |
| 27 | static const int dt_int64; | |
| 28 | static const int dt_string; | |
| 29 | static const int dt_bool; | |
| 30 | static const int dt_float16; | |
| 31 | static const int dt_double; | |
| 32 | static const int dt_uint32; | |
| 33 | static const int dt_uint64; | |
| 34 | static const int dt_complex64; | |
| 35 | static const int dt_complex128; | |
| 36 | static const int dt_bfloat16; | |
28b2ab10Wenbing Li5 years ago | 37 | }; |
| 38 | | |
| 39 | struct PyCustomOpKernel { | |
6c3b496eWenbing Li4 years ago | 40 | PyCustomOpKernel(OrtApi api, const OrtKernelInfo* info, uint64_t id, const std::vector<std::string>& attrs); |
28b2ab10Wenbing Li5 years ago | 41 | void Compute(OrtKernelContext* context); |
| 42 | | |
| 43 | private: | |
| 44 | OrtApi api_; // keep a copy of the struct, whose ref is used in the ort_ | |
| 45 | Ort::CustomOpApi ort_; | |
| 46 | uint64_t obj_id_; | |
a0769463Xavier Dupré5 years ago | 47 | std::map<std::string, std::string> attrs_values_; |
28b2ab10Wenbing Li5 years ago | 48 | }; |
| 49 | | |
| 50 | struct PyCustomOpFactory : Ort::CustomOpBase<PyCustomOpFactory, PyCustomOpKernel> { | |
6c3b496eWenbing Li4 years ago | 51 | PyCustomOpFactory() { |
| 52 | // STL vector needs it. | |
| 53 | } | |
| 54 | | |
| 55 | PyCustomOpFactory(const PyCustomOpDef* opdef, const std::string& domain, const std::string& op) { | |
e36205eeXavier Dupré5 years ago | 56 | if (opdef == nullptr) |
| 57 | throw std::runtime_error("Python definition is empty."); | |
28b2ab10Wenbing Li5 years ago | 58 | opdef_ = opdef; |
6c3b496eWenbing Li4 years ago | 59 | op_domain_ = domain; |
| 60 | op_type_ = op; | |
28b2ab10Wenbing Li5 years ago | 61 | } |
| 62 | | |
a0769463Xavier Dupré5 years ago | 63 | void* CreateKernel(OrtApi api, const OrtKernelInfo* info) const { |
| 64 | return new PyCustomOpKernel(api, info, opdef_->obj_id, opdef_->attrs); | |
28b2ab10Wenbing Li5 years ago | 65 | }; |
| 66 | | |
| 67 | const char* GetName() const { | |
6c3b496eWenbing Li4 years ago | 68 | return op_type_.c_str(); |
28b2ab10Wenbing Li5 years ago | 69 | }; |
| 70 | | |
| 71 | size_t GetInputTypeCount() const { | |
e36205eeXavier Dupré5 years ago | 72 | return opdef_->input_types.size(); |
28b2ab10Wenbing Li5 years ago | 73 | }; |
| 74 | | |
| 75 | ONNXTensorElementDataType GetInputType(size_t idx) const { | |
e36205eeXavier Dupré5 years ago | 76 | return static_cast<ONNXTensorElementDataType>(opdef_->input_types[idx]); |
28b2ab10Wenbing Li5 years ago | 77 | }; |
| 78 | | |
a0769463Xavier Dupré5 years ago | 79 | const std::vector<std::string>& GetAttributesNames() const { |
| 80 | return opdef_->attrs; | |
| 81 | } | |
| 82 | | |
28b2ab10Wenbing Li5 years ago | 83 | size_t GetOutputTypeCount() const { |
e36205eeXavier Dupré5 years ago | 84 | return opdef_->output_types.size(); |
28b2ab10Wenbing Li5 years ago | 85 | }; |
| 86 | | |
| 87 | ONNXTensorElementDataType GetOutputType(size_t idx) const { | |
e36205eeXavier Dupré5 years ago | 88 | return static_cast<ONNXTensorElementDataType>(opdef_->output_types[idx]); |
| 89 | } | |
28b2ab10Wenbing Li5 years ago | 90 | |
5320af1eWenbing Li3 years ago | 91 | const PyCustomOpDef* opdef_ = nullptr; |
6c3b496eWenbing Li4 years ago | 92 | std::string op_type_; |
| 93 | std::string op_domain_; | |
28b2ab10Wenbing Li5 years ago | 94 | }; |