dml: add per-instance mutexes to fix concurrent session crashes#28007
Open
oysteinkrog wants to merge 1 commit intomicrosoft:mainfrom
Open
dml: add per-instance mutexes to fix concurrent session crashes#28007oysteinkrog wants to merge 1 commit intomicrosoft:mainfrom
oysteinkrog wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Add thread-safety to 4 DML EP data structures that race when multiple InferenceSessions run concurrently on the same D3D12 device: - BucketizedBufferAllocator: std::mutex on Alloc/FreeResource, std::atomic for m_defaultRoundingMode. FreeResource releases lock before calling ExecutionContext::QueueReference to prevent lock-order inversion (allocator→context vs context→queue→allocator). - CommandQueue: std::recursive_mutex on all methods (re-entrance: ExecuteCommandList→ExecuteCommandLists, Close→GetCurrentCompletionEvent). - ExecutionContext: std::recursive_mutex on all public/private methods (re-entrance: Flush↔SetCommandRecorder cycle). std::atomic<bool> for m_closed to eliminate data race in IsClosed(). - DescriptorPool: std::mutex on AllocDescriptors, Trim, GetTotalCapacity. Each session has its own instances of these objects, so the mutexes only serialize intra-session calls. Cross-session concurrency is fully preserved. Fixes 0x8000FFFF "Catastrophic failure" in MLOperatorAuthorImpl.cpp when running concurrent DML inference sessions with per-session command queues. Verified with concurrent inference stress tests (2-3 models running simultaneously, 1000+ iterations) — crashes consistently without the fix, stable with the fix.
Author
@microsoft-github-policy-service agree company="Initial Force AS" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds thread-safety to 4 DML EP data structures that race when multiple
InferenceSessioninstances run concurrently on the same D3D12 device. Without these locks, concurrent DML sessions crash with0x8000FFFF("Catastrophic failure") inMLOperatorAuthorImpl.cpp.Problem
When creating multiple
InferenceSessioninstances that share the same D3D12 device (e.g., running person detection and pose estimation models simultaneously), the DML EP crashes because several internal data structures are not thread-safe:BucketizedBufferAllocator::Alloc/FreeResource— concurrent allocations corrupt bucket listsCommandQueuemethods — concurrent command list submissions raceExecutionContext— concurrentFlush/SetCommandRecordercalls raceDescriptorPool::AllocDescriptors— concurrent descriptor allocation corrupts pool stateFix
Add per-instance mutexes to each of the 4 classes:
BucketizedBufferAllocatorstd::mutex+std::atomicform_defaultRoundingModeFreeResourcereleases lock before callingQueueReferenceto prevent lock-order inversionCommandQueuestd::recursive_mutexExecuteCommandList→ExecuteCommandLists,Close→GetCurrentCompletionEventExecutionContextstd::recursive_mutex+std::atomic<bool>form_closedFlush↔SetCommandRecordercycleDescriptorPoolstd::mutexAllocDescriptors,Trim,GetTotalCapacityEach session has its own instances of these objects, so the mutexes only serialize intra-session calls. Cross-session concurrency is fully preserved.
Verification
Tested with concurrent inference stress tests:
Tested on NVIDIA GeForce RTX 5070 Ti with DirectML, Windows 11.
Motivation and Context
Applications that run multiple ML models concurrently (e.g., real-time sports analysis with person detection + pose estimation) need concurrent DML sessions for performance. The current code assumes single-threaded access to per-session EP objects, which breaks when sessions share a D3D12 device.
This is a minimal fix — only adding locks where data races were observed. No API changes, no behavioral changes for single-session usage.