Skip to content

Commit e9e868c

Browse files
authored
Back out "Add XNNPACK backend option for workspace sharing"
Differential Revision: D81634155 Pull Request resolved: #13921
1 parent 9cc092c commit e9e868c

14 files changed

+81
-996
lines changed

backends/test/multi_method_delegate_test.cpp

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@
55
#include <thread>
66
#include <vector>
77

8-
#include <executorch/backends/xnnpack/runtime/XNNPACKBackend.h>
9-
10-
#include <executorch/runtime/backend/interface.h>
11-
#include <executorch/runtime/backend/options.h>
128
#include <executorch/runtime/executor/program.h>
139
#include <executorch/runtime/platform/runtime.h>
1410

1511
#include <executorch/extension/data_loader/file_data_loader.h>
1612
#include <executorch/extension/memory_allocator/malloc_memory_allocator.h>
1713
#include <executorch/extension/runner_util/inputs.h>
1814

19-
using executorch::backends::xnnpack::workspace_sharing_mode_option_key;
20-
using executorch::backends::xnnpack::WorkspaceSharingMode;
21-
using executorch::backends::xnnpack::xnnpack_backend_key;
22-
23-
using executorch::runtime::BackendOptions;
2415
using executorch::runtime::Error;
2516
using executorch::runtime::EValue;
2617
using executorch::runtime::HierarchicalAllocator;
@@ -135,61 +126,34 @@ class XNNPACKMultiDelegateTest : public ETPTEMethodRunBaseTest {
135126
num_threads = 40;
136127
kMethodName = "forward";
137128
}
129+
};
138130

139-
// This test is to validate the assumption that the delegate is thread safe.
140-
// That includes the following:
141-
// 1. The delegate can be initilized by multiple threads in parallel.
142-
// 2. The delegate can be executed by multiple threads in parallel.
143-
// 3. The delegate can be destroyed by multiple threads in parallel.
144-
// Regardless of the underlying implementation of the delegate.
145-
// This is particularly important when we have shared resources across
146-
// delegate instances through a singleton backend instance.
147-
void runStressTest() {
148-
ASSERT_NE(kTestPTE1Path.size(), 0);
149-
ASSERT_NE(kTestPTE2Path.size(), 0);
150-
ASSERT_NE(num_threads, 0);
151-
ASSERT_NE(kMethodName.size(), 0);
152-
153-
std::vector<std::thread> threads(num_threads);
154-
std::atomic<size_t> count{0};
155-
156-
for (int i = 0; i < num_threads; i++) {
157-
threads[i] = std::thread([&, i]() {
158-
run(i, i % 7 ? kTestPTE1Path : kTestPTE2Path, kMethodName, count);
159-
});
160-
}
161-
for (int i = 0; i < num_threads; i++) {
162-
threads[i].join();
163-
}
164-
ASSERT_EQ(count, num_threads);
131+
// This test is to validate the assumption that the delegate is thread safe.
132+
// That includes the following:
133+
// 1. The delegate can be initilized by multiple threads in parallel.
134+
// 2. The delegate can be executed by multiple threads in parallel.
135+
// 3. The delegate can be destroyed by multiple threads in parallel.
136+
// Regardless of the underlying implementation of the delegate.
137+
// This is particularly important when we have shared resources across
138+
// delegate instances through a singleton backend instance.
139+
TEST_F(XNNPACKMultiDelegateTest, MultipleThreads) {
140+
ASSERT_NE(kTestPTE1Path.size(), 0);
141+
ASSERT_NE(kTestPTE2Path.size(), 0);
142+
ASSERT_NE(num_threads, 0);
143+
ASSERT_NE(kMethodName.size(), 0);
144+
145+
std::vector<std::thread> threads(num_threads);
146+
std::atomic<size_t> count{0};
147+
148+
for (int i = 0; i < num_threads; i++) {
149+
threads[i] = std::thread([&, i]() {
150+
run(i, i % 7 ? kTestPTE1Path : kTestPTE2Path, kMethodName, count);
151+
});
165152
}
166-
167-
void setWorkspaceSharingMode(WorkspaceSharingMode mode) {
168-
executorch::runtime::runtime_init();
169-
170-
BackendOptions<1> backend_options;
171-
backend_options.set_option(
172-
workspace_sharing_mode_option_key, static_cast<int>(mode));
173-
174-
auto status = executorch::runtime::set_option(
175-
xnnpack_backend_key, backend_options.view());
176-
ASSERT_EQ(status, Error::Ok);
153+
for (int i = 0; i < num_threads; i++) {
154+
threads[i].join();
177155
}
178-
};
179-
180-
TEST_F(XNNPACKMultiDelegateTest, MultipleThreadsSharingDisabled) {
181-
setWorkspaceSharingMode(WorkspaceSharingMode::Disabled);
182-
runStressTest();
183-
}
184-
185-
TEST_F(XNNPACKMultiDelegateTest, MultipleThreadsPerModelSharing) {
186-
setWorkspaceSharingMode(WorkspaceSharingMode::PerModel);
187-
runStressTest();
188-
}
189-
190-
TEST_F(XNNPACKMultiDelegateTest, MultipleThreadsGlobalSharing) {
191-
setWorkspaceSharingMode(WorkspaceSharingMode::Global);
192-
runStressTest();
156+
ASSERT_EQ(count, num_threads);
193157
}
194158

195159
// TODO(T208989291): Add more tests here. For example,

backends/xnnpack/runtime/XNNCompiler.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,16 +1895,24 @@ ET_NODISCARD Error XNNCompiler::compileModel(
18951895
xnn_weights_cache_t weights_cache_ptr = nullptr;
18961896
#endif
18971897

1898-
// NOLINTBEGIN(facebook-hte-NullableDereference) - weights cache is allowed to
1899-
// be null
1898+
#ifdef ENABLE_XNNPACK_SHARED_WORKSPACE
1899+
ET_CHECK_OR_RETURN_ERROR(
1900+
workspace != nullptr, Internal, "Failed to initialize XNNPACK workspace");
19001901
status = xnn_create_runtime_v4(
19011902
subgraph.get(),
19021903
weights_cache_ptr,
19031904
workspace,
19041905
::executorch::extension::threadpool::get_pthreadpool(),
19051906
runtime_flags,
19061907
&runtime_ptr);
1907-
// NOLINTEND(facebook-hte-NullableDereference)
1908+
#else
1909+
status = xnn_create_runtime_v3(
1910+
subgraph.get(),
1911+
weights_cache_ptr,
1912+
::executorch::extension::threadpool::get_pthreadpool(),
1913+
runtime_flags,
1914+
&runtime_ptr);
1915+
#endif
19081916

19091917
ET_CHECK_OR_RETURN_ERROR(
19101918
xnn_status_success == status,

backends/xnnpack/runtime/XNNExecutor.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
#pragma once
1010

1111
#include <executorch/backends/xnnpack/runtime/XNNStatus.h>
12-
#include <executorch/backends/xnnpack/runtime/XNNWorkspace.h>
1312
#include <executorch/backends/xnnpack/runtime/profiling/XNNProfiler.h>
1413
#include <executorch/runtime/backend/interface.h>
1514
#include <executorch/runtime/core/error.h>
1615
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
1716

1817
#include <xnnpack.h>
18+
#include <map>
1919
#include <memory>
2020
#include <vector>
2121

@@ -35,11 +35,9 @@ class XNNExecutor {
3535
std::vector<uint32_t> output_ids_;
3636
std::vector<xnn_external_value> externals_;
3737
std::vector<std::string> packed_data_names_;
38-
std::shared_ptr<XNNWorkspace> workspace_;
3938

4039
public:
41-
XNNExecutor(std::shared_ptr<XNNWorkspace> workspace)
42-
: workspace_(workspace) {}
40+
XNNExecutor() = default;
4341

4442
inline size_t getNumInputs() {
4543
return input_ids_.size();
@@ -53,10 +51,6 @@ class XNNExecutor {
5351
return packed_data_names_;
5452
}
5553

56-
inline XNNWorkspace& get_workspace() {
57-
return *workspace_;
58-
}
59-
6054
/**
6155
* Initialize the XNNExecutor with a given runtime and input/output ids.
6256
* The input/output ids are expected to be sorted in order of their

0 commit comments

Comments
 (0)