diff --git a/onnxruntime/core/providers/cuda/cuda_execution_provider.cc b/onnxruntime/core/providers/cuda/cuda_execution_provider.cc index 514d6d50d6..d4e5d39b15 100644 --- a/onnxruntime/core/providers/cuda/cuda_execution_provider.cc +++ b/onnxruntime/core/providers/cuda/cuda_execution_provider.cc @@ -198,6 +198,8 @@ void CUDAExecutionProvider::AddDeferredReleaseCPUPtr(void* p) { } Status CUDAExecutionProvider::OnRunStart() { + // always set CUDA device when session::Run() in case it runs in a worker thread + CUDA_RETURN_IF_ERROR(cudaSetDevice(GetDeviceId())); auto cpu_alloc = GetAllocator(0, OrtMemTypeCPU); // check if cudaEvents has passed for deferred release // note that we need to take a mutex in case of multi-threaded Run()