Xnnpack disable workspace nonlock#17780
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/17780
Note: Links to docs will display an error until the docs builds have been completed.
|
|
Hi @Froskekongen! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
See #17301 |
|
@pytorchbot label "release notes: When Disable Workspace mode is enabled for XNNPack, |
|
Didn't find following labels among repository labels: release notes: When Disable Workspace mode is enabled for XNNPack, |
| return create_result.error(); | ||
| } | ||
|
|
||
| create_result.get()->disable_locking(); |
There was a problem hiding this comment.
Nit: Can we pass this in as a arg to XNNWorkspace::create?
GregoryComer
left a comment
There was a problem hiding this comment.
Waiting for CI to run, but I'm okay to merge if it's green.
|
@GregoryComer lint error should be fixed now. |
There was a problem hiding this comment.
Pull request overview
This PR removes the mutex lock from XNNWorkspace::acquire() when workspace sharing is in Disabled mode (each delegate instance has its own workspace with no concurrent access). Since no sharing occurs in Disabled mode, the lock is unnecessary overhead. The change adds a lock_required_ flag set via a new disable_locking() method, called immediately after workspace creation for Disabled mode.
Changes:
XNNWorkspace.h: Addslock_required_flag anddisable_locking()method;acquire()skips locking whenlock_required_isfalse.XNNWorkspaceManager.cpp: Callsdisable_locking()on newly-created workspaces when inDisabledmode.test_workspace_manager.cpp/CMakeLists.txt: Adds two new tests verifying locking behavior per mode, and registers the test file in the build.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
backends/xnnpack/runtime/XNNWorkspace.h |
Adds lock_required_ member and disable_locking() public method; modifies acquire() to skip locking when flag is false |
backends/xnnpack/runtime/XNNWorkspaceManager.cpp |
Calls disable_locking() on workspace after creation in Disabled mode |
backends/xnnpack/test/runtime/test_workspace_manager.cpp |
Adds DisabledModeAcquireDoesNotLock and PerModelAcquireStillLocks tests |
backends/xnnpack/test/CMakeLists.txt |
Adds test_workspace_manager.cpp to the test build target |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| void disable_locking() { | ||
| lock_required_ = false; | ||
| } |
There was a problem hiding this comment.
The lock_required_ field is a plain bool that is written by disable_locking() and read by acquire(). Although the current single call site in XNNWorkspaceManager.cpp calls disable_locking() before the workspace is shared with any other thread (making it safe in practice), the public disable_locking() method has no documented threading constraints, and calling it on an already-shared workspace while acquire() is being called concurrently from another thread would be an undefined behavior data race. Consider making lock_required_ a std::atomic<bool> to ensure correctness regardless of call order, or at minimum add a comment documenting the thread-safety requirement (that it must only be called before the workspace is shared with other threads).
| TEST_F(XNNWorkspaceManagerTest, PerModelAcquireStillLocks) { | ||
| workspace_manager_->set_sharing_mode(WorkspaceSharingMode::PerModel); | ||
|
|
||
| auto workspace_result = workspace_manager_->get_or_create_workspace(12345); | ||
| ASSERT_TRUE(workspace_result.ok()); | ||
| auto workspace = workspace_result.get(); | ||
|
|
||
| auto [lock, ptr] = workspace->acquire(); | ||
| ASSERT_NE(ptr, nullptr); | ||
| EXPECT_TRUE(lock.owns_lock()); | ||
| } |
There was a problem hiding this comment.
The PR adds DisabledModeAcquireDoesNotLock and PerModelAcquireStillLocks to verify locking behavior per mode, but there is no corresponding GlobalAcquireStillLocks test for the Global sharing mode. Since Global mode shares a workspace across all callers and also relies on the mutex to serialize access, adding a test verifying that acquire() in Global mode returns a locked unique_lock would make the test suite complete and symmetric.
Summary
Remove lock on XNNPACK Disabled Workspace Mode.
Test plan
See test.
cc @GregoryComer @digantdesai @cbilgin