Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
30 changes: 28 additions & 2 deletions onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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},
Expand Down Expand Up @@ -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_);
Expand All @@ -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.
{
Expand Down Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_)},
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include <cctype>
#include <string>
#include <utility>

#include "core/providers/shared_library/provider_api.h"
#include "core/providers/migraphx/migraphx_execution_provider_info.h"
Expand All @@ -18,9 +20,30 @@ const EnumNameMapping<ArenaExtendStrategy> 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<char>(std::tolower(static_cast<unsigned char>(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 {
Expand Down Expand Up @@ -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},
Expand All @@ -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)},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +44,10 @@ constexpr auto kUserComputeStream = "user_compute_stream"sv;

extern const EnumNameMapping<ArenaExtendStrategy> 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"};
Expand Down
Loading