Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@
return std::make_shared<MIGraphXProviderFactory>(info);
}

/*
//TODO: Interface change might require changes in other parts of win-onnxruntime?
Comment thread
nieubank marked this conversation as resolved.
Outdated
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory(const void* param) {
if (param == nullptr) {
LOGS_DEFAULT(ERROR) << "[NvTensorRTRTX EP] Passed NULL options to CreateExecutionProviderFactory()";
return nullptr;
}

// std::array<const void*, 2> pointers_array = *reinterpret_cast<const std::array<const void*, 2>*>(param);
const ProviderOptions* provider_options = reinterpret_cast<const ProviderOptions*>(param);
// const ConfigOptions* config_options = reinterpret_cast<const ConfigOptions*>(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<MIGraphXProviderFactory>(info);
}
*/

std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory(const void* provider_options) override {
auto& options = *reinterpret_cast<const OrtMIGraphXProviderOptions*>(provider_options);
MIGraphXExecutionProviderInfo info;
Expand Down Expand Up @@ -144,6 +168,27 @@
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<IExecutionProvider>& ep) override {
//if (num_devices != 1) {
Comment thread
nieubank marked this conversation as resolved.
Outdated
// return Status(common::ONNXRUNTIME, ORT_EP_FAIL, "[MigraphX/AMDGPU EP] only supports one device.");
//}

const ConfigOptions* config_options = &session_options.GetConfigOptions();
Comment thread
wonchung-microsoft marked this conversation as resolved.

std::array<const void*, 2> configs_array = {&provider_options, config_options};
const void* arg = reinterpret_cast<const void*>(&configs_array);
auto ep_factory = CreateExecutionProviderFactory(&provider_options);
ep = ep_factory->CreateProvider(session_options, logger);

return Status::OK();
}

void Initialize() override {
InitializeRegistry();
}
Expand All @@ -156,9 +201,117 @@

} // 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;
Comment thread
wonchung-microsoft marked this conversation as resolved.
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<const MigraphXEpFactory*>(this_ptr);
return factory->ep_name.c_str();
}

static const char* GetVendorImpl(const OrtEpFactory* this_ptr) {
const auto* factory = static_cast<const MigraphXEpFactory*>(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<MigraphXEpFactory*>(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;
Comment thread
wonchung-microsoft marked this conversation as resolved.
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"};

Check warning on line 277 in onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <string> for string [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc:277: Add #include <string> for string [build/include_what_you_use] [4]

// AMD vendor ID. Refer to the ACPI ID registry (search AMD): https://uefi.org/ACPI_ID_List
Comment thread
nieubank marked this conversation as resolved.
Outdated
const uint32_t vendor_id{0x1022}; //TODO: set correct value for AMD GPU

Check warning on line 280 in onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Missing username in TODO; it should look like "// TODO(my_username): Stuff." [readability/todo] [2] Raw Output: onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc:280: Missing username in TODO; it should look like "// TODO(my_username): Stuff." [readability/todo] [2]
Comment thread
nieubank marked this conversation as resolved.
Outdated
const OrtHardwareDeviceType ort_hw_device_type; // Supported OrtHardwareDevice
};

extern "C" {
//
// Public symbols
//
OrtStatus* CreateEpFactories(const char* /*registration_name*/, const OrtApiBase* ort_api_base,
Comment thread
nieubank marked this conversation as resolved.
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<MigraphXEpFactory>(*ort_api,

Check warning on line 293 in onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <memory> for make_unique<> [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/migraphx/migraphx_provider_factory.cc:293: Add #include <memory> for make_unique<> [build/include_what_you_use] [4]
onnxruntime::kMIGraphXExecutionProvider,
OrtHardwareDeviceType_GPU);

Comment thread
wonchung-microsoft marked this conversation as resolved.
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<MigraphXEpFactory*>(factory);
return nullptr;
}

ORT_API(onnxruntime::Provider*, GetProvider) {
return &onnxruntime::g_provider;
}

}
Comment thread
wonchung-microsoft marked this conversation as resolved.
2 changes: 2 additions & 0 deletions onnxruntime/core/providers/migraphx/symbols.def
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
EXPORTS
GetProvider
CreateEpFactories
ReleaseEpFactory
Loading