From 94dd1f6b013bfcebe0ecf7b9a9ad9e763232aefb Mon Sep 17 00:00:00 2001 From: Eashan Garg Date: Wed, 22 Apr 2026 15:14:44 -0700 Subject: [PATCH] Unique .so paths to support multiple CUDA delegates Summary: Some export flows need to generate multiple .so files (for ex/, exporting audio encoder and speech decoder of gemma3n as separate .ptes with their own blobs) Differential Revision: D101883539 --- backends/cuda/runtime/cuda_backend.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backends/cuda/runtime/cuda_backend.cpp b/backends/cuda/runtime/cuda_backend.cpp index eb0a07b8d8f..99a21de1e93 100644 --- a/backends/cuda/runtime/cuda_backend.cpp +++ b/backends/cuda/runtime/cuda_backend.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -314,10 +315,14 @@ class ET_EXPERIMENTAL CudaBackend final so_blob_key.c_str(), static_cast(aoti_dso_buffer.error())); - // Generate dynamic temporary file path + // Generate dynamic temporary file path with unique counter to support + // multiple CUDA delegates in the same process. Atomic to avoid races + // when multiple delegates are loaded concurrently. + static std::atomic delegate_counter{0}; + int delegate_id = delegate_counter.fetch_add(1, std::memory_order_relaxed); filesystem::path temp_dir = filesystem::temp_directory_path(); filesystem::path so_path = - temp_dir / (so_blob_key + to_string(get_process_id()) + ".so"); + temp_dir / (so_blob_key + to_string(get_process_id()) + "_" + to_string(delegate_id) + ".so"); // Create a temporary file ofstream outfile(so_path, ios::binary);