From 3c673023bfdf74891f4a74113b59216098c3457e Mon Sep 17 00:00:00 2001 From: Karim Vadsariya Date: Mon, 23 Jun 2025 10:28:08 -0700 Subject: [PATCH 01/15] [MigraphX] Enable automatic selection of MigraphX EP for PREFER_GPU policy --- .../migraphx/migraphx_provider_factory.cc | 129 ++++++++++++++++++ .../core/providers/migraphx/symbols.def | 2 + 2 files changed, 131 insertions(+) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 4a3945ac680d0..2b560a79f9a63 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -144,6 +144,27 @@ struct MIGraphX_Provider : Provider { return onnxruntime::MIGraphXExecutionProviderInfo::ToProviderOptions(options); } + Status CreateIExecutionProvider(const OrtHardwareDevice* const* /*devices*/, + const OrtKeyValuePairs* const* /*ep_metadata*/, + size_t num_devices, + ProviderOptions& provider_options, + const OrtSessionOptions& session_options, + const OrtLogger& logger, + std::unique_ptr& ep) override { + if (num_devices != 1) { + return Status(common::ONNXRUNTIME, ORT_EP_FAIL, "[MigraphX/AMDGPU EP] only supports one device."); + } + + const ConfigOptions* config_options = &session_options.GetConfigOptions(); + + std::array configs_array = {&provider_options, config_options}; + const void* arg = reinterpret_cast(&configs_array); + auto ep_factory = CreateExecutionProviderFactory(arg); + ep = ep_factory->CreateProvider(session_options, logger); + + return Status::OK(); + } + void Initialize() override { InitializeRegistry(); } @@ -156,9 +177,117 @@ struct MIGraphX_Provider : Provider { } // namespace onnxruntime +#include "core/framework/error_code_helper.h" + +// OrtEpApi infrastructure to be able to use the MigraphX/AMDGPU EP as an OrtEpFactory for auto EP selection. +struct MigraphXEpFactory : OrtEpFactory { + MigraphXEpFactory(const OrtApi& ort_api_in, + const char* ep_name, + OrtHardwareDeviceType hw_type) + : ort_api{ort_api_in}, ep_name{ep_name}, ort_hw_device_type{hw_type} { + GetName = GetNameImpl; + GetVendor = GetVendorImpl; + GetSupportedDevices = GetSupportedDevicesImpl; + CreateEp = CreateEpImpl; + ReleaseEp = ReleaseEpImpl; + } + + // Returns the name for the EP. Each unique factory configuration must have a unique name. + // Ex: a factory that supports NPU should have a different than a factory that supports GPU. + static const char* GetNameImpl(const OrtEpFactory* this_ptr) { + const auto* factory = static_cast(this_ptr); + return factory->ep_name.c_str(); + } + + static const char* GetVendorImpl(const OrtEpFactory* this_ptr) { + const auto* factory = static_cast(this_ptr); + return factory->vendor.c_str(); + } + + // Creates and returns OrtEpDevice instances for all OrtHardwareDevices that this factory supports. + // An EP created with this factory is expected to be able to execute a model with *all* supported + // hardware devices at once. A single instance of MigraphX EP is not currently setup to partition a model among + // multiple different MigraphX backends at once (e.g, npu, cpu, gpu), so this factory instance is set to only + // support one backend: gpu. To support a different backend, like npu, create a different factory instance + // that only supports NPU. + static OrtStatus* GetSupportedDevicesImpl(OrtEpFactory* this_ptr, + const OrtHardwareDevice* const* devices, + size_t num_devices, + OrtEpDevice** ep_devices, + size_t max_ep_devices, + size_t* p_num_ep_devices) { + size_t& num_ep_devices = *p_num_ep_devices; + auto* factory = static_cast(this_ptr); + + for (size_t i = 0; i < num_devices && num_ep_devices < max_ep_devices; ++i) { + const OrtHardwareDevice& device = *devices[i]; + if (factory->ort_api.HardwareDevice_Type(&device) == factory->ort_hw_device_type && + factory->ort_api.HardwareDevice_VendorId(&device) == factory->vendor_id) { + OrtKeyValuePairs* ep_options = nullptr; + factory->ort_api.CreateKeyValuePairs(&ep_options); + ORT_API_RETURN_IF_ERROR( + factory->ort_api.GetEpApi()->CreateEpDevice(factory, &device, nullptr, ep_options, + &ep_devices[num_ep_devices++])); + } + } + + return nullptr; + } + + static OrtStatus* CreateEpImpl(OrtEpFactory* /*this_ptr*/, + _In_reads_(num_devices) const OrtHardwareDevice* const* /*devices*/, + _In_reads_(num_devices) const OrtKeyValuePairs* const* /*ep_metadata*/, + _In_ size_t /*num_devices*/, + _In_ const OrtSessionOptions* /*session_options*/, + _In_ const OrtLogger* /*logger*/, + _Out_ OrtEp** /*ep*/) { + return onnxruntime::CreateStatus(ORT_INVALID_ARGUMENT, "[MigraphX/AMDGPU EP] EP factory does not support this method."); + } + + static void ReleaseEpImpl(OrtEpFactory* /*this_ptr*/, OrtEp* /*ep*/) { + // no-op as we never create an EP here. + } + + const OrtApi& ort_api; + const std::string ep_name; + const std::string vendor{"AMD"}; + + // AMD vendor ID. Refer to the ACPI ID registry (search AMD): https://uefi.org/ACPI_ID_List + const uint32_t vendor_id{0x1022}; + const OrtHardwareDeviceType ort_hw_device_type; // Supported OrtHardwareDevice +}; + extern "C" { +// +// Public symbols +// +OrtStatus* CreateEpFactories(const char* /*registration_name*/, const OrtApiBase* ort_api_base, + OrtEpFactory** factories, size_t max_factories, size_t* num_factories) { + const OrtApi* ort_api = ort_api_base->GetApi(ORT_API_VERSION); + + // Factory could use registration_name or define its own EP name. + auto factory_gpu = std::make_unique(*ort_api, + onnxruntime::kMIGraphXExecutionProvider, + OrtHardwareDeviceType_GPU); + + if (max_factories < 1) { + return ort_api->CreateStatus(ORT_INVALID_ARGUMENT, + "Not enough space to return EP factory. Need at least one."); + } + + factories[0] = factory_gpu.release(); + *num_factories = 1; + + return nullptr; +} + +OrtStatus* ReleaseEpFactory(OrtEpFactory* factory) { + delete static_cast(factory); + return nullptr; +} ORT_API(onnxruntime::Provider*, GetProvider) { return &onnxruntime::g_provider; } + } diff --git a/onnxruntime/core/providers/migraphx/symbols.def b/onnxruntime/core/providers/migraphx/symbols.def index 4ec2f7914c208..3afed01da1966 100644 --- a/onnxruntime/core/providers/migraphx/symbols.def +++ b/onnxruntime/core/providers/migraphx/symbols.def @@ -1,2 +1,4 @@ EXPORTS GetProvider + CreateEpFactories + ReleaseEpFactory From a4403e020fbfeff63a5743f3d6e3d2528aa7fde7 Mon Sep 17 00:00:00 2001 From: Karim Vadsariya Date: Tue, 24 Jun 2025 03:25:10 -0700 Subject: [PATCH 02/15] bypass check until vendor id is finalized --- .../core/providers/migraphx/migraphx_provider_factory.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 2b560a79f9a63..7bdbff23b8998 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -221,8 +221,8 @@ struct MigraphXEpFactory : OrtEpFactory { for (size_t i = 0; i < num_devices && num_ep_devices < max_ep_devices; ++i) { const OrtHardwareDevice& device = *devices[i]; - if (factory->ort_api.HardwareDevice_Type(&device) == factory->ort_hw_device_type && - factory->ort_api.HardwareDevice_VendorId(&device) == factory->vendor_id) { + if (factory->ort_api.HardwareDevice_Type(&device) == factory->ort_hw_device_type || true){ + //factory->ort_api.HardwareDevice_VendorId(&device) == factory->vendor_id) { OrtKeyValuePairs* ep_options = nullptr; factory->ort_api.CreateKeyValuePairs(&ep_options); ORT_API_RETURN_IF_ERROR( From f4646a746d108382d5c1b27babbf01f0417fed07 Mon Sep 17 00:00:00 2001 From: Karim Vadsariya Date: Tue, 24 Jun 2025 05:56:34 -0700 Subject: [PATCH 03/15] Fix EPF --- .../migraphx/migraphx_provider_factory.cc | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 7bdbff23b8998..533a63df329bd 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -76,7 +76,29 @@ struct MIGraphX_Provider : Provider { return std::make_shared(info); } - std::shared_ptr CreateExecutionProviderFactory(const void* provider_options) override { + //TODO: Interface change might require changes in other parts of win-onnxruntime? + std::shared_ptr CreateExecutionProviderFactory(const void* param) { + if (param == nullptr) { + LOGS_DEFAULT(ERROR) << "[NvTensorRTRTX EP] Passed NULL options to CreateExecutionProviderFactory()"; + return nullptr; + } + + // std::array pointers_array = *reinterpret_cast*>(param); + const ProviderOptions* provider_options = reinterpret_cast(param); + // const ConfigOptions* config_options = reinterpret_cast(pointers_array[1]); + + if (provider_options == nullptr) { + LOGS_DEFAULT(ERROR) << "[NvTensorRTRTX EP] Passed NULL ProviderOptions to CreateExecutionProviderFactory()"; + return nullptr; + } + + MIGraphXExecutionProviderInfo info = {}; + + UpdateProviderOptions(&info, *provider_options); + return std::make_shared(info); + } + + /* std::shared_ptr CreateExecutionProviderFactory(const void* provider_options) override { auto& options = *reinterpret_cast(provider_options); MIGraphXExecutionProviderInfo info; info.device_id = static_cast(options.device_id); @@ -103,7 +125,7 @@ struct MIGraphX_Provider : Provider { info.arena_extend_strategy = static_cast(options.migraphx_arena_extend_strategy); info.mem_limit = options.migraphx_mem_limit; return std::make_shared(info); - } + }*/ void UpdateProviderOptions(void* provider_options, const ProviderOptions& options) override { auto internal_options = onnxruntime::MIGraphXExecutionProviderInfo::FromProviderOptions(options); @@ -159,7 +181,7 @@ struct MIGraphX_Provider : Provider { std::array configs_array = {&provider_options, config_options}; const void* arg = reinterpret_cast(&configs_array); - auto ep_factory = CreateExecutionProviderFactory(arg); + auto ep_factory = CreateExecutionProviderFactory(&provider_options); ep = ep_factory->CreateProvider(session_options, logger); return Status::OK(); @@ -253,7 +275,7 @@ struct MigraphXEpFactory : OrtEpFactory { const std::string vendor{"AMD"}; // AMD vendor ID. Refer to the ACPI ID registry (search AMD): https://uefi.org/ACPI_ID_List - const uint32_t vendor_id{0x1022}; + const uint32_t vendor_id{0x1022}; //TODO: set correct value for AMD GPU const OrtHardwareDeviceType ort_hw_device_type; // Supported OrtHardwareDevice }; From c93b3493a5a9a4cb7eca2efc30db1bea6e4ff246 Mon Sep 17 00:00:00 2001 From: ozhang Date: Wed, 16 Jul 2025 18:25:00 -0400 Subject: [PATCH 04/15] Reenable device check and change factory creation --- .../migraphx/migraphx_provider_factory.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 533a63df329bd..d94adcf4870d5 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -76,6 +76,7 @@ struct MIGraphX_Provider : Provider { return std::make_shared(info); } + /* //TODO: Interface change might require changes in other parts of win-onnxruntime? std::shared_ptr CreateExecutionProviderFactory(const void* param) { if (param == nullptr) { @@ -97,8 +98,9 @@ struct MIGraphX_Provider : Provider { UpdateProviderOptions(&info, *provider_options); return std::make_shared(info); } + */ - /* std::shared_ptr CreateExecutionProviderFactory(const void* provider_options) override { + std::shared_ptr CreateExecutionProviderFactory(const void* provider_options) override { auto& options = *reinterpret_cast(provider_options); MIGraphXExecutionProviderInfo info; info.device_id = static_cast(options.device_id); @@ -125,7 +127,7 @@ struct MIGraphX_Provider : Provider { info.arena_extend_strategy = static_cast(options.migraphx_arena_extend_strategy); info.mem_limit = options.migraphx_mem_limit; return std::make_shared(info); - }*/ + } void UpdateProviderOptions(void* provider_options, const ProviderOptions& options) override { auto internal_options = onnxruntime::MIGraphXExecutionProviderInfo::FromProviderOptions(options); @@ -173,9 +175,9 @@ struct MIGraphX_Provider : Provider { const OrtSessionOptions& session_options, const OrtLogger& logger, std::unique_ptr& ep) override { - if (num_devices != 1) { - return Status(common::ONNXRUNTIME, ORT_EP_FAIL, "[MigraphX/AMDGPU EP] only supports one device."); - } + //if (num_devices != 1) { + // return Status(common::ONNXRUNTIME, ORT_EP_FAIL, "[MigraphX/AMDGPU EP] only supports one device."); + //} const ConfigOptions* config_options = &session_options.GetConfigOptions(); @@ -243,7 +245,7 @@ struct MigraphXEpFactory : OrtEpFactory { for (size_t i = 0; i < num_devices && num_ep_devices < max_ep_devices; ++i) { const OrtHardwareDevice& device = *devices[i]; - if (factory->ort_api.HardwareDevice_Type(&device) == factory->ort_hw_device_type || true){ + if (factory->ort_api.HardwareDevice_Type(&device) == factory->ort_hw_device_type){ //factory->ort_api.HardwareDevice_VendorId(&device) == factory->vendor_id) { OrtKeyValuePairs* ep_options = nullptr; factory->ort_api.CreateKeyValuePairs(&ep_options); From 45117b268fba1d6f2081787ba61109ab90a41811 Mon Sep 17 00:00:00 2001 From: Karim Vadsariya Date: Mon, 9 Jun 2025 14:45:19 -0700 Subject: [PATCH 05/15] [MIGRAPHX] Add ORT generic interface build support for MigraphX --- cmake/CMakeLists.txt | 5 +++++ onnxruntime/core/platform/posix/env.cc | 2 +- .../providers/shared_library/provider_bridge_provider.cc | 2 +- .../core/providers/shared_library/provider_interfaces.h | 2 +- onnxruntime/core/session/provider_bridge_ort.cc | 2 +- tools/ci_build/build.py | 1 + 6 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index b01110b2a4a03..1666a265537ec 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -858,6 +858,11 @@ if (onnxruntime_USE_MIGRAPHX) list(APPEND ORT_PROVIDER_FLAGS -DUSE_MIGRAPHX=1) list(APPEND ONNXRUNTIME_PROVIDER_NAMES migraphx) endif() + +if (onnxruntime_USE_MIGRAPHX_INTERFACE AND (NOT onnxruntime_USE_MIGRAPHX)) + list(APPEND ORT_PROVIDER_FLAGS -DUSE_MIGRAPHX_INTERFACE=1) +endif() + if (onnxruntime_USE_ARMNN) list(APPEND ORT_PROVIDER_FLAGS -DUSE_ARMNN=1) list(APPEND ONNXRUNTIME_PROVIDER_NAMES armnn) diff --git a/onnxruntime/core/platform/posix/env.cc b/onnxruntime/core/platform/posix/env.cc index 0e43d054d5c5e..1a8aeccb1960f 100644 --- a/onnxruntime/core/platform/posix/env.cc +++ b/onnxruntime/core/platform/posix/env.cc @@ -223,7 +223,7 @@ class PosixThread : public EnvThread { } else { errno = ret; auto [err_no, err_msg] = GetErrnoInfo(); -#if !defined(USE_MIGRAPHX) +#if !defined(USE_MIGRAPHX) && !defined(USE_MIGRAPHX_INTERFACE) LOGS_DEFAULT(ERROR) << "pthread_setaffinity_np failed for thread: " << syscall(SYS_gettid) << ", index: " << p->index << ", mask: " << *p->affinity diff --git a/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc b/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc index c9ff0d807633f..42fd5348f39aa 100644 --- a/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc +++ b/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc @@ -361,7 +361,7 @@ std::unique_ptr CreateGPUDataTransfer() { } #endif -#ifdef USE_MIGRAPHX +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_INTERFACE) std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) { return g_host->CreateMIGraphXAllocator(device_id, name); } diff --git a/onnxruntime/core/providers/shared_library/provider_interfaces.h b/onnxruntime/core/providers/shared_library/provider_interfaces.h index 44dd70211327e..73006a71ddb6b 100644 --- a/onnxruntime/core/providers/shared_library/provider_interfaces.h +++ b/onnxruntime/core/providers/shared_library/provider_interfaces.h @@ -198,7 +198,7 @@ struct ProviderHost { virtual Status CudaCall_false(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) = 0; virtual void CudaCall_true(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) = 0; -#ifdef USE_MIGRAPHX +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_INTERFACE) virtual std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) = 0; virtual std::unique_ptr CreateMIGraphXPinnedAllocator(int16_t device_id, const char* name) = 0; #endif diff --git a/onnxruntime/core/session/provider_bridge_ort.cc b/onnxruntime/core/session/provider_bridge_ort.cc index 3db35ae8769e0..39be601dadac3 100644 --- a/onnxruntime/core/session/provider_bridge_ort.cc +++ b/onnxruntime/core/session/provider_bridge_ort.cc @@ -292,7 +292,7 @@ struct ProviderHostImpl : ProviderHost { Status CudaCall_false(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) override { return GetProviderInfo_CUDA().CudaCall_false(retCode, exprString, libName, successCode, msg, file, line); } void CudaCall_true(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) override { GetProviderInfo_CUDA().CudaCall_true(retCode, exprString, libName, successCode, msg, file, line); } -#ifdef USE_MIGRAPHX +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_INTERFACE) std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) override { return GetProviderInfo_MIGraphX().CreateMIGraphXAllocator(device_id, name); } std::unique_ptr CreateMIGraphXPinnedAllocator(int16_t device_id, const char* name) override { return GetProviderInfo_MIGraphX().CreateMIGraphXPinnedAllocator(device_id, name); } #endif diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index 893f3c80fa4b8..e913ff79804fd 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -454,6 +454,7 @@ def generate_build_tree( "-Donnxruntime_USE_OPENVINO_INTERFACE=" + ("ON" if args.enable_generic_interface else "OFF"), "-Donnxruntime_USE_VITISAI_INTERFACE=" + ("ON" if args.enable_generic_interface else "OFF"), "-Donnxruntime_USE_QNN_INTERFACE=" + ("ON" if args.enable_generic_interface else "OFF"), + "-Donnxruntime_USE_MIGRAPHX_INTERFACE=" + ("ON" if args.enable_generic_interface else "OFF"), # set vars for migraphx "-Donnxruntime_USE_MIGRAPHX=" + ("ON" if args.use_migraphx else "OFF"), "-Donnxruntime_DISABLE_CONTRIB_OPS=" + ("ON" if args.disable_contrib_ops else "OFF"), From b29af05fda60756a67371c8cc6ebade03ab1d372 Mon Sep 17 00:00:00 2001 From: Karim Vadsariya Date: Tue, 10 Jun 2025 10:35:03 -0700 Subject: [PATCH 06/15] Remove preprocesor check for error msg --- onnxruntime/core/platform/posix/env.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/onnxruntime/core/platform/posix/env.cc b/onnxruntime/core/platform/posix/env.cc index 1a8aeccb1960f..fed697ea962bc 100644 --- a/onnxruntime/core/platform/posix/env.cc +++ b/onnxruntime/core/platform/posix/env.cc @@ -223,13 +223,11 @@ class PosixThread : public EnvThread { } else { errno = ret; auto [err_no, err_msg] = GetErrnoInfo(); -#if !defined(USE_MIGRAPHX) && !defined(USE_MIGRAPHX_INTERFACE) LOGS_DEFAULT(ERROR) << "pthread_setaffinity_np failed for thread: " << syscall(SYS_gettid) << ", index: " << p->index << ", mask: " << *p->affinity << ", error code: " << err_no << " error msg: " << err_msg << ". Specify the number of threads explicitly so the affinity is not set."; -#endif } } #endif From 73edb49d707218ffce5d49b8f6d007e196d7c2ed Mon Sep 17 00:00:00 2001 From: Karim Vadsariya Date: Tue, 10 Jun 2025 18:02:45 -0700 Subject: [PATCH 07/15] Rename preprocessor flag for uniformity and add missing places --- cmake/CMakeLists.txt | 2 +- onnxruntime/core/providers/provider_factory_creators.h | 2 +- .../core/providers/shared_library/provider_bridge_provider.cc | 2 +- .../core/providers/shared_library/provider_interfaces.h | 2 +- onnxruntime/core/session/provider_bridge_ort.cc | 2 +- onnxruntime/python/onnxruntime_pybind_state.cc | 2 +- onnxruntime/python/onnxruntime_pybind_state_common.h | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 1666a265537ec..bb4a7385df798 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -860,7 +860,7 @@ if (onnxruntime_USE_MIGRAPHX) endif() if (onnxruntime_USE_MIGRAPHX_INTERFACE AND (NOT onnxruntime_USE_MIGRAPHX)) - list(APPEND ORT_PROVIDER_FLAGS -DUSE_MIGRAPHX_INTERFACE=1) + list(APPEND ORT_PROVIDER_FLAGS -DUSE_MIGRAPHX_PROVIDER_INTERFACE=1) endif() if (onnxruntime_USE_ARMNN) diff --git a/onnxruntime/core/providers/provider_factory_creators.h b/onnxruntime/core/providers/provider_factory_creators.h index 76812f9e83be6..b0d850ca04841 100644 --- a/onnxruntime/core/providers/provider_factory_creators.h +++ b/onnxruntime/core/providers/provider_factory_creators.h @@ -38,7 +38,7 @@ #include "core/providers/dnnl/dnnl_provider_factory_creator.h" #endif -#if defined(USE_MIGRAPHX) +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) #include "core/providers/migraphx/migraphx_provider_factory_creator.h" #endif diff --git a/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc b/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc index 42fd5348f39aa..a5206d3769c3d 100644 --- a/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc +++ b/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc @@ -361,7 +361,7 @@ std::unique_ptr CreateGPUDataTransfer() { } #endif -#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_INTERFACE) +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) { return g_host->CreateMIGraphXAllocator(device_id, name); } diff --git a/onnxruntime/core/providers/shared_library/provider_interfaces.h b/onnxruntime/core/providers/shared_library/provider_interfaces.h index 73006a71ddb6b..4cfdf72fd208f 100644 --- a/onnxruntime/core/providers/shared_library/provider_interfaces.h +++ b/onnxruntime/core/providers/shared_library/provider_interfaces.h @@ -198,7 +198,7 @@ struct ProviderHost { virtual Status CudaCall_false(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) = 0; virtual void CudaCall_true(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) = 0; -#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_INTERFACE) +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) virtual std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) = 0; virtual std::unique_ptr CreateMIGraphXPinnedAllocator(int16_t device_id, const char* name) = 0; #endif diff --git a/onnxruntime/core/session/provider_bridge_ort.cc b/onnxruntime/core/session/provider_bridge_ort.cc index 39be601dadac3..c80ac14ce0943 100644 --- a/onnxruntime/core/session/provider_bridge_ort.cc +++ b/onnxruntime/core/session/provider_bridge_ort.cc @@ -292,7 +292,7 @@ struct ProviderHostImpl : ProviderHost { Status CudaCall_false(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) override { return GetProviderInfo_CUDA().CudaCall_false(retCode, exprString, libName, successCode, msg, file, line); } void CudaCall_true(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) override { GetProviderInfo_CUDA().CudaCall_true(retCode, exprString, libName, successCode, msg, file, line); } -#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_INTERFACE) +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) override { return GetProviderInfo_MIGraphX().CreateMIGraphXAllocator(device_id, name); } std::unique_ptr CreateMIGraphXPinnedAllocator(int16_t device_id, const char* name) override { return GetProviderInfo_MIGraphX().CreateMIGraphXPinnedAllocator(device_id, name); } #endif diff --git a/onnxruntime/python/onnxruntime_pybind_state.cc b/onnxruntime/python/onnxruntime_pybind_state.cc index bdc4f65e590d9..ec4d8c6330c8d 100644 --- a/onnxruntime/python/onnxruntime_pybind_state.cc +++ b/onnxruntime/python/onnxruntime_pybind_state.cc @@ -952,7 +952,7 @@ static std::shared_ptr CreateExecutionProviderFactory << "TensorRT-ExecutionProvider.html#requirements to ensure all dependencies are met."; #endif } else if (type == kMIGraphXExecutionProvider) { -#ifdef USE_MIGRAPHX +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) std::string calibration_table; std::string save_model_path; std::string load_model_path; diff --git a/onnxruntime/python/onnxruntime_pybind_state_common.h b/onnxruntime/python/onnxruntime_pybind_state_common.h index b3251abbc427e..706c151936192 100644 --- a/onnxruntime/python/onnxruntime_pybind_state_common.h +++ b/onnxruntime/python/onnxruntime_pybind_state_common.h @@ -46,7 +46,7 @@ struct OrtStatus { #define BACKEND_DNNL "" #endif -#if USE_MIGRAPHX +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) #define BACKEND_MIGRAPHX "-MIGRAPHX" #else #define BACKEND_MIGRAPHX "" @@ -132,7 +132,7 @@ struct OrtStatus { #if defined(USE_NV) || defined(USE_NV_PROVIDER_INTERFACE) #include "core/providers/nv_tensorrt_rtx/nv_provider_factory.h" #endif -#ifdef USE_MIGRAPHX +#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) #include "core/providers/migraphx/migraphx_provider_factory.h" #include "core/providers/migraphx/migraphx_execution_provider_info.h" #endif From e653e45d0e5be3ea397d12a3632ed3087b37b30d Mon Sep 17 00:00:00 2001 From: Won Chung Date: Tue, 22 Jul 2025 14:22:34 -0700 Subject: [PATCH 08/15] Apply latest EP Factory interface change and add new test --- .../migraphx/migraphx_provider_factory.cc | 13 ++++++---- .../providers/migraphx/migraphx_basic_test.cc | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index d94adcf4870d5..013f16bee78fc 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -207,8 +207,9 @@ struct MIGraphX_Provider : Provider { struct MigraphXEpFactory : OrtEpFactory { MigraphXEpFactory(const OrtApi& ort_api_in, const char* ep_name, - OrtHardwareDeviceType hw_type) - : ort_api{ort_api_in}, ep_name{ep_name}, ort_hw_device_type{hw_type} { + OrtHardwareDeviceType hw_type, + const OrtLogger& default_logger_in) + : ort_api{ort_api_in}, ep_name{ep_name}, ort_hw_device_type{hw_type}, default_logger{default_logger_in} { GetName = GetNameImpl; GetVendor = GetVendorImpl; GetSupportedDevices = GetSupportedDevicesImpl; @@ -273,11 +274,11 @@ struct MigraphXEpFactory : OrtEpFactory { } const OrtApi& ort_api; + const OrtLogger& default_logger; const std::string ep_name; const std::string vendor{"AMD"}; - // AMD vendor ID. Refer to the ACPI ID registry (search AMD): https://uefi.org/ACPI_ID_List - const uint32_t vendor_id{0x1022}; //TODO: set correct value for AMD GPU + const uint32_t vendor_id{0x1002}; const OrtHardwareDeviceType ort_hw_device_type; // Supported OrtHardwareDevice }; @@ -286,13 +287,15 @@ extern "C" { // Public symbols // OrtStatus* CreateEpFactories(const char* /*registration_name*/, const OrtApiBase* ort_api_base, + const OrtLogger* default_logger, OrtEpFactory** factories, size_t max_factories, size_t* num_factories) { const OrtApi* ort_api = ort_api_base->GetApi(ORT_API_VERSION); // Factory could use registration_name or define its own EP name. auto factory_gpu = std::make_unique(*ort_api, onnxruntime::kMIGraphXExecutionProvider, - OrtHardwareDeviceType_GPU); + OrtHardwareDeviceType_GPU, + *default_logger); if (max_factories < 1) { return ort_api->CreateStatus(ORT_INVALID_ARGUMENT, diff --git a/onnxruntime/test/providers/migraphx/migraphx_basic_test.cc b/onnxruntime/test/providers/migraphx/migraphx_basic_test.cc index 72d6f5f6e09dd..5de7885a9452a 100644 --- a/onnxruntime/test/providers/migraphx/migraphx_basic_test.cc +++ b/onnxruntime/test/providers/migraphx/migraphx_basic_test.cc @@ -188,5 +188,30 @@ TEST(MIGraphXExecutionProviderTest, canEvalArgument) { ASSERT_EQ(canEvalNodeArgument(gv, node2, {1}, input_nodes), true); } +TEST(MIGraphXExecutionProviderTest, AutoEp_PreferGpu) { + PathString model_name = ORT_TSTR("migraphx_basic_test.onnx"); + + onnxruntime::Model model("test", false, DefaultLoggingManager().DefaultLogger()); + std::vector dims = {1, 3, 2}; + CreateBaseModel(model, dims); + + auto status = onnxruntime::Model::Save(model, model_name); + ASSERT_TRUE(status.IsOK()); + + auto env = Ort::Env(); + env.UpdateEnvWithCustomLogLevel(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING); + + { + env.RegisterExecutionProviderLibrary(kMIGraphXExecutionProvider, ORT_TSTR("onnxruntime_providers_migraphx.dll")); + + Ort::SessionOptions so; + so.SetEpSelectionPolicy(OrtExecutionProviderDevicePolicy_PREFER_GPU); + Ort::Session session_object(env, model_name.c_str(), so); + EXPECT_TRUE(SessionHasEp(session_object, kMIGraphXExecutionProvider)); + } + + env.UnregisterExecutionProviderLibrary(kMIGraphXExecutionProvider); +} + } // namespace test } // namespace onnxruntime From 29560e015c3856f6f9a919a064f27890dd8d0c4e Mon Sep 17 00:00:00 2001 From: Won Chung Date: Thu, 3 Jul 2025 09:48:20 -0700 Subject: [PATCH 09/15] [MIGRAPHX] Remove #ifdef MIGRAPHX by default for generic interface Remove #ifdef MIGRAPHX so that the ort bin built for generic interface and ep bin built without use_migraphx are still compatible. Currently, ort bin built with use_generic has different struct from ep bin like VitisAI which does not utilize use_generic nor use_migraphx, ending up in a crash. --- .../core/providers/shared_library/provider_bridge_provider.cc | 2 -- onnxruntime/core/providers/shared_library/provider_interfaces.h | 2 -- onnxruntime/core/session/provider_bridge_ort.cc | 2 -- 3 files changed, 6 deletions(-) diff --git a/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc b/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc index a5206d3769c3d..031a4df59d83f 100644 --- a/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc +++ b/onnxruntime/core/providers/shared_library/provider_bridge_provider.cc @@ -361,7 +361,6 @@ std::unique_ptr CreateGPUDataTransfer() { } #endif -#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) { return g_host->CreateMIGraphXAllocator(device_id, name); } @@ -369,7 +368,6 @@ std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const cha std::unique_ptr CreateMIGraphXPinnedAllocator(int16_t device_id, const char* name) { return g_host->CreateMIGraphXPinnedAllocator(device_id, name); } -#endif std::string GetEnvironmentVar(const std::string& var_name) { return g_host->GetEnvironmentVar(var_name); diff --git a/onnxruntime/core/providers/shared_library/provider_interfaces.h b/onnxruntime/core/providers/shared_library/provider_interfaces.h index 4cfdf72fd208f..5c9c1a0ae163f 100644 --- a/onnxruntime/core/providers/shared_library/provider_interfaces.h +++ b/onnxruntime/core/providers/shared_library/provider_interfaces.h @@ -198,10 +198,8 @@ struct ProviderHost { virtual Status CudaCall_false(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) = 0; virtual void CudaCall_true(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) = 0; -#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) virtual std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) = 0; virtual std::unique_ptr CreateMIGraphXPinnedAllocator(int16_t device_id, const char* name) = 0; -#endif #ifdef USE_ROCM virtual std::unique_ptr CreateROCMAllocator(int16_t device_id, const char* name) = 0; diff --git a/onnxruntime/core/session/provider_bridge_ort.cc b/onnxruntime/core/session/provider_bridge_ort.cc index c80ac14ce0943..26b5c373f345c 100644 --- a/onnxruntime/core/session/provider_bridge_ort.cc +++ b/onnxruntime/core/session/provider_bridge_ort.cc @@ -292,10 +292,8 @@ struct ProviderHostImpl : ProviderHost { Status CudaCall_false(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) override { return GetProviderInfo_CUDA().CudaCall_false(retCode, exprString, libName, successCode, msg, file, line); } void CudaCall_true(int retCode, const char* exprString, const char* libName, int successCode, const char* msg, const char* file, const int line) override { GetProviderInfo_CUDA().CudaCall_true(retCode, exprString, libName, successCode, msg, file, line); } -#if defined(USE_MIGRAPHX) || defined(USE_MIGRAPHX_PROVIDER_INTERFACE) std::unique_ptr CreateMIGraphXAllocator(int16_t device_id, const char* name) override { return GetProviderInfo_MIGraphX().CreateMIGraphXAllocator(device_id, name); } std::unique_ptr CreateMIGraphXPinnedAllocator(int16_t device_id, const char* name) override { return GetProviderInfo_MIGraphX().CreateMIGraphXPinnedAllocator(device_id, name); } -#endif std::unique_ptr CreateGPUDataTransfer() override { return GetProviderInfo_CUDA().CreateGPUDataTransfer(); } From 02cd7b37b4ed6954a0aa1a3cf2389bc63efc2497 Mon Sep 17 00:00:00 2001 From: Won Chung Date: Tue, 22 Jul 2025 15:19:01 -0700 Subject: [PATCH 10/15] nit: remove comments --- .../migraphx/migraphx_provider_factory.cc | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 013f16bee78fc..0040e4639b965 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -76,30 +76,6 @@ struct MIGraphX_Provider : Provider { return std::make_shared(info); } - /* - //TODO: Interface change might require changes in other parts of win-onnxruntime? - std::shared_ptr CreateExecutionProviderFactory(const void* param) { - if (param == nullptr) { - LOGS_DEFAULT(ERROR) << "[NvTensorRTRTX EP] Passed NULL options to CreateExecutionProviderFactory()"; - return nullptr; - } - - // std::array pointers_array = *reinterpret_cast*>(param); - const ProviderOptions* provider_options = reinterpret_cast(param); - // const ConfigOptions* config_options = reinterpret_cast(pointers_array[1]); - - if (provider_options == nullptr) { - LOGS_DEFAULT(ERROR) << "[NvTensorRTRTX EP] Passed NULL ProviderOptions to CreateExecutionProviderFactory()"; - return nullptr; - } - - MIGraphXExecutionProviderInfo info = {}; - - UpdateProviderOptions(&info, *provider_options); - return std::make_shared(info); - } - */ - std::shared_ptr CreateExecutionProviderFactory(const void* provider_options) override { auto& options = *reinterpret_cast(provider_options); MIGraphXExecutionProviderInfo info; @@ -175,9 +151,6 @@ struct MIGraphX_Provider : Provider { const OrtSessionOptions& session_options, const OrtLogger& logger, std::unique_ptr& ep) override { - //if (num_devices != 1) { - // return Status(common::ONNXRUNTIME, ORT_EP_FAIL, "[MigraphX/AMDGPU EP] only supports one device."); - //} const ConfigOptions* config_options = &session_options.GetConfigOptions(); From a0350e7508e08e4997f80062ab4ac2f40e98cd8e Mon Sep 17 00:00:00 2001 From: wonchung-microsoft Date: Tue, 22 Jul 2025 21:16:44 -0700 Subject: [PATCH 11/15] lintrunner Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 0040e4639b965..b016fdb43c95a 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -151,7 +151,6 @@ struct MIGraphX_Provider : Provider { const OrtSessionOptions& session_options, const OrtLogger& logger, std::unique_ptr& ep) override { - const ConfigOptions* config_options = &session_options.GetConfigOptions(); std::array configs_array = {&provider_options, config_options}; From 1fdf96dbad5dea1f6b5368a3d5ac9e0cc0c7a50e Mon Sep 17 00:00:00 2001 From: wonchung-microsoft Date: Tue, 22 Jul 2025 21:16:55 -0700 Subject: [PATCH 12/15] lintrunner Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../core/providers/migraphx/migraphx_provider_factory.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index b016fdb43c95a..7cc1881a649ab 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -178,9 +178,9 @@ struct MIGraphX_Provider : Provider { // OrtEpApi infrastructure to be able to use the MigraphX/AMDGPU EP as an OrtEpFactory for auto EP selection. struct MigraphXEpFactory : OrtEpFactory { MigraphXEpFactory(const OrtApi& ort_api_in, - const char* ep_name, - OrtHardwareDeviceType hw_type, - const OrtLogger& default_logger_in) + const char* ep_name, + OrtHardwareDeviceType hw_type, + const OrtLogger& default_logger_in) : ort_api{ort_api_in}, ep_name{ep_name}, ort_hw_device_type{hw_type}, default_logger{default_logger_in} { GetName = GetNameImpl; GetVendor = GetVendorImpl; From 19ea584f5e88729a235d2533aebd765f0bf027c9 Mon Sep 17 00:00:00 2001 From: wonchung-microsoft Date: Tue, 22 Jul 2025 21:17:05 -0700 Subject: [PATCH 13/15] lintrunner Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../core/providers/migraphx/migraphx_provider_factory.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 7cc1881a649ab..4a5710ef09395 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -218,8 +218,8 @@ struct MigraphXEpFactory : OrtEpFactory { for (size_t i = 0; i < num_devices && num_ep_devices < max_ep_devices; ++i) { const OrtHardwareDevice& device = *devices[i]; - if (factory->ort_api.HardwareDevice_Type(&device) == factory->ort_hw_device_type){ - //factory->ort_api.HardwareDevice_VendorId(&device) == factory->vendor_id) { + if (factory->ort_api.HardwareDevice_Type(&device) == factory->ort_hw_device_type) { + // factory->ort_api.HardwareDevice_VendorId(&device) == factory->vendor_id) { OrtKeyValuePairs* ep_options = nullptr; factory->ort_api.CreateKeyValuePairs(&ep_options); ORT_API_RETURN_IF_ERROR( From c1efbbad76d8b33250855f32671586f5d1461c78 Mon Sep 17 00:00:00 2001 From: wonchung-microsoft Date: Tue, 22 Jul 2025 21:17:14 -0700 Subject: [PATCH 14/15] lintrunner Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 4a5710ef09395..63e3c31de6772 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -288,5 +288,4 @@ OrtStatus* ReleaseEpFactory(OrtEpFactory* factory) { ORT_API(onnxruntime::Provider*, GetProvider) { return &onnxruntime::g_provider; } - } From bcf5cb586a52bac347ad14d6a5986989f61a0a6c Mon Sep 17 00:00:00 2001 From: wonchung-microsoft Date: Tue, 22 Jul 2025 21:19:15 -0700 Subject: [PATCH 15/15] lintrunner Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../core/providers/migraphx/migraphx_provider_factory.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc index 63e3c31de6772..84c7fe3e4d4ab 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc @@ -265,9 +265,9 @@ OrtStatus* CreateEpFactories(const char* /*registration_name*/, const OrtApiBase // Factory could use registration_name or define its own EP name. auto factory_gpu = std::make_unique(*ort_api, - onnxruntime::kMIGraphXExecutionProvider, - OrtHardwareDeviceType_GPU, - *default_logger); + onnxruntime::kMIGraphXExecutionProvider, + OrtHardwareDeviceType_GPU, + *default_logger); if (max_factories < 1) { return ort_api->CreateStatus(ORT_INVALID_ARGUMENT,