Skip to content

Commit 0a93edb

Browse files
authored
Cuda Graph on by default (microsoft#26929)
### Description Support for Cuda Graph on by default ### Motivation and Context We wanted to have Cuda Graph support on by default for NV TRT-RTX EP. Added the use of RTX graph capture and removed the access external checks for the same
1 parent 65e694e commit 0a93edb

2 files changed

Lines changed: 13 additions & 43 deletions

File tree

onnxruntime/core/providers/nv_tensorrt_rtx/nv_execution_provider.cc

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,6 +2866,14 @@ Status NvExecutionProvider::CreateNodeComputeInfoFromGraph(const GraphViewer& gr
28662866
trt_runtime_config = std::unique_ptr<nvinfer1::IRuntimeConfig>(trt_engine->createRuntimeConfig());
28672867
if (trt_runtime_config && cuda_graph_enable_) {
28682868
trt_runtime_config->setDynamicShapesKernelSpecializationStrategy(nvinfer1::DynamicShapesKernelSpecializationStrategy::kEAGER);
2869+
#if TRT_MAJOR_RTX > 1 || (TRT_MAJOR_RTX == 1 && TRT_MINOR_RTX >= 3)
2870+
auto cuda_strategy_flag = trt_runtime_config->setCudaGraphStrategy(nvinfer1::CudaGraphStrategy::kWHOLE_GRAPH_CAPTURE);
2871+
LOGS_DEFAULT(INFO) << "[NvTensorRTRTX EP] CUDA graph strategy with RTX Graph capture enabled : " << cuda_strategy_flag;
2872+
#else
2873+
LOGS_DEFAULT(WARNING) << "[NvTensorRTRTX EP] CUDA graph is enabled but RTX Graph capture is not available. "
2874+
<< "The current TRT RTX version does not support RTX Graph. "
2875+
<< "Please upgrade to TRT RTX >= 1.3 to use RTX Graph capture feature for optimal CUDA graph performance.";
2876+
#endif
28692877
}
28702878
trt_runtime_config->setExecutionContextAllocationStrategy(nvinfer1::ExecutionContextAllocationStrategy::kUSER_MANAGED);
28712879
if (!runtime_cache_.empty()) {
@@ -3148,22 +3156,8 @@ Status NvExecutionProvider::CreateNodeComputeInfoFromGraph(const GraphViewer& gr
31483156
}
31493157
}
31503158

3151-
// Start CUDA graph capture with the correct stream
3152-
// Note: We need to set the stream and start capture here because this is where we have access to the actual compute stream
3153-
// Get the graph annotation ID that was stored during OnRunStart
3154-
CudaGraphAnnotation_t cuda_graph_annotation_id = GetPerThreadContext().GetCurrentGraphAnnotationId();
3155-
bool graph_replay_on_this_run = false;
3156-
bool should_start_capture = false;
3157-
3158-
HandleCudaGraphStart(stream, require_io_binding, cuda_graph_annotation_id,
3159-
graph_replay_on_this_run, should_start_capture);
3160-
3161-
if (!graph_replay_on_this_run) {
3162-
if (!trt_context->enqueueV3(stream)) {
3163-
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "NvTensorRTRTX EP execution context enqueue failed.");
3164-
}
3165-
} else {
3166-
ORT_RETURN_IF_ERROR(GetPerThreadContext().ReplayGraph(cuda_graph_annotation_id, sync_stream_after_enqueue_));
3159+
if (!trt_context->enqueueV3(stream)) {
3160+
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "NvTensorRTRTX EP execution context enqueue failed.");
31673161
}
31683162

31693163
/*
@@ -3181,11 +3175,6 @@ Status NvExecutionProvider::CreateNodeComputeInfoFromGraph(const GraphViewer& gr
31813175
* However, if cuda graph is enabled, TRT EP won't call cudaStreamSynchronize() since it's not allowed during graph capture.
31823176
*/
31833177

3184-
if (cuda_graph_enable_ && should_start_capture) {
3185-
GetPerThreadContext().CaptureEnd(cuda_graph_annotation_id);
3186-
ORT_RETURN_IF_ERROR(GetPerThreadContext().ReplayGraph(cuda_graph_annotation_id, sync_stream_after_enqueue_));
3187-
}
3188-
31893178
if (sync_stream_after_enqueue_) {
31903179
CUDA_RETURN_IF_ERROR(cudaStreamSynchronize(stream));
31913180
}
@@ -3474,22 +3463,8 @@ Status NvExecutionProvider::CreateNodeComputeInfoFromPrecompiledEngine(const Gra
34743463
trt_context->setAuxStreams(aux_streams_, (int32_t)auxiliary_streams_);
34753464
}
34763465

3477-
// Start CUDA graph capture with the correct stream
3478-
// Note: We need to set the stream and start capture here because this is where we have access to the actual compute stream
3479-
// Get the graph annotation ID that was stored during OnRunStart
3480-
CudaGraphAnnotation_t cuda_graph_annotation_id = GetPerThreadContext().GetCurrentGraphAnnotationId();
3481-
bool graph_replay_on_this_run = false;
3482-
bool should_start_capture = false;
3483-
3484-
HandleCudaGraphStart(stream, require_io_binding, cuda_graph_annotation_id,
3485-
graph_replay_on_this_run, should_start_capture);
3486-
3487-
if (!graph_replay_on_this_run) {
3488-
if (!trt_context->enqueueV3(stream)) {
3489-
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "NvTensorRTRTX EP execution context enqueue failed.");
3490-
}
3491-
} else {
3492-
ORT_RETURN_IF_ERROR(GetPerThreadContext().ReplayGraph(cuda_graph_annotation_id, sync_stream_after_enqueue_));
3466+
if (!trt_context->enqueueV3(stream)) {
3467+
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "NvTensorRTRTX EP execution context enqueue failed.");
34933468
}
34943469

34953470
/*
@@ -3507,11 +3482,6 @@ Status NvExecutionProvider::CreateNodeComputeInfoFromPrecompiledEngine(const Gra
35073482
* However, if cuda graph is enabled, TRT EP won't call cudaStreamSynchronize() since it's not allowed during graph capture.
35083483
*/
35093484

3510-
if (cuda_graph_enable_ && should_start_capture) {
3511-
GetPerThreadContext().CaptureEnd(cuda_graph_annotation_id);
3512-
ORT_RETURN_IF_ERROR(GetPerThreadContext().ReplayGraph(cuda_graph_annotation_id, sync_stream_after_enqueue_));
3513-
}
3514-
35153485
if (sync_stream_after_enqueue_) {
35163486
CUDA_RETURN_IF_ERROR(cudaStreamSynchronize(stream));
35173487
}

onnxruntime/core/providers/nv_tensorrt_rtx/nv_execution_provider_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct NvExecutionProviderInfo {
4646
std::string profile_min_shapes{""};
4747
std::string profile_max_shapes{""};
4848
std::string profile_opt_shapes{""};
49-
bool cuda_graph_enable{false};
49+
bool cuda_graph_enable{true};
5050
bool multi_profile_enable{false};
5151
bool dump_ep_context_model{false};
5252
std::string ep_context_file_path{""};

0 commit comments

Comments
 (0)