diff --git a/onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc b/onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc index 082d988a92c88..25c80fb38f23e 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc @@ -152,6 +152,7 @@ static std::mutex g_hip_alloc_mutex; MIGraphXExecutionProvider::MIGraphXExecutionProvider(const MIGraphXExecutionProviderInfo& info) : IExecutionProvider{kMIGraphXExecutionProvider, OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, OrtDevice::VendorIds::AMD, info.device_id)}, device_id_{info.device_id}, + target_device_{info.target_device.empty() ? "gpu" : info.target_device}, fp16_enable_{info.fp16_enable}, #if HIP_VERSION_MAJOR > 6 || (HIP_VERSION_MAJOR == 6 && (HIP_VERSION_MINOR > 4 || (HIP_VERSION_MINOR == 4 && HIP_VERSION_PATCH >= 2))) bf16_enable_{info.bf16_enable}, @@ -161,7 +162,7 @@ MIGraphXExecutionProvider::MIGraphXExecutionProvider(const MIGraphXExecutionProv #endif int8_enable_{info.int8_enable}, model_cache_path_{info.model_cache_dir}, - t_{info.target_device.c_str()}, + t_{target_device_.c_str()}, exhaustive_tune_{info.exhaustive_tune}, metadef_id_generator_{ModelMetadefIdGenerator::Create()}, external_alloc_{info.external_alloc}, @@ -189,6 +190,25 @@ MIGraphXExecutionProvider::MIGraphXExecutionProvider(const MIGraphXExecutionProv // Overwrite initialized values with values from environment variables. LOGS_DEFAULT(INFO) << "[MIGraphX EP] MIGraphX ENV Override Variables Set:"; + + // Compile target override (gpu/ref/cpu). Reconstruct the MIGraphX target if a + // valid value is provided; an invalid value keeps the existing target. + { + const auto compile_target_env = GetEnvironmentVar(std::string{migraphx_env_vars::kCompileTarget}); + if (!compile_target_env.empty()) { + std::string normalized_target; + if (ValidateMIGraphXCompileTarget(compile_target_env, normalized_target).IsOK()) { + target_device_ = normalized_target; + t_ = migraphx::target{target_device_.c_str()}; + LOGS_DEFAULT(INFO) << "\n " << migraphx_env_vars::kCompileTarget << ": " << target_device_; + } else { + LOGS_DEFAULT(WARNING) + << "[MIGraphX EP] Ignoring invalid " << migraphx_env_vars::kCompileTarget + << "='" << compile_target_env << "'. Supported targets are 'gpu', 'ref', 'cpu', and 'mps'."; + } + } + } + GET_ENV_BOOL(migraphx_env_vars::kFP16Enable, fp16_enable_); #if HIP_VERSION_MAJOR > 6 || (HIP_VERSION_MAJOR == 6 && (HIP_VERSION_MINOR > 4 || (HIP_VERSION_MINOR == 4 && HIP_VERSION_PATCH >= 2))) GET_ENV_BOOL(migraphx_env_vars::kBF16Enable, bf16_enable_); @@ -200,7 +220,12 @@ MIGraphXExecutionProvider::MIGraphXExecutionProvider(const MIGraphXExecutionProv GET_ENV(migraphx_env_vars::kINT8CalibrationTableName, int8_calibration_cache_name_); GET_ENV(migraphx_env_vars::kINT8UseNativeMIGraphXCalibrationTable, int8_use_native_migraphx_calibration_table_); GET_ENV_STRING(migraphx_env_vars::kCachePath, calibration_cache_path_); - GET_ENV_STRING(migraphx_env_vars::kModelCachePath, model_cache_path_); + + // Only consult the env var when the provider option didn't supply a path, + // so an explicit migraphx_model_cache_dir is never silently overridden. + if (model_cache_path_.empty()) { + GET_ENV_STRING(migraphx_env_vars::kModelCachePath, model_cache_path_); + } // Strip surrounding quotes from cache path. { @@ -309,6 +334,7 @@ MIGraphXExecutionProvider::MIGraphXExecutionProvider(const MIGraphXExecutionProv LOGS_DEFAULT(VERBOSE) << "[MIGraphX EP] MIGraphX provider Session Options:" << "\n " << migraphx_provider_option::kDeviceId << ": " << device_id_ + << "\n " << migraphx_provider_option::kCompileTarget << ": " << target_device_ << "\n " << migraphx_provider_option::kFp16Enable << ": " << fp16_enable_ << "\n " << migraphx_provider_option::kBf16Enable << ": " << bf16_enable_ << "\n " << migraphx_provider_option::kFp8Enable << ": " << fp8_enable_ diff --git a/onnxruntime/core/providers/migraphx/migraphx_execution_provider.h b/onnxruntime/core/providers/migraphx/migraphx_execution_provider.h index 6ce431c127c8c..5cf0c8ee729a1 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_execution_provider.h +++ b/onnxruntime/core/providers/migraphx/migraphx_execution_provider.h @@ -25,6 +25,7 @@ using namespace std::literals::string_view_literals; namespace onnxruntime { namespace migraphx_env_vars { +constexpr auto kCompileTarget = "ORT_MIGRAPHX_COMPILE_TARGET"sv; constexpr auto kFP16Enable = "ORT_MIGRAPHX_FP16_ENABLE"sv; constexpr auto kBF16Enable = "ORT_MIGRAPHX_BF16_ENABLE"sv; constexpr auto kFP8Enable = "ORT_MIGRAPHX_FP8_ENABLE"sv; @@ -274,6 +275,7 @@ class MIGraphXExecutionProvider : public IExecutionProvider { ProviderOptions GetProviderOptions() const override { return { {std::string{migraphx_provider_option::kDeviceId}, MakeStringWithClassicLocale(device_id_)}, + {std::string{migraphx_provider_option::kCompileTarget}, target_device_}, {std::string{migraphx_provider_option::kFp16Enable}, MakeStringWithClassicLocale(fp16_enable_)}, {std::string{migraphx_provider_option::kBf16Enable}, MakeStringWithClassicLocale(bf16_enable_)}, {std::string{migraphx_provider_option::kFp8Enable}, MakeStringWithClassicLocale(fp8_enable_)}, @@ -296,6 +298,8 @@ class MIGraphXExecutionProvider : public IExecutionProvider { private: OrtDevice::DeviceId device_id_{0}; + // MIGraphX compile target: "gpu" (default), "ref", "cpu", or "mps". + std::string target_device_{"gpu"}; bool fp16_enable_ = false; bool bf16_enable_ = false; bool fp8_enable_ = false; diff --git a/onnxruntime/core/providers/migraphx/migraphx_execution_provider_info.cc b/onnxruntime/core/providers/migraphx/migraphx_execution_provider_info.cc index 33876c173c41b..9e0a654b14d73 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_execution_provider_info.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_execution_provider_info.cc @@ -1,7 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +#include #include +#include #include "core/providers/shared_library/provider_api.h" #include "core/providers/migraphx/migraphx_execution_provider_info.h" @@ -18,9 +20,30 @@ const EnumNameMapping arena_extend_strategy_mapping{ {ArenaExtendStrategy::kSameAsRequested, "kSameAsRequested"}, }; +// Validates and normalizes a MIGraphX compile target name. Accepts the three +// MIGraphX targets ("gpu", "ref", "cpu") case-insensitively. +Status ValidateMIGraphXCompileTarget(const std::string& value_str, std::string& target_device) { + std::string normalized; + normalized.reserve(value_str.size()); + for (char ch : value_str) { + normalized.push_back(static_cast(std::tolower(static_cast(ch)))); + } + ORT_RETURN_IF_NOT( + normalized == "gpu" || normalized == "ref" || normalized == "cpu" || normalized == "mps", + "Invalid MIGraphX compile target: '", value_str, + "'. Supported targets are 'gpu', 'ref', 'cpu', and 'mps'."); + target_device = std::move(normalized); + return Status::OK(); +} + MIGraphXExecutionProviderInfo::MIGraphXExecutionProviderInfo(const ProviderOptions& options) { ORT_THROW_IF_ERROR( ProviderOptionsParser{} + .AddValueParser( + migraphx_provider_option::kCompileTarget, + [this](const std::string& value_str) -> Status { + return ValidateMIGraphXCompileTarget(value_str, target_device); + }) .AddValueParser( migraphx_provider_option::kDeviceId, [this](const std::string& value_str) -> Status { @@ -95,6 +118,13 @@ MIGraphXExecutionProviderInfo::MIGraphXExecutionProviderInfo(const OrtMIGraphXPr bf16_enable{options.migraphx_bf16_enable != 0}, fp8_enable{options.migraphx_fp8_enable != 0}, int8_enable{options.migraphx_int8_enable != 0}, + int8_calibration_table_name{options.migraphx_int8_calibration_table_name != nullptr + ? options.migraphx_int8_calibration_table_name + : ""}, + int8_use_native_calibration_table{options.migraphx_use_native_calibration_table != 0}, + model_cache_dir{options.migraphx_cache_dir != nullptr + ? ToPathString(std::string{options.migraphx_cache_dir}) + : std::filesystem::path{}}, exhaustive_tune{options.migraphx_exhaustive_tune != 0}, mem_limit{options.migraphx_mem_limit}, arena_extend_strategy{options.migraphx_arena_extend_strategy}, @@ -104,6 +134,7 @@ MIGraphXExecutionProviderInfo::MIGraphXExecutionProviderInfo(const OrtMIGraphXPr ProviderOptions MIGraphXExecutionProviderInfo::ToProviderOptions() const { return { {std::string{migraphx_provider_option::kDeviceId}, MakeStringWithClassicLocale(device_id)}, + {std::string{migraphx_provider_option::kCompileTarget}, target_device}, {std::string{migraphx_provider_option::kFp16Enable}, MakeStringWithClassicLocale(fp16_enable)}, {std::string{migraphx_provider_option::kBf16Enable}, MakeStringWithClassicLocale(bf16_enable)}, {std::string{migraphx_provider_option::kFp8Enable}, MakeStringWithClassicLocale(fp8_enable)}, diff --git a/onnxruntime/core/providers/migraphx/migraphx_execution_provider_info.h b/onnxruntime/core/providers/migraphx/migraphx_execution_provider_info.h index 1a7754bd70656..5acbaf29a062c 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_execution_provider_info.h +++ b/onnxruntime/core/providers/migraphx/migraphx_execution_provider_info.h @@ -21,6 +21,7 @@ namespace onnxruntime { namespace migraphx_provider_option { constexpr auto kDeviceId = "device_id"sv; +constexpr auto kCompileTarget = "migraphx_compile_target"sv; constexpr auto kFp16Enable = "migraphx_fp16_enable"sv; constexpr auto kBf16Enable = "migraphx_bf16_enable"sv; constexpr auto kFp8Enable = "migraphx_fp8_enable"sv; @@ -43,6 +44,10 @@ constexpr auto kUserComputeStream = "user_compute_stream"sv; extern const EnumNameMapping arena_extend_strategy_mapping; +// Validates and normalizes a MIGraphX compile target name ("gpu", "ref", "cpu", "mps"). +// On success, writes the lower-cased target into target_device. +Status ValidateMIGraphXCompileTarget(const std::string& value_str, std::string& target_device); + // Information needed to construct trt execution providers. struct MIGraphXExecutionProviderInfo { std::string target_device{"gpu"};