From cc88f31daee6c1d7eea6693e31e69adeb43e7267 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Wed, 26 Aug 2026 20:35:28 +0200 Subject: [PATCH] Move to ONNXRuntime 1.29.0 and MIGraphX backend on AMD --- Common/ML/CMakeLists.txt | 1 - Common/ML/include/ML/OrtInterface.h | 2 +- Common/ML/src/OrtInterface.cxx | 26 ++++++++++++++----- .../Base/cuda/GPUReconstructionCUDA.cu | 20 +++++++------- GPU/GPUTracking/Base/hip/CMakeLists.txt | 1 - GPU/GPUTracking/Definitions/GPUSettingsList.h | 2 +- GPU/GPUTracking/Standalone/CMakeLists.txt | 5 ++-- 7 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Common/ML/CMakeLists.txt b/Common/ML/CMakeLists.txt index 0ed52e1a23e20..1d98c99ed00e2 100644 --- a/Common/ML/CMakeLists.txt +++ b/Common/ML/CMakeLists.txt @@ -16,7 +16,6 @@ o2_add_library(ML # Pass ORT variables as a preprocessor definition target_compile_definitions(${targetName} PRIVATE - $<$:ORT_ROCM_BUILD> $<$:ORT_CUDA_BUILD> $<$:ORT_MIGRAPHX_BUILD> $<$:ORT_TENSORRT_BUILD>) diff --git a/Common/ML/include/ML/OrtInterface.h b/Common/ML/include/ML/OrtInterface.h index 987ce8fb4d6dd..d16dd1356e8ce 100644 --- a/Common/ML/include/ML/OrtInterface.h +++ b/Common/ML/include/ML/OrtInterface.h @@ -118,7 +118,7 @@ class OrtModel // Environment settings bool mInitialized = false, mDeterministicMode = false; - std::string mModelPath, mEnvName = "", mDeviceType = "CPU", mThreadAffinity = ""; // device options should be cpu, rocm, migraphx, cuda + std::string mModelPath, mEnvName = "", mDeviceType = "CPU", mThreadAffinity = ""; // device options should be CPU, MIGRAPHX or CUDA (ROCM is a deprecated alias of MIGRAPHX) int32_t mIntraOpNumThreads = 1, mInterOpNumThreads = 1, mDeviceId = -1, mEnableProfiling = 0, mLoggingLevel = 0, mAllocateDeviceMemory = 0, mEnableOptimizations = 0; std::string printShape(const std::vector&); diff --git a/Common/ML/src/OrtInterface.cxx b/Common/ML/src/OrtInterface.cxx index 9eccb9638d882..7d27435d32b84 100644 --- a/Common/ML/src/OrtInterface.cxx +++ b/Common/ML/src/OrtInterface.cxx @@ -19,6 +19,8 @@ // ONNX includes #include +#include +#include #include namespace o2 @@ -70,6 +72,15 @@ void OrtModel::initOptions(std::unordered_map optionsM mEnvName = (optionsMap.contains("onnx-environment-name") ? optionsMap["onnx-environment-name"] : "onnx_model_inference"); mDeterministicMode = (optionsMap.contains("deterministic-compute") ? std::stoi(optionsMap["deterministic-compute"]) : 0); + // Device types are matched case-sensitively below, so accept "cpu"/"cuda" spellings too. + std::transform(mDeviceType.begin(), mDeviceType.end(), mDeviceType.begin(), [](unsigned char c) { return std::toupper(c); }); + + // ONNXRuntime dropped the ROCm execution provider after v1.22; MIGraphX is the AMD path. + if (mDeviceType == "ROCM") { + LOG(warning) << "(ORT) device-type \"ROCM\" is deprecated: ONNXRuntime no longer ships a ROCm execution provider. Using MIGraphX instead."; + mDeviceType = "MIGRAPHX"; + } + if (mDeviceType == "CPU") { (mPImplOrt->sessionOptions).SetIntraOpNumThreads(mIntraOpNumThreads); (mPImplOrt->sessionOptions).SetInterOpNumThreads(mInterOpNumThreads); @@ -83,8 +94,8 @@ void OrtModel::initOptions(std::unordered_map optionsM } } - // OrtROCMProviderOptions rocm_options{}; - // (mPImplOrt->sessionOptions).AppendExecutionProvider_ROCM(rocm_options); + // GPU execution providers are appended by GPUReconstructionCUDA::SetONNXGPUStream(), which + // knows the lane's device; registering the same provider twice is a hard error in ONNXRuntime. (mPImplOrt->sessionOptions).DisableMemPattern(); (mPImplOrt->sessionOptions).DisableCpuMemArena(); @@ -181,13 +192,16 @@ void OrtModel::memoryOnDevice(int32_t deviceIndex) (mPImplOrt->sessionOptions).AddConfigEntry("session.use_device_allocator_for_initializers", "1"); // See kOrtSessionOptionsUseDeviceAllocatorForInitializers, https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h (mPImplOrt->sessionOptions).AddConfigEntry("session.use_env_allocators", "1"); // This should enable to use the volatile memory allocation defined in O2/GPU/GPUTracking/TPCClusterFinder/GPUTPCNNClusterizerHost.cxx; not working yet: ONNX still assigns new memory at init time (mPImplOrt->sessionOptions).AddConfigEntry("session_options.enable_cpu_mem_arena", "0"); // This should enable to use the volatile memory allocation defined in O2/GPU/GPUTracking/TPCClusterFinder/GPUTPCNNClusterizerHost.cxx; not working yet: ONNX still assigns new memory at init time - // Arena memory shrinkage comes at performance cost - // For now prefer to use single allocation, enabled by O2/GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.cu -> SetONNXGPUStream -> rocm_options.arena_extend_strategy = 0; + // Arena memory shrinkage comes at performance cost; prefer the providers' default + // kNextPowerOfTwo arena extend strategy (single growing allocation). (mPImplOrt->runOptions).AddConfigEntry("memory.enable_memory_arena_shrinkage", ("gpu:" + std::to_string(deviceIndex)).c_str()); // See kOrtRunOptionsConfigEnableMemoryArenaShrinkage, https://github.com/microsoft/onnxruntime/blob/90c263f471bbce724e77d8e62831d3a9fa838b2f/include/onnxruntime/core/session/onnxruntime_run_options_config_keys.h#L27 + // Allocator names understood by OrtApi::CreateMemoryInfo: "Cuda" -> (GPU, NVIDIA) and + // "Hip" -> (GPU, AMD), the device the MIGraphX EP registers on. The buffers bound here are + // device pointers, so plain device memory, not the pinned-host "HipPinned"/"CudaPinned". std::string dev_mem_str = ""; - if (mDeviceType == "ROCM") { - dev_mem_str = "HipPinned"; + if (mDeviceType == "MIGRAPHX") { + dev_mem_str = "Hip"; } if (mDeviceType == "CUDA") { dev_mem_str = "Cuda"; diff --git a/GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.cu b/GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.cu index 63992bed65fc5..6ffbefc656a93 100644 --- a/GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.cu +++ b/GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.cu @@ -666,15 +666,17 @@ void GPUReconstructionCUDA::SetONNXGPUStream(Ort::SessionOptions& sessionOptions ORTCHK(api->SessionOptionsAppendExecutionProvider_CUDA_V2(sessionOptions, cudaOptions)); api->ReleaseCUDAProviderOptions(cudaOptions); -#elif defined(ORT_ROCM_BUILD) - // const auto& api = Ort::GetApi(); - // api.GetCurrentGpuDeviceId(deviceId); - OrtROCMProviderOptions rocmOptions; - rocmOptions.has_user_compute_stream = 1; // Indicate that we are passing a user stream - rocmOptions.arena_extend_strategy = 0; // kNextPowerOfTwo = 0, kSameAsRequested = 1 -> https://github.com/search?q=repo%3Amicrosoft%2Fonnxruntime%20kSameAsRequested&type=code - // rocm_options.gpu_mem_limit = 1073741824; // 0 means no limit - rocmOptions.user_compute_stream = mInternals->Streams[stream]; - sessionOptions.AppendExecutionProvider_ROCM(rocmOptions); +#elif defined(ORT_MIGRAPHX_BUILD) + // ONNXRuntime dropped the ROCm execution provider after v1.22; MIGraphX is the AMD path, + // appended through the generic provider interface (the legacy options struct is frozen). + // Unlike CUDA, MIGraphX has no user-compute-stream option (as of v1.29.0 the EP owns its own + // hipStream_t), so inference does not run on the lane's stream. + const OrtApi* api = OrtGetApiBase()->GetApi(ORT_API_VERSION); + const std::string device = std::to_string(*deviceId); + const char* keys[] = {"device_id"}; + const char* values[] = {device.c_str()}; + ORTCHK(api->SessionOptionsAppendExecutionProvider(sessionOptions, "MIGraphX", keys, values, sizeof(keys) / sizeof(keys[0]))); + GPUInfo("ONNXRuntime: MIGraphX execution provider registered on device %s (lane %d)", device.c_str(), stream); #endif } diff --git a/GPU/GPUTracking/Base/hip/CMakeLists.txt b/GPU/GPUTracking/Base/hip/CMakeLists.txt index 50d710fd9d557..7614da6bed97e 100644 --- a/GPU/GPUTracking/Base/hip/CMakeLists.txt +++ b/GPU/GPUTracking/Base/hip/CMakeLists.txt @@ -199,7 +199,6 @@ target_compile_definitions(${targetName} PRIVATE $:ORT_ROCM_BUILD> $<$:ORT_MIGRAPHX_BUILD>) target_link_libraries(${targetName} PRIVATE onnxruntime::onnxruntime) endif() diff --git a/GPU/GPUTracking/Definitions/GPUSettingsList.h b/GPU/GPUTracking/Definitions/GPUSettingsList.h index 842576621c39c..1ba74f1c5659a 100644 --- a/GPU/GPUTracking/Definitions/GPUSettingsList.h +++ b/GPU/GPUTracking/Definitions/GPUSettingsList.h @@ -260,7 +260,7 @@ EndConfig() // Settings steering the processing of NN Clusterization BeginSubConfig(GPUSettingsProcessingNNclusterizer, nn, configStandalone.proc, "NN", 0, "Processing settings for neural network clusterizer", proc_nn) AddOption(applyNNclusterizer, int, 0, "", 0, "(bool, default = 0), if the neural network clusterizer should be used.") -AddOption(nnInferenceDevice, std::string, "CPU", "", 0, "(std::string) Specify inference device (cpu (default), rocm, cuda)") +AddOption(nnInferenceDevice, std::string, "CPU", "", 0, "(std::string) Specify inference device (CPU (default), MIGRAPHX (AMD), CUDA (NVIDIA); ROCM is a deprecated alias for MIGRAPHX)") AddOption(nnInferenceDeviceId, unsigned int, 0, "", 0, "(unsigned int) Specify inference device id") AddOption(nnInferenceAllocateDevMem, int, 0, "", 0, "(bool, default = 0), if the device memory should be allocated for inference") AddOption(nnInferenceInputDType, std::string, "FP32", "", 0, "(std::string) Specify the datatype for which inference is performed (FP32: default, fp16)") // fp32 or fp16 diff --git a/GPU/GPUTracking/Standalone/CMakeLists.txt b/GPU/GPUTracking/Standalone/CMakeLists.txt index a4c5817d1f7f9..27c73c68c20fc 100644 --- a/GPU/GPUTracking/Standalone/CMakeLists.txt +++ b/GPU/GPUTracking/Standalone/CMakeLists.txt @@ -131,8 +131,9 @@ find_package(O2GPU REQUIRED) if(GPUCA_CONFIG_ONNX) find_package(onnxruntime REQUIRED) - if(HIP_ENABLED AND NOT DEFINED ORT_ROCM_BUILD) - set(ORT_ROCM_BUILD ON) + # ONNXRuntime dropped the ROCm execution provider after v1.22; MIGraphX is the AMD path. + if(HIP_ENABLED AND NOT DEFINED ORT_MIGRAPHX_BUILD) + set(ORT_MIGRAPHX_BUILD ON) elseif(CUDA_ENABLED AND NOT DEFINED ORT_CUDA_BUILD) set(ORT_CUDA_BUILD ON) endif()