Skip to content

Commit 009fa08

Browse files
committed
fix: Correct mutex scope in execute_engine
1 parent 5f1d12f commit 009fa08

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

core/runtime/execute_engine.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ std::vector<at::Tensor> execute_engine(std::vector<at::Tensor> inputs, c10::intr
176176
}
177177
}
178178

179+
// nvinfer1::IExecutionContext::enqueue is not thread safe and we need a mutex for it.
180+
// setTensorAddress should be in same scope to prevent inconsistent state.
181+
std::unique_lock<std::mutex> lock(compiled_engine->mu);
182+
179183
{ // Input Setup
180184
std::unique_ptr<torch::autograd::profiler::RecordProfile> input_profiler_guard;
181185
if (compiled_engine->profile_execution) {
@@ -307,9 +311,6 @@ std::vector<at::Tensor> execute_engine(std::vector<at::Tensor> inputs, c10::intr
307311
compiled_engine->engine_stream = c10::cuda::getStreamFromPool(false, current_device_id);
308312
}
309313

310-
// nvinfer1::IExecutionContext::enqueue is not thread safe and we need a mutex for it.
311-
std::unique_lock<std::mutex> lock(compiled_engine->mu);
312-
313314
{ // Engine Execution (execute on engine stream)
314315
c10::cuda::CUDAStreamGuard stream_guard(compiled_engine->engine_stream);
315316

0 commit comments

Comments
 (0)