From 98f61fbb25aeab97e0bc2a434d9b009b99baccaf Mon Sep 17 00:00:00 2001 From: Yueqing Zhang Date: Wed, 15 Oct 2025 20:28:18 -0500 Subject: [PATCH] imp --- .../core/providers/vitisai/imp/global_api.cc | 35 ++++++++----------- .../vitisai/include/vaip/global_api.h | 6 ++-- .../vitisai/vitisai_provider_factory.cc | 17 ++++----- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/onnxruntime/core/providers/vitisai/imp/global_api.cc b/onnxruntime/core/providers/vitisai/imp/global_api.cc index d7c9f43e8751d..fdb0792ba6588 100644 --- a/onnxruntime/core/providers/vitisai/imp/global_api.cc +++ b/onnxruntime/core/providers/vitisai/imp/global_api.cc @@ -593,25 +593,17 @@ struct ExternalEpLibaray { ExternalEpLibaray(const std::string& libray_name) : libray_name_{libray_name} { Ensure(); } - onnxruntime::Provider* (*GetProvider)(); + onnxruntime::Provider* (*get_provider_api)(); void Ensure() { if (handle_) return; auto& env = Provider_GetHost()->Env__Default(); -#ifdef _WIN32 - // this dll is already linked to the executable, normally a test program - if (!handle_) { - // First try loading with full path - auto library_filename = PathString(libray_name_.c_str()); - auto module_relative_full_path = env.GeRuntimePath() + PathString(LIBRARY_PREFIX ORT_TSTR("onnxruntime_vitisai_ep") LIBRARY_EXTENSION); - ORT_THROW_IF_ERROR(env.LoadDynamicLibrary(module_relative_full_path, true, &handle_)); - } -#else - auto full_path = env.GetRuntimePath() + PathString(LIBRARY_PREFIX ORT_TSTR("onnxruntime_vitisai_ep") LIBRARY_EXTENSION); + auto library_filename = PathString(LIBRARY_PREFIX) + PathString(libray_name_.begin(), libray_name_.end()) + LIBRARY_EXTENSION; + auto full_path = env.GetRuntimePath() + library_filename; ORT_THROW_IF_ERROR(env.LoadDynamicLibrary(full_path, true, &handle_)); -#endif - ORT_THROW_IF_ERROR(env.GetSymbolFromLibrary(handle_, "GetProvider", (void**)&GetProvider)); + ORT_THROW_IF_ERROR(env.GetSymbolFromLibrary(handle_, "GetProvider", (void**)&get_provider_api)); + get_provider_api()->Initialize(); } void Clear() { if (handle_) { @@ -626,16 +618,19 @@ struct ExternalEpLibaray { std::string libray_name_; void* handle_{}; }; -static std::unordered_map g_external_ep_libaries; +static std::unordered_map> g_external_ep_libaries; -std::unique_ptr -CreateExecutionProviderFromAnotherEp(const std::string& lib, +std::unique_ptr +CreateExecutionProviderFromAnotherEp(const std::string& lib, const OrtSessionOptions& session_options, std::unordered_map& provider_options) { auto it = g_external_ep_libaries.find(lib); if (it == g_external_ep_libaries.end()) { - it = g_external_ep_libaries.emplace(lib).first; + it = g_external_ep_libaries.emplace(lib, std::make_unique(lib)).first; } - auto ep_factory= it->second->GetProvider()->CreateExecutionProviderFactory(static_cast(&provider_options)); - auto ep = ep_factory->CreateProvider(); - return ep; + auto get_provider_func = it->second->get_provider_api; + auto provider = get_provider_func(); + std::unique_ptr ret; + std::ignore = provider->CreateIExecutionProvider(nullptr, nullptr, 0, const_cast(provider_options), session_options, *((OrtLogger*)nullptr), ret); + + return ret; } \ No newline at end of file diff --git a/onnxruntime/core/providers/vitisai/include/vaip/global_api.h b/onnxruntime/core/providers/vitisai/include/vaip/global_api.h index 0f8cf7f88b181..567f2cb4b39e3 100644 --- a/onnxruntime/core/providers/vitisai/include/vaip/global_api.h +++ b/onnxruntime/core/providers/vitisai/include/vaip/global_api.h @@ -6,10 +6,12 @@ #define ORT_API_MANUAL_INIT #include "core/session/onnxruntime_cxx_api.h" #include "core/framework/provider_options.h" +#include "core/framework/execution_provider.h" #include "vaip/my_ort.h" #include "vaip/dll_safe.h" #include "vaip/custom_op.h" #include +#include void initialize_vitisai_ep(); void deinitialize_vitisai_ep(); vaip_core::DllSafe>> compile_onnx_model(const onnxruntime::GraphViewer& graph_viewer, const onnxruntime::logging::Logger& logger, const onnxruntime::ProviderOptions& options); @@ -40,6 +42,6 @@ using EventInfo = std::tuple< void profiler_collect( std::vector& api_events, std::vector& kernel_events); -std::unique_ptr -CreateExecutionProviderFromAnotherEp(const std::string& lib, +std::unique_ptr +CreateExecutionProviderFromAnotherEp(const std::string& lib, const OrtSessionOptions& session_options, std::unordered_map& provider_options); diff --git a/onnxruntime/core/providers/vitisai/vitisai_provider_factory.cc b/onnxruntime/core/providers/vitisai/vitisai_provider_factory.cc index 91be4c1f9f571..19ee73488a3ac 100644 --- a/onnxruntime/core/providers/vitisai/vitisai_provider_factory.cc +++ b/onnxruntime/core/providers/vitisai/vitisai_provider_factory.cc @@ -7,7 +7,6 @@ #include #include #include - #include "vaip/global_api.h" #include "./vitisai_execution_provider.h" #include "core/framework/execution_provider.h" @@ -28,10 +27,7 @@ struct VitisAIProviderFactory : IExecutionProviderFactory { }; std::unique_ptr VitisAIProviderFactory::CreateProvider() { - auto it = info_.find("external_ep_libray"); - if (it != info_.end()) { - return CreateExecutionProviderFromAnotherEp(*it, info_); - } + // can not get session options, so this api can't create anotherep return std::make_unique(info_); } @@ -60,9 +56,14 @@ std::unique_ptr VitisAIProviderFactory::CreateProvider(const provider_options["ort_session_config." + key] = value; } } - auto it = provider_options.find("external_ep_libray"); + + auto it = provider_options.find("llm"); // to be finalized + if (it != provider_options.end() && info_.find("external_ep_library") == info_.end()) { + provider_options["external_ep_libray"] = "onnxruntime_providers_ryzenai"; + } + it = provider_options.find("external_ep_libray"); if (it != provider_options.end()) { - return CreateExecutionProviderFromAnotherEp(*it, provider_options); + return CreateExecutionProviderFromAnotherEp(it->second, session_options, provider_options); } auto ep_instance = std::make_unique(provider_options); ep_instance->SetLogger(reinterpret_cast(&session_logger)); @@ -88,7 +89,7 @@ struct VitisAI_Provider : Provider { } }; // Get provider specific custom op domain list. Provider has the resposibility to release OrtCustomOpDomain instances it creates. - void GetCustomOpDomainList(IExecutionProviderFactory*, std::vector&) override {}; + void GetCustomOpDomainList(IExecutionProviderFactory*, std::vector&) override{}; // Called right after loading the shared library, if this throws any errors Shutdown() will be called and the library unloaded void Initialize() override { initialize_vitisai_ep(); } // Called right before unloading the shared library