Skip to content

[TensorRT EP] Call cudaSetDevice at compute function for handling multithreading scenario #24010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 13, 2025
Merged
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
18 changes: 18 additions & 0 deletions onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3538,6 +3538,15 @@ Status TensorrtExecutionProvider::CreateNodeComputeInfoFromGraph(const GraphView

// Create compute function
compute_info.compute_func = [this](FunctionState state, const OrtApi* api, OrtKernelContext* context) {
// The GPU device is set again here to handle multithreading scenarios.
// Consider the following:
// Users can create multiple threads to initialize separate inference sessions on different devices (not just the default device 0)
// Later, additional threads may be spawned to execute inference_session.Run(), which calls this compute function.
// Since new threads default to using device 0, it’s necessary to explicitly set the correct device to ensure computations run on the intended GPU.
// Note: Based on our measurements on the A100 GPU with CUDA 12, the execution time for cudaSetDevice is approximately 0.004 ms, which is negligible
// and does not impact runtime performance.
CUDA_CALL_THROW(cudaSetDevice(device_id_));

Ort::KernelContext ctx(context);

TensorrtFuncState* trt_state = reinterpret_cast<TensorrtFuncState*>(state);
Expand Down Expand Up @@ -4212,6 +4221,15 @@ Status TensorrtExecutionProvider::CreateNodeComputeInfoFromPrecompiledEngine(con

// Create compute function
compute_info.compute_func = [this](FunctionState state, const OrtApi* api, OrtKernelContext* context) {
// The GPU device is set again here to handle multithreading scenarios.
// Consider the following:
// Users can create multiple threads to initialize separate inference sessions on different devices (not just the default device 0)
// Later, additional threads may be spawned to execute inference_session.Run(), which calls this compute function.
// Since new threads default to using device 0, it’s necessary to explicitly set the correct device to ensure computations run on the intended GPU.
// Note: Based on our measurements on the A100 GPU with CUDA 12, the execution time for cudaSetDevice is approximately 0.004 ms, which is negligible
// and does not impact runtime performance.
CUDA_CALL_THROW(cudaSetDevice(device_id_));

Ort::KernelContext ctx(context);

TensorrtShortFuncState* trt_state = reinterpret_cast<TensorrtShortFuncState*>(state);
Expand Down
Loading