Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion Common/ML/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ o2_add_library(ML

# Pass ORT variables as a preprocessor definition
target_compile_definitions(${targetName} PRIVATE
$<$<BOOL:${ORT_ROCM_BUILD}>:ORT_ROCM_BUILD>
$<$<BOOL:${ORT_CUDA_BUILD}>:ORT_CUDA_BUILD>
$<$<BOOL:${ORT_MIGRAPHX_BUILD}>:ORT_MIGRAPHX_BUILD>
$<$<BOOL:${ORT_TENSORRT_BUILD}>:ORT_TENSORRT_BUILD>)
2 changes: 1 addition & 1 deletion Common/ML/include/ML/OrtInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>&);
Expand Down
26 changes: 20 additions & 6 deletions Common/ML/src/OrtInterface.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// ONNX includes
#include <onnxruntime_cxx_api.h>

#include <algorithm>
#include <cctype>
#include <sstream>

namespace o2
Expand Down Expand Up @@ -70,6 +72,15 @@ void OrtModel::initOptions(std::unordered_map<std::string, std::string> 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);
Expand All @@ -83,8 +94,8 @@ void OrtModel::initOptions(std::unordered_map<std::string, std::string> 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();
Expand Down Expand Up @@ -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";
Expand Down
20 changes: 11 additions & 9 deletions GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 0 additions & 1 deletion GPU/GPUTracking/Base/hip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ target_compile_definitions(${targetName} PRIVATE $<TARGET_PROPERTY:O2::GPUTracki

if (onnxruntime_FOUND)
target_compile_definitions(${targetName} PRIVATE
$<$<BOOL:${ORT_ROCM_BUILD}>:ORT_ROCM_BUILD>
$<$<BOOL:${ORT_MIGRAPHX_BUILD}>:ORT_MIGRAPHX_BUILD>)
target_link_libraries(${targetName} PRIVATE onnxruntime::onnxruntime)
endif()
Expand Down
2 changes: 1 addition & 1 deletion GPU/GPUTracking/Definitions/GPU设置List.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ EndConfig()
// 设置 steering the processing of NN Clusterization
BeginSubConfig(GPU设置ProcessingNNclusterizer, 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
Expand Down
5 changes: 3 additions & 2 deletions GPU/GPUTracking/Standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down