Skip to content

Commit a4bd823

Browse files
authored
Fix race condition in XNNPACK weights cache during concurrent init()
Differential Revision: D105753995 Pull Request resolved: pytorch#19682
1 parent d66a37c commit a4bd823

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

backends/xnnpack/runtime/XNNPACKBackend.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@ class XnnpackBackend final
9191
auto workspace = workspace_result.get();
9292

9393
bool use_weight_cache = options_.resolve_weight_cache(context);
94+
// Hold the lock for the entire init-compile-finalize sequence to prevent
95+
// concurrent inits from resetting is_finalized_ or overwriting
96+
// named_data_map_ while compileModel is using the shared weights cache.
97+
std::unique_lock<std::mutex> lock_weights_cache(
98+
weights_cache_mutex_, std::defer_lock);
9499
if (use_weight_cache) {
95-
const std::lock_guard<std::mutex> lock_weight_cache(weights_cache_mutex_);
100+
lock_weights_cache.lock();
96101
weights_cache_->initialize_for_runtime(
97102
context.get_runtime_allocator(), named_data_map);
98103
}

0 commit comments

Comments
 (0)