|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#if !defined(ORT_EP_API_ADAPTER_HEADER_INCLUDED) |
| 7 | +#error "This header should not be included directly. Include ep/_pch.h instead." |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <memory> |
| 11 | + |
| 12 | +#include "core/framework/data_types.h" |
| 13 | + |
| 14 | +namespace onnxruntime { |
| 15 | +namespace ep { |
| 16 | +namespace adapter { |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Gets an OrtMLDataType for a tensor type. Throws on error. |
| 20 | +/// </summary> |
| 21 | +/// <param name="elem_type"></param> |
| 22 | +/// <returns></returns> |
| 23 | +inline const OrtDataType* GetTensorType(ONNXTensorElementDataType elem_type) { |
| 24 | + const OrtEpApi& ep_api = Ort::GetEpApi(); |
| 25 | + const OrtDataType* result = nullptr; |
| 26 | + |
| 27 | + Ort::ThrowOnError(ep_api.GetTensorDataType(elem_type, &result)); |
| 28 | + return result; |
| 29 | +} |
| 30 | + |
| 31 | +inline const OrtDataType* MLDataTypeToOrtDataType(MLDataType ml_type) { |
| 32 | + auto tensor_type = ml_type->AsTensorType(); |
| 33 | + EP_ENFORCE(tensor_type != nullptr, "EP Kernel registration only supports tensor types."); |
| 34 | + auto elem_type = tensor_type->GetElementType(); |
| 35 | + auto primitive_type = static_cast<const PrimitiveDataTypeBase*>(elem_type); |
| 36 | + auto onnx_type = static_cast<ONNXTensorElementDataType>(primitive_type->GetDataType()); |
| 37 | + return GetTensorType(onnx_type); |
| 38 | +} |
| 39 | + |
| 40 | +struct KernelDefBuilder { |
| 41 | + static std::unique_ptr<KernelDefBuilder> Create() { return std::make_unique<KernelDefBuilder>(); } |
| 42 | + |
| 43 | + explicit KernelDefBuilder() {} |
| 44 | + |
| 45 | + KernelDefBuilder& SetName(const char* op_name) { |
| 46 | + builder_.SetOperatorType(op_name); |
| 47 | + return *this; |
| 48 | + } |
| 49 | + |
| 50 | + KernelDefBuilder& SetDomain(const char* domain) { |
| 51 | + builder_.SetDomain(domain); |
| 52 | + return *this; |
| 53 | + } |
| 54 | + |
| 55 | + KernelDefBuilder& SinceVersion(int since_version) { |
| 56 | + return SinceVersion(since_version, INT_MAX); |
| 57 | + } |
| 58 | + |
| 59 | + KernelDefBuilder& SinceVersion(int since_version_start, int since_version_end) { |
| 60 | + builder_.SetSinceVersion(since_version_start, since_version_end); |
| 61 | + return *this; |
| 62 | + } |
| 63 | + |
| 64 | + KernelDefBuilder& Provider(const char* provider_type) { |
| 65 | + builder_.SetExecutionProvider(provider_type); |
| 66 | + return *this; |
| 67 | + } |
| 68 | + |
| 69 | + KernelDefBuilder& TypeConstraint(const char* arg_name, std::vector<MLDataType> types) { |
| 70 | + std::vector<const OrtDataType*> ort_types; |
| 71 | + ort_types.reserve(types.size()); |
| 72 | + for (const auto& type : types) { |
| 73 | + ort_types.push_back(MLDataTypeToOrtDataType(type)); |
| 74 | + } |
| 75 | + builder_.AddTypeConstraint(arg_name, ort_types); |
| 76 | + return *this; |
| 77 | + } |
| 78 | + |
| 79 | + KernelDefBuilder& TypeConstraint(const char* arg_name, MLDataType type) { |
| 80 | + builder_.AddTypeConstraint(arg_name, MLDataTypeToOrtDataType(type)); |
| 81 | + return *this; |
| 82 | + } |
| 83 | + |
| 84 | + KernelDefBuilder& MayInplace(const std::vector<std::pair<int, int>>& inplaces) { |
| 85 | + for (const auto& pair : inplaces) { |
| 86 | + builder_.AddInputOutputMutableAlias(pair.first, pair.second); |
| 87 | + } |
| 88 | + return *this; |
| 89 | + } |
| 90 | + KernelDefBuilder& MayInplace(int input_index, int output_index) { |
| 91 | + builder_.AddInputOutputMutableAlias(input_index, output_index); |
| 92 | + return *this; |
| 93 | + } |
| 94 | + |
| 95 | + KernelDefBuilder& Alias(const std::vector<std::pair<int, int>>& aliases) { |
| 96 | + for (const auto& pair : aliases) { |
| 97 | + builder_.AddInputOutputAlias(pair.first, pair.second); |
| 98 | + } |
| 99 | + return *this; |
| 100 | + } |
| 101 | + KernelDefBuilder& Alias(int input_index, int output_index) { |
| 102 | + builder_.AddInputOutputAlias(input_index, output_index); |
| 103 | + return *this; |
| 104 | + } |
| 105 | + |
| 106 | + KernelDefBuilder& InputMemoryType(OrtMemType type, int input_index) { |
| 107 | + builder_.SetInputMemType(input_index, type); |
| 108 | + return *this; |
| 109 | + } |
| 110 | + |
| 111 | + KernelDefBuilder& InputMemoryType(OrtMemType type, const std::vector<int>& input_indexes) { |
| 112 | + for (int input_index : input_indexes) { |
| 113 | + builder_.SetInputMemType(input_index, type); |
| 114 | + } |
| 115 | + return *this; |
| 116 | + } |
| 117 | + |
| 118 | + KernelDefBuilder& OutputMemoryType(OrtMemType type, int output_index) { |
| 119 | + builder_.SetOutputMemType(output_index, type); |
| 120 | + return *this; |
| 121 | + } |
| 122 | + |
| 123 | + KernelDefBuilder& OutputMemoryType(OrtMemType type, const std::vector<int>& output_indexes) { |
| 124 | + for (int output_index : output_indexes) { |
| 125 | + builder_.SetOutputMemType(output_index, type); |
| 126 | + } |
| 127 | + return *this; |
| 128 | + } |
| 129 | + |
| 130 | + KernelDefBuilder& ExecQueueId(int queue_id) { return *this; } |
| 131 | + |
| 132 | + Ort::KernelDef Build() { return builder_.Build(); } |
| 133 | + |
| 134 | + private: |
| 135 | + Ort::KernelDefBuilder builder_; |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace adapter |
| 139 | +} // namespace ep |
| 140 | +} // namespace onnxruntime |
0 commit comments