Skip to content

Commit fb420f3

Browse files
authored
Fix bug with mixed weight cache + workspace sharing
Differential Revision: D106412035 Pull Request resolved: #19777
1 parent 79fe3a3 commit fb420f3

4 files changed

Lines changed: 13 additions & 36 deletions

File tree

backends/xnnpack/runtime/XNNExecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ ET_NODISCARD Error XNNExecutor::initialize(
9393
* delegate->execute()
9494
*/
9595
ET_NODISCARD Error XNNExecutor::prepare_args(Span<EValue*> args) {
96-
ET_CHECK_MSG(
96+
ET_DCHECK_MSG(
9797
!destroyed_.load(std::memory_order_acquire),
9898
"XNNExecutor::prepare_args called after destroy");
9999

backends/xnnpack/runtime/XNNExecutor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class XNNExecutor {
4545
: workspace_(workspace) {}
4646

4747
~XNNExecutor() {
48-
ET_CHECK_MSG(
48+
ET_DCHECK_MSG(
4949
!in_use_.load(std::memory_order_acquire),
5050
"XNNExecutor destroyed while in use");
5151
destroyed_.store(true, std::memory_order_release);

backends/xnnpack/runtime/XNNPACKBackend.cpp

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <executorch/runtime/core/evalue.h>
1717
#include <executorch/runtime/executor/pte_data_map.h>
1818

19-
#include <cinttypes>
2019
#include <memory>
2120
#include <mutex>
2221

@@ -101,6 +100,7 @@ class XnnpackBackend final
101100
lock_weights_cache.lock();
102101
weights_cache_->initialize_for_runtime(
103102
context.get_runtime_allocator(), named_data_map);
103+
workspace->set_uses_weight_cache();
104104
}
105105

106106
auto [workspace_lock, workspace_ptr] = workspace->acquire();
@@ -131,16 +131,6 @@ class XnnpackBackend final
131131
return err;
132132
}
133133

134-
ET_LOG(
135-
Info,
136-
"XnnpackBackend::init delegate=%p workspace_id=%" PRIu64
137-
" workspace_ptr=%p program_id=0x%" PRIxPTR " weight_cache=%s",
138-
(void*)executor,
139-
workspace->id(),
140-
(void*)workspace_ptr,
141-
program_id,
142-
use_weight_cache ? "true" : "false");
143-
144134
return executor;
145135
}
146136

@@ -151,18 +141,10 @@ class XnnpackBackend final
151141
auto executor = static_cast<xnnpack::delegate::XNNExecutor*>(handle);
152142

153143
auto workspace = executor->get_workspace();
154-
ET_LOG(
155-
Info,
156-
"XnnpackBackend::execute begin delegate=%p workspace_id=%" PRIu64
157-
" num_args=%zu weight_cache=%s",
158-
(void*)executor,
159-
workspace->id(),
160-
(size_t)args.size(),
161-
executor->uses_weight_cache() ? "true" : "false");
162144

163145
std::unique_lock<std::mutex> lock_weights_cache(
164146
weights_cache_mutex_, std::defer_lock);
165-
if (executor->uses_weight_cache()) {
147+
if (executor->uses_weight_cache() || workspace->uses_weight_cache()) {
166148
lock_weights_cache.lock();
167149
}
168150

@@ -183,14 +165,6 @@ class XnnpackBackend final
183165
// Convert output data types if necessary (e.g., int32 -> int64 for Long)
184166
err = executor->convert_outputs(args);
185167

186-
ET_LOG(
187-
Info,
188-
"XnnpackBackend::execute end delegate=%p workspace_id=%" PRIu64
189-
" err=0x%x",
190-
(void*)executor,
191-
workspace->id(),
192-
(unsigned int)err);
193-
194168
return err;
195169
}
196170

@@ -199,12 +173,6 @@ class XnnpackBackend final
199173
auto executor = static_cast<xnnpack::delegate::XNNExecutor*>(handle);
200174
auto workspace = executor->get_workspace();
201175

202-
ET_LOG(
203-
Info,
204-
"XnnpackBackend::destroy delegate=%p workspace_id=%" PRIu64,
205-
(void*)executor,
206-
workspace->id());
207-
208176
const std::lock_guard<std::mutex> lock_weights_cache(
209177
weights_cache_mutex_);
210178

backends/xnnpack/runtime/XNNWorkspace.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ class XNNWorkspace {
5959
lock_required_ = false;
6060
}
6161

62+
void set_uses_weight_cache() {
63+
uses_weight_cache_.store(true, std::memory_order_release);
64+
}
65+
66+
bool uses_weight_cache() const {
67+
return uses_weight_cache_.load(std::memory_order_acquire);
68+
}
69+
6270
static runtime::Result<std::shared_ptr<XNNWorkspace>> create() {
6371
// Because this class can't be moved, we need to construct it in-place.
6472
xnn_workspace_t workspace = nullptr;
@@ -80,6 +88,7 @@ class XNNWorkspace {
8088
std::mutex mutex_;
8189
uint64_t id_;
8290
bool lock_required_ = true;
91+
std::atomic<bool> uses_weight_cache_{false};
8392
WorkspacePtr workspace_;
8493
};
8594

0 commit comments

Comments
 (0)