// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #if !defined(ORT_EP_API_ADAPTER_HEADER_INCLUDED) #error "This header should not be included directly. Include ep/adapters.h instead." #endif #include #include #include "core/framework/data_types.h" #include "core/session/onnxruntime_type_conversion.h" namespace onnxruntime { namespace ep { namespace adapter { /// /// Gets an OrtMLDataType for a tensor type. Throws on error. /// /// /// inline const OrtDataType* GetTensorType(ONNXTensorElementDataType elem_type) { const OrtEpApi& ep_api = Ort::GetEpApi(); const OrtDataType* result = nullptr; Ort::ThrowOnError(ep_api.GetTensorDataType(elem_type, &result)); return result; } /// /// Gets an OrtDataType for a tensor type. Returns nullptr if the host ORT does not support the type. /// inline const OrtDataType* TryGetTensorType(ONNXTensorElementDataType elem_type) { const OrtEpApi& ep_api = Ort::GetEpApi(); const OrtDataType* result = nullptr; Ort::Status status(ep_api.GetTensorDataType(elem_type, &result)); if (!status.IsOK()) { if (status.GetErrorCode() == ORT_INVALID_ARGUMENT || status.GetErrorCode() == ORT_NOT_IMPLEMENTED) { return nullptr; } Ort::ThrowOnError(status); } return result; } inline const OrtDataType* MLDataTypeToOrtDataType(MLDataType ml_type) { auto tensor_type = ml_type->AsTensorType(); EP_ENFORCE(tensor_type != nullptr, "EP Kernel registration only supports tensor types."); auto elem_type = tensor_type->GetElementType(); auto primitive_type = static_cast(elem_type); auto onnx_type = utils::ToOrtTensorElementDataType(primitive_type->GetDataType()); return GetTensorType(onnx_type); } /// /// Converts an MLDataType to an OrtDataType. Returns nullptr if the host ORT does not support the type. /// This enables forward-compatible plugins to register kernels with type constraints that include newer /// data types without failing when loaded into an older host ORT. /// inline const OrtDataType* TryMLDataTypeToOrtDataType(MLDataType ml_type) { auto tensor_type = ml_type->AsTensorType(); EP_ENFORCE(tensor_type != nullptr, "EP Kernel registration only supports tensor types."); auto elem_type = tensor_type->GetElementType(); auto primitive_type = static_cast(elem_type); auto onnx_type = utils::ToOrtTensorElementDataType(primitive_type->GetDataType()); return TryGetTensorType(onnx_type); } /// /// An adapter class partially implementing the interface of `onnxruntime::KernelDefBuilder`. /// struct KernelDefBuilder { static std::unique_ptr Create() { return std::make_unique(); } explicit KernelDefBuilder() {} KernelDefBuilder& SetName(const char* op_name) { builder_.SetOperatorType(op_name); return *this; } KernelDefBuilder& SetDomain(const char* domain) { builder_.SetDomain(domain); return *this; } KernelDefBuilder& SinceVersion(int since_version) { return SinceVersion(since_version, INT_MAX); } KernelDefBuilder& SinceVersion(int since_version_start, int since_version_end) { builder_.SetSinceVersion(since_version_start, since_version_end); return *this; } KernelDefBuilder& Provider(const char* provider_type) { builder_.SetExecutionProvider(provider_type); return *this; } KernelDefBuilder& TypeConstraint(const char* arg_name, std::vector types) { std::vector ort_types; ort_types.reserve(types.size()); for (const auto& type : types) { const OrtDataType* ort_type = TryMLDataTypeToOrtDataType(type); if (ort_type != nullptr) { ort_types.push_back(ort_type); } } if (types.empty() || !ort_types.empty()) { builder_.AddTypeConstraint(arg_name, ort_types); } else { valid_ = false; } return *this; } KernelDefBuilder& TypeConstraint(const char* arg_name, MLDataType type) { const OrtDataType* ort_type = TryMLDataTypeToOrtDataType(type); if (ort_type != nullptr) { builder_.AddTypeConstraint(arg_name, ort_type); } else { valid_ = false; } return *this; } KernelDefBuilder& MayInplace(const std::vector>& inplaces) { for (const auto& pair : inplaces) { builder_.AddInputOutputMutableAlias(pair.first, pair.second); } return *this; } KernelDefBuilder& MayInplace(int input_index, int output_index) { builder_.AddInputOutputMutableAlias(input_index, output_index); return *this; } KernelDefBuilder& Alias(const std::vector>& aliases) { for (const auto& pair : aliases) { builder_.AddInputOutputAlias(pair.first, pair.second); } return *this; } KernelDefBuilder& Alias(int input_index, int output_index) { builder_.AddInputOutputAlias(input_index, output_index); return *this; } KernelDefBuilder& InputMemoryType(OrtMemType type, int input_index) { builder_.SetInputMemType(input_index, type); return *this; } KernelDefBuilder& InputMemoryType(OrtMemType type, const std::vector& input_indexes) { for (int input_index : input_indexes) { builder_.SetInputMemType(input_index, type); } return *this; } KernelDefBuilder& OutputMemoryType(OrtMemType type, int output_index) { builder_.SetOutputMemType(output_index, type); return *this; } KernelDefBuilder& OutputMemoryType(OrtMemType type, const std::vector& output_indexes) { for (int output_index : output_indexes) { builder_.SetOutputMemType(output_index, type); } return *this; } // ExecQueueId is intentionally a no-op. The plugin EP manages stream // assignment externally; the queue id hint is not needed. KernelDefBuilder& ExecQueueId(int /*queue_id*/) { return *this; } Ort::KernelDef Build() { return valid_ ? builder_.Build() : Ort::KernelDef{nullptr}; } private: Ort::KernelDefBuilder builder_; bool valid_ = true; }; } // namespace adapter } // namespace ep } // namespace onnxruntime