Skip to content

Xnnpack disable workspace nonlock#17780

Open
Froskekongen wants to merge 5 commits intopytorch:mainfrom
Froskekongen:xnnpack-disable-workspace-nonlock
Open

Xnnpack disable workspace nonlock#17780
Froskekongen wants to merge 5 commits intopytorch:mainfrom
Froskekongen:xnnpack-disable-workspace-nonlock

Conversation

@Froskekongen
Copy link

@Froskekongen Froskekongen commented Mar 2, 2026

Summary

Remove lock on XNNPACK Disabled Workspace Mode.

Test plan

See test.

cc @GregoryComer @digantdesai @cbilgin

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 2, 2026

🔗 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.

⚠️ 8 Awaiting Approval

As of commit 3871367 with merge base 9aab9e7 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla
Copy link

meta-cla bot commented Mar 2, 2026

Hi @Froskekongen!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 2, 2026
@Froskekongen
Copy link
Author

See #17301

@Froskekongen
Copy link
Author

@pytorchbot label "release notes: When Disable Workspace mode is enabled for XNNPack, execute does not lock."

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 2, 2026

Didn't find following labels among repository labels: release notes: When Disable Workspace mode is enabled for XNNPack, execute does not lock.

@GregoryComer GregoryComer self-assigned this Mar 2, 2026
@GregoryComer GregoryComer self-requested a review March 2, 2026 19:37
@GregoryComer GregoryComer added module: xnnpack Issues related to xnnpack delegation and the code under backends/xnnpack/ release notes: xnnpack Changes to the XNNPack backend delegate labels Mar 2, 2026
return create_result.error();
}

create_result.get()->disable_locking();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we pass this in as a arg to XNNWorkspace::create?

Copy link
Member

@GregoryComer GregoryComer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for CI to run, but I'm okay to merge if it's green.

@Froskekongen
Copy link
Author

@GregoryComer lint error should be fixed now.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: Adds lock_required_ flag and disable_locking() method; acquire() skips locking when lock_required_ is false.
  • XNNWorkspaceManager.cpp: Calls disable_locking() on newly-created workspaces when in Disabled mode.
  • 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.

Comment on lines +58 to +60
void disable_locking() {
lock_required_ = false;
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment on lines +154 to +164
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());
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: xnnpack Issues related to xnnpack delegation and the code under backends/xnnpack/ release notes: xnnpack Changes to the XNNPack backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants